Add modification for app getting logic

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 9dc885e888
commit dc518dcca2

@ -80,12 +80,19 @@ public class Application {
value = "Id of the Related device type of the application", value = "Id of the Related device type of the application",
example = "1, 2, 3") example = "1, 2, 3")
private int deviceTypeId; private int deviceTypeId;
@ApiModelProperty(name = "deviceType", @ApiModelProperty(name = "deviceType",
value = "Related device type of the application", value = "Related device type of the application",
required = true, required = true,
example = "IoS, Android, Arduino, RaspberryPi etc") example = "IoS, Android, Arduino, RaspberryPi etc")
private String deviceType; private String deviceType;
@ApiModelProperty(name = "status",
value = "Application status",
required = true,
example = "REMOVED, ACTIVE")
private String status;
@ApiModelProperty(name = "applicationReleases", @ApiModelProperty(name = "applicationReleases",
value = "List of application releases", value = "List of application releases",
required = true) required = true)
@ -190,4 +197,8 @@ public class Application {
public void setDeviceTypeId(int deviceTypeId) { public void setDeviceTypeId(int deviceTypeId) {
this.deviceTypeId = deviceTypeId; this.deviceTypeId = deviceTypeId;
} }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
} }

@ -18,6 +18,8 @@
*/ */
package org.wso2.carbon.device.application.mgt.common; package org.wso2.carbon.device.application.mgt.common;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
/** /**
* Filter represents a criteria that can be used for searching applications. * Filter represents a criteria that can be used for searching applications.
*/ */
@ -63,6 +65,11 @@ public class Filter {
*/ */
private String currentAppReleaseState; private String currentAppReleaseState;
/***
* Supported device type for the application. i.e Android, iOS, Windows etc
*/
private DeviceType deviceType;
public int getLimit() { public int getLimit() {
return limit; return limit;
} }
@ -119,11 +126,13 @@ public class Filter {
this.appCategory = appCategory; this.appCategory = appCategory;
} }
public String getCurrentAppReleaseState() { public String getCurrentAppReleaseState() { return currentAppReleaseState; }
return currentAppReleaseState;
}
public void setCurrentAppReleaseState(String currentAppReleaseState) { public void setCurrentAppReleaseState(String currentAppReleaseState) {
this.currentAppReleaseState = currentAppReleaseState; this.currentAppReleaseState = currentAppReleaseState;
} }
public DeviceType getDeviceType() { return deviceType; }
public void setDeviceType(DeviceType deviceType) { this.deviceType = deviceType; }
} }

