Merge branch 'application-mgt-new' into 'application-mgt-new'

Fix application installing issue

See merge request entgra/carbon-device-mgt!42
merge-requests/53/head
Inosh Perara 6 years ago
commit b06e6ee711

@ -60,15 +60,17 @@ public class Util {
List<Application> applications = new ArrayList<>(); List<Application> applications = new ArrayList<>();
Application application = null; Application application = null;
int applicationId = -1; int applicationId = -1;
boolean hasNext = rs.next();
while (rs.next()) { while (hasNext) {
if (applicationId != rs.getInt("APP_ID")) { if (applicationId != rs.getInt("APP_ID")) {
if (application != null) { if (application != null) {
applications.add(application); applications.add(application);
} }
applicationId = rs.getInt("APP_ID"); applicationId = rs.getInt("APP_ID");
application = new Application(); application = new Application();
application.setTags(new ArrayList<>());
application.setUnrestrictedRoles(new ArrayList<>());
application.setId(applicationId); application.setId(applicationId);
application.setName(rs.getString("APP_NAME")); application.setName(rs.getString("APP_NAME"));
application.setType(rs.getString("APP_TYPE")); application.setType(rs.getString("APP_TYPE"));
@ -76,25 +78,28 @@ public class Util {
application.setSubType(rs.getString("SUB_TYPE")); application.setSubType(rs.getString("SUB_TYPE"));
application.setPaymentCurrency(rs.getString("CURRENCY")); application.setPaymentCurrency(rs.getString("CURRENCY"));
application.setIsRestricted(rs.getBoolean("RESTRICTED")); application.setIsRestricted(rs.getBoolean("RESTRICTED"));
String tag = rs.getString("APP_TAG");
List<String> tags = new ArrayList<>(); String unrestrictedRole = rs.getString("ROLE");
tags.add(rs.getString("APP_TAG").toLowerCase()); if (tag != null) {
application.setTags(tags); application.getTags().add(tag);
}
List<String> unrestrictedRoles = new ArrayList<>(); if (unrestrictedRole != null) {
unrestrictedRoles.add(rs.getString("ROLE").toLowerCase()); application.getUnrestrictedRoles().add(unrestrictedRole);
application.setUnrestrictedRoles(unrestrictedRoles); }
} else { } else {
String tag = rs.getString("APP_TAG").toLowerCase(); String tag = rs.getString("APP_TAG");
String unrestrictedRole = rs.getString("ROLE").toLowerCase(); String unrestrictedRole = rs.getString("ROLE");
if (application != null && !application.getTags().contains(tag)) { if (application != null) {
if (tag != null && !application.getTags().contains(tag)) {
application.getTags().add(tag); application.getTags().add(tag);
} }
if (application != null && !application.getUnrestrictedRoles().contains(unrestrictedRole)) { if (unrestrictedRole != null && !application.getUnrestrictedRoles().contains(unrestrictedRole)) {
application.getUnrestrictedRoles().add(unrestrictedRole); application.getUnrestrictedRoles().add(unrestrictedRole);
} }
} }
if (rs.last()) { }
hasNext = rs.next();
if (!hasNext) {
applications.add(application); applications.add(application);
} }
} }
@ -119,6 +124,8 @@ public class Util {
while (rs.next()) { while (rs.next()) {
if (iteration == 0) { if (iteration == 0) {
application = new Application(); application = new Application();
application.setTags(new ArrayList<>());
application.setUnrestrictedRoles(new ArrayList<>());
applicatioId = rs.getInt("APP_ID"); applicatioId = rs.getInt("APP_ID");
application.setId(applicatioId); application.setId(applicatioId);
application.setName(rs.getString("APP_NAME")); application.setName(rs.getString("APP_NAME"));
@ -130,13 +137,12 @@ public class Util {
application.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); application.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID"));
} }
String tag = rs.getString("APP_TAG").toLowerCase(); String tag = rs.getString("APP_TAG");
String unrestrictedRole = rs.getString("ROLE").toLowerCase(); String unrestrictedRole = rs.getString("ROLE");
if (application.getTags() != null && application.getTags().contains(tag)) { if (tag != null && !application.getTags().contains(tag)) {
application.getTags().add(tag); application.getTags().add(tag);
} }
if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() if (unrestrictedRole != null && !application.getUnrestrictedRoles().contains(unrestrictedRole)) {
.contains(unrestrictedRole)) {
application.getUnrestrictedRoles().add(unrestrictedRole); application.getUnrestrictedRoles().add(unrestrictedRole);
} }
iteration++; iteration++;

@ -100,17 +100,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName); log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName);
} }
ConnectionManagerUtil.openDBConnection();
validateAppCreatingRequest(application, tenantId); validateAppCreatingRequest(application, tenantId);
validateAppReleasePayload(application.getApplicationReleases().get(0)); validateAppReleasePayload(application.getApplicationReleases().get(0));
DeviceType deviceType; DeviceType deviceType;
ApplicationRelease applicationRelease; ApplicationRelease applicationRelease;
List<ApplicationRelease> applicationReleases = new ArrayList<>(); List<ApplicationRelease> applicationReleases = new ArrayList<>();
try { try {
ConnectionManagerUtil.beginDBTransaction();
// Getting the device type details to get device type ID for internal mappings // Getting the device type details to get device type ID for internal mappings
deviceType = Util.getDeviceManagementService().getDeviceType(application.getDeviceType()); deviceType = Util.getDeviceManagementService().getDeviceType(application.getDeviceType());
ConnectionManagerUtil.beginDBTransaction();
if (deviceType == null) { if (deviceType == null) {
log.error("Device type is not matched with application type"); log.error("Device type is not matched with application type");
@ -165,7 +163,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false); lifecycleState.setUpdatedBy(userName);
this.lifecycleStateDAO.addLifecycleState(lifecycleState, appId, applicationRelease.getUuid(), tenantId);
applicationRelease.setLifecycleState(lifecycleState); applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease); applicationReleases.add(applicationRelease);
@ -192,8 +191,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg, e); log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -693,20 +690,31 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @param application Application that need to be validated * @param application Application that need to be validated
* @throws ValidationException Validation Exception * @throws ValidationException Validation Exception
*/ */
private void validateApplicationExistence(Application application, int tenantId) throws ApplicationManagementException { private void validateApplicationExistence(Application application, int tenantId)
throws ApplicationManagementException {
Filter filter = new Filter(); Filter filter = new Filter();
filter.setFullMatch(true); filter.setFullMatch(true);
filter.setAppName(application.getName().trim()); filter.setAppName(application.getName().trim());
filter.setOffset(0); filter.setOffset(0);
filter.setLimit(1); filter.setLimit(1);
try {
ConnectionManagerUtil.openDBConnection();
ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId);
if (applicationList != null && applicationList.getApplications() != null && !applicationList.getApplications() if (applicationList != null && applicationList.getApplications() != null && !applicationList
.isEmpty()) { .getApplications().isEmpty()) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Already an application registered with same name - " + applicationList.getApplications().get(0) "Already an application registered with same name - " + applicationList.getApplications().get(0)
.getName()); .getName());
} }
} catch (DBConnectionException e) {
throw new ApplicationManagementException("test 1");
} catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException("test 2");
} finally {
ConnectionManagerUtil.closeDBConnection();
}
} }
/** /**
@ -919,6 +927,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException("Failed to get application and application management", e); throw new ApplicationManagementException("Failed to get application and application management", e);
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException("Failed to get lifecycle state from database", e); throw new ApplicationManagementException("Failed to get lifecycle state from database", e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
return lifecycleState; return lifecycleState;
} }

@ -77,6 +77,13 @@ public class ApplicationManagementServiceComponent {
ApplicationManagementDAOFactory.init(dataSourceName); ApplicationManagementDAOFactory.init(dataSourceName);
// ApplicationManagementDAOFactory.initDatabases(); // ApplicationManagementDAOFactory.initDatabases();
List<LifecycleState> lifecycleStates = ConfigurationManager.getInstance().
getConfiguration().getLifecycleStates();
LifecycleStateManger lifecycleStateManger = ApplicationManagementUtil.getLifecycleStateMangerInstance();
lifecycleStateManger.init(lifecycleStates);
DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger);
bundleContext.registerService(LifecycleStateManger.class.getName(), lifecycleStateManger, null);
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
DataHolder.getInstance().setApplicationManager(applicationManager); DataHolder.getInstance().setApplicationManager(applicationManager);
bundleContext.registerService(ApplicationManager.class.getName(), applicationManager, null); bundleContext.registerService(ApplicationManager.class.getName(), applicationManager, null);
@ -94,12 +101,6 @@ public class ApplicationManagementServiceComponent {
DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager); DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager);
bundleContext.registerService(ApplicationStorageManager.class.getName(), applicationStorageManager, null); bundleContext.registerService(ApplicationStorageManager.class.getName(), applicationStorageManager, null);
List<LifecycleState> lifecycleStates = ConfigurationManager.getInstance().
getConfiguration().getLifecycleStates();
LifecycleStateManger lifecycleStateManger = new LifecycleStateManger(lifecycleStates);
DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger);
bundleContext.registerService(LifecycleStateManger.class.getName(), lifecycleStateManger, null);
UIConfiguration uiConfiguration = ConfigurationManager.getInstance(). UIConfiguration uiConfiguration = ConfigurationManager.getInstance().
getConfiguration().getUiConfiguration(); getConfiguration().getUiConfiguration();
ConfigManager configManager = new ConfigManagerImpl(uiConfiguration); ConfigManager configManager = new ConfigManagerImpl(uiConfiguration);

@ -15,7 +15,7 @@ public class LifecycleStateManger {
private Map<String, State> lifecycleStates; private Map<String, State> lifecycleStates;
public LifecycleStateManger(List<LifecycleState> states) { public void init(List<LifecycleState> states){
lifecycleStates = new HashMap<>(); lifecycleStates = new HashMap<>();
for (LifecycleState s : states) { for (LifecycleState s : states) {
if (s.getProceedingStates() != null) { if (s.getProceedingStates() != null) {

@ -27,6 +27,7 @@ import org.wso2.carbon.device.application.mgt.common.services.ReviewManager;
import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.config.Extension; import org.wso2.carbon.device.application.mgt.core.config.Extension;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -63,6 +64,12 @@ public class ApplicationManagementUtil {
return getInstance(extension, ApplicationStorageManager.class); return getInstance(extension, ApplicationStorageManager.class);
} }
public static LifecycleStateManger getLifecycleStateMangerInstance() throws InvalidConfigurationException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Extension extension = configurationManager.getExtension(Extension.Name.LifecycleStateManager);
return getInstance(extension, LifecycleStateManger.class);
}
private static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException { private static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException {
try { try {
Class theClass = Class.forName(extension.getClassName()); Class theClass = Class.forName(extension.getClassName());

@ -25,7 +25,8 @@ public class LifecycleManagementTest {
ConfigurationManager configurationManager = ConfigurationManager.getInstance(); ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Configuration configuration = configurationManager.getConfiguration(); Configuration configuration = configurationManager.getConfiguration();
lifecycleStates = configuration.getLifecycleStates(); lifecycleStates = configuration.getLifecycleStates();
lifecycleStateManger = new LifecycleStateManger(lifecycleStates); lifecycleStateManger = new LifecycleStateManger();
lifecycleStateManger.init(lifecycleStates);
} }
@Test @Test

Loading…
Cancel
Save