Fixed application getting issues and improved the application getting functionality

Improved application functionalities. Modified service layer, DAO layer and API layer
feature/appm-store/pbac
lasantha 7 years ago
parent ef0e7ee738
commit 6e3cc085ff

@ -132,13 +132,17 @@ public class ApplicationRelease {
this.appHashValue = appHashValue;
}
public void setIsSharedWithAllTenants(int isSharedWithAllTenants) { this.isSharedWithAllTenants = isSharedWithAllTenants; }
public void setIsSharedWithAllTenants(int isSharedWithAllTenants) {
this.isSharedWithAllTenants = isSharedWithAllTenants;
}
public void setMetaData(String metaData) {
this.metaData = metaData;
}
public int getId() { return id; }
public int getId() {
return id;
}
public String getVersion() {
return version;

@ -23,26 +23,17 @@ package org.wso2.carbon.device.application.mgt.common;
*/
public class Filter {
/**
* Order which the search results should be shown. Ascending or Descending.
*/
public enum SortingOrder {
ASC, DESC
}
private int limit;
private int offset;
private String appName;
private String searchQuery;
private String appType;
private boolean isFullMatch;
private SortingOrder sortingOrder;
private int limit;
private String sortBy;
private int offset;
private String userName;
private String sortBy;
public int getLimit() {
return limit;
@ -60,20 +51,12 @@ public class Filter {
this.offset = offset;
}
public String getSearchQuery() {
return searchQuery;
public String getAppName() {
return appName;
}
public void setSearchQuery(String searchQuery) {
this.searchQuery = searchQuery;
}
public SortingOrder getSortingOrder() {
return sortingOrder;
}
public void setSortingOrder(SortingOrder sortingOrder) {
this.sortingOrder = sortingOrder;
public void setAppName(String appName) {
this.appName = appName;
}
public String getSortBy() {
@ -84,14 +67,6 @@ public class Filter {
this.sortBy = sortBy;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public boolean isFullMatch() {
return isFullMatch;
}
@ -100,8 +75,11 @@ public class Filter {
isFullMatch = fullMatch;
}
public boolean hasCondition() {
return searchQuery != null;
public String getAppType() {
return appType;
}
public void setAppType(String appType) {
this.appType = appType;
}
}

@ -181,7 +181,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
throw new ApplicationManagementDAOException("Filter need to be instantiated");
}
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
if (filter.getAppType() != null) {
sql += " AND AP_APP.TYPE ";
sql += "= ?";
}
if (filter.getAppName() != null) {
sql += " AND LOWER (AP_APP.NAME) ";
if (filter.isFullMatch()) {
sql += "= ?";
@ -200,11 +204,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt = conn.prepareStatement(sql);
stmt.setInt(++index, tenantId);
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
if (filter.getAppType() != null) {
stmt.setString(++index, filter.getAppType());
}
if (filter.getAppName() != null) {
if (filter.isFullMatch()) {
stmt.setString(++index, filter.getSearchQuery().toLowerCase());
stmt.setString(++index, filter.getAppName().toLowerCase());
} else {
stmt.setString(++index, "%" + filter.getSearchQuery().toLowerCase() + "%");
stmt.setString(++index, "%" + filter.getAppName().toLowerCase() + "%");
}
}
@ -212,9 +219,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.setInt(++index, filter.getOffset());
rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs));
pagination.setSize(filter.getOffset());
pagination.setCount(this.getApplicationCount(filter));
applicationList.setPagination(pagination);
applicationList.getPagination().setSize(filter.getOffset());
applicationList.getPagination().setCount(applicationList.getApplications().size());
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
@ -287,15 +294,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection();
sql += "SELECT count(APP.ID) AS APP_COUNT FROM AP_APP AS APP WHERE TENANT_ID = ?";
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
if (filter.getAppName() != null) {
sql += " AND LOWER (APP.NAME) LIKE ? ";
}
sql += ";";
stmt = conn.prepareStatement(sql);
int index = 0;
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
stmt.setString(++index, "%" + filter.getSearchQuery().toLowerCase() + "%");
if (filter.getAppName() != null) {
stmt.setString(++index, "%" + filter.getAppName().toLowerCase() + "%");
}
rs = stmt.executeQuery();
if (rs.next()) {

@ -27,9 +27,11 @@ import org.wso2.carbon.device.application.mgt.common.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.ApplicationType;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.LifecycleStateTransition;
import org.wso2.carbon.device.application.mgt.common.SortingOrder;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
import org.wso2.carbon.device.application.mgt.common.User;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
@ -53,10 +55,11 @@ import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.Timestamp;
import java.util.Date;
/**
* Default Concrete implementation of Application Management related implementations.
@ -64,6 +67,8 @@ import java.util.List;
public class ApplicationManagerImpl implements ApplicationManager {
private static final Log log = LogFactory.getLog(ApplicationManagerImpl.class);
private static final int DEFAULT_LIMIT = 20;
private static final int DEFAULT_OFFSET = 10;
private DeviceTypeDAO deviceTypeDAO;
private VisibilityDAO visibilityDAO;
private ApplicationDAO applicationDAO;
@ -90,7 +95,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
validateAppCreatingRequest(application);
validateReleaseCreateRequest(application.getApplicationReleases());
validateReleaseCreatingRequest(application.getApplicationReleases());
DeviceType deviceType;
ApplicationRelease applicationRelease;
try {
@ -149,8 +154,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationList applicationList;
List<ApplicationRelease> applicationReleases;
filter = validateFilter(filter);
if (filter == null) {
throw new ApplicationManagementException("Filter validation failed, Please verify the request payload");
}
try {
filter.setUserName(userName);
ConnectionManagerUtil.openDBConnection();
applicationList = applicationDAO.getApplications(filter, tenantId);
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
@ -427,17 +436,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
*/
private void validateAppCreatingRequest(Application application) throws ValidationException {
Boolean isValidApplicationType;
try {
if (application.getName() == null) {
throw new ValidationException("Application name cannot be empty");
}
if (application.getUser() == null || application.getUser().getUserName() == null
|| application.getUser().getTenantId() == 0) {
|| application.getUser().getTenantId() == -1) {
throw new ValidationException("Username and tenant Id cannot be empty");
}
if (application.getAppCategory() == null) {
throw new ValidationException("Username and tenant Id cannot be empty");
}
try {
isValidApplicationType = isValidAppType(application);
if (!isValidApplicationType) {
throw new ValidationException("App Type contains in the application creating payload doesn't match with " +
"supported app types");
}
validateApplicationExistence(application);
} catch (ApplicationManagementException e) {
throw new ValidationException("Error occured while validating whether there is already an application "
@ -445,6 +463,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
private Boolean isValidAppType(Application application) {
for (ApplicationType applicationType : ApplicationType.values()) {
if (applicationType.toString().equals(application.getType())) {
return true;
}
}
return false;
}
/**
* To validate the application existence
*
@ -454,7 +481,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private void validateApplicationExistence(Application application) throws ApplicationManagementException {
Filter filter = new Filter();
filter.setFullMatch(true);
filter.setSearchQuery(application.getName().trim());
filter.setAppName(application.getName().trim());
filter.setOffset(0);
filter.setLimit(1);
@ -544,7 +571,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @param applicationReleases ApplicationRelease that need to be created.
* @throws ApplicationManagementException Application Management Exception.
*/
private void validateReleaseCreateRequest(List<ApplicationRelease> applicationReleases)
private void validateReleaseCreatingRequest(List<ApplicationRelease> applicationReleases)
throws ApplicationManagementException {
if (applicationReleases.isEmpty() || applicationReleases.size() > 1) {
@ -747,4 +774,34 @@ public class ApplicationManagerImpl implements ApplicationManager {
//todo update application
return application;
}
private Filter validateFilter(Filter filter) {
if (filter != null) {
if (filter.getLimit() == 0) {
filter.setLimit(DEFAULT_LIMIT);
}
if (filter.getOffset() == 0) {
filter.setOffset(DEFAULT_OFFSET);
}
if (!SortingOrder.ASC.toString().equals(filter.getSortBy()) &&
!SortingOrder.DESC.toString().equals(filter.getSortBy())) {
return null;
}
if (filter.getAppType() != null) {
Boolean isValidRequest = false;
for (ApplicationType applicationType: ApplicationType.values()){
if(applicationType.toString().equals(filter.getAppType())){
isValidRequest = true;
break;
}
}
if (!isValidRequest){
return null;
}
}
}
return filter;
}
}

@ -32,11 +32,8 @@ import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.*;
import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorResponse;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import java.util.List;
import javax.validation.Valid;
@ -146,17 +143,10 @@ public interface ApplicationManagementAPI {
})
Response getApplications(
@ApiParam(
name = "offset",
value = "Provide from which position apps should return", defaultValue = "20")
@QueryParam("offset") int offset,
@ApiParam(
name = "limit",
value = "Provide how many apps it should return", defaultValue = "0")
@QueryParam("limit") int limit,
@ApiParam(
name = "searchQuery",
value = "Relevant search query to search on", defaultValue = "*")
@QueryParam("searchQuery") String searchQuery
name = "filter",
value = "Filter to get application list",
required = true)
@Valid Filter filter
);
@GET
@ -395,8 +385,12 @@ public interface ApplicationManagementAPI {
response = ErrorResponse.class)
})
Response updateApplicationArtifact(
@ApiParam(name = "id", value = "Id of the application", required = true) @PathParam("uuid") int applicationId,
@ApiParam(name = "uuid", value = "UUID of the application", required = true) @PathParam("uuid") String applicationUUID,
@ApiParam(name = "appType", value = "Type of the application i.e Android, iOS etc", required = true)
@PathParam("appType") String appType,
@ApiParam(name = "id", value = "Id of the application", required = true)
@PathParam("uuid") int applicationId,
@ApiParam(name = "uuid", value = "UUID of the application", required = true)
@PathParam("uuid") String applicationUUID,
@Multipart("binaryFile") Attachment binaryFile);
@PUT

@ -56,7 +56,6 @@ import javax.ws.rs.core.Response;
@Path("/publisher/applications")
public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
private static final int DEFAULT_LIMIT = 20;
private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class);
@ -64,28 +63,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Override
@Consumes("application/json")
public Response getApplications(
@QueryParam("offset") int offset,
@QueryParam("limit") int limit,
@QueryParam("query") String searchQuery) {
@Valid Filter filter) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
if (limit == 0) {
limit = DEFAULT_LIMIT;
}
Filter filter = new Filter();
filter.setOffset(offset);
filter.setLimit(limit);
filter.setSearchQuery(searchQuery);
ApplicationList applications = applicationManager.getApplications(filter);
if (applications.getApplications().isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).entity
("Couldn't find any application for requested query.").build();
}
return Response.status(Response.Status.OK).entity(applications).build();
} catch (NotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
} catch (ApplicationManagementException e) {
String msg = "Error occurred while getting the application list for publisher ";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@ -100,7 +93,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
Application application = applicationManager.getApplication(appType, appName);
if (application == null) {
return Response.status(Response.Status.NOT_FOUND).entity
("Application with UUID " + appType + " not found").build();
("Application with application type: " + appType + " not found").build();
}
return Response.status(Response.Status.OK).entity(application).build();
@ -126,32 +119,32 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
InputStream iconFileStream;
InputStream bannerFileStream;
List<InputStream> attachments = new ArrayList<>();
List<ApplicationRelease> applicationReleases = new ArrayList<>();
try {
if (iconFile == null) {
throw new ApplicationManagementException(
"Icon file is not uploaded for the application release of " + application.getName() +
log.error("Icon file is not uploaded for the application release of " + application.getName() +
" of application type " + application.getType());
return Response.status(Response.Status.BAD_REQUEST).build();
}
if (bannerFile == null) {
throw new ApplicationManagementException(
"Banner file is not uploaded for the application release of " + application.getName() +
log.error("Banner file is not uploaded for the application release of " + application.getName() +
" of application type " + application.getType());
return Response.status(Response.Status.BAD_REQUEST).build();
}
if (attachmentList == null || attachmentList.isEmpty()) {
throw new ApplicationManagementException(
"Screenshots are not uploaded for the application release of " + application.getName() +
log.error("Screenshots are not uploaded for the application release of " + application.getName() +
" of application type " + application.getType());
return Response.status(Response.Status.BAD_REQUEST).build();
}
if (binaryFile == null) {
throw new ApplicationManagementException(
"Binary file is not uploaded for the application release of " + application.getName() +
log.error("Binary file is not uploaded for the application release of " + application.getName() +
" of application type " + application.getType());
return Response.status(Response.Status.BAD_REQUEST).build();
}
iconFileStream = iconFile.getDataHandler().getInputStream();
bannerFileStream = bannerFile.getDataHandler().getInputStream();
@ -159,7 +152,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
attachments.add(screenshot.getDataHandler().getInputStream());
}
applicationRelease = applicationStorageManager.uploadReleaseArtifacts(applicationRelease,
applicationRelease = applicationStorageManager.uploadReleaseArtifact(applicationRelease, application.getType(),
binaryFile.getDataHandler().getInputStream());
if (applicationRelease.getAppStoredLoc() == null || applicationRelease.getAppHashValue() == null) {
@ -169,18 +162,19 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
bannerFileStream, attachments);
applicationRelease.setUuid(UUID.randomUUID().toString());
applicationReleases.add(applicationRelease);
application.setApplicationReleases(applicationReleases);
Application createdApplication = applicationManager.createApplication(application);
if (application != null) {
if (createdApplication != null) {
return Response.status(Response.Status.CREATED).entity(createdApplication).build();
} else {
log.error("Given device type is not matched with existing device types");
return Response.status(Response.Status.BAD_REQUEST).build();
log.error("Application Creation Failed");
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (ResourceManagementException e) {
log.error("Error occurred while uploading the releases artifacts of the application "
+ application.getName(), e);
@ -232,16 +226,18 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
return Response.status(Response.Status.OK)
.entity("Successfully uploaded artifacts for the application " + applicationUuid).build();
} catch (NotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
String msg = "Couldn't found application release details and storage details";
log.error(msg, e);
return APIUtil.getResponse(e, Response.Status.NOT_FOUND);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application";
String msg = "Error occurred while updating the application";
log.error(msg, e);
return APIUtil.getResponse(e, Response.Status.BAD_REQUEST);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
log.error("Exception while trying to read icon, banner files for the application " + applicationUuid);
return APIUtil.getResponse(new ApplicationManagementException(
"Exception while trying to read icon, " + "banner files for the application " + applicationUuid, e),
Response.Status.BAD_REQUEST);
Response.Status.INTERNAL_SERVER_ERROR);
} catch (ResourceManagementException e) {
log.error("Error occurred while uploading the image artifacts of the application with the uuid "
+ applicationUuid, e);
@ -251,29 +247,28 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Override
@PUT
@Path("/app-artifacts/{appId}/{uuid}")
@Path("/app-artifacts/{appType}/{appId}/{uuid}")
public Response updateApplicationArtifact(
@PathParam("appType") String appType,
@PathParam("appId") int applicationId,
@PathParam("uuid") String applicationUuuid,
@Multipart("binaryFile") Attachment binaryFile) {
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationRelease applicationRelease;
try {
if (binaryFile != null) {
if (binaryFile == null) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Uploading artifacts for the application is failed " + applicationUuuid).build();
}
applicationRelease = applicationManager.validateApplicationRelease(applicationUuuid);
applicationRelease = applicationStorageManager.updateReleaseArtifacts(applicationRelease,
applicationRelease = applicationStorageManager.updateReleaseArtifacts(applicationRelease, appType,
binaryFile.getDataHandler().getInputStream());
applicationManager.updateRelease(applicationId, applicationRelease);
return Response.status(Response.Status.OK)
.entity("Successfully uploaded artifacts for the application " + applicationUuuid).build();
}
return Response.status(Response.Status.BAD_REQUEST)
.entity("Uploading artifacts for the application is failed " + applicationUuuid).build();
} catch (IOException e) {
log.error("Exception while trying to read icon, banner files for the application " + applicationUuuid);
return APIUtil.getResponse(new ApplicationManagementException(
@ -325,21 +320,18 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
List<InputStream> attachments = new ArrayList<>();
try {
applicationRelease = applicationManager.validateApplicationRelease(applicationUUID);
Application application = applicationManager.validateApplication(applicationId);
if (binaryFile != null) {
applicationRelease = applicationStorageManager.updateReleaseArtifacts(applicationRelease, binaryFile.getDataHandler().getInputStream());
applicationRelease = applicationStorageManager.updateReleaseArtifacts(applicationRelease,
application.getType(), binaryFile.getDataHandler().getInputStream());
}
if (iconFile != null) {
iconFileStream = iconFile.getDataHandler().getInputStream();
}
if (bannerFile != null) {
bannerFileStream = bannerFile.getDataHandler().getInputStream();
}
if (!attachmentList.isEmpty()) {
for (Attachment screenshot : attachmentList) {
attachments.add(screenshot.getDataHandler().getInputStream());
@ -423,7 +415,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} catch (ApplicationManagementException e) {
String msg = "Error occurred while getting lifecycle state.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return Response.status(Response.Status.OK).entity(lifecycleState).build();
}
@ -440,7 +432,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} catch (ApplicationManagementException e) {
String msg = "Error occurred while adding lifecycle state.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return Response.status(Response.Status.CREATED).entity("Lifecycle state added successfully.").build();
}

@ -75,7 +75,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
Filter filter = new Filter();
filter.setOffset(offset);
filter.setLimit(limit);
filter.setSearchQuery(searchQuery);
filter.setAppName(searchQuery);
ApplicationList applications = applicationManager.getApplications(filter);

Loading…
Cancel
Save