From dc518dcca2d8f3107ef16c5765d967d3c9c16c31 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 21 Mar 2019 18:58:50 +0530 Subject: [PATCH] Add modification for app getting logic --- .../application/mgt/common/Application.java | 11 ++ .../{DeviceType.java => DeviceTypes.java} | 2 +- .../device/application/mgt/common/Filter.java | 15 +- .../application/mgt/core/dao/common/Util.java | 1 + .../GenericApplicationDAOImpl.java | 29 ++-- .../mgt/core/impl/ApplicationManagerImpl.java | 137 ++++++++++-------- .../impl/ApplicationStorageManagerImpl.java | 7 +- .../services/ApplicationManagementAPI.java | 4 + .../impl/ApplicationManagementAPIImpl.java | 15 +- 9 files changed, 140 insertions(+), 81 deletions(-) rename components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/{DeviceType.java => DeviceTypes.java} (74%) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index 96d4ec97c6..f336ad3807 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -80,12 +80,19 @@ public class Application { value = "Id of the Related device type of the application", example = "1, 2, 3") private int deviceTypeId; + @ApiModelProperty(name = "deviceType", value = "Related device type of the application", required = true, example = "IoS, Android, Arduino, RaspberryPi etc") private String deviceType; + @ApiModelProperty(name = "status", + value = "Application status", + required = true, + example = "REMOVED, ACTIVE") + private String status; + @ApiModelProperty(name = "applicationReleases", value = "List of application releases", required = true) @@ -190,4 +197,8 @@ public class Application { public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; } + + public String getStatus() { return status; } + + public void setStatus(String status) { this.status = status; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/DeviceType.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/DeviceTypes.java similarity index 74% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/DeviceType.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/DeviceTypes.java index b3bddc448e..673db9a5da 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/DeviceType.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/DeviceTypes.java @@ -1,5 +1,5 @@ package org.wso2.carbon.device.application.mgt.common; -public enum DeviceType { +public enum DeviceTypes { ANDROID, IOS } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java index 732fd0a611..c87dee5933 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java @@ -18,6 +18,8 @@ */ package org.wso2.carbon.device.application.mgt.common; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; + /** * Filter represents a criteria that can be used for searching applications. */ @@ -63,6 +65,11 @@ public class Filter { */ private String currentAppReleaseState; + /*** + * Supported device type for the application. i.e Android, iOS, Windows etc + */ + private DeviceType deviceType; + public int getLimit() { return limit; } @@ -119,11 +126,13 @@ public class Filter { this.appCategory = appCategory; } - public String getCurrentAppReleaseState() { - return currentAppReleaseState; - } + public String getCurrentAppReleaseState() { return currentAppReleaseState; } public void setCurrentAppReleaseState(String currentAppReleaseState) { this.currentAppReleaseState = currentAppReleaseState; } + + public DeviceType getDeviceType() { return deviceType; } + + public void setDeviceType(DeviceType deviceType) { this.deviceType = deviceType; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index 14c344d6b2..cbffbced31 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -78,6 +78,7 @@ public class Util { application.setSubType(rs.getString("SUB_TYPE")); application.setPaymentCurrency(rs.getString("CURRENCY")); application.setIsRestricted(rs.getBoolean("RESTRICTED")); + application.setStatus(rs.getString("STATUS")); String tag = rs.getString("APP_TAG"); String unrestrictedRole = rs.getString("ROLE"); if (tag != null) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 7c336935fa..8d18bf0daf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -133,28 +133,32 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ResultSet rs = null; ApplicationList applicationList = new ApplicationList(); Pagination pagination = new Pagination(); - String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY" - + " AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP.CURRENCY AS CURRENCY, " - + "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " - + "AS ROLE FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) " + String sql = "SELECT " + + "AP_APP.ID AS APP_ID," + + " AP_APP.NAME AS APP_NAME," + + " AP_APP.TYPE AS APP_TYPE," + + " AP_APP.APP_CATEGORY AS APP_CATEGORY," + + " AP_APP.SUB_TYPE AS SUB_TYPE," + + " AP_APP.CURRENCY AS CURRENCY, " + + "AP_APP.RESTRICTED AS RESTRICTED," + + " AP_APP_TAG.TAG AS APP_TAG," + + " AP_UNRESTRICTED_ROLE.ROLE AS ROLE " + + "FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) " + "LEFT JOIN AP_UNRESTRICTED_ROLE ON AP_APP.ID = AP_UNRESTRICTED_ROLE.AP_APP_ID) " - + "WHERE AP_APP.TENANT_ID = ? AND AP_APP.STATUS != ?"; + + "WHERE AP_APP.TENANT_ID = ?"; if (filter == null) { throw new ApplicationManagementDAOException("Filter need to be instantiated"); } - if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { sql += " AND AP_APP.TYPE "; sql += "= ?"; } - if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) { sql += " AND AP_APP.APP_CATEGORY "; sql += "= ?"; } - if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { sql += " AND LOWER (AP_APP.NAME) "; if (filter.isFullMatch()) { @@ -163,6 +167,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += "LIKE ?"; } } + if (filter.getDeviceType() != null ) { + sql += " AND AP_APP.DEVICE_TYPE_ID "; + sql += "= ?"; + } String defaultSortOrder = "ASC"; if (filter.getSortBy() != null && !filter.getSortBy().isEmpty()) { @@ -177,7 +185,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic conn = this.getDBConnection(); stmt = conn.prepareStatement(sql); stmt.setInt(paramIndex++, tenantId); - stmt.setString(paramIndex++, AppLifecycleState.REMOVED.toString()); if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { stmt.setString(paramIndex++, filter.getAppType()); @@ -192,6 +199,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.setString(paramIndex++, "%" + filter.getAppName().toLowerCase() + "%"); } } + if (filter.getDeviceType() != null ) { + stmt.setInt(paramIndex++, filter.getDeviceType().getId()); + } + if (filter.getLimit() == 0) { stmt.setInt(paramIndex++, 100); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index f0ee07acee..1e61e33da2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -86,7 +86,6 @@ public class ApplicationManagerImpl implements ApplicationManager { private LifecycleStateDAO lifecycleStateDAO; private LifecycleStateManger lifecycleStateManger; - public ApplicationManagerImpl() { initDataAccessObjects(); lifecycleStateManger = DataHolder.getInstance().getLifecycleStateManager(); @@ -95,7 +94,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private void initDataAccessObjects() { this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); - this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); + this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); } @@ -106,8 +105,7 @@ public class ApplicationManagerImpl implements ApplicationManager { * @throws RequestValidatingException if application creating request is invalid, returns {@link RequestValidatingException} * @throws ApplicationManagementException Catch all other throwing exceptions and returns {@link ApplicationManagementException} */ - @Override - public Application createApplication(Application application) + @Override public Application createApplication(Application application) throws RequestValidatingException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -205,11 +203,11 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { + @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); ApplicationList applicationList; + List appList; List applicationReleases; filter = validateFilter(filter); @@ -218,9 +216,31 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { + if (filter.getDeviceType() != null ) { + boolean isValidDeviceType = false; + String deviceType = filter.getDeviceType().getName(); + List deviceTypes = Util.getDeviceManagementService().getDeviceTypes(); + for (DeviceType dt : deviceTypes) { + if (dt.getName().equals(deviceType)) { + filter.getDeviceType().setId(dt.getId()); + isValidDeviceType = true; + break; + } + } + if (!isValidDeviceType) { + throw new BadRequestException("Invalid device type is found, device type is " + deviceType); + } + } ConnectionManagerUtil.openDBConnection(); applicationList = applicationDAO.getApplications(filter, tenantId); - if(applicationList != null && applicationList.getApplications() != null && !applicationList + appList = applicationList.getApplications(); + for ( Application app : appList){ + if (AppLifecycleState.REMOVED.toString().equals(app.getStatus())){ + appList.remove(app); + } + } + applicationList.setApplications(appList); + if (applicationList.getApplications() != null && !applicationList .getApplications().isEmpty()) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationList = getRoleRestrictedApplicationList(applicationList, userName); @@ -238,13 +258,15 @@ public class ApplicationManagerImpl implements ApplicationManager { } catch (ApplicationManagementDAOException e) { throw new ApplicationManagementException( "DAO exception while getting applications for the user " + userName + " of tenant " + tenantId, e); + } catch (DeviceManagementException e) { + throw new ApplicationManagementException( + "Error Occured when getting device type instance for " + filter.getDeviceType()); } finally { ConnectionManagerUtil.closeDBConnection(); } } - @Override - public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) + @Override public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -255,9 +277,8 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); Application existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (existingApplication == null){ - throw new NotFoundException( - "Couldn't find application for the application Id: " + applicationId); + if (existingApplication == null) { + throw new NotFoundException("Couldn't find application for the application Id: " + applicationId); } // todo check whether admin or app creator. @@ -286,7 +307,8 @@ public class ApplicationManagerImpl implements ApplicationManager { .createRelease(applicationRelease, existingApplication.getId(), tenantId); LifecycleState lifecycleState = getLifecycleStateInstant(AppLifecycleState.CREATED.toString(), AppLifecycleState.CREATED.toString()); - this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), tenantId); + this.lifecycleStateDAO + .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), tenantId); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; } catch (TransactionManagementException e) { @@ -303,22 +325,21 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException( "Error occurred while adding new application release lifecycle state to the application release: " + applicationId, e); - } catch (ApplicationManagementDAOException e){ + } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while adding new application release for application " + applicationId, e); - }catch (UserStoreException e) { + } catch (UserStoreException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred whecn checking whether user is admin user or not. Application release: " + applicationId, e); - } finally{ + } finally { ConnectionManagerUtil.closeDBConnection(); } } - @Override - public Application getApplicationById(int appId, String state) throws ApplicationManagementException { + @Override public Application getApplicationById(int appId, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; @@ -358,8 +379,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public Application getApplicationByUuid(String uuid, String state) throws ApplicationManagementException { + @Override public Application getApplicationByUuid(String uuid, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; @@ -399,8 +419,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - private boolean isRoleExists(Collection unrestrictedRoleList, String userName) - throws UserStoreException { + private boolean isRoleExists(Collection unrestrictedRoleList, String userName) throws UserStoreException { String[] roleList; roleList = getRolesOfUser(userName); for (String unrestrictedRole : unrestrictedRoleList) { @@ -462,8 +481,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException { + @Override public Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; @@ -472,7 +490,7 @@ public class ApplicationManagerImpl implements ApplicationManager { application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId); if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(), - userName)) { + userName)) { return application; } return null; @@ -484,7 +502,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } } -// todo rethink about this method + // todo rethink about this method private List getReleases(Application application, String releaseState) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -521,8 +539,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - if (AppLifecycleState.PUBLISHED.toString() - .equals(state) && filteredReleases.size() > 1) { + if (AppLifecycleState.PUBLISHED.toString().equals(state) && filteredReleases.size() > 1) { log.warn("There are more than one application releases is found which is in PUBLISHED state"); filteredReleases.sort((r1, r2) -> { if (r1.getLifecycleState().getUpdatedAt().after(r2.getLifecycleState().getUpdatedAt())) { @@ -538,8 +555,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return applicationReleases; } - @Override - public List deleteApplication(int applicationId) throws ApplicationManagementException { + @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); List storedLocations = new ArrayList<>(); @@ -549,7 +565,6 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.beginDBTransaction(); application = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (application == null) { throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId); } @@ -589,7 +604,8 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Can't delete application release which has the UUID:" + applicationRelease - .getUuid() + " and its belongs to the application which has application ID:" + .getUuid() + + " and its belongs to the application which has application ID:" + applicationId + " You have to move the lifecycle state from " + currentState + " to acceptable state"); } @@ -663,8 +679,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return directions; } - @Override - public String deleteApplicationRelease(int applicationId, String releaseUuid) + @Override public String deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -766,7 +781,7 @@ public class ApplicationManagerImpl implements ApplicationManager { UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), permission, - CarbonConstants.UI_PERMISSION_ACTION); + CarbonConstants.UI_PERMISSION_ACTION); } /** @@ -802,7 +817,7 @@ public class ApplicationManagerImpl implements ApplicationManager { "App Type contains in the application creating payload doesn't match with supported app types"); } - if (application.getApplicationReleases().size() > 1 ){ + if (application.getApplicationReleases().size() > 1) { throw new RequestValidatingException( "Invalid payload. Application creating payload should contains one application release, but " + "the payload contains more than one"); @@ -911,8 +926,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } //todo check whether package names are same - @Override - public void updateApplicationArtifact(int appId, String deviceType, String uuid, InputStream binaryFile) + @Override public void updateApplicationArtifact(int appId, String deviceType, String uuid, InputStream binaryFile) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -928,8 +942,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } if (!isValidDeviceType) { throw new ValidationException( - "Invalid request to update application release artifact, invalid application type: " + deviceType - + " for Application id: " + appId + " application release uuid: " + uuid); + "Invalid request to update application release artifact, invalid application type: " + + deviceType + " for Application id: " + appId + " application release uuid: " + uuid); } if (appId <= 0) { throw new ValidationException( @@ -974,7 +988,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException("Error occured while updating application artifact.", e); } catch (DeviceManagementException e) { throw new ApplicationManagementException("Error occured while getting supported device types in IoTS", e); - } catch (DBConnectionException e) { + } catch (DBConnectionException e) { throw new ApplicationManagementException( "Error occured when getting DB connection to update application release artifact of the application, appid: " + appId + " and uuid " + uuid + ".", e); @@ -1025,13 +1039,12 @@ public class ApplicationManagerImpl implements ApplicationManager { throws ApplicationManagementException { if (applicationRelease.getVersion() == null) { throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for " - + "creating release. It cannot be found."); + + "creating release. It cannot be found."); } } - @Override - public LifecycleState getLifecycleState(int applicationId, String releaseUuid) throws - ApplicationManagementException { + @Override public LifecycleState getLifecycleState(int applicationId, String releaseUuid) + throws ApplicationManagementException { LifecycleState lifecycleState; try { ConnectionManagerUtil.openDBConnection(); @@ -1039,7 +1052,8 @@ public class ApplicationManagerImpl implements ApplicationManager { if (lifecycleState == null) { return null; } - lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManger.getNextLifecycleStates(lifecycleState.getCurrentState()))); + lifecycleState.setNextStates( + new ArrayList<>(lifecycleStateManger.getNextLifecycleStates(lifecycleState.getCurrentState()))); } catch (LifeCycleManagementDAOException e) { throw new ApplicationManagementException("Failed to get lifecycle state from database", e); @@ -1049,8 +1063,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return lifecycleState; } - @Override - public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state) + @Override public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { @@ -1098,8 +1111,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public Application updateApplication(int applicationId, Application application) throws ApplicationManagementException{ + @Override public Application updateApplication(int applicationId, Application application) + throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -1115,10 +1128,10 @@ public class ApplicationManagerImpl implements ApplicationManager { existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId); if (existingApplication == null) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException("Tried to update Application which is not in the publisher, " + - "Please verify application details"); + throw new NotFoundException("Tried to update Application which is not in the publisher, " + + "Please verify application details"); } - if (!existingApplication.getUnrestrictedRoles().isEmpty()){ + if (!existingApplication.getUnrestrictedRoles().isEmpty()) { isExistingAppRestricted = true; } @@ -1150,9 +1163,9 @@ public class ApplicationManagerImpl implements ApplicationManager { if (isExistingAppRestricted && application.getUnrestrictedRoles().isEmpty()) { visibilityDAO.deleteUnrestrictedRoles(existingApplication.getUnrestrictedRoles(), application.getId(), tenantId); - } else if(!isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()){ + } else if (!isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()) { visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), application.getId(), tenantId); - } else if ( isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()) { + } else if (isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()) { addingRoleList = getDifference(application.getUnrestrictedRoles(), existingApplication.getUnrestrictedRoles()); removingRoleList = getDifference(existingApplication.getUnrestrictedRoles(), @@ -1174,7 +1187,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationDAO.deleteTags(removingTags, application.getId(), tenantId); } return applicationDAO.editApplication(application, tenantId); - } catch (UserStoreException e) { + } catch (UserStoreException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while checking whether logged in user is ADMIN or not when updating application of application id: " @@ -1183,8 +1196,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while updating the application, application id: " + applicationId); - } - catch (VisibilityManagementDAOException e){ + } catch (VisibilityManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while updating the visibility restriction of the application. Application id is " @@ -1221,7 +1233,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private List getDifference(List list1, Collection list2) { List list = new ArrayList<>(); for (T t : list1) { - if(!list2.contains(t)) { + if (!list2.contains(t)) { list.add(t); } } @@ -1244,10 +1256,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } //todo check whether package names are same - @Override - public boolean updateRelease(int applicationId, String releaseUuid, String deviceType, ApplicationRelease updateRelease, - InputStream binaryFileStram, InputStream iconFileStream, InputStream bannerFileStream, - List attachments) throws ApplicationManagementException{ + @Override public boolean updateRelease(int applicationId, String releaseUuid, String deviceType, + ApplicationRelease updateRelease, InputStream binaryFileStram, InputStream iconFileStream, + InputStream bannerFileStream, List attachments) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 320b8b4f5a..293704b8f2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -32,14 +32,13 @@ import org.apache.commons.validator.routines.UrlValidator; import org.wso2.carbon.context.PrivilegedCarbonContext; 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.DeviceType; +import org.wso2.carbon.device.application.mgt.common.DeviceTypes; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.exception.ParsingException; import org.wso2.carbon.device.application.mgt.core.util.ArtifactsParser; -import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; import org.xml.sax.SAXException; @@ -203,11 +202,11 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager "application UUID " + applicationRelease.getUuid()); } - if (DeviceType.ANDROID.toString().equalsIgnoreCase(deviceType)) { + if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) { ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(cloneInputStream[2]); applicationRelease.setVersion(apkMeta.getVersionName()); applicationRelease.setPackageName(apkMeta.getPackageName()); - } else if (DeviceType.IOS.toString().equalsIgnoreCase(deviceType)) { + } else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) { NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile); applicationRelease .setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java index 51087e22ee..d39310b7e3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java @@ -124,6 +124,10 @@ public interface ApplicationManagementAPI { response = ErrorResponse.class) }) Response getApplications( + @ApiParam( + name = "deviceType", + value = "Supporting device Type of the application") + @QueryParam("device-type") String deviceType, @ApiParam( name = "name", value = "Name of the application") diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java index 0d9b0e4e38..d40ea6ebec 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java @@ -35,6 +35,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagemen import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; import java.io.IOException; import java.io.InputStream; @@ -68,6 +69,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Override @Consumes("application/json") public Response getApplications( + @QueryParam("device-type") String deviceType, @QueryParam("name") String appName, @QueryParam("type") String appType, @QueryParam("category") String appCategory, @@ -96,13 +98,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { if (releaseState != null && !releaseState.isEmpty()) { filter.setCurrentAppReleaseState(releaseState); } + if (deviceType != null && !deviceType.isEmpty()) { + DeviceType dt = new DeviceType(); + dt.setName(deviceType); + filter.setDeviceType(dt); + } 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 (ApplicationManagementException e) { + } catch(BadRequestException e){ + String msg = "Couldn't found a device type for " + deviceType; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + }catch (ApplicationManagementException e) { String msg = "Error occurred while getting the application list for publisher "; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); @@ -158,6 +169,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { try { if (isInvalidReleaseCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application.getType())) { +// todo add msg return Response.status(Response.Status.BAD_REQUEST).build(); } @@ -210,6 +222,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } catch (ApplicationManagementException e) { String msg = "Error occurred while creating the application"; log.error(msg, e); +// todo add msg into return return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (ResourceManagementException e) { String msg =