@Test public void testAddApplication() { /* Adding dummy application to the application store */ String testAppIdentifier = "test sample1"; try { DeviceManagementDAOFactory.openConnection(); applicationDAO.addApplication( TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234); } catch (DeviceManagementDAOException | SQLException e) { log.error("Error occurred while adding application test sample1", e); } finally { DeviceManagementDAOFactory.closeConnection(); } /* Retrieving the application by its name */ Application target = null; try { target = this.getApplication(testAppIdentifier, -1234); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving application info"; log.error(msg, e); Assert.fail(msg, e); } Assert.assertEquals( target.getApplicationIdentifier(), testAppIdentifier, "Application added is not as same as " + "what's " + "retrieved"); }
/** * Add a new environment and one EnvironmentProperty for each defined property * * @param application * @param environmentName * @return * @throws ValidationException */ @Override public Environment addEnvironment(Application application, String environmentName) throws ValidationException { Application app = applicationDAO.findById(application.getId()); if (app == null) { String[] inserts = new String[] {application.getId().toString()}; throw validationException(INVALID_APPLICATION_MESSAGE, inserts); } for (Environment e : app.getEnvironmentList()) { if (e.getName().equals(environmentName)) { String[] inserts = new String[] {environmentName, app.getName()}; throw validationException(DUPLICATE_ENVIRONMENT_MESSAGE, inserts); } } Environment e = new Environment(); e.setName(environmentName); app.addEnvironment(e); e = environmentDAO.create(e); for (Property p : app.getPropertyList()) { EnvironmentProperty ep = new EnvironmentProperty(); ep.setEnvironment(e); ep.setProperty(p); environmentPropertyDAO.create(ep); } return e; }
/** * @param application * @param propertyName * @return * @throws ValidationException */ @Override public Property addProperty(Application application, String propertyName) throws ValidationException { Application app = applicationDAO.findById(application.getId()); if (app == null) { String[] inserts = new String[] {application.getId().toString()}; throw validationException(INVALID_APPLICATION_MESSAGE, inserts); } for (Property p : app.getPropertyList()) { if (p.getName().equals(propertyName)) { String[] inserts = new String[] {propertyName, app.getName()}; throw validationException(DUPLICATE_PROPERTY_MESSAGE, inserts); } } Property p = new Property(); p.setName(propertyName); app.addProperty(p); p = propertyDAO.create(p); for (Environment e : app.getEnvironmentList()) { EnvironmentProperty ep = new EnvironmentProperty(); ep.setEnvironment(e); ep.setProperty(p); environmentPropertyDAO.create(ep); } return p; }
@Override public List<Application> getApplicationListForDevice(DeviceIdentifier deviceId) throws ApplicationManagementException { Device device; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, tenantId); if (device == null) { if (log.isDebugEnabled()) { log.debug( "No device is found upon the device identifier '" + deviceId.getId() + "' and type '" + deviceId.getType() + "'. Therefore returning null"); } return null; } return applicationDAO.getInstalledApplications(device.getId()); } catch (DeviceManagementDAOException e) { throw new ApplicationManagementException( "Error occurred while fetching the Application List of '" + deviceId.getType() + "' device carrying the identifier'" + deviceId.getId(), e); } catch (SQLException e) { throw new ApplicationManagementException( "Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } }
private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException { Application application = null; try { DeviceManagementDAOFactory.openConnection(); application = applicationDAO.getApplication(appIdentifier, tenantId); } catch (SQLException e) { log.error( "Error occurred while metadata corresponding to the application '" + appIdentifier + "'", e); } finally { DeviceManagementDAOFactory.closeConnection(); } return application; }
/** * @param applicationName * @return * @throws ValidationException */ @Override public Application addApplication(String applicationName) throws ValidationException { Query q = em.createQuery("select a from Application a where a.name=:name"); q.setParameter("name", applicationName); List<Application> applicationList = q.getResultList(); if (applicationList.size() > 0) { String[] inserts = new String[] {applicationName}; throw validationException(DUPLICATE_APPLICATION_MESSAGE, inserts); } Application application = new Application(); application.setName(applicationName); return applicationDAO.create(application); }
@Override public void updateApplicationListInstalledInDevice( DeviceIdentifier deviceIdentifier, List<Application> applications) throws ApplicationManagementException { List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier); try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); if (log.isDebugEnabled()) { log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId()); } if (log.isDebugEnabled()) { log.debug("num of apps installed:" + installedAppList.size()); } List<Application> appsToAdd = new ArrayList<>(); List<Integer> appIdsToRemove = new ArrayList<>(installedAppList.size()); for (Application installedApp : installedAppList) { if (!applications.contains(installedApp)) { if (log.isDebugEnabled()) { log.debug("Remove app Id:" + installedApp.getId()); } appIdsToRemove.add(installedApp.getId()); } } applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId); Application installedApp; List<Integer> applicationIds = new ArrayList<>(); for (Application application : applications) { if (!installedAppList.contains(application)) { installedApp = applicationDAO.getApplication( application.getApplicationIdentifier(), application.getVersion(), tenantId); if (installedApp == null) { appsToAdd.add(application); } else { applicationIds.add(installedApp.getId()); } } } if (log.isDebugEnabled()) { log.debug("num of apps add:" + appsToAdd.size()); } applicationIds.addAll(applicationDAO.addApplications(appsToAdd, tenantId)); if (log.isDebugEnabled()) { log.debug("num of app Ids:" + applicationIds.size()); } applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId); if (log.isDebugEnabled()) { log.debug("num of remove app Ids:" + appIdsToRemove.size()); } DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new ApplicationManagementException( "Error occurred saving application list to the device", e); } catch (TransactionManagementException e) { throw new ApplicationManagementException("Error occurred while initializing transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); } }