@ -78,6 +78,7 @@ public class Util {
application.setSubType(rs.getString("SUB_TYPE")); application.setSubType(rs.getString("SUB_TYPE"));
application.setPaymentCurrency(rs.getString("CURRENCY")); application.setPaymentCurrency(rs.getString("CURRENCY"));
application.setIsRestricted(rs.getBoolean("RESTRICTED")); application.setIsRestricted(rs.getBoolean("RESTRICTED"));
application.setStatus(rs.getString("STATUS"));
String tag = rs.getString("APP_TAG"); String tag = rs.getString("APP_TAG");
String unrestrictedRole = rs.getString("ROLE"); String unrestrictedRole = rs.getString("ROLE");
if (tag != null) { if (tag != null) {

@ -133,28 +133,32 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
ResultSet rs = null; ResultSet rs = null;
ApplicationList applicationList = new ApplicationList(); ApplicationList applicationList = new ApplicationList();
Pagination pagination = new Pagination(); Pagination pagination = new Pagination();
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" String sql = "SELECT "
+ " AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP.CURRENCY AS CURRENCY, " + "AP_APP.ID AS APP_ID,"
+ "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " + " AP_APP.NAME AS APP_NAME,"
+ "AS ROLE FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) " + " 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 LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) "
+ "LEFT JOIN AP_UNRESTRICTED_ROLE ON AP_APP.ID = AP_UNRESTRICTED_ROLE.AP_APP_ID) " + "LEFT JOIN AP_UNRESTRICTED_ROLE ON AP_APP.ID = AP_UNRESTRICTED_ROLE.AP_APP_ID) "
+ "WHERE AP_APP.TENANT_ID = ? AND AP_APP.STATUS != ?"; + "WHERE AP_APP.TENANT_ID = ?";
if (filter == null) { if (filter == null) {
throw new ApplicationManagementDAOException("Filter need to be instantiated"); throw new ApplicationManagementDAOException("Filter need to be instantiated");
} }
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
sql += " AND AP_APP.TYPE "; sql += " AND AP_APP.TYPE ";
sql += "= ?"; sql += "= ?";
} }
if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) { if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) {
sql += " AND AP_APP.APP_CATEGORY "; sql += " AND AP_APP.APP_CATEGORY ";
sql += "= ?"; sql += "= ?";
} }
if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { if (filter.getAppName() != null && !filter.getAppName().isEmpty()) {
sql += " AND LOWER (AP_APP.NAME) "; sql += " AND LOWER (AP_APP.NAME) ";
if (filter.isFullMatch()) { if (filter.isFullMatch()) {
@ -163,6 +167,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
sql += "LIKE ?"; sql += "LIKE ?";
} }
} }
if (filter.getDeviceType() != null ) {
sql += " AND AP_APP.DEVICE_TYPE_ID ";
sql += "= ?";
}
String defaultSortOrder = "ASC"; String defaultSortOrder = "ASC";
if (filter.getSortBy() != null && !filter.getSortBy().isEmpty()) { if (filter.getSortBy() != null && !filter.getSortBy().isEmpty()) {
@ -177,7 +185,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection(); conn = this.getDBConnection();
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(paramIndex++, tenantId); stmt.setInt(paramIndex++, tenantId);
stmt.setString(paramIndex++, AppLifecycleState.REMOVED.toString());
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
stmt.setString(paramIndex++, filter.getAppType()); stmt.setString(paramIndex++, filter.getAppType());
@ -192,6 +199,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.setString(paramIndex++, "%" + filter.getAppName().toLowerCase() + "%"); stmt.setString(paramIndex++, "%" + filter.getAppName().toLowerCase() + "%");
} }
} }
if (filter.getDeviceType() != null ) {
stmt.setInt(paramIndex++, filter.getDeviceType().getId());
}
if (filter.getLimit() == 0) { if (filter.getLimit() == 0) {
stmt.setInt(paramIndex++, 100); stmt.setInt(paramIndex++, 100);

@ -86,7 +86,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
private LifecycleStateDAO lifecycleStateDAO; private LifecycleStateDAO lifecycleStateDAO;
private LifecycleStateManger lifecycleStateManger; private LifecycleStateManger lifecycleStateManger;
public ApplicationManagerImpl() { public ApplicationManagerImpl() {
initDataAccessObjects(); initDataAccessObjects();
lifecycleStateManger = DataHolder.getInstance().getLifecycleStateManager(); lifecycleStateManger = DataHolder.getInstance().getLifecycleStateManager();
@ -95,7 +94,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private void initDataAccessObjects() { private void initDataAccessObjects() {
this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO();
this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
} }
@ -106,8 +105,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @throws RequestValidatingException if application creating request is invalid, returns {@link RequestValidatingException} * @throws RequestValidatingException if application creating request is invalid, returns {@link RequestValidatingException}
* @throws ApplicationManagementException Catch all other throwing exceptions and returns {@link ApplicationManagementException} * @throws ApplicationManagementException Catch all other throwing exceptions and returns {@link ApplicationManagementException}
*/ */
@Override @Override public Application createApplication(Application application)
public Application createApplication(Application application)
throws RequestValidatingException, ApplicationManagementException { throws RequestValidatingException, ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -205,11 +203,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
@Override @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException {
public ApplicationList getApplications(Filter filter) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationList applicationList; ApplicationList applicationList;
List<Application> appList;
List<ApplicationRelease> applicationReleases; List<ApplicationRelease> applicationReleases;
filter = validateFilter(filter); filter = validateFilter(filter);
@ -218,9 +216,31 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
try { try {
if (filter.getDeviceType() != null ) {
boolean isValidDeviceType = false;
String deviceType = filter.getDeviceType().getName();
List<DeviceType> deviceTypes = Util.getDeviceManagementService().getDeviceTypes();
for (DeviceType dt : deviceTypes) {
if (dt.getName().equals(deviceType)) {
filter.getDeviceType().setId(dt.getId());
isValidDeviceType = true;
break;
}
}
if (!isValidDeviceType) {
throw new BadRequestException("Invalid device type is found, device type is " + deviceType);
}
}
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
applicationList = applicationDAO.getApplications(filter, tenantId); applicationList = applicationDAO.getApplications(filter, tenantId);
if(applicationList != null && applicationList.getApplications() != null && !applicationList appList = applicationList.getApplications();
for ( Application app : appList){
if (AppLifecycleState.REMOVED.toString().equals(app.getStatus())){
appList.remove(app);
}
}
applicationList.setApplications(appList);
if (applicationList.getApplications() != null && !applicationList
.getApplications().isEmpty()) { .getApplications().isEmpty()) {
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationList = getRoleRestrictedApplicationList(applicationList, userName); applicationList = getRoleRestrictedApplicationList(applicationList, userName);
@ -238,13 +258,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"DAO exception while getting applications for the user " + userName + " of tenant " + tenantId, e); "DAO exception while getting applications for the user " + userName + " of tenant " + tenantId, e);
} catch (DeviceManagementException e) {
throw new ApplicationManagementException(
"Error Occured when getting device type instance for " + filter.getDeviceType());
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease)
public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -255,9 +277,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
Application existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId); Application existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (existingApplication == null){ if (existingApplication == null) {
throw new NotFoundException( throw new NotFoundException("Couldn't find application for the application Id: " + applicationId);
"Couldn't find application for the application Id: " + applicationId);
} }
// todo check whether admin or app creator. // todo check whether admin or app creator.
@ -286,7 +307,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
.createRelease(applicationRelease, existingApplication.getId(), tenantId); .createRelease(applicationRelease, existingApplication.getId(), tenantId);
LifecycleState lifecycleState = getLifecycleStateInstant(AppLifecycleState.CREATED.toString(), LifecycleState lifecycleState = getLifecycleStateInstant(AppLifecycleState.CREATED.toString(),
AppLifecycleState.CREATED.toString()); AppLifecycleState.CREATED.toString());
this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), tenantId); this.lifecycleStateDAO
.addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), tenantId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return applicationRelease; return applicationRelease;
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
@ -303,22 +325,21 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while adding new application release lifecycle state to the application release: " "Error occurred while adding new application release lifecycle state to the application release: "
+ applicationId, e); + applicationId, e);
} catch (ApplicationManagementDAOException e){ } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while adding new application release for application " + applicationId, e); "Error occurred while adding new application release for application " + applicationId, e);
}catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred whecn checking whether user is admin user or not. Application release: " "Error occurred whecn checking whether user is admin user or not. Application release: "
+ applicationId, e); + applicationId, e);
} finally{ } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override public Application getApplicationById(int appId, String state) throws ApplicationManagementException {
public Application getApplicationById(int appId, String state) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application; Application application;
@ -358,8 +379,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
@Override @Override public Application getApplicationByUuid(String uuid, String state) throws ApplicationManagementException {
public Application getApplicationByUuid(String uuid, String state) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application; Application application;
@ -399,8 +419,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
private boolean isRoleExists(Collection<String> unrestrictedRoleList, String userName) private boolean isRoleExists(Collection<String> unrestrictedRoleList, String userName) throws UserStoreException {
throws UserStoreException {
String[] roleList; String[] roleList;
roleList = getRolesOfUser(userName); roleList = getRolesOfUser(userName);
for (String unrestrictedRole : unrestrictedRoleList) { for (String unrestrictedRole : unrestrictedRoleList) {
@ -462,8 +481,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
@Override @Override public Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException {
public Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application; Application application;
@ -472,7 +490,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId); application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId);
if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(), if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(),
userName)) { userName)) {
return application; return application;
} }
return null; return null;
@ -484,7 +502,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
// todo rethink about this method // todo rethink about this method
private List<ApplicationRelease> getReleases(Application application, String releaseState) private List<ApplicationRelease> getReleases(Application application, String releaseState)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -521,8 +539,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
if (AppLifecycleState.PUBLISHED.toString() if (AppLifecycleState.PUBLISHED.toString().equals(state) && filteredReleases.size() > 1) {
.equals(state) && filteredReleases.size() > 1) {
log.warn("There are more than one application releases is found which is in PUBLISHED state"); log.warn("There are more than one application releases is found which is in PUBLISHED state");
filteredReleases.sort((r1, r2) -> { filteredReleases.sort((r1, r2) -> {
if (r1.getLifecycleState().getUpdatedAt().after(r2.getLifecycleState().getUpdatedAt())) { if (r1.getLifecycleState().getUpdatedAt().after(r2.getLifecycleState().getUpdatedAt())) {
@ -538,8 +555,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
return applicationReleases; return applicationReleases;
} }
@Override @Override public List<String> deleteApplication(int applicationId) throws ApplicationManagementException {
public List<String> deleteApplication(int applicationId) throws ApplicationManagementException {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
List<String> storedLocations = new ArrayList<>(); List<String> storedLocations = new ArrayList<>();
@ -549,7 +565,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
application = this.applicationDAO.getApplicationById(applicationId, tenantId); application = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (application == null) { if (application == null) {
throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId); throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId);
} }
@ -589,7 +604,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Can't delete application release which has the UUID:" + applicationRelease "Can't delete application release which has the UUID:" + applicationRelease
.getUuid() + " and its belongs to the application which has application ID:" .getUuid()
+ " and its belongs to the application which has application ID:"
+ applicationId + " You have to move the lifecycle state from " + applicationId + " You have to move the lifecycle state from "
+ currentState + " to acceptable state"); + currentState + " to acceptable state");
} }
@ -663,8 +679,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
return directions; return directions;
} }
@Override @Override public String deleteApplicationRelease(int applicationId, String releaseUuid)
public String deleteApplicationRelease(int applicationId, String releaseUuid)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -766,7 +781,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId);
return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager()
.isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), permission, .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), permission,
CarbonConstants.UI_PERMISSION_ACTION); CarbonConstants.UI_PERMISSION_ACTION);
} }
/** /**
@ -802,7 +817,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
"App Type contains in the application creating payload doesn't match with supported app types"); "App Type contains in the application creating payload doesn't match with supported app types");
} }
if (application.getApplicationReleases().size() > 1 ){ if (application.getApplicationReleases().size() > 1) {
throw new RequestValidatingException( throw new RequestValidatingException(
"Invalid payload. Application creating payload should contains one application release, but " "Invalid payload. Application creating payload should contains one application release, but "
+ "the payload contains more than one"); + "the payload contains more than one");
@ -911,8 +926,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
//todo check whether package names are same //todo check whether package names are same
@Override @Override public void updateApplicationArtifact(int appId, String deviceType, String uuid, InputStream binaryFile)
public void updateApplicationArtifact(int appId, String deviceType, String uuid, InputStream binaryFile)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
@ -928,8 +942,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
if (!isValidDeviceType) { if (!isValidDeviceType) {
throw new ValidationException( throw new ValidationException(
"Invalid request to update application release artifact, invalid application type: " + deviceType "Invalid request to update application release artifact, invalid application type: "
+ " for Application id: " + appId + " application release uuid: " + uuid); + deviceType + " for Application id: " + appId + " application release uuid: " + uuid);
} }
if (appId <= 0) { if (appId <= 0) {
throw new ValidationException( throw new ValidationException(
@ -974,7 +988,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException("Error occured while updating application artifact.", e); throw new ApplicationManagementException("Error occured while updating application artifact.", e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new ApplicationManagementException("Error occured while getting supported device types in IoTS", e); throw new ApplicationManagementException("Error occured while getting supported device types in IoTS", e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occured when getting DB connection to update application release artifact of the application, appid: " "Error occured when getting DB connection to update application release artifact of the application, appid: "
+ appId + " and uuid " + uuid + ".", e); + appId + " and uuid " + uuid + ".", e);
@ -1025,13 +1039,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
throws ApplicationManagementException { throws ApplicationManagementException {
if (applicationRelease.getVersion() == null) { if (applicationRelease.getVersion() == null) {
throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for " throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for "
+ "creating release. It cannot be found."); + "creating release. It cannot be found.");
} }
} }
@Override @Override public LifecycleState getLifecycleState(int applicationId, String releaseUuid)
public LifecycleState getLifecycleState(int applicationId, String releaseUuid) throws throws ApplicationManagementException {
ApplicationManagementException {
LifecycleState lifecycleState; LifecycleState lifecycleState;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
@ -1039,7 +1052,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (lifecycleState == null) { if (lifecycleState == null) {
return null; return null;
} }
lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManger.getNextLifecycleStates(lifecycleState.getCurrentState()))); lifecycleState.setNextStates(
new ArrayList<>(lifecycleStateManger.getNextLifecycleStates(lifecycleState.getCurrentState())));
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException("Failed to get lifecycle state from database", e); throw new ApplicationManagementException("Failed to get lifecycle state from database", e);
@ -1049,8 +1063,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
return lifecycleState; return lifecycleState;
} }
@Override @Override public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state)
public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
@ -1098,8 +1111,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
@Override @Override public Application updateApplication(int applicationId, Application application)
public Application updateApplication(int applicationId, Application application) throws ApplicationManagementException{ throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -1115,10 +1128,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId); existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (existingApplication == null) { if (existingApplication == null) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException("Tried to update Application which is not in the publisher, " + throw new NotFoundException("Tried to update Application which is not in the publisher, "
"Please verify application details"); + "Please verify application details");
} }
if (!existingApplication.getUnrestrictedRoles().isEmpty()){ if (!existingApplication.getUnrestrictedRoles().isEmpty()) {
isExistingAppRestricted = true; isExistingAppRestricted = true;
} }
@ -1150,9 +1163,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (isExistingAppRestricted && application.getUnrestrictedRoles().isEmpty()) { if (isExistingAppRestricted && application.getUnrestrictedRoles().isEmpty()) {
visibilityDAO.deleteUnrestrictedRoles(existingApplication.getUnrestrictedRoles(), application.getId(), visibilityDAO.deleteUnrestrictedRoles(existingApplication.getUnrestrictedRoles(), application.getId(),
tenantId); tenantId);
} else if(!isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()){ } else if (!isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()) {
visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), application.getId(), tenantId); visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), application.getId(), tenantId);
} else if ( isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()) { } else if (isExistingAppRestricted && !application.getUnrestrictedRoles().isEmpty()) {
addingRoleList = getDifference(application.getUnrestrictedRoles(), addingRoleList = getDifference(application.getUnrestrictedRoles(),
existingApplication.getUnrestrictedRoles()); existingApplication.getUnrestrictedRoles());
removingRoleList = getDifference(existingApplication.getUnrestrictedRoles(), removingRoleList = getDifference(existingApplication.getUnrestrictedRoles(),
@ -1174,7 +1187,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationDAO.deleteTags(removingTags, application.getId(), tenantId); applicationDAO.deleteTags(removingTags, application.getId(), tenantId);
} }
return applicationDAO.editApplication(application, tenantId); return applicationDAO.editApplication(application, tenantId);
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while checking whether logged in user is ADMIN or not when updating application of application id: " "Error occurred while checking whether logged in user is ADMIN or not when updating application of application id: "
@ -1183,8 +1196,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while updating the application, application id: " + applicationId); "Error occurred while updating the application, application id: " + applicationId);
} } catch (VisibilityManagementDAOException e) {
catch (VisibilityManagementDAOException e){
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while updating the visibility restriction of the application. Application id is " "Error occurred while updating the visibility restriction of the application. Application id is "
@ -1221,7 +1233,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private <T> List<T> getDifference(List<T> list1, Collection<T> list2) { private <T> List<T> getDifference(List<T> list1, Collection<T> list2) {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (T t : list1) { for (T t : list1) {
if(!list2.contains(t)) { if (!list2.contains(t)) {
list.add(t); list.add(t);
} }
} }
@ -1244,10 +1256,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
//todo check whether package names are same //todo check whether package names are same
@Override @Override public boolean updateRelease(int applicationId, String releaseUuid, String deviceType,
public boolean updateRelease(int applicationId, String releaseUuid, String deviceType, ApplicationRelease updateRelease, ApplicationRelease updateRelease, InputStream binaryFileStram, InputStream iconFileStream,
InputStream binaryFileStram, InputStream iconFileStream, InputStream bannerFileStream, InputStream bannerFileStream, List<InputStream> attachments) throws ApplicationManagementException {
List<InputStream> attachments) throws ApplicationManagementException{
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();

@ -32,14 +32,13 @@ import org.apache.commons.validator.routines.UrlValidator;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.ApplicationType;
import org.wso2.carbon.device.application.mgt.common.DeviceType; import org.wso2.carbon.device.application.mgt.common.DeviceTypes;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; 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.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.core.exception.ParsingException; import org.wso2.carbon.device.application.mgt.core.exception.ParsingException;
import org.wso2.carbon.device.application.mgt.core.util.ArtifactsParser; import org.wso2.carbon.device.application.mgt.core.util.ArtifactsParser;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.Constants;
import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -203,11 +202,11 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
"application UUID " + applicationRelease.getUuid()); "application UUID " + applicationRelease.getUuid());
} }
if (DeviceType.ANDROID.toString().equalsIgnoreCase(deviceType)) { if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) {
ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(cloneInputStream[2]); ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(cloneInputStream[2]);
applicationRelease.setVersion(apkMeta.getVersionName()); applicationRelease.setVersion(apkMeta.getVersionName());
applicationRelease.setPackageName(apkMeta.getPackageName()); applicationRelease.setPackageName(apkMeta.getPackageName());
} else if (DeviceType.IOS.toString().equalsIgnoreCase(deviceType)) { } else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) {
NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile); NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile);
applicationRelease applicationRelease
.setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString()); .setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString());

@ -124,6 +124,10 @@ public interface ApplicationManagementAPI {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getApplications( Response getApplications(
@ApiParam(
name = "deviceType",
value = "Supporting device Type of the application")
@QueryParam("device-type") String deviceType,
@ApiParam( @ApiParam(
name = "name", name = "name",
value = "Name of the application") value = "Name of the application")

@ -35,6 +35,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagemen
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; 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.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -68,6 +69,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Override @Override
@Consumes("application/json") @Consumes("application/json")
public Response getApplications( public Response getApplications(
@QueryParam("device-type") String deviceType,
@QueryParam("name") String appName, @QueryParam("name") String appName,
@QueryParam("type") String appType, @QueryParam("type") String appType,
@QueryParam("category") String appCategory, @QueryParam("category") String appCategory,
@ -96,13 +98,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
if (releaseState != null && !releaseState.isEmpty()) { if (releaseState != null && !releaseState.isEmpty()) {
filter.setCurrentAppReleaseState(releaseState); filter.setCurrentAppReleaseState(releaseState);
} }
if (deviceType != null && !deviceType.isEmpty()) {
DeviceType dt = new DeviceType();
dt.setName(deviceType);
filter.setDeviceType(dt);
}
ApplicationList applications = applicationManager.getApplications(filter); ApplicationList applications = applicationManager.getApplications(filter);
if (applications.getApplications().isEmpty()) { if (applications.getApplications().isEmpty()) {
return Response.status(Response.Status.NOT_FOUND) return Response.status(Response.Status.NOT_FOUND)
.entity("Couldn't find any application for requested query.").build(); .entity("Couldn't find any application for requested query.").build();
} }
return Response.status(Response.Status.OK).entity(applications).build(); return Response.status(Response.Status.OK).entity(applications).build();
} catch (ApplicationManagementException e) { } catch(BadRequestException e){
String msg = "Couldn't found a device type for " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}catch (ApplicationManagementException e) {
String msg = "Error occurred while getting the application list for publisher "; String msg = "Error occurred while getting the application list for publisher ";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
@ -158,6 +169,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
try { try {
if (isInvalidReleaseCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application.getType())) { if (isInvalidReleaseCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application.getType())) {
// todo add msg
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }
@ -210,6 +222,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application"; String msg = "Error occurred while creating the application";
log.error(msg, e); log.error(msg, e);
// todo add msg into return
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (ResourceManagementException e) { } catch (ResourceManagementException e) {
String msg = String msg =

Loading…
Cancel
Save