diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index 77c84bdf6a..4ec135b074 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -35,8 +35,6 @@ public class Application { private String uuid; - private String identifier; - private String name; private String shortDescription; @@ -119,14 +117,6 @@ public class Application { this.uuid = uuid; } - public String getIdentifier() { - return identifier; - } - - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - public String getName() { return name; } @@ -233,7 +223,7 @@ public class Application { @Override public String toString() { - return "UUID : " + uuid + "\tIdentifier : " + identifier + "\tName : " + name + "\tShort Description : " + return "UUID : " + uuid + "\tName : " + name + "\tShort Description : " + shortDescription + "\tLifecycle State : " + currentLifecycle.getLifecycleState(); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/VisibilityManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/VisibilityManager.java index 670ad5ce79..f8983ef85d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/VisibilityManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/VisibilityManager.java @@ -19,17 +19,39 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.Visibility; +import org.wso2.carbon.device.application.mgt.common.exception.VisibilityManagementException; + +import java.sql.Connection; /** - * This interface manages all the operations related with Application Visibility, and in the case of the restr + * This interface manages all the operations related with Application Visibility. + * This will be invoking the necessary backend calls for the data bases layer + * and provide the functional implementation. */ public interface VisibilityManager { - void addVisibilityMapping(String applicationId, Visibility visibility); - - Visibility getVisibility(String applicationId); + /** + * Add (if there is no visibility configuration for the application) or + * Update (if there is already existing configuration for the application) + * the visibility related configuration for the application + * + * @param applicationUUID The ID of the application + * @param visibility The visibility configuration for the particular application. + */ + void put(String applicationUUID, Visibility visibility) throws VisibilityManagementException; - void updateVisibilityMapping(String applicationId, Visibility visibility); + /** + * Returns the Visibility configuration of the provided applicationUUID. + * + * @param applicationUUID The ID of the application + * @return Visibility configuration + */ + Visibility get(String applicationUUID) throws VisibilityManagementException; - void removeVisibilityMapping(String applicationId); + /** + * Remove the visibility configuration mapping for the provided application. + * + * @param applicationUUID The ID of the application + */ + void remove(String applicationUUID) throws VisibilityManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java index 933219888e..6d93e091f0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java @@ -18,5 +18,23 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; +import org.wso2.carbon.device.application.mgt.common.Visibility; +import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException; + +import java.util.List; + +/** + * This interface provides the list of operations that are performed in the database layer with respect to the visibility. + */ public interface VisibilityDAO { + + int getVisibilityID(Visibility.Type visibilityType) throws VisibilityManagementDAOException; + + void add(int applicationID, int visibilityTypeID, List allowedList) + throws VisibilityManagementDAOException; + + void delete(int applicationId) throws VisibilityManagementDAOException; + + Visibility get(int applicationID) throws VisibilityManagementDAOException; + } 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/DAOFactory.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java index 4a29ee90a7..8ba6eb7daf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java @@ -25,12 +25,14 @@ import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; 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.PlatformDAO; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.release.GenericApplicationReleaseDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.OracleMsSQLPlatformDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.visibility.GenericVisibilityDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -118,6 +120,23 @@ public class DAOFactory { throw new IllegalStateException("Database engine has not initialized properly."); } + /** + * To get the instance of VisibilityDAOImplementation of the particular database engine. + * @return specific VisibilityDAOImplementation + */ + public static VisibilityDAO getVisibilityDAO(){ + if (databaseEngine != null) { + switch (databaseEngine) { + case Constants.DataBaseTypes.DB_TYPE_H2: + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericVisibilityDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); + } + /** * This method initializes the databases by creating the database. * 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 769e8a50ed..2743a120c6 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 @@ -59,7 +59,6 @@ public class Util { application.setId(rs.getInt("ID")); application.setName(rs.getString("NAME")); application.setUuid(rs.getString("UUID")); - application.setIdentifier(rs.getString("IDENTIFIER")); application.setShortDescription(rs.getString("SHORT_DESCRIPTION")); application.setDescription(rs.getString("DESCRIPTION")); application.setScreenShotCount(rs.getInt("SCREEN_SHOT_COUNT")); 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/AbstractDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java index bbc1f85c0e..cc89f0e69b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java @@ -31,9 +31,4 @@ public abstract class AbstractDAOImpl { protected Connection getDBConnection() throws DBConnectionException { return ConnectionManagerUtil.getDBConnection(); } - - @Deprecated - protected Connection getConnection() throws DBConnectionException { - return ConnectionManagerUtil.getConnection(); - } } 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 de6896921c..3aca533e85 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 @@ -67,7 +67,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic int index = 0; try { conn = this.getDBConnection(); - sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, " + sql += "INSERT INTO APPM_APPLICATION (UUID, NAME, SHORT_DESCRIPTION, DESCRIPTION, " + "VIDEO_NAME, SCREEN_SHOT_COUNT, CREATED_BY, CREATED_AT, MODIFIED_AT, " + "APPLICATION_CATEGORY_ID, PLATFORM_ID, TENANT_ID, LIFECYCLE_STATE_ID, " + "LIFECYCLE_STATE_MODIFIED_AT, LIFECYCLE_STATE_MODIFIED_BY) VALUES " @@ -75,7 +75,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(++index, application.getUuid()); - stmt.setString(++index, application.getIdentifier()); stmt.setString(++index, application.getName()); stmt.setString(++index, application.getShortDescription()); stmt.setString(++index, application.getDescription()); @@ -133,7 +132,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { conn = this.getDBConnection(); - sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, " + sql += "SELECT APP.*, APL.NAME AS APL_NAME, " + "CAT.ID AS CAT_ID, CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, " + "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS " + "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON " @@ -222,7 +221,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } try { - conn = this.getConnection(); + conn = this.getDBConnection(); sql += "SELECT COUNT(APP.ID) AS APP_COUNT "; sql += "FROM APPM_APPLICATION AS APP "; sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID "; @@ -266,7 +265,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { conn = this.getDBConnection(); - sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, " + sql += "SELECT APP.*, APL.NAME AS APL_NAME, " + "CAT.ID AS CAT_ID, CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, " + "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS " + "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON " 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/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java new file mode 100644 index 0000000000..86bfd3ad8f --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -0,0 +1,156 @@ +/* +* Copyright (c) 2017, 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.dao.impl.visibility; + +import org.wso2.carbon.device.application.mgt.common.Visibility; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; +import org.wso2.carbon.device.application.mgt.core.dao.common.Util; +import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; +import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Generic database level implementation for the DAO which can be used by different databases. + */ +public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements VisibilityDAO { + + @Override + public int getVisibilityID(Visibility.Type visibilityType) throws VisibilityManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + try { + Connection connection = getDBConnection(); + String sql = "SELECT ID FROM APPM_RESOURCE_TYPE WHERE NAME = ?"; + stmt = connection.prepareStatement(sql); + stmt.setString(1, visibilityType.toString().toUpperCase()); + resultSet = stmt.executeQuery(); + if (resultSet.next()) { + return resultSet.getInt("ID"); + } + return -1; + } catch (DBConnectionException e) { + throw new VisibilityManagementDAOException("Error occured while obtaining the connection " + + "for the visibility management of applicatons"); + } catch (SQLException e) { + throw new VisibilityManagementDAOException("Error occured when trying to get the ID of the" + + " visibility type - " + visibilityType.toString()); + } finally { + Util.cleanupResources(stmt, resultSet); + } + } + + @Override + public void add(int applicationID, int visibilityTypeID, List allowedList) + throws VisibilityManagementDAOException { + PreparedStatement stmt = null; + try { + Connection connection = getDBConnection(); + String sql = "INSERT INTO APPM_VISIBILITY (?, ?, ?)"; + stmt = connection.prepareStatement(sql); + if (allowedList == null) { + stmt.setString(1, null); + stmt.setInt(2, visibilityTypeID); + stmt.setInt(3, applicationID); + stmt.execute(); + } else { + for (String allowed : allowedList) { + stmt.setString(1, allowed); + stmt.setInt(2, visibilityTypeID); + stmt.setInt(3, applicationID); + stmt.addBatch(); + } + stmt.executeBatch(); + } + } catch (DBConnectionException e) { + throw new VisibilityManagementDAOException("Error occured while obtaining the connection " + + "for adding the visibility mapping for the application ID - " + applicationID); + } catch (SQLException e) { + throw new VisibilityManagementDAOException("Error occured while adding the visibility mapping " + + "for the application ID - " + applicationID); + } finally { + Util.cleanupResources(stmt, null); + } + } + + @Override + public void delete(int applicationId) throws VisibilityManagementDAOException { + PreparedStatement stmt = null; + try { + Connection connection = getDBConnection(); + String sql = "DELETE FROM APPM_VISIBILITY WHERE APPLICATION_ID = ?"; + stmt = connection.prepareStatement(sql); + stmt.setInt(1, applicationId); + stmt.execute(); + } catch (DBConnectionException e) { + throw new VisibilityManagementDAOException("Error occured while obtaining the connection " + + "for deleting the visibility mapping for the application ID - " + applicationId); + } catch (SQLException e) { + throw new VisibilityManagementDAOException("Error occured while deleting the visibility mapping " + + "for the application ID - " + applicationId); + } finally { + Util.cleanupResources(stmt, null); + } + } + + public Visibility get(int applicationId) throws VisibilityManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + final String VISIBILITY_TYPE = "VISIBILITY_TYPE"; + final String ALLOWED_VAL = "ALLOWED_VAL"; + try { + Connection connection = getDBConnection(); + String sql = "SELECT APPM_VISIBILITY.VALUE as " + ALLOWED_VAL + ", APPM_RESOURCE_TYPE.NAME AS " + + VISIBILITY_TYPE + " FROM APPM_VISIBILITY JOIN APPM_RESOURCE_TYPE " + + "ON APPM_VISIBILITY.RESOURCE_TYPE_ID = APPM_RESOURCE_TYPE.ID " + + "WHERE APPM_VISIBILITY.APPLICATION_ID = ?"; + stmt = connection.prepareStatement(sql); + stmt.setInt(1, applicationId); + resultSet = stmt.executeQuery(); + Visibility visibility = new Visibility(); + List allowedVal = new ArrayList<>(); + while (resultSet.next()) { + if (visibility.getType() == null) { + visibility.setType(Visibility.Type.valueOf(resultSet.getString(VISIBILITY_TYPE))); + } + String val = resultSet.getString(ALLOWED_VAL); + if (val != null) { + allowedVal.add(val); + } + } + if (!allowedVal.isEmpty()){ + visibility.setAllowedList(allowedVal); + } + return visibility; + } catch (DBConnectionException e) { + throw new VisibilityManagementDAOException("Error occured while obtaining the connection " + + "for getting the visibility mapping for the application ID - " + applicationId); + } catch (SQLException e) { + throw new VisibilityManagementDAOException("Error occured while getting the visibility mapping " + + "for the application ID - " + applicationId); + } finally { + Util.cleanupResources(stmt, resultSet); + } + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/VisibilityManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/VisibilityManagementDAOException.java new file mode 100644 index 0000000000..0b4209658e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/VisibilityManagementDAOException.java @@ -0,0 +1,35 @@ +/* +* Copyright (c) 2017, 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; + +/** + * This is the specialized exception which is thrown when there are database level problems encountered + * when performing the visibility management + */ +public class VisibilityManagementDAOException extends ApplicationManagementException { + + public VisibilityManagementDAOException(String message, Throwable throwable) { + super(message, throwable); + } + + public VisibilityManagementDAOException(String message) { + super(message, new Exception()); + } +} 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 6b2e0411f7..72eff22a86 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 @@ -74,7 +74,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.beginDBTransaction(); // Validating the platform - Platform platform = DAOFactory.getPlatformDAO() + Platform platform = DataHolder.getInstance().getPlatformManager() .getPlatform(application.getUser().getTenantId(), application.getPlatform().getIdentifier()); if (platform == null) { throw new NotFoundException("Invalid platform"); @@ -122,8 +122,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); if (application.getPlatform() != null && application.getPlatform().getIdentifier() != null) { - PlatformDAO platformDAO = DAOFactory.getPlatformDAO(); - Platform platform = platformDAO + Platform platform = DataHolder.getInstance().getPlatformManager() .getPlatform(tenantId, application.getPlatform().getIdentifier()); if (platform == null) { ConnectionManagerUtil.commitDBTransaction(); @@ -136,6 +135,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); application.setModifiedAt(new Date()); Application modifiedApplication = applicationDAO.editApplication(application, tenantId); + DataHolder.getInstance().getVisibilityManager().put(application.getUuid(), application.getVisibility()); ConnectionManagerUtil.commitDBTransaction(); return modifiedApplication; } catch (ApplicationManagementDAOException e) { @@ -161,6 +161,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationDAO.deleteTags(appId); applicationDAO.deleteProperties(appId); applicationDAO.deleteApplication(uuid, tenantId); + DataHolder.getInstance().getVisibilityManager().remove(uuid); ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -189,7 +190,11 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.openDBConnection(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); - return applicationDAO.getApplications(filter, tenantId); + ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); + for (Application application : applicationList.getApplications()) { + application.setVisibility(DataHolder.getInstance().getVisibilityManager().get(application.getUuid())); + } + return applicationList; } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -206,7 +211,7 @@ public class ApplicationManagerImpl implements ApplicationManager { for (LifecycleStateTransition lifecycleStateTransition : nextLifeCycles) { if (log.isDebugEnabled()) { log.debug("Lifecycle state of the application " + applicationUuid + " can be changed to" - + lifecycleStateTransition.getNextState()); + + lifecycleStateTransition.getNextState()); } if (lifecycleStateTransition.getNextState().equalsIgnoreCase(lifecycleIdentifier)) { isAvailableNextState = true; @@ -302,7 +307,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationDAO().getApplication(uuid, tenantId, userName); + Application application = DAOFactory.getApplicationDAO().getApplication(uuid, tenantId, userName); + application.setVisibility(DataHolder.getInstance().getVisibilityManager().get(uuid)); + return application; } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -344,12 +351,13 @@ public class ApplicationManagerImpl implements ApplicationManager { * @return true if the current user has the permission, otherwise false. * @throws UserStoreException UserStoreException */ - private boolean isAuthorized (String username, int tenantId, String permission) throws UserStoreException { + private boolean isAuthorized(String username, int tenantId, String permission) throws UserStoreException { UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), permission, CarbonConstants.UI_PERMISSION_ACTION); } + /** * To validate the application * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/VisibilityManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/VisibilityManagerImpl.java index 612b041233..18c487208e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/VisibilityManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/VisibilityManagerImpl.java @@ -17,30 +17,108 @@ */ package org.wso2.carbon.device.application.mgt.core.impl; +import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.Visibility; +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.TransactionManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.VisibilityManagementException; import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; +import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; +import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; /** - * This is the defaut implementation for the visibility manager. + * This is the default implementation for the visibility manager. */ public class VisibilityManagerImpl implements VisibilityManager { - @Override - public void addVisibilityMapping(String applicationId, Visibility visibility) { - - } @Override - public Visibility getVisibility(String applicationId) { - return null; + public void put(String applicationUUID, Visibility visibility) throws VisibilityManagementException { + if (visibility.getAllowedList() == null && !visibility.getType().equals(Visibility.Type.PUBLIC)) { + throw new VisibilityManagementException("Visibility is configured for '" + visibility.getType() + + "' but doesn't have any allowed list provided!"); + } + boolean isTransactionStarted = false; + try { + Application application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); + if (application != null) { + isTransactionStarted = ConnectionManagerUtil.isTransactionStarted(); + if (!isTransactionStarted) { + ConnectionManagerUtil.beginDBTransaction(); + } + int id = application.getId(); + VisibilityDAO visibilityDAO = DAOFactory.getVisibilityDAO(); + int visibilityTypeId = visibilityDAO.getVisibilityID(visibility.getType()); + visibilityDAO.delete(id); + visibilityDAO.add(id, visibilityTypeId, visibility.getAllowedList()); + if (!isTransactionStarted) { + ConnectionManagerUtil.commitDBTransaction(); + } + } else { + throw new VisibilityManagementException("No application was found with application UUID - " + applicationUUID); + } + } catch (ApplicationManagementException e) { + if (!isTransactionStarted){ + ConnectionManagerUtil.rollbackDBTransaction(); + } + throw new VisibilityManagementException("Problem occured when trying to fetch the application with UUID - " + + applicationUUID, e); + } finally { + if (!isTransactionStarted) { + ConnectionManagerUtil.closeDBConnection(); + } + } } @Override - public void updateVisibilityMapping(String applicationId, Visibility visibility) { - + public Visibility get(String applicationUUID) throws VisibilityManagementException { + try { + Application application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); + if (application != null) { + int id = application.getId(); + VisibilityDAO visibilityDAO = DAOFactory.getVisibilityDAO(); + return visibilityDAO.get(id); + } else { + throw new VisibilityManagementException("No application was found with application UUID - " + applicationUUID); + } + } catch (ApplicationManagementException e) { + throw new VisibilityManagementException("Problem occured when trying to fetch the application with UUID - " + + applicationUUID, e); + } } @Override - public void removeVisibilityMapping(String applicationId) { - + public void remove(String applicationUUID) throws VisibilityManagementException { + boolean isTransactionStarted = false; + try { + Application application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); + if (application != null) { + isTransactionStarted = ConnectionManagerUtil.isTransactionStarted(); + if (!isTransactionStarted) { + ConnectionManagerUtil.beginDBTransaction(); + } + int id = application.getId(); + VisibilityDAO visibilityDAO = DAOFactory.getVisibilityDAO(); + visibilityDAO.delete(id); + if (!isTransactionStarted) { + ConnectionManagerUtil.commitDBTransaction(); + } + } else { + throw new VisibilityManagementException("No application was found with application UUID - " + applicationUUID); + } + } catch (ApplicationManagementException e) { + if (!isTransactionStarted){ + ConnectionManagerUtil.rollbackDBTransaction(); + } + throw new VisibilityManagementException("Problem occurred when trying to fetch the application with UUID - " + + applicationUUID, e); + } finally { + if (!isTransactionStarted) { + ConnectionManagerUtil.closeDBConnection(); + } + } } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java index 426b13b02e..a153b0bc14 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java @@ -27,7 +27,6 @@ import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateMana import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; -import org.wso2.carbon.device.application.mgt.common.services.VisibilityTypeManager; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.user.core.service.RealmService; @@ -52,8 +51,6 @@ public class DataHolder { private PlatformManager platformManager; - private VisibilityTypeManager visibilityTypeManager; - private SubscriptionManager subscriptionManager; private VisibilityManager visibilityManager; @@ -126,14 +123,6 @@ public class DataHolder { this.platformManager = platformManager; } - public VisibilityTypeManager getVisibilityTypeManager() { - return visibilityTypeManager; - } - - public void setVisibilityTypeManager(VisibilityTypeManager visibilityTypeManager) { - this.visibilityTypeManager = visibilityTypeManager; - } - public SubscriptionManager getSubscriptionManager() { return subscriptionManager; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java index 1a808090ef..3ea5dcf23f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java @@ -32,7 +32,6 @@ import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateMana import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; -import org.wso2.carbon.device.application.mgt.common.services.VisibilityTypeManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; @@ -113,10 +112,6 @@ public class ServiceComponent { DataHolder.getInstance().setVisibilityManager(visibilityManager); bundleContext.registerService(VisibilityManager.class.getName(), visibilityManager, null); - VisibilityTypeManager visibilityTypeManager = ApplicationManagementUtil.getVisibilityTypeManagerInstance(); - DataHolder.getInstance().setVisibilityTypeManager(visibilityTypeManager); - bundleContext.registerService(VisibilityTypeManager.class.getName(), visibilityTypeManager, null); - ApplicationStorageManager applicationStorageManager = ApplicationManagementUtil .getApplicationStorageManagerInstance(); DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java index de1a03255b..8f8868a000 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java @@ -30,7 +30,6 @@ import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateMana import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; -import org.wso2.carbon.device.application.mgt.common.services.VisibilityTypeManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.config.Extension; @@ -81,12 +80,6 @@ public class ApplicationManagementUtil { return getInstance(extension, PlatformManager.class); } - public static VisibilityTypeManager getVisibilityTypeManagerInstance() throws InvalidConfigurationException { - ConfigurationManager configurationManager = ConfigurationManager.getInstance(); - Extension extension = configurationManager.getExtension(Extension.Name.VisibilityTypeManager); - return getInstance(extension, VisibilityTypeManager.class); - } - public static VisibilityManager getVisibilityManagerInstance() throws InvalidConfigurationException { ConfigurationManager configurationManager = ConfigurationManager.getInstance(); Extension extension = configurationManager.getExtension(Extension.Name.VisibilityManager); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index ce957da19e..5122f5e137 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -161,6 +161,11 @@ public class ConnectionManagerUtil { return inTransaction; } + public static boolean isTransactionStarted() throws DBConnectionException { + Connection connection = getDBConnection(); + return inTransaction(connection); + } + @Deprecated public static ThreadLocal getCurrentConnection() { return currentConnection; diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index 4845697026..5c6d5558a3 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -352,27 +352,20 @@ CREATE TABLE IF NOT EXISTS `APPM_VISIBILITY` ( `ID` INT NOT NULL AUTO_INCREMENT, `VALUE` VARCHAR(255) NOT NULL, `RESOURCE_TYPE_ID` INT NOT NULL, - `APPLICATION_RELEASE_ID` INT NULL, `APPLICATION_ID` INT NULL, - PRIMARY KEY (`ID`, `RESOURCE_TYPE_ID`), + PRIMARY KEY (`ID`), INDEX `FK_APPM_VISIBILITY_RESOURCE_TYPE` (`RESOURCE_TYPE_ID` ASC), - INDEX `FK_VISIBILITY_APPLICATION_RELEASE` (`APPLICATION_RELEASE_ID` ASC), INDEX `FK_VISIBILITY_APPLICATION` (`APPLICATION_ID` ASC), CONSTRAINT `fk_APPM_VISIBILITY_APPM_RESOURCE_TYPE1` FOREIGN KEY (`RESOURCE_TYPE_ID`) REFERENCES `APPM_RESOURCE_TYPE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT `fk_APPM_VISIBILITY_APPM_APPLICATION_RELEASE1` - FOREIGN KEY (`APPLICATION_RELEASE_ID`) - REFERENCES `APPM_APPLICATION_RELEASE` (`ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, CONSTRAINT `fk_APPM_VISIBILITY_APPM_APPLICATION1` FOREIGN KEY (`APPLICATION_ID`) REFERENCES `APPM_APPLICATION` (`ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + ON DELETE CASCADE + ON UPDATE CASCADE ) ENGINE = InnoDB;