Improve application releases getting logic

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 9978af097d
commit 74be4d8a82

@ -131,15 +131,6 @@ public interface ApplicationManager {
*/ */
Boolean isUserAllowable(List<UnrestrictedRole> unrestrictedRoles, String userName) throws ApplicationManagementException; Boolean isUserAllowable(List<UnrestrictedRole> unrestrictedRoles, String userName) throws ApplicationManagementException;
/**
* To get the release of a particular Application.
*
* @param applicationId ID of the Application.
* @param state state of the Application.
* @return the List of the Application releases related with the particular Application.
* @throws ApplicationManagementException Application Management Exception.
*/
List<ApplicationRelease> getReleaseInState(int applicationId, String state) throws ApplicationManagementException;
/** /**
* To get all the releases of a particular Application. * To get all the releases of a particular Application.

@ -59,17 +59,17 @@ public class Util {
List<Application> applications = new ArrayList<>(); List<Application> applications = new ArrayList<>();
Application application = null; Application application = null;
int applicatioId = -1; int applicationId = -1;
while (rs.next()) { while (rs.next()) {
if (applicatioId != rs.getInt("APP_ID")) { if (applicationId != rs.getInt("APP_ID")) {
if (application != null) { if (application != null) {
applications.add(application); applications.add(application);
} }
applicatioId = rs.getInt("APP_ID"); applicationId = rs.getInt("APP_ID");
application = new Application(); application = new Application();
application.setId(applicatioId); 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"));
application.setAppCategory(rs.getString("APP_CATEGORY")); application.setAppCategory(rs.getString("APP_CATEGORY"));
@ -160,29 +160,37 @@ public class Util {
/** /**
* Populates {@link ApplicationRelease} object with the result obtained from the database. * Populates {@link ApplicationRelease} object with the result obtained from the database.
* *
* @param rs {@link ResultSet} from obtained from the database * @param resultSet {@link ResultSet} from obtained from the database
* @return {@link ApplicationRelease} object populated with the data * @return {@link ApplicationRelease} object populated with the data
* @throws SQLException If unable to populate {@link ApplicationRelease} object with the data * @throws SQLException If unable to populate {@link ApplicationRelease} object with the data
*/ */
public static ApplicationRelease readApplicationRelease(ResultSet rs) throws SQLException { public static ApplicationRelease loadApplicationRelease(ResultSet resultSet) throws SQLException {
ApplicationRelease appRelease = new ApplicationRelease(); 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.setPackageName(resultSet.getString("PACKAGE_NAME"));
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"));
applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE"));
applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED"));
applicationRelease.setMetaData(resultSet.getString("APP_META_INFO"));
applicationRelease.setRating(resultSet.getDouble("RATING"));
appRelease.setId(rs.getInt("RELEASE_ID")); LifecycleState lifecycleState = new LifecycleState();
appRelease.setVersion(rs.getString("VERSION")); lifecycleState.setCurrentState(resultSet.getString("CURRENT_STATE"));
appRelease.setUuid(rs.getString("UUID")); lifecycleState.setPreviousState(resultSet.getString("PREVIOUS_STATE"));
appRelease.setReleaseType(rs.getString("RELEASE_TYPE")); lifecycleState.setUpdatedBy(resultSet.getString("UPDATED_BY"));
appRelease.setPrice(rs.getDouble("APP_PRICE")); lifecycleState.setUpdatedAt(resultSet.getTimestamp("UPDATED_AT"));
appRelease.setAppHashValue(rs.getString("APP_HASH_VALUE"));
appRelease.setAppStoredLoc(rs.getString("STORED_LOCATION"));
appRelease.setBannerLoc(rs.getString("BANNER_LOCATION"));
appRelease.setRating(rs.getDouble("RATING"));
appRelease.setIsSharedWithAllTenants(rs.getInt("SHARED_WITH_ALL_TENANTS"));
appRelease.setMetaData(rs.getString("APP_META_INFO"));
appRelease.setScreenshotLoc1(rs.getString("SC_1_LOCATION"));
appRelease.setScreenshotLoc2(rs.getString("SC_2_LOCATION"));
appRelease.setScreenshotLoc3(rs.getString("SC_3_LOCATION"));
return appRelease; applicationRelease.setLifecycleState(lifecycleState);
return applicationRelease;
} }
/** /**

@ -644,7 +644,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
Application application = null; Application application = null;
while (rs.next()) { while (rs.next()) {
ApplicationRelease appRelease = Util.readApplicationRelease(rs); ApplicationRelease appRelease = Util.loadApplicationRelease(rs);
application = new Application(); application = new Application();
application.setId(rs.getInt("APP_ID")); application.setId(rs.getInt("APP_ID"));

@ -138,7 +138,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return constructApplicationRelease(resultSet); return Util.loadApplicationRelease(resultSet);
} }
return null; return null;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
@ -185,7 +185,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return constructApplicationRelease(resultSet); return Util.loadApplicationRelease(resultSet);
} }
return null; return null;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
@ -219,7 +219,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
+ "AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, " + "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.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 AS APP_META_INFO, " + "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO AS APP_META_INFO, "
+ "AR.RATING AS RATING FROM AP_APP_RELEASE AS AR where AR.AP_APP_ID=? AND AR.TENANT_ID = ?;"; + "AR.RATING AS RATING AL.CURRENT_STATE AS CURRENT_STATE, AL.PREVIOUS_STATE AS PREVIOUSE_STATE, "
+ "AL.UPDATED_BY AS UPDATED_BY, AL.UPDATED_AT AS UPDATED_AT FROM AP_APP_RELEASE "
+ "AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE AR.AP_APP_ID=? AND AL.AP_APP_RELEASE_ID=AR.ID AND "
+ "AR.TENANT_ID=? AND AL.TENANT_ID=AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC LIMIT 1;";
try { try {
connection = this.getDBConnection(); connection = this.getDBConnection();
@ -229,9 +232,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
ApplicationRelease applicationRelease = constructApplicationRelease(resultSet); ApplicationRelease applicationRelease = Util.loadApplicationRelease(resultSet);
applicationReleases.add(applicationRelease); applicationReleases.add(applicationRelease);
} }
return applicationReleases; return applicationReleases;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
@ -253,24 +255,26 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<ApplicationRelease> applicationReleases = new ArrayList<>(); List<ApplicationRelease> applicationReleases = new ArrayList<>();
String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID AS UUID, AR.RELEASE_TYPE AS "
+ " AR.STORED_LOCATION, AR.ICON_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR" + + "RELEASE_TYPE, AR.PACKAGE_NAME AS PACKAGE_NAME, AR.APP_PRICE AS APP_PRICE, AR.STORED_LOCATION AS "
".SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, " + "STORED_LOCATION, AR.BANNER_LOCATION AS BANNER_LOCATION, ICON_LOCATION, AR.SC_1_LOCATION AS "
+ "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.RATING FROM AP_APP_RELEASE AS " + "SCREEN_SHOT_1, AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, "
+ "AR where AR.TENANT_ID = ? AND AR.AP_APP_ID=(SELECT AP_APP_ID" + + "AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO AS "
" FROM AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID = ? AND CURRENT_STATE = ? AND TENANT_ID = ?);"; + "APP_META_INFO , AR.RATING AS RATING, AL.CURRENT_STATE, AL.PREVIOUS_STATE, AL.UPDATED_BY, "
+ "AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL "
+ "WHERE AR.AP_APP_ID=? AND AL.AP_APP_RELEASE_ID=AR.ID AND AL.CURRENT_STATE=? AND AR.TENANT_ID=? AND "
+ "AL.TENANT_ID=AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;";
try { try {
connection = this.getDBConnection(); connection = this.getDBConnection();
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
statement.setInt(1, tenantId); statement.setInt(1, appId);
statement.setInt(2, appId); statement.setString(2, state);
statement.setString(3, state); statement.setInt(3, tenantId);
statement.setInt(4, tenantId);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
ApplicationRelease appRelease = constructApplicationRelease(resultSet); ApplicationRelease appRelease = Util.loadApplicationRelease(resultSet);
applicationReleases.add(appRelease); applicationReleases.add(appRelease);
} }
return applicationReleases; return applicationReleases;
@ -499,31 +503,4 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
} }
} }
/**
* This method is capable to construct {@link ApplicationRelease} and return the object
*
* @param resultSet result set obtained from the query executing.
* @throws SQLException SQL exception while accessing result set data.
*/
private ApplicationRelease constructApplicationRelease(ResultSet resultSet) throws SQLException {
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.setPackageName(resultSet.getString("PACKAGE_NAME"));
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"));
applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE"));
applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED"));
applicationRelease.setMetaData(resultSet.getString("APP_META_INFO"));
applicationRelease.setRating(resultSet.getDouble("RATING"));
return applicationRelease;
}
} }

