From 4512a4e2d7965aeefc6f74b2260443d0b2e0ac5e Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 23 Sep 2018 21:17:24 +0530 Subject: [PATCH] working state on happy path app create --- .../mgt/core/impl/ApplicationManagerImpl.java | 20 +++++++++++-- ...ApplicationManagementServiceComponent.java | 1 + .../core/lifecycle/LifecycleStateManger.java | 30 ++++++++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) 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 ec236c453d0..d92c4feda51 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 @@ -100,13 +100,14 @@ public class ApplicationManagerImpl implements ApplicationManager { + " the user : " + application.getUser().getUserName()); } + + ConnectionManagerUtil.openDBConnection(); validateAppCreatingRequest(application); validateAppReleasePayload(application.getApplicationReleases().get(0)); DeviceType deviceType; ApplicationRelease applicationRelease; List applicationReleases = new ArrayList<>(); try { - ConnectionManagerUtil.getDBConnection(); ConnectionManagerUtil.beginDBTransaction(); MAMDeviceConnectorImpl mamDeviceConnector = new MAMDeviceConnectorImpl(); // Getting the device type details to get device type ID for internal mappings @@ -188,7 +189,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); - } catch (Exception e) { + } catch (Exception e) { String msg = "Unknown exception while creating application."; log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); @@ -743,7 +744,8 @@ public class ApplicationManagerImpl implements ApplicationManager { state.setUpdatedBy(userName); if (state.getCurrentState() != null && state.getPreviousState() != null) { - if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState())) { + + if (getLifecycleManagementService().isValidStateChange(state.getPreviousState(), state.getCurrentState())) { this.lifecycleStateDAO .addLifecycleState(state, applicationId, releaseId, tenantId); } else { @@ -859,4 +861,16 @@ public class ApplicationManagerImpl implements ApplicationManager { } return list; } + + public LifecycleStateManger getLifecycleManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + LifecycleStateManger deviceManagementProviderService = + (LifecycleStateManger) ctx.getOSGiService(LifecycleStateManger.class, null); + if (deviceManagementProviderService == null) { + String msg = "DeviceImpl Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceManagementProviderService; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java index cf891d5d1c7..f6e8ec9279e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java @@ -103,6 +103,7 @@ public class ApplicationManagementServiceComponent { getConfiguration().getLifecycleStates(); LifecycleStateManger lifecycleStateManger = new LifecycleStateManger(lifecycleStates); DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger); + bundleContext.registerService(LifecycleStateManger.class.getName(), lifecycleStateManger, null); log.info("ApplicationManagement core bundle has been successfully initialized"); } catch (Throwable e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java index 1a0218b20b9..ea367351002 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java @@ -3,6 +3,7 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -26,9 +27,36 @@ public class LifecycleStateManger { } public boolean isValidStateChange(String currentState, String nextState) { - if (lifecycleStates.get(currentState).getProceedingStates().contains(nextState)) { + if (currentState.equalsIgnoreCase(nextState)) { return true; } + + State state = getMatchingState(currentState); + if (state != null) { + return getMatchingNextState(state.getProceedingStates(), nextState); + } + return false; + } + + private State getMatchingState(String currentState) { + Iterator it = lifecycleStates.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry)it.next(); + if(pair.getKey().toString().equalsIgnoreCase(currentState)) { + return lifecycleStates.get(pair.getKey().toString()); + } + it.remove(); + } + return null; + } + + private boolean getMatchingNextState(Set proceedingStates, String nextState) { + + for (String state: proceedingStates) { + if (state.equalsIgnoreCase(nextState)) { + return true; + } + } return false; } }