From ab841939a451ecf195998c5e87b98d74ae6d5523 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 22 May 2019 18:45:08 +0530 Subject: [PATCH] Add improvement for user subscription method in APPM --- .../GenericApplicationReleaseDAOImpl.java | 42 +++--- .../mgt/core/impl/ApplicationManagerImpl.java | 127 +----------------- .../core/impl/SubscriptionManagerImpl.java | 2 +- .../services/SubscriptionManagementAPI.java | 22 +++ .../impl/SubscriptionManagementAPIImpl.java | 24 ++-- 5 files changed, 60 insertions(+), 157 deletions(-) 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/release/GenericApplicationReleaseDAOImpl.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/release/GenericApplicationReleaseDAOImpl.java index 2065f8ac554..fca382660c8 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/release/GenericApplicationReleaseDAOImpl.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/release/GenericApplicationReleaseDAOImpl.java @@ -47,11 +47,11 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * To insert the ApplicationDTO Release Details. * * @param appId Id of the application - * @param applicationRelease ApplicationDTO Release the properties of which that need to be inserted. + * @param applicationReleaseDTO ApplicationDTO Release the properties of which that need to be inserted. * @param tenantId Tenant Id * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. */ - @Override public ApplicationReleaseDTO createRelease(ApplicationReleaseDTO applicationRelease, int appId, int tenantId) + @Override public ApplicationReleaseDTO createRelease(ApplicationReleaseDTO applicationReleaseDTO, int appId, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; @@ -83,31 +83,31 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql, generatedColumns); - statement.setString(1, applicationRelease.getDescription()); - statement.setString(2, applicationRelease.getVersion()); + statement.setString(1, applicationReleaseDTO.getDescription()); + statement.setString(2, applicationReleaseDTO.getVersion()); statement.setInt(3, tenantId); - statement.setString(4, applicationRelease.getUuid()); - statement.setString(5, String.valueOf(applicationRelease.getReleaseType())); - statement.setString(6, String.valueOf(applicationRelease.getPackageName())); - statement.setDouble(7, applicationRelease.getPrice()); - statement.setString(8, applicationRelease.getInstallerName()); - statement.setString(9, applicationRelease.getIconName()); - statement.setString(10, applicationRelease.getBannerName()); - statement.setString(11, applicationRelease.getScreenshotName1()); - statement.setString(12, applicationRelease.getScreenshotName2()); - statement.setString(13, applicationRelease.getScreenshotName3()); - statement.setString(14, applicationRelease.getAppHashValue()); - statement.setBoolean(15, applicationRelease.getIsSharedWithAllTenants()); - statement.setString(16, applicationRelease.getMetaData()); - statement.setString(17, applicationRelease.getSupportedOsVersions()); - statement.setString(18, applicationRelease.getCurrentState().toUpperCase()); + statement.setString(4, applicationReleaseDTO.getUuid()); + statement.setString(5, String.valueOf(applicationReleaseDTO.getReleaseType())); + statement.setString(6, String.valueOf(applicationReleaseDTO.getPackageName())); + statement.setDouble(7, applicationReleaseDTO.getPrice()); + statement.setString(8, applicationReleaseDTO.getInstallerName()); + statement.setString(9, applicationReleaseDTO.getIconName()); + statement.setString(10, applicationReleaseDTO.getBannerName()); + statement.setString(11, applicationReleaseDTO.getScreenshotName1()); + statement.setString(12, applicationReleaseDTO.getScreenshotName2()); + statement.setString(13, applicationReleaseDTO.getScreenshotName3()); + statement.setString(14, applicationReleaseDTO.getAppHashValue()); + statement.setBoolean(15, applicationReleaseDTO.getIsSharedWithAllTenants()); + statement.setString(16, applicationReleaseDTO.getMetaData()); + statement.setString(17, applicationReleaseDTO.getSupportedOsVersions()); + statement.setString(18, applicationReleaseDTO.getCurrentState().toUpperCase()); statement.setInt(19, appId); statement.executeUpdate(); resultSet = statement.getGeneratedKeys(); if (resultSet.next()) { - applicationRelease.setId(resultSet.getInt(1)); + applicationReleaseDTO.setId(resultSet.getInt(1)); } - return applicationRelease; + return applicationReleaseDTO; } catch (SQLException e) { log.error("SQL Exception while trying to release an application by executing the query " + sql, e); throw new ApplicationManagementDAOException( 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 33e3855cdfa..e5bf96578b9 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 @@ -92,13 +92,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Queue; -import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -250,7 +245,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException(msg, e); } - //insert application data into databse + //insert application data into database ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); try { @@ -1059,6 +1054,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return roleList; } + //todo no usage public ApplicationDTO getApplication(String appType, String appName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -1159,33 +1155,6 @@ public class ApplicationManagerImpl implements ApplicationManager { // return filterAppReleaseByCurrentState(applicationReleases, releaseState); } -// private List filterAppReleaseByCurrentState(List applicationReleases, -// String state) { -// List filteredReleases = new ArrayList<>(); -// -// if (state != null && !state.isEmpty()) { -// for (ApplicationReleaseDTO applicationRelease : applicationReleases) { -// if (state.equals(applicationRelease.getLifecycleStateChangeFlow().getCurrentState())) { -// filteredReleases.add(applicationRelease); -// } -// } -// -// 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.getLifecycleStateChangeFlow().getUpdatedAt().after(r2.getLifecycleStateChangeFlow().getUpdatedAt())) { -// return -1; -// } else if (r2.getLifecycleStateChangeFlow().getUpdatedAt().after(r1.getLifecycleStateChangeFlow().getUpdatedAt())) { -// return 1; -// } -// return 0; -// }); -// } -// return filteredReleases; -// } -// return applicationReleases; -// } - @Override public void deleteApplication(int applicationId) throws ApplicationManagementException { if (log.isDebugEnabled()) { @@ -1283,53 +1252,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - private List searchLifecycleStateFlow(String start, String finish) throws ApplicationManagementException { - Map nextNodeMap = new HashMap<>(); - List directions = new LinkedList<>(); - Queue queue = new LinkedList<>(); - - String currentNode = start; - queue.add(currentNode); - - Set visitedNodes = new HashSet<>(); - visitedNodes.add(currentNode); - while (!queue.isEmpty()) { - currentNode = queue.remove(); - if (currentNode.equals(finish)) { - break; - } else { - List nextStates = lifecycleStateManager.getNextLifecycleStates(currentNode); - if (nextStates.contains(finish)) { - queue = new LinkedList<>(); - queue.add(finish); - nextNodeMap.put(currentNode, finish); - } else { - for (String node : nextStates) { - if (!visitedNodes.contains(node)) { - queue.add(node); - visitedNodes.add(node); - nextNodeMap.put(currentNode, node); - } - } - } - } - } - - //If all nodes are explored and the destination node hasn't been found. - if (!currentNode.equals(finish)) { - String errorMsg = "can't found a feasible path from " + start + " to " + finish; - throw new ApplicationManagementException(errorMsg); - } - - //Reconstruct path - for (String node = start; node != null; node = nextNodeMap.get(node)) { - if (!node.equals(start)) { - directions.add(node); - } - } - return directions; - } - @Override public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException { @@ -1547,49 +1469,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - /** - * To get role restricted application list. - * - * @param applicationList list of applications. - * @param userName user name - * @return ApplicationDTO related with the UUID - */ -// private ApplicationList getRoleRestrictedApplicationList(ApplicationList applicationList, String userName) -// throws ApplicationManagementException { -// ApplicationList roleRestrictedApplicationList = new ApplicationList(); -// ArrayList unRestrictedApplications = new ArrayList<>(); -// for (ApplicationDTO application : applicationList.getApplications()) { -// if (application.getUnrestrictedRoles().isEmpty()) { -// unRestrictedApplications.add(application); -// } else { -// try { -// if (hasUserRole(application.getUnrestrictedRoles(), userName)) { -// unRestrictedApplications.add(application); -// } -// } catch (UserStoreException e) { -// throw new ApplicationManagementException("Role restriction verifying is failed"); -// } -// } -// } -// roleRestrictedApplicationList.setApplications(unRestrictedApplications); -// return roleRestrictedApplicationList; -// } - - /** - * To validate a app release creating request and app updating request to make sure all the pre-conditions - * satisfied. - * - * @param applicationRelease ApplicationReleaseDTO that need to be created. - * @throws ApplicationManagementException ApplicationDTO Management Exception. - */ - private void validateAppReleasePayload(ApplicationReleaseDTO applicationRelease) - throws ApplicationManagementException { - if (applicationRelease.getVersion() == null) { - throw new ApplicationManagementException("ApplicationReleaseDTO version name is a mandatory parameter for " - + "creating release. It cannot be found."); - } - } - @Override public List getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -2430,10 +2309,8 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new RequestValidatingException(msg); } - } - @Override public void validateReleaseCreatingRequest(ApplicationReleaseWrapper applicationReleaseWrapper, String applicationType) throws RequestValidatingException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 48ae6181c03..46e0bc11f53 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -160,7 +160,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { + " users."); } - //todo check valid user list + //todo check valid user list - throw BadRequest exception ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); List operationTriggeredDeviceIdentifiers = new ArrayList<>(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index f91e755aa15..39f23456539 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -28,6 +28,8 @@ import io.swagger.annotations.Info; import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.application.mgt.common.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.PaginationResult; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponseTmp; import org.wso2.carbon.device.application.mgt.common.EnterpriseInstallationDetails; @@ -181,6 +183,26 @@ public interface SubscriptionManagementAPI { ) @ApiResponses( value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully add an operation to install application for user devices..", + response = PaginationResult.class, + responseContainer = "PaginationResult"), + @ApiResponse( + code = 400, + message = + "Bad Request. \n Found invalid payload with the request."), + @ApiResponse( + code = 403, + message = "Don't have permission to install application release."), + @ApiResponse( + code = 404, + message = "Not Found. \n Not found an application release for requested UUID."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while adding operation to install " + + "application for users.", + response = ErrorResponse.class) }) Response installApplicationForRoles ( diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 9929d2072e9..4dfdb27f76c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -94,7 +94,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ public Response installApplicationForUsers( @PathParam("uuid") String uuid, @Valid List users) { - if (users.isEmpty()){ + if (users.isEmpty()) { String msg = "In order to install application release which has UUID " + uuid + ", you should provide list " + "of users. But found an empty list of users."; log.error(msg); @@ -104,23 +104,27 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); ApplicationInstallResponse response = subscriptionManager.installApplicationForUsers(uuid, users); return Response.status(Response.Status.OK).entity(response).build(); - - //todo - } catch(BadRequestException e){ + } catch (NotFoundException e) { + String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload"; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (BadRequestException e) { String msg = "Found invalid payload for installing application which has UUID: " + uuid + ". Hence verify the payload"; log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } catch(ForbiddenException e){ - String msg = "Application release is not in the installable state. Hence you are not permitted to install the aplication."; + } catch (ForbiddenException e) { + String msg = "Application release is not in the installable state. Hence you are not permitted to install " + + "the application."; log.error(msg); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - }catch (ApplicationManagementException e) { - String msg = - "Error occurred while installing the application release which has UUID: " + uuid + " for devices"; + } catch (ApplicationManagementException e) { + String msg = "Error occurred while installing the application release which has UUID: " + uuid + + " for user devices"; log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } } + } + } @Override @POST