diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 2556238e616..b65a266c32d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -84,17 +84,6 @@ public interface ApplicationDAO { */ Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException; - /** - * To get the application with the given id - * - * @param id ID of the application. - * @param tenantId ID of the tenant. - * @return the application - * @throws ApplicationManagementDAOException Application Management DAO Exception. - */ - Application getApplicationById(String id, int tenantId) throws - ApplicationManagementDAOException; - /** * To get the application with the given uuid * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index 1add29c8355..b3ffce7e7bd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -67,6 +67,18 @@ public interface ApplicationReleaseDAO { List getReleases(String applicationName, String applicationType, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the release by state. + * + * @param appId Id of the Application + * @param tenantId tenant id of the application + * @param state state of the application + * @return list of the application releases + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + List getReleaseByState(int appId, int tenantId, String state) + throws ApplicationManagementDAOException; + /** * To update an Application release. * 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 b3c0e401204..736ca723230 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 @@ -21,13 +21,18 @@ package org.wso2.carbon.device.application.mgt.core.dao.common; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; +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.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -131,19 +136,20 @@ public class Util { application.setSubType(rs.getString("SUB_TYPE")); application.setPaymentCurrency(rs.getString("CURRENCY")); application.setIsRestricted(rs.getBoolean("RESTRICTED")); + application.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); } - Tag tag = new Tag(); - tag.setTagName(rs.getString("APP_TAG")); - UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); - unrestrictedRole.setRole(rs.getString("ROLE")); - if (application.getTags() != null && application.getTags().contains(tag)) { - application.getTags().add(tag); - } - if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() - .contains(unrestrictedRole)) { - application.getUnrestrictedRoles().add(unrestrictedRole); - } +// Tag tag = new Tag(); +// tag.setTagName(rs.getString("APP_TAG")); +// UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); +// unrestrictedRole.setRole(rs.getString("ROLE")); +// if (application.getTags() != null && application.getTags().contains(tag)) { +// application.getTags().add(tag); +// } +// if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles() +// .contains(unrestrictedRole)) { +// application.getUnrestrictedRoles().add(unrestrictedRole); +// } iteration++; } } @@ -217,4 +223,84 @@ public class Util { } return paginationRequest; } + + private static ApplicationManager applicationManager; + private static ApplicationStorageManager applicationStorageManager; + private static SubscriptionManager subscriptionManager; + + public static ApplicationManager getApplicationManager() { + if (applicationManager == null) { + synchronized (Util.class) { + if (applicationManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + applicationManager = + (ApplicationManager) ctx.getOSGiService(ApplicationManager.class, null); + if (applicationManager == null) { + String msg = "Application Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return applicationManager; + } + + /** + * To get the Application Storage Manager from the osgi context. + * @return ApplicationStoreManager instance in the current osgi context. + */ + public static ApplicationStorageManager getApplicationStorageManager() { + if (applicationStorageManager == null) { + synchronized (Util.class) { + if (applicationStorageManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + applicationStorageManager = (ApplicationStorageManager) ctx + .getOSGiService(ApplicationStorageManager.class, null); + if (applicationStorageManager == null) { + String msg = "Application Storage Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return applicationStorageManager; + } + + + /** + * To get the Subscription Manager from the osgi context. + * @return SubscriptionManager instance in the current osgi context. + */ + public static SubscriptionManager getSubscriptionManager() { + if (subscriptionManager == null) { + synchronized (Util.class) { + if (subscriptionManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + subscriptionManager = + (SubscriptionManager) ctx.getOSGiService(SubscriptionManager.class, null); + if (subscriptionManager == null) { + String msg = "Subscription Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + + return subscriptionManager; + } + + public static DeviceManagementProviderService getDeviceManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + DeviceManagementProviderService deviceManagementProviderService = + (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.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/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 ab294dc15ff..87e9e687b8c 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 @@ -337,48 +337,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public Application getApplicationById(String id, int tenantId) throws - ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting application with the id:" + id); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - 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, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " - + "AP_APP.APP_ID= ? AND AP_APP.TENANT_ID=?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, id); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application with the id:" + id); - } - - return Util.loadApplication(rs); - - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app id " + id + - " While executing query ", e); - } catch (JSONException e) { - throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - Util.cleanupResources(stmt, rs); - } - } - @Override public Application getApplicationById(int applicationId, int tenantId) throws ApplicationManagementDAOException { @@ -391,11 +349,12 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { conn = this.getDBConnection(); 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, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.ID=? AND " - + "AP_APP.TENANT_ID=?;"; + "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, " + + "DM_DEVICE_TYPE_ID AS DEVICE_TYPE_ID " + + "FROM AP_APP " + + "WHERE AP_APP.ID=? AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); stmt.setInt(1, applicationId); 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 f01753a53b7..09ddddf5bea 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 @@ -64,9 +64,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements ResultSet resultSet = null; String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE," - + "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE," + + "STORED_LOCATION,ICON_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, " + + "APP_HASH_VALUE," + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,CREATED_BY,AP_APP_ID) " - + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?);"; + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?);"; int index = 0; String generatedColumns[] = {"ID"}; @@ -80,6 +81,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setString(++index, String.valueOf(applicationRelease.getPackageName())); statement.setDouble(++index, applicationRelease.getPrice()); statement.setString(++index, applicationRelease.getAppStoredLoc()); + statement.setString(++index, applicationRelease.getIconLoc()); statement.setString(++index, applicationRelease.getBannerLoc()); statement.setString(++index, applicationRelease.getScreenshotLoc1()); statement.setString(++index, applicationRelease.getScreenshotLoc2()); @@ -123,7 +125,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, " + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, " + "AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE, AR.STORED_LOCATION, AR.BANNER_LOCATION, " + "AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS " + "SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, " @@ -175,9 +177,9 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELESE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," - + " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " - + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + + String sql = "SELECT AR.ID AS RELEASE_ID, AR.PACKAGE_NAME, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR" + + ".RELEASE_TYPE, AR.APP_PRICE, AR.STORED_LOCATION, AR.ICON_LOCATION, AR.BANNER_LOCATION, AR" + + ".SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + "HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, AR" + ".PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AL.CURRENT_STATE, AL.PREVIOUSE_STATE, AL.UPDATED_BY, " + "AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE " + @@ -276,6 +278,63 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } + @Override + public List getReleaseByState(int appId, int tenantId, String state) throws + ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + ResultSet resultSet = null; + List applicationReleases = new ArrayList<>(); + String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," + + " AR.STORED_LOCATION, AR.ICON_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR" + + ".SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " + + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, " + + "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AR.RATING FROM AP_APP_RELEASE AS " + + "AR where AR.TENANT_ID = ? AND AR.AP_APP_ID=(SELECT AP_APP_ID" + + " FROM AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID = ? AND CURRENT_STATE = ? AND TENANT_ID = ?);"; + + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setInt(1, tenantId); + statement.setInt(2, appId); + statement.setString(3, state); + statement.setInt(4, tenantId); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + ApplicationRelease applicationRelease = new ApplicationRelease(); + applicationRelease.setId(resultSet.getInt("RELEASE_ID")); + applicationRelease.setVersion(resultSet.getString("RELEASE_VERSION")); + applicationRelease.setUuid(resultSet.getString("UUID")); + applicationRelease.setReleaseType(resultSet.getString("RELEASE_TYPE")); + applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); + applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); + applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); + applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); + applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); + applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); + applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); + applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE")); + applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); + applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); + applicationRelease.setApplicationCreator(resultSet.getString("CREATED_BY")); + applicationRelease.setRating(resultSet.getDouble("RATING")); + applicationReleases.add(applicationRelease); + } + return applicationReleases; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database connection exception while trying to get the " + + "release details of the application with id " + appId, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error while getting all the release details of the app id" + appId + " application" + + ", while executing the query " + sql, e); + } finally { + Util.cleanupResources(statement, resultSet); + } + } + /** * To Update starts of an application release. * @@ -354,11 +413,13 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; - String sql = "UPDATE AP_APP_RELEASE SET VERSION = ? AND UUID = ? AND RELEASE_TYPE = ? AND PACKAGE_NAME = ? " - + "AND APP_PRICE = ? AND STORED_LOCATION = ? AND BANNER_LOCATION = ? AND SC_1_LOCATION = ? " - + "AND SC_2_LOCATION = ? AND SC_3_LOCATION = ? AND APP_HASH_VALUE = ? AND SHARED_WITH_ALL_TENANTS = ? " - + "AND APP_META_INFO = ? AND CREATED_BY = ? AND CREATED_AT = ? WHERE AP_APP_ID = ? AND TENANT_ID = ? " - + "AND ID = ?;"; + String sql = + "UPDATE AP_APP_RELEASE " + + "SET VERSION = ? , UUID = ? , RELEASE_TYPE = ? , PACKAGE_NAME = ? " + + ", APP_PRICE = ? , STORED_LOCATION = ? , ICON_LOCATION = ? , BANNER_LOCATION = ? , " + + "SC_1_LOCATION = ? , SC_2_LOCATION = ? , SC_3_LOCATION = ? , APP_HASH_VALUE = ? " + + ", SHARED_WITH_ALL_TENANTS = ? , APP_META_INFO = ? " + + "WHERE AP_APP_ID = ? AND TENANT_ID = ? AND ID = ?;"; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); @@ -368,15 +429,17 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setString(4, applicationRelease.getPackageName()); statement.setDouble(5, applicationRelease.getPrice()); statement.setString(6, applicationRelease.getAppStoredLoc()); - statement.setString(7, applicationRelease.getBannerLoc()); - statement.setString(8, applicationRelease.getScreenshotLoc1()); - statement.setString(9, applicationRelease.getScreenshotLoc2()); - statement.setString(10, applicationRelease.getScreenshotLoc3()); - statement.setString(11, applicationRelease.getAppHashValue()); - statement.setInt(12, applicationRelease.getIsSharedWithAllTenants()); - statement.setString(13, applicationRelease.getMetaData()); - statement.setString(14, applicationRelease.getApplicationCreator()); - statement.setTimestamp(15, new Timestamp(System.currentTimeMillis())); + statement.setString(7, applicationRelease.getIconLoc()); + statement.setString(8, applicationRelease.getBannerLoc()); + statement.setString(9, applicationRelease.getScreenshotLoc1()); + statement.setString(10, applicationRelease.getScreenshotLoc2()); + statement.setString(11, applicationRelease.getScreenshotLoc3()); + statement.setString(12, applicationRelease.getAppHashValue()); + statement.setInt(13, applicationRelease.getIsSharedWithAllTenants()); + statement.setString(14, applicationRelease.getMetaData()); + statement.setInt(15, applicationId); + statement.setInt(16, tenantId); + statement.setInt(17, applicationRelease.getId()); statement.executeUpdate(); } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( @@ -435,6 +498,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements applicationRelease.setPrice(resultSet.getDouble("APP_PRICE")); applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION")); applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION")); + applicationRelease.setIconLoc(resultSet.getString("ICON_LOCATION")); applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1")); applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2")); applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3")); @@ -442,7 +506,6 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); applicationRelease.setApplicationCreator(resultSet.getString("CREATED_BY")); - applicationRelease.setRating(resultSet.getDouble("RATING")); return applicationRelease; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java new file mode 100644 index 00000000000..3bdd8a8d9c3 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/BadRequestException.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.application.mgt.core.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +/** + * Custom exception class for wrapping BadRequest related exceptions. + */ + +public class BadRequestException extends ApplicationManagementException { + + public BadRequestException(String message, Throwable throwable) { + super(message, throwable); + } + + public BadRequestException(String message) { + setMessage(message); + } + +} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ForbiddenException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ForbiddenException.java new file mode 100644 index 00000000000..9618d8bf50c --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ForbiddenException.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.application.mgt.core.exception; + + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +/** + * Exception class that is corresponding to 401 Forbidden response + */ + +public class ForbiddenException extends ApplicationManagementException { + + public ForbiddenException(String message, Throwable throwable) { + super(message, throwable); + } + + public ForbiddenException(String message) { + setMessage(message); + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java new file mode 100644 index 00000000000..0409520cd18 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/UnexpectedServerErrorException.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.core.exception; + + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +public class UnexpectedServerErrorException extends ApplicationManagementException { + + public UnexpectedServerErrorException(String message, Throwable throwable) { + super(message, throwable); + } + + public UnexpectedServerErrorException(String message) { + setMessage(message); + } + +} \ No newline at end of file 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 d8c7706972b..60b02ad4bf2 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 @@ -31,18 +31,21 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationSubscriptionType import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.LifecycleState; -import org.wso2.carbon.device.application.mgt.common.SortingOrder; import org.wso2.carbon.device.application.mgt.common.Tag; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.User; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +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.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; +import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; @@ -51,7 +54,6 @@ import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.user.api.UserRealm; @@ -59,6 +61,7 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -277,18 +280,20 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public Application getApplicationById(int id) throws ApplicationManagementException { + public Application getApplicationById(int appId, String state, boolean handleConnections) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; boolean isAppAllowed = false; - List applicationReleases; + List applicationReleases = null; try { - ConnectionManagerUtil.openDBConnection(); + if (handleConnections) { + ConnectionManagerUtil.openDBConnection(); + } application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplicationById(id, tenantId); + .getApplicationById(appId, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationReleases = getReleases(application.getId()); + applicationReleases = getReleaseInState(appId, state); application.setApplicationReleases(applicationReleases); return application; } @@ -305,14 +310,18 @@ public class ApplicationManagerImpl implements ApplicationManager { return null; } - applicationReleases = getReleases(application.getId()); + if (state != null) { + applicationReleases = getReleaseInState(appId, state); + } application.setApplicationReleases(applicationReleases); return application; } catch (UserStoreException e) { throw new ApplicationManagementException( - "User-store exception while getting application with the application id " + id); + "User-store exception while getting application with the application id " + appId); } finally { - ConnectionManagerUtil.closeDBConnection(); + if (handleConnections) { + ConnectionManagerUtil.closeDBConnection(); + } } } @@ -453,6 +462,20 @@ public class ApplicationManagerImpl implements ApplicationManager { } + @Override + public List getReleaseInState(int applicationId, String state) throws + ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + + Application application = getApplicationIfAccessible(applicationId); + if (log.isDebugEnabled()) { + log.debug("Request is received to retrieve all the releases related with the application " + application + .toString()); + } + ConnectionManagerUtil.getDBConnection(); + return this.applicationReleaseDAO.getReleaseByState(applicationId, tenantId, state); + } + @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -648,7 +671,7 @@ public class ApplicationManagerImpl implements ApplicationManager { * @param applicationUuid UUID of the Application. * @return Application related with the UUID */ - public ApplicationRelease getAppReleaseIfExists(int applicationId, String applicationUuid) throws + private ApplicationRelease getAppReleaseIfExists(int applicationId, String applicationUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; @@ -662,19 +685,16 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + "parameter to get the relevant application."); } - ConnectionManagerUtil.getDBConnection(); applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId); if (applicationRelease == null) { - throw new ApplicationManagementException("Doesn't exist a application release for application ID: " + - applicationId + "and application UUID: " + - applicationUuid); + log.error("Doesn't exist a application release for application ID: " + applicationId + + "and application UUID: " + applicationUuid); } return applicationRelease; } - @Override - public ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) throws + private ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) throws ApplicationManagementException { validateAppReleasePayload(applicationRelease); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -682,9 +702,57 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Updating the Application release. UUID: " + applicationRelease.getUuid() + ", " + "Application Id: " + appId); } + + applicationRelease = this.applicationReleaseDAO.updateRelease(appId, applicationRelease, tenantId); + return applicationRelease; + + } + + @Override + public ApplicationRelease updateApplicationImageArtifact(int appId, String uuid, InputStream iconFileStream, InputStream + bannerFileStream, List attachments) + throws ApplicationManagementException, ResourceManagementException { + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + ApplicationRelease applicationRelease; + try { + ConnectionManagerUtil.getDBConnection(); + applicationRelease = getAppReleaseIfExists(appId, uuid); + if (applicationRelease == null) { + throw new NotFoundException("No App release associated with the app Id " + appId + "and UUID "+ uuid); + } + applicationStorageManager + .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); + updateRelease(appId, applicationRelease); + return applicationRelease; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public ApplicationRelease updateApplicationArtifact(int appId, String uuid, InputStream binaryFile) + throws ApplicationManagementException, ResourceManagementException, RequestValidatingException, DeviceManagementException { + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + ApplicationRelease applicationRelease; try { - ConnectionManagerUtil.openDBConnection(); - applicationRelease = this.applicationReleaseDAO.updateRelease(appId, applicationRelease, tenantId); + ConnectionManagerUtil.getDBConnection(); + applicationRelease = getAppReleaseIfExists(appId, uuid); + + Application application = getApplicationById(appId, null, false); + + List deviceTypes = Util.getDeviceManagementService().getDeviceTypes(); + for (DeviceType deviceType:deviceTypes) { + if (deviceType.getId() == application.getDeviceTypeId()) { + application.setDeviceType(deviceType.getName()); + } + } + if (applicationRelease == null) { + throw new NotFoundException("No App release associated with the app Id " + appId + "and UUID "+ uuid); + } + applicationStorageManager + .updateReleaseArtifacts(applicationRelease, application.getType(), application.getDeviceType(), + binaryFile); + updateRelease(appId, applicationRelease); return applicationRelease; } finally { ConnectionManagerUtil.closeDBConnection(); @@ -762,8 +830,6 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException("Failed to get lifecycle state", e); } catch (ApplicationManagementException e) { throw new ApplicationManagementException("Failed to get application and application management", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } return lifecycleState; } 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 ccce9dddfe1..b8bfdd1ef46 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 @@ -133,8 +133,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } @Override - public ApplicationRelease updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFileStream, - InputStream bannerFileStream, List screenShotStreams) + public void updateImageArtifacts(ApplicationRelease applicationRelease, InputStream + iconFileStream, InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -164,9 +164,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager count++; } } - return uploadImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, screenShotStreams); } catch (ApplicationStorageManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationStorageManagementException("Application Storage exception while trying to" + " update the screen-shot count for the application Release " + applicationRelease.getUuid() + " for the tenant " + tenantId, e); @@ -235,7 +233,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager + applicationRelease.getUuid(), e); } catch (ParsingException e) { throw new ApplicationStorageManagementException( - "Error occured while parsing the artifact file. Application release UUID is " + applicationRelease + "Error occurred while parsing the artifact file. Application release UUID is " + applicationRelease .getUuid(), e); } @@ -295,8 +293,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void deleteApplicationReleaseArtifacts(String directoryPath) throws ApplicationStorageManagementException { - String artifactPath = storagePath + directoryPath; - File artifact = new File(artifactPath); + File artifact = new File(directoryPath); if (artifact.exists()) { try {