Add API for getting applications

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent e467453fd3
commit e747c0bdb0

@ -19,6 +19,7 @@
package org.wso2.carbon.device.application.mgt.common;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO;
import org.wso2.carbon.device.application.mgt.common.response.Application;
import java.util.List;
@ -27,15 +28,15 @@ import java.util.List;
*/
public class ApplicationList {
private List<ApplicationDTO> applications;
private List<Application> applications;
private Pagination pagination;
public List<ApplicationDTO> getApplications() {
public List<Application> getApplications() {
return applications;
}
public void setApplications(List<ApplicationDTO> applications) {
public void setApplications(List<Application> applications) {
this.applications = applications;
}

@ -18,25 +18,68 @@
*/
package org.wso2.carbon.device.application.mgt.common;
import java.util.List;
/**
* Filter represents a criteria that can be used for searching applications.
*/
public class Filter {
/**
* Name of the application
/***
* Supported device type for the application.
* e.g :- Android, iOS, Windows
*/
private String deviceType;
/***
* Name of the application.
*/
private String appName;
/**
* Type of the application
/***
* Type of the application.
* e.g :- ENTERPRISE, PUBLIC
*/
private String appType;
/***
* Subscription type of the application.
* e.g :- FREE, PAID etc
*/
private String subscriptionType;
/***
* Minimum rating of the application.
* e.g :- 4,5
*/
private int minimumRating;
/***
* Application release version.
*/
private String version;
/***
* Release type of the application release.
* e.g :- Alpha, Beta
*/
private String appReleaseType;
/**
* Category list of the application
*/
private List<String> appCategories;
/**
* Category of the application
* Tag list of the application
*/
private List<String> tags;
/***
* Unrestricted role list. Visibility of the application can restricted through user roles and users can view the
* application who has at least one role in unrestricted role list
*/
private String appCategory;
private List<String> unrestrictedRoles;
/**
* Checking the application name matches fully with given name
@ -59,14 +102,10 @@ public class Filter {
private String sortBy;
/**
* Set as True if required to have only published application release, otherwise set to False
* Current application release state.
* e.g :- CREATED. IN_REVIEW, PUBLISHED etc
*/
private String currentAppReleaseState;
/***
* Supported device type for the application. i.e Android, iOS, Windows etc
*/
private int deviceTypeId;
private String appReleaseState;
public int getLimit() {
return limit;
@ -116,21 +155,43 @@ public class Filter {
this.appType = appType;
}
public String getAppCategory() {
return appCategory;
public List<String> getAppCategories() {
return appCategories;
}
public void setAppCategory(String appCategory) {
this.appCategory = appCategory;
public void setAppCategories(List<String> appCategories) {
this.appCategories = appCategories;
}
public String getCurrentAppReleaseState() { return currentAppReleaseState; }
public List<String> getTags() { return tags; }
public void setCurrentAppReleaseState(String currentAppReleaseState) {
this.currentAppReleaseState = currentAppReleaseState;
}
public void setTags(List<String> tags) { this.tags = tags; }
public List<String> getUnrestrictedRoles() { return unrestrictedRoles; }
public void setUnrestrictedRoles(List<String> unrestrictedRoles) { this.unrestrictedRoles = unrestrictedRoles; }
public String getAppReleaseState() { return appReleaseState; }
public void setAppReleaseState(String appReleaseState) { this.appReleaseState = appReleaseState; }
public String getDeviceType() { return deviceType; }
public void setDeviceType(String deviceType) { this.deviceType = deviceType; }
public String getSubscriptionType() { return subscriptionType; }
public void setSubscriptionType(String subscriptionType) { this.subscriptionType = subscriptionType; }
public int getMinimumRating() { return minimumRating; }
public void setMinimumRating(int minimumRating) { this.minimumRating = minimumRating; }
public String getVersion() { return version; }
public void setVersion(String version) { this.version = version; }
public int getDeviceTypeId() { return deviceTypeId; }
public String getAppReleaseType() { return appReleaseType; }
public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; }
public void setAppReleaseType(String appReleaseType) { this.appReleaseType = appReleaseType; }
}

@ -0,0 +1,42 @@
package org.wso2.carbon.device.application.mgt.common.config;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.
*/
import javax.xml.bind.annotation.XmlElement;
public class RatingConfiguration {
private int minRatingValue;
private int maxRatingValue;
@XmlElement(name = "MinRatingValue")
public int getMinRatingValue() {
return minRatingValue;
}
public void setMinRatingValue(int minRatingValue) {
this.minRatingValue = minRatingValue;
}
@XmlElement(name = "MaxRatingValue")
public int getMaxRatingValue() {
return maxRatingValue;
}
public void setMaxRatingValue(int maxRatingValue) {
this.maxRatingValue = maxRatingValue;
}
}

@ -83,7 +83,7 @@ public interface ApplicationManager {
* @return Applications that matches the given filter criteria.
* @throws ApplicationManagementException ApplicationDTO Management Exception
*/
ApplicationList getApplications(Filter filter, String deviceTypeName) throws ApplicationManagementException;
ApplicationList getApplications(Filter filter) throws ApplicationManagementException;
/**
* To get the ApplicationDTO for given Id.

@ -18,6 +18,7 @@
*/
package org.wso2.carbon.device.application.mgt.core.config;
import org.wso2.carbon.device.application.mgt.common.config.RatingConfiguration;
import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration;
import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState;
@ -48,6 +49,8 @@ public class Configuration {
private String artifactDownloadEndpoint;
private RatingConfiguration ratingConfiguration;
@XmlElement(name = "DatasourceName", required = true)
public String getDatasourceName() {
return datasourceName;
@ -82,15 +85,21 @@ public class Configuration {
this.lifecycleStates = lifecycleStates;
}
@XmlElement(name = "UIConfigs")
public UIConfiguration getUiConfiguration() {
return uiConfiguration;
}
@XmlElement(name = "UIConfigs")
public void setUiConfiguration(UIConfiguration uiConfiguration) {
this.uiConfiguration = uiConfiguration;
}
@XmlElement(name = "RatingConfig")
public RatingConfiguration getRatingConfiguration() { return ratingConfiguration; }
public void setRatingConfiguration(
RatingConfiguration ratingConfiguration) { this.ratingConfiguration = ratingConfiguration; }
@XmlElement(name = "ArtifactDownloadEndpoint", required = true)
public String getArtifactDownloadEndpoint() {
return artifactDownloadEndpoint;

@ -54,6 +54,11 @@ public interface ApplicationDAO {
void addTagMapping (List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException;
List<String> getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException;
List<String> getAppCategories (int appId, int tenantId) throws ApplicationManagementDAOException;
List<CategoryDTO> getAllCategories(int tenantId) throws ApplicationManagementDAOException;
@ -76,11 +81,12 @@ public interface ApplicationDAO {
* To get the applications that satisfy the given criteria.
*
* @param filter Filter criteria.
* @param deviceTypeId ID of the device type
* @param tenantId Id of the tenant.
* @return ApplicationDTO list
* @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception.
*/
ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException;
List<ApplicationDTO> getApplications(Filter filter, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException;
/**
* To get the UUID of latest app release that satisfy the given criteria.

@ -74,7 +74,7 @@ public class Util {
// application.setId(applicationId);
// application.setName(rs.getString("APP_NAME"));
// application.setType(rs.getString("APP_TYPE"));
// application.setAppCategory(rs.getString("APP_CATEGORY"));
// application.setAppCategories(rs.getString("APP_CATEGORY"));
// application.setSubType(rs.getString("SUB_TYPE"));
// application.setPaymentCurrency(rs.getString("CURRENCY"));
// application.setIsRestricted(rs.getBoolean("RESTRICTED"));
@ -139,25 +139,10 @@ public class Util {
application.setStatus(rs.getString("APP_STATUS"));
application.setAppRating(rs.getInt("APP_RATING"));
application.setDeviceTypeId(rs.getInt("APP_DEVICE_TYPE_ID"));
application.getApplicationReleases().add(loadAppRelease(rs));
} else {
ApplicationReleaseDTO appRelease = new ApplicationReleaseDTO();
appRelease.setDescription(rs.getString("RELEASE_DESCRIPTION"));
appRelease.setUuid(rs.getString("RELEASE_UUID"));
appRelease.setReleaseType(rs.getString("RELEASE_TYPE"));
appRelease.setVersion(rs.getString("RELEASE_VERSION"));
appRelease.setInstallerName(rs.getString("AP_RELEASE_STORED_LOC"));
appRelease.setBannerName(rs.getString("RELEASE_BANNER_LOC"));
appRelease.setScreenshotName1("RELEASE_SC1");
appRelease.setScreenshotName2("RELEASE_SC2");
appRelease.setScreenshotName3("RELEASE_SC3");
appRelease.setPrice(rs.getDouble(" RELEASE_PRICE"));
appRelease.setMetaData(rs.getString("RELEASE_META_INFO"));
appRelease.setSupportedOsVersions(rs.getString("RELEASE_SUP_OS_VERSIONS"));
appRelease.setRating(rs.getDouble("RELEASE_RATING"));
appRelease.setCurrentState(rs.getString("RELEASE_CURRENT_STATE"));
appRelease.setRatedUsers(rs.getInt("RATED_USER_COUNT"));
if (application != null && application.getApplicationReleases() != null) {
application.getApplicationReleases().add(appRelease);
application.getApplicationReleases().add(loadAppRelease(rs));
}
}
hasNext = rs.next();
@ -168,6 +153,33 @@ public class Util {
return applications;
}
/**
* Populates {@link ApplicationReleaseDTO} object with the result obtained from the database.
*
* @param rs {@link ResultSet} from obtained from the database
* @return {@link ApplicationReleaseDTO} object populated with the data
* @throws SQLException If unable to populate {@link ApplicationReleaseDTO} object with the data
*/
public static ApplicationReleaseDTO loadAppRelease(ResultSet rs) throws SQLException {
ApplicationReleaseDTO appRelease = new ApplicationReleaseDTO();
appRelease.setDescription(rs.getString("RELEASE_DESCRIPTION"));
appRelease.setUuid(rs.getString("RELEASE_UUID"));
appRelease.setReleaseType(rs.getString("RELEASE_TYPE"));
appRelease.setVersion(rs.getString("RELEASE_VERSION"));
appRelease.setInstallerName(rs.getString("AP_RELEASE_STORED_LOC"));
appRelease.setBannerName(rs.getString("AP_RELEASE_BANNER_LOC"));
appRelease.setScreenshotName1("AP_RELEASE_SC1");
appRelease.setScreenshotName2("AP_RELEASE_SC2");
appRelease.setScreenshotName3("AP_RELEASE_SC3");
appRelease.setPrice(rs.getDouble("RELEASE_PRICE"));
appRelease.setMetaData(rs.getString("RELEASE_META_INFO"));
appRelease.setSupportedOsVersions(rs.getString("RELEASE_SUP_OS_VERSIONS"));
appRelease.setRating(rs.getDouble("RELEASE_RATING"));
appRelease.setCurrentState(rs.getString("RELEASE_CURRENT_STATE"));
appRelease.setRatedUsers(rs.getInt("RATED_USER_COUNT"));
return appRelease;
}
/**
* To create application object from the result set retrieved from the Database.

@ -18,6 +18,7 @@
*/
package org.wso2.carbon.device.application.mgt.core.dao.impl.application;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
@ -124,119 +125,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
// @Override
// public ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException {
// if (log.isDebugEnabled()) {
// log.debug("Getting application data from the database");
// log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
// }
// int paramIndex = 1;
// Connection conn;
// PreparedStatement stmt = null;
// ResultSet rs = null;
// ApplicationList applicationList = new ApplicationList();
// 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 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) "
// + "WHERE AP_APP.TENANT_ID = ?";
//
//
// if (filter == null) {
// throw new ApplicationManagementDAOException("Filter need to be instantiated");
// }
// if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
// sql += " AND AP_APP.TYPE ";
// sql += "= ?";
// }
// if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) {
// sql += " AND AP_APP.APP_CATEGORY ";
// sql += "= ?";
// }
// if (filter.getAppName() != null && !filter.getAppName().isEmpty()) {
// sql += " AND LOWER (AP_APP.NAME) ";
// if (filter.isFullMatch()) {
// sql += "= ?";
// } else {
// sql += "LIKE ?";
// }
// }
// if (filter.getDeviceTypeName() != null ) {
// sql += " AND AP_APP.DEVICE_TYPE_ID ";
// sql += "= ?";
// }
//
// String defaultSortOrder = "ASC";
// if (filter.getSortBy() != null && !filter.getSortBy().isEmpty()) {
// defaultSortOrder = filter.getSortBy();
// }
// sql += " ORDER BY APP_ID " + defaultSortOrder +" LIMIT ? OFFSET ? ";
//
// pagination.setLimit(filter.getLimit());
// pagination.setOffset(filter.getOffset());
//
// try {
// conn = this.getDBConnection();
// stmt = conn.prepareStatement(sql);
// stmt.setInt(paramIndex++, tenantId);
//
// if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
// stmt.setString(paramIndex++, filter.getAppType());
// }
// if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) {
// stmt.setString(paramIndex++, filter.getAppCategory());
// }
// if (filter.getAppName() != null && !filter.getAppName().isEmpty()) {
// if (filter.isFullMatch()) {
// stmt.setString(paramIndex++, filter.getAppName().toLowerCase());
// } else {
// stmt.setString(paramIndex++, "%" + filter.getAppName().toLowerCase() + "%");
// }
// }
// if (filter.getDeviceTypeName() != null ) {
// stmt.setInt(paramIndex++, filter.getDeviceTypeName().getId());
// }
//
//
// if (filter.getLimit() == 0) {
// stmt.setInt(paramIndex++, 100);
// } else {
// stmt.setInt(paramIndex++, filter.getLimit());
// }
// stmt.setInt(paramIndex, filter.getOffset());
// rs = stmt.executeQuery();
// applicationList.setApplications(Util.loadApplications(rs));
// applicationList.setPagination(pagination);
// applicationList.getPagination().setSize(filter.getOffset());
// applicationList.getPagination().setCount(applicationList.getApplications().size());
//
// } catch (SQLException e) {
// throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
// + " " + tenantId + ". While executing " + sql, e);
// } catch (DBConnectionException e) {
// throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while "
// + "getting application list for the tenant " + tenantId,
// e);
// } catch (JSONException e) {
// throw new ApplicationManagementDAOException("Error occurred while parsing JSON ", e);
// } finally {
// Util.cleanupResources(stmt, rs);
// }
// return applicationList;
// }
@Override
public ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException {
public List<ApplicationDTO> getApplications(Filter filter,int deviceTypeId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting application data from the database");
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
@ -245,8 +135,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
ApplicationList applicationList = new ApplicationList();
Pagination pagination = new Pagination();
String sql = "SELECT "
+ "AP_APP.ID AS APP_ID, "
+ "AP_APP.NAME AS APP_NAME, "
@ -281,15 +169,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (filter == null) {
throw new ApplicationManagementDAOException("Filter need to be instantiated");
}
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
sql += " AND AP_APP.TYPE ";
sql += "= ?";
}
if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) {
sql += " AND AP_APP.APP_CATEGORY ";
sql += "= ?";
if (!StringUtils.isEmpty(filter.getAppType())) {
sql += " AND AP_APP.TYPE = ?";
}
if (filter.getAppName() != null && !filter.getAppName().isEmpty()) {
if (!StringUtils.isEmpty(filter.getAppName())) {
sql += " AND LOWER (AP_APP.NAME) ";
if (filter.isFullMatch()) {
sql += "= ?";
@ -297,19 +181,30 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
sql += "LIKE ?";
}
}
if (filter.getDeviceTypeId() > 0 ) {
sql += " AND AP_APP.DEVICE_TYPE_ID ";
sql += "= ?";
if (!StringUtils.isEmpty(filter.getSubscriptionType())) {
sql += " AND AP_APP.SUB_TYPE = ?";
}
String defaultSortOrder = "ASC";
if (filter.getSortBy() != null && !filter.getSortBy().isEmpty()) {
defaultSortOrder = filter.getSortBy();
if (filter.getMinimumRating() > 0) {
sql += " AND AP_APP.RATING >= ?";
}
if (!StringUtils.isEmpty(filter.getVersion())) {
sql += " AND AP_APP_RELEASE.VERSION = ?";
}
if (!StringUtils.isEmpty(filter.getAppReleaseType())) {
sql += " AND AP_APP_RELEASE.RELEASE_TYPE = ?";
}
if (!StringUtils.isEmpty(filter.getAppReleaseState())) {
sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?";
}
if (deviceTypeId > 0) {
sql += " AND AP_APP.DEVICE_TYPE_ID = ?";
}
sql += " ORDER BY APP_ID " + defaultSortOrder +" LIMIT ? OFFSET ? ";
pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset());
String sortingOrder = "ASC";
if (!StringUtils.isEmpty(filter.getSortBy() )) {
sortingOrder = filter.getSortBy();
}
sql += " ORDER BY APP_ID " + sortingOrder +" LIMIT ? OFFSET ? ";
try {
conn = this.getDBConnection();
@ -319,9 +214,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
stmt.setString(paramIndex++, filter.getAppType());
}
if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) {
stmt.setString(paramIndex++, filter.getAppCategory());
}
if (filter.getAppName() != null && !filter.getAppName().isEmpty()) {
if (filter.isFullMatch()) {
stmt.setString(paramIndex++, filter.getAppName().toLowerCase());
@ -329,11 +221,24 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.setString(paramIndex++, "%" + filter.getAppName().toLowerCase() + "%");
}
}
if (filter.getDeviceTypeId() > 0 ) {
stmt.setInt(paramIndex++, filter.getDeviceTypeId());
if (!StringUtils.isEmpty(filter.getSubscriptionType())) {
stmt.setString(paramIndex++, filter.getSubscriptionType());
}
if (filter.getMinimumRating() > 0) {
stmt.setInt(paramIndex++, filter.getMinimumRating());
}
if (!StringUtils.isEmpty(filter.getVersion())) {
stmt.setString(paramIndex++, filter.getVersion());
}
if (!StringUtils.isEmpty(filter.getAppReleaseType())) {
stmt.setString(paramIndex++, filter.getAppReleaseType());
}
if (!StringUtils.isEmpty(filter.getAppReleaseState())) {
stmt.setString(paramIndex++, filter.getAppReleaseState());
}
if (deviceTypeId > 0 ) {
stmt.setInt(paramIndex++, deviceTypeId);
}
if (filter.getLimit() == 0) {
stmt.setInt(paramIndex++, 100);
} else {
@ -341,11 +246,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
stmt.setInt(paramIndex, filter.getOffset());
rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs));
applicationList.setPagination(pagination);
applicationList.getPagination().setSize(filter.getOffset());
applicationList.getPagination().setCount(applicationList.getApplications().size());
return Util.loadApplications(rs);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
+ " " + tenantId + ". While executing " + sql, e);
@ -358,7 +259,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} finally {
Util.cleanupResources(stmt, rs);
}
return applicationList;
}
@Override
@ -972,6 +872,70 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public List<String> getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get tags for given application.");
}
Connection conn;
List<String> tags = new ArrayList<>();
String sql = "SELECT tag.TAG AS TAG "
+ "FROM "
+ "AP_APP_TAG tag INNER JOIN AP_APP_TAG_MAPPING tag_map ON tag.ID = tag_map.AP_APP_TAG_ID "
+ "INNER JOIN AP_APP app ON tag_map.AP_APP_ID = app.ID "
+ "WHERE app.ID = ? and app.TENANT_ID = ?";
try {
conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, appId);
stmt.setInt(2, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
tags.add(rs.getString("TAG"));
}
}
}
return tags;
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection when adding tags", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding tags", e);
}
}
@Override
public List<String> getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get categories for given application.");
}
Connection conn;
List<String> categories = new ArrayList<>();
String sql = "SELECT CATEGORY "
+ "FROM "
+ "AP_APP_CATEGORY cat INNER JOIN AP_APP_CATEGORY_MAPPING cat_map ON cat.ID = cat_map.AP_APP_CATEGORY_ID "
+ "INNER JOIN AP_APP app ON cat_map.AP_APP_ID = app.ID "
+ "WHERE app.ID = ? and app.TENANT_ID = ?";
try {
conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, appId);
stmt.setInt(2, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
categories.add(rs.getString("CATEGORY"));
}
}
}
return categories;
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection when adding tags", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding tags", e);
}
}
@Override
public void deleteTags(List<String> tags, int applicationId, int tenantId) throws ApplicationManagementDAOException {

@ -79,7 +79,9 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
PreparedStatement stmt = null;
ResultSet rs = null;
List<String> unrestrictedRoles = new ArrayList<>();
String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLE WHERE AP_APP_ID = ? AND TENANT_ID = ?;";
String sql = "SELECT ROLE "
+ "FROM AP_UNRESTRICTED_ROLE "
+ "WHERE AP_APP_ID = ? AND TENANT_ID = ?";
try{
conn = this.getDBConnection();
conn.setAutoCommit(false);

@ -30,6 +30,8 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller;
import org.wso2.carbon.device.application.mgt.common.Pagination;
import org.wso2.carbon.device.application.mgt.common.config.RatingConfiguration;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO;
@ -158,15 +160,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
filter.setFullMatch(true);
filter.setAppName(applicationDTO.getName().trim());
filter.setDeviceTypeId(applicationDTO.getDeviceTypeId());
filter.setOffset(0);
filter.setLimit(1);
ConnectionManagerUtil.beginDBTransaction();
ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId);
if (!applicationList.getApplications().isEmpty()) {
List<ApplicationDTO> applicationList = applicationDAO
.getApplications(filter, applicationDTO.getDeviceTypeId(), tenantId);
if (!applicationList.isEmpty()) {
String msg =
"Already an application registered with same name - " + applicationList.getApplications().get(0)
"Already an application registered with same name - " + applicationList.get(0)
.getName();
log.error(msg);
throw new RequestValidatingException(msg);
@ -411,52 +413,110 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public ApplicationList getApplications(Filter filter, String deviceTypeName)
throws ApplicationManagementException {
public ApplicationList getApplications(Filter filter) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationList applicationList;
List<ApplicationDTO> apps;
List<ApplicationReleaseDTO> releases;
DeviceType deviceType;
filter = validateFilter(filter);
if (filter == null) {
throw new ApplicationManagementException("Filter validation failed, Please verify the request payload");
}
ApplicationList applicationList = new ApplicationList();
List<ApplicationDTO> appDTOs;
List<Application> applications = new ArrayList<>();
List<ApplicationDTO> filteredApplications = new ArrayList<>();
DeviceType deviceType = null;
try {
if (!StringUtils.isEmpty(deviceTypeName)){
try {
deviceType = getDeviceTypeData(deviceTypeName);
filter.setDeviceTypeId(deviceType.getId());
} catch (UnexpectedServerErrorException e){
throw new ApplicationManagementException(e.getMessage(), e);
}
validateFilter(filter);
//set default values
if (StringUtils.isEmpty(filter.getSortBy())) {
filter.setSortBy("ASC");
}
if (filter.getLimit() == 0) {
filter.setLimit(20);
}
String deviceTypename = filter.getDeviceType();
if (!StringUtils.isEmpty(deviceTypename)) {
deviceType = getDeviceTypeData(deviceTypename);
}
ConnectionManagerUtil.openDBConnection();
// todo modify this logic, join app n release tables
applicationList = applicationDAO.getApplications(filter, tenantId);
apps = applicationList.getApplications();
for ( ApplicationDTO app : apps){
if (AppLifecycleState.REMOVED.toString().equals(app.getStatus())){
apps.remove(app);
}
if (deviceType == null) {
appDTOs = applicationDAO.getApplications(filter, 0, tenantId);
} else {
appDTOs = applicationDAO.getApplications(filter, deviceType.getId(), tenantId);
}
applicationList.setApplications(apps);
if (applicationList.getApplications() != null && !applicationList
.getApplications().isEmpty()) {
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationList = getRoleRestrictedApplicationList(applicationList, userName);
for (ApplicationDTO app : appDTOs) {
boolean isSearchingApp = true;
List<String> tags = filter.getTags();
List<String> categories = filter.getAppCategories();
List<String> unrestrictedRoles = filter.getUnrestrictedRoles();
if (lifecycleStateManager.getEndState().equals(app.getStatus())) {
isSearchingApp = false;
}
for (ApplicationDTO application : applicationList.getApplications()) {
releases = getReleases(application, filter.getCurrentAppReleaseState());
application.setApplicationReleases(releases);
if (unrestrictedRoles != null && !unrestrictedRoles.isEmpty()) {
if (!isRoleExists(unrestrictedRoles, userName)) {
String msg = "At least one filtering role is not assigned for the user: " + userName
+ ". Hence user " + userName
+ " Can't filter applications by giving these unrestriced role list";
log.error(msg);
throw new BadRequestException(msg);
}
List<String> appUnrestrictedRoles = visibilityDAO.getUnrestrictedRoles(app.getId(), tenantId);
boolean isUnrestrictedRoleExistForApp = false;
for (String role : unrestrictedRoles) {
if (appUnrestrictedRoles.contains(role)) {
isUnrestrictedRoleExistForApp = true;
break;
}
}
if (!isUnrestrictedRoleExistForApp) {
isSearchingApp = false;
}
}
if (tags != null && !tags.isEmpty()) {
List<String> appTagList = applicationDAO.getAppTags(app.getId(), tenantId);
boolean isTagExistForApp = false;
for (String tag : tags) {
if (appTagList.contains(tag)) {
isTagExistForApp = true;
break;
}
}
if (!isTagExistForApp) {
isSearchingApp = false;
}
}
if (categories != null && !categories.isEmpty()) {
List<String> appTagList = applicationDAO.getAppCategories(app.getId(), tenantId);
boolean isCategoryExistForApp = false;
for (String category : categories) {
if (appTagList.contains(category)) {
isCategoryExistForApp = true;
break;
}
}
if (!isCategoryExistForApp) {
isSearchingApp = false;
}
}
if (isSearchingApp) {
filteredApplications.add(app);
}
}
for(ApplicationDTO appDTO : filteredApplications){
applications.add(appDtoToAppResponse(appDTO));
}
applicationList.setApplications(applications);
Pagination pagination = new Pagination();
applicationList.setPagination(pagination);
applicationList.getPagination().setSize(filter.getOffset());
applicationList.getPagination().setCount(applicationList.getApplications().size());
return applicationList;
} catch (UserStoreException e) {
} catch (UnexpectedServerErrorException e){
throw new ApplicationManagementException(e.getMessage(), e);
}catch (UserStoreException e) {
throw new ApplicationManagementException(
"User-store exception while checking whether the user " + userName + " of tenant " + tenantId
+ " has the publisher permission", e);
@ -1165,26 +1225,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @param userName user name
* @return ApplicationDTO related with the UUID
*/
private ApplicationList getRoleRestrictedApplicationList(ApplicationList applicationList, String userName)
throws ApplicationManagementException {
ApplicationList roleRestrictedApplicationList = new ApplicationList();
ArrayList<ApplicationDTO> unRestrictedApplications = new ArrayList<>();
for (ApplicationDTO application : applicationList.getApplications()) {
if (application.getUnrestrictedRoles().isEmpty()) {
unRestrictedApplications.add(application);
} else {
try {
if (isRoleExists(application.getUnrestrictedRoles(), userName)) {
unRestrictedApplications.add(application);
}
} catch (UserStoreException e) {
throw new ApplicationManagementException("Role restriction verifying is failed");
}
}
}
roleRestrictedApplicationList.setApplications(unRestrictedApplications);
return roleRestrictedApplicationList;
}
// private ApplicationList getRoleRestrictedApplicationList(ApplicationList applicationList, String userName)
// throws ApplicationManagementException {
// ApplicationList roleRestrictedApplicationList = new ApplicationList();
// ArrayList<ApplicationDTO> unRestrictedApplications = new ArrayList<>();
// for (ApplicationDTO application : applicationList.getApplications()) {
// if (application.getUnrestrictedRoles().isEmpty()) {
// unRestrictedApplications.add(application);
// } else {
// try {
// if (isRoleExists(application.getUnrestrictedRoles(), userName)) {
// unRestrictedApplications.add(application);
// }
// } catch (UserStoreException e) {
// throw new ApplicationManagementException("Role restriction verifying is failed");
// }
// }
// }
// roleRestrictedApplicationList.setApplications(unRestrictedApplications);
// return roleRestrictedApplicationList;
// }
/**
* To validate a app release creating request and app updating request to make sure all the pre-conditions
@ -1397,20 +1457,55 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
private Filter validateFilter(Filter filter) {
if (filter != null && filter.getAppType() != null) {
boolean isValidRequest = false;
private void validateFilter(Filter filter) throws BadRequestException {
if (filter == null) {
String msg = "Filter validation is failed, Filter shouldn't be null, hence please verify the request payload";
log.error(msg);
throw new BadRequestException(msg);
}
String appType = filter.getAppType();
if (!StringUtils.isEmpty(appType)) {
boolean isValidAppType = false;
for (ApplicationType applicationType : ApplicationType.values()) {
if (applicationType.toString().equals(filter.getAppType())) {
isValidRequest = true;
if (applicationType.toString().equals(appType)) {
isValidAppType = true;
break;
}
}
if (!isValidRequest) {
return null;
if (!isValidAppType) {
String msg =
"Filter validation is failed, Invalid application type is found in filter. Application Type: "
+ appType + " Please verify the request payload";
log.error(msg);
throw new BadRequestException(msg);
}
}
return filter;
RatingConfiguration ratingConfiguration = ConfigurationManager.getInstance().getConfiguration()
.getRatingConfiguration();
int defaultMinRating = ratingConfiguration.getMinRatingValue();
int defaultMaxRating = ratingConfiguration.getMaxRatingValue();
int filteringMinRating = filter.getMinimumRating();
if (filteringMinRating != 0 && (filteringMinRating < defaultMinRating || filteringMinRating > defaultMaxRating))
{
String msg = "Filter validation is failed, Minimum rating value: " + filteringMinRating
+ " is not in the range of default minimum rating value " + defaultMaxRating
+ " and default maximum rating " + defaultMaxRating;
log.error(msg);
throw new BadRequestException(msg);
}
String appReleaseState = filter.getAppReleaseState();
if (!StringUtils.isEmpty(appReleaseState) && !lifecycleStateManager.isStateExist(appReleaseState)) {
String msg = "Filter validation is failed, Requesting to filter by invalid app release state: "
+ appReleaseState;
log.error(msg);
throw new BadRequestException(msg);
}
}
private <T> List<T> getDifference(List<T> list1, Collection<T> list2) {

@ -172,7 +172,7 @@ public class LifecycleStateManager {
return initialState;
}
public String getEntState() throws LifecycleManagementException {
public String getEndState() throws LifecycleManagementException {
String endState = null;
for (Map.Entry<String, State> stringStateEntry : lifecycleStates.entrySet()) {
if (stringStateEntry.getValue().isEndState()) {
@ -189,6 +189,15 @@ public class LifecycleStateManager {
return endState;
}
public boolean isStateExist(String currentState) {
for (Map.Entry<String, State> stringStateEntry : lifecycleStates.entrySet()) {
if (stringStateEntry.getKey().equalsIgnoreCase(currentState)) {
return true;
}
}
return false;
}
public void setLifecycleStates(Map<String, State> lifecycleStates) {
this.lifecycleStates = lifecycleStates;
}

@ -18,6 +18,7 @@
package org.wso2.carbon.device.application.mgt.core.util;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -164,26 +165,29 @@ public class APIUtil {
return appmDataHandler;
}
public static Filter constructFilter( String appName, String appType, String appCategory,
boolean isFullMatch, String releaseState, int offset, int limit, String sortBy) {
Filter filter = new Filter();
filter.setOffset(offset);
filter.setLimit(limit);
filter.setSortBy(sortBy);
filter.setFullMatch(isFullMatch);
if (appName != null && !appName.isEmpty()) {
filter.setAppName(appName);
}
if (appType != null && !appType.isEmpty()) {
filter.setAppType(appType);
}
if (appCategory != null && !appCategory.isEmpty()) {
filter.setAppCategory(appCategory);
}
if (releaseState != null && !releaseState.isEmpty()) {
filter.setCurrentAppReleaseState(releaseState);
}
return filter;
}
// public static Filter constructFilter( String appName, String appType, String appCategory, String tags,
// boolean isFullMatch, String releaseState, int offset, int limit, String sortBy) {
// Filter filter = new Filter();
// filter.setOffset(offset);
// filter.setLimit(limit);
// filter.setSortBy(sortBy);
// filter.setFullMatch(isFullMatch);
// if (!StringUtils.isEmpty(appName)) {
// filter.setAppName(appName);
// }
// if (!StringUtils.isEmpty(appType)) {
// filter.setAppType(appType);
// }
// if (!StringUtils.isEmpty(appCategory)) {
// filter.setAppCategories(appCategory);
// }
// if (!StringUtils.isEmpty(tags)) {
// filter.setAppCategories(appCategory);
// }
// if (!StringUtils.isEmpty(releaseState)) {
// filter.setAppReleaseState(releaseState);
// }
// return filter;
// }
}

@ -95,7 +95,7 @@ public interface ApplicationManagementAPI {
String SCOPE = "scope";
@GET
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
@ -125,48 +125,12 @@ public interface ApplicationManagementAPI {
code = 500,
message = "Internal Server Error. \n Error occurred while getting the application list.",
response = ErrorResponse.class)
})
Response getApplications(
@ApiParam(
name = "deviceType",
value = "Supporting device Type of the application")
@QueryParam("device-type") String deviceType,
@ApiParam(
name = "name",
value = "Name of the application")
@QueryParam("name") String appName,
@ApiParam(
name = "type",
value = "Type of the application")
@QueryParam("type") String appType,
@ApiParam(
name = "category",
value = "CategoryDTO of the application")
@QueryParam("category") String appCategory,
@ApiParam(
name = "exact-match",
value = "Is it requesting exactly matching application or partially matching application.")
@QueryParam("exact-match") boolean isFullMatch,
@ApiParam(
name = "release-state",
value = "Current state of the application release")
@QueryParam("release-state") String releaseState,
@ApiParam(
name = "offset",
value = "offset",
defaultValue = "0")
@QueryParam("offset") int offset,
@ApiParam(
name = "limit",
value = "limit",
defaultValue = "20")
@QueryParam("limit") int limit,
@ApiParam(
name = "sort",
value = "Sorting type",
defaultValue = "AES")
@QueryParam("sort") String sortBy
);
}) Response getApplications(
@ApiParam(
name = "Filter",
value = "Get application filter",
required = true)
@Valid Filter filter);
@GET
@Path("/{appId}")

@ -70,33 +70,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class);
@GET
@POST
@Override
@Consumes("application/json")
public Response getApplications(
@QueryParam("device-type") String deviceType,
@QueryParam("name") String appName,
@QueryParam("type") String appType,
@QueryParam("category") String appCategory,
@QueryParam("exact-match") boolean isFullMatch,
@QueryParam("release-state") String releaseState,
@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("20") @QueryParam("limit") int limit,
@DefaultValue("ASC") @QueryParam("sort") String sortBy) {
@Valid Filter filter ){
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
Filter filter = APIUtil
.constructFilter(appName, appType, appCategory, isFullMatch, releaseState, offset,
limit, sortBy);
ApplicationList applications = applicationManager.getApplications(filter, deviceType);
ApplicationList applications = applicationManager.getApplications(filter);
if (applications.getApplications().isEmpty()) {
return Response.status(Response.Status.NOT_FOUND)
.entity("Couldn't find any application for the requested query.").build();
}
return Response.status(Response.Status.OK).entity(applications).build();
} catch(BadRequestException e){
String msg = "Couldn't found a device type for " + deviceType;
String msg = "Incompatible request payload is found. Please try with valid reuest payload.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}catch (ApplicationManagementException e) {

@ -67,17 +67,17 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
filter.setLimit(limit);
filter.setSortBy(sortBy);
filter.setFullMatch(isFullMatch);
filter.setCurrentAppReleaseState(AppLifecycleState.PUBLISHED.toString());
filter.setAppReleaseState(AppLifecycleState.PUBLISHED.toString());
if (appName != null && !appName.isEmpty()) {
filter.setAppName(appName);
}
if (appType != null && !appType.isEmpty()) {
filter.setAppType(appType);
}
if (appCategory != null && !appCategory.isEmpty()) {
filter.setAppCategory(appCategory);
}
ApplicationList applications = applicationManager.getApplications(filter, null);
// if (appCategory != null && !appCategory.isEmpty()) {
// filter.setAppCategories(appCategory);
// }
ApplicationList applications = applicationManager.getApplications(filter);
if (applications.getApplications().isEmpty()) {
return Response.status(Response.Status.NOT_FOUND)
.entity("Couldn't find any application for requested query.").build();

@ -174,4 +174,9 @@
<Category>EMM</Category>
<Category>IoT</Category>
</AppCategories>
<RatingConfig>
<MinRatingValue>1</MinRatingValue>
<MaxRatingValue>10</MaxRatingValue>
</RatingConfig>
</ApplicationManagementConfiguration>

Loading…
Cancel
Save