@ -117,6 +117,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
return null; return null;
} }
if (!application.getUnrestrictedRoles().isEmpty()) {
application.setIsRestricted(true);
} else {
application.setIsRestricted(false);
}
// Insert to application table // Insert to application table
int appId = this.applicationDAO.createApplication(application, deviceType.getId()); int appId = this.applicationDAO.createApplication(application, deviceType.getId());
@ -134,18 +139,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId); log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId);
} }
} }
if (!application.getUnrestrictedRoles().isEmpty()) { if (application.getIsRestricted()) {
application.setIsRestricted(true);
this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId); this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId);
if(log.isDebugEnabled()){ if(log.isDebugEnabled()){
log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." + log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." +
" App Id:" + appId); " App Id:" + appId);
} }
} else {
if(log.isDebugEnabled()){
log.debug("App is not restricted to role. App Id:" + appId);
}
application.setIsRestricted(false);
} }
if (application.getApplicationReleases().size() > 1 ){ if (application.getApplicationReleases().size() > 1 ){
throw new ApplicationManagementException( throw new ApplicationManagementException(
@ -284,10 +283,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (handleConnections) { if (handleConnections) {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
} }
application = ApplicationManagementDAOFactory.getApplicationDAO() application = this.applicationDAO.getApplicationById(appId, tenantId);
.getApplicationById(appId, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationReleases = getReleaseInState(appId, state); applicationReleases = getReleaseInState(appId, state, tenantId);
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(applicationReleases);
return application; return application;
} }
@ -303,9 +301,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (!isAppAllowed) { if (!isAppAllowed) {
return null; return null;
} }
if (state != null) { applicationReleases = getReleaseInState(appId, state, tenantId);
applicationReleases = getReleaseInState(appId, state);
}
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(applicationReleases);
return application; return application;
} catch (UserStoreException e) { } catch (UserStoreException e) {
@ -439,8 +435,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
for (ApplicationRelease applicationRelease : applicationReleases) { for (ApplicationRelease applicationRelease : applicationReleases) {
LifecycleState lifecycleState = null; LifecycleState lifecycleState = null;
try { try {
lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while getting the latest lifecycle state for the application release UUID: " "Error occurred while getting the latest lifecycle state for the application release UUID: "
@ -471,20 +466,27 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
private List<ApplicationRelease> getReleaseInState(int applicationId, String state, int tenantId)
throws ApplicationManagementException {
List<ApplicationRelease> applicationReleases;
List<ApplicationRelease> filteredReleases = new ArrayList<>();
// todo check whether this rquired or not
@Override
public List<ApplicationRelease> getReleaseInState(int applicationId, String state) throws
ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Application application = getApplicationIfAccessible(applicationId); Application application = getApplicationIfAccessible(applicationId);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request is received to retrieve all the releases related with the application " + application log.debug("Request is received to retrieve all the releases related with the application " + application
.toString()); .toString());
} }
ConnectionManagerUtil.getDBConnection(); applicationReleases = this.applicationReleaseDAO.getReleases(applicationId, tenantId);
return this.applicationReleaseDAO.getReleaseByState(applicationId, tenantId, state); if (state != null && !state.isEmpty()) {
for (ApplicationRelease applicationRelease : applicationReleases) {
if (state.equals(applicationRelease.getLifecycleState().getCurrentState())) {
filteredReleases.add(applicationRelease);
}
}
return filteredReleases;
}
return applicationReleases;
} }
@Override @Override

Loading…
Cancel
Save