Improve application edit functionality

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 9fab48839d
commit 460394c9da

@ -55,7 +55,6 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
Response.ResponseBuilder response = Response
.ok(fileInputStream, MediaType.APPLICATION_OCTET_STREAM);
response.status(Response.Status.OK);
// response.type("application/html");
response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
return response.build();
} catch (NotFoundException e) {

@ -79,12 +79,6 @@ public class ApplicationDTO {
example = "1, 2, 3")
private int deviceTypeId;
@ApiModelProperty(name = "deviceTypeName",
value = "Related device type of the application",
required = true,
example = "IoS, Android, Arduino, RaspberryPi etc")
private String deviceTypeName;
@ApiModelProperty(name = "appRating",
value = "Rating of the aplication")
private int appRating;
@ -168,12 +162,6 @@ public class ApplicationDTO {
this.unrestrictedRoles = unrestrictedRoles;
}
public String getDeviceTypeName() {
return deviceTypeName;
}
public void setDeviceTypeName(String deviceTypeName) { this.deviceTypeName = deviceTypeName; }
public int getDeviceTypeId() {
return deviceTypeId;
}

@ -66,6 +66,12 @@ public class ApplicationRelease {
example = "alpha, beta etc")
private String releaseType;
@ApiModelProperty(name = "currentStatus",
value = "CurrentStatus of the Application Release.",
required = true,
example = "CREATED, IN-REVIEW, PUBLISHED etc")
private String currentStatus;
@ApiModelProperty(name = "price",
value = "Price of the application release",
required = true)
@ -172,4 +178,8 @@ public class ApplicationRelease {
public String getSupportedOsVersions() { return supportedOsVersions; }
public void setSupportedOsVersions(String supportedOsVersions) { this.supportedOsVersions = supportedOsVersions; }
public String getCurrentStatus() { return currentStatus; }
public void setCurrentStatus(String currentStatus) { this.currentStatus = currentStatus; }
}

@ -58,7 +58,8 @@ public interface ApplicationManager {
* @return Updated Application
* @throws ApplicationManagementException ApplicationDTO Management Exception
*/
void updateApplication(int applicationId, ApplicationWrapper applicationWrapper) throws ApplicationManagementException;
void updateApplication(int applicationId, ApplicationWrapper applicationWrapper)
throws ApplicationManagementException;
/**
* Delete an application identified by the unique ID.

@ -57,9 +57,9 @@ public interface ApplicationDAO {
List<String> getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException;
List<String> getAppCategories (int appId, int tenantId) throws ApplicationManagementDAOException;
void deleteTagMapping (List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException;
List<String> getAppCategories (int appId, int tenantId) throws ApplicationManagementDAOException;
List<CategoryDTO> getAllCategories(int tenantId) throws ApplicationManagementDAOException;

@ -934,6 +934,37 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public void deleteTagMapping (List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException{
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Tag mappings.");
}
Connection conn;
String sql = "DELETE FROM "
+ "AP_APP_TAG_MAPPING tm "
+ "WHERE "
+ "tm.AP_APP_TAG_ID = ? AND "
+ "tm.AP_APP_ID = ? AND "
+ "tm.TENANT_ID = ?";
try {
conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){
for (Integer tagId : tagIds){
stmt.setInt(1, tagId);
stmt.setInt(2, applicationId);
stmt.setInt(3, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
}
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection when deleting tag mapppig", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping", e);
}
}
@Override
public List<String> getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {

@ -145,8 +145,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationReleaseDTO initialApplicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0);
applicationDTO.getApplicationReleaseDTOs().clear();
ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), applicationDTO.getDeviceTypeName(),
initialApplicationReleaseDTO, applicationArtifact);
ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(),
applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact);
applicationDTO.getApplicationReleaseDTOs().add(addImageArtifacts(applicationReleaseDTO, applicationArtifact));
} catch (UnexpectedServerErrorException e) {
String msg = "Error occurred when getting Device Type data.";
@ -550,7 +550,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (!haveAllUserRoles(filteringUnrestrictedRoles, 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";
+ " Can't filter applications by giving these unrestricted role list";
log.error(msg);
throw new BadRequestException(msg);
}
@ -582,8 +582,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg);
throw new NotFoundException(msg);
}
DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(),
applicationDTO.getDeviceTypeName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper),
deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper),
applicationArtifact);
applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact);
@ -630,18 +631,23 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationDTO applicationDTO;
List<ApplicationReleaseDTO> filteredApplicationReleaseDTOs;
boolean isVisibleApp = false;
try {
ConnectionManagerUtil.openDBConnection();
applicationDTO = this.applicationDAO.getApplicationById(appId, tenantId);
if (applicationDTO == null) {
throw new NotFoundException("Couldn't find an application for application Id: " + appId);
String msg = "Couldn't find an application for application Id: " + appId;
log.error(msg);
throw new NotFoundException(msg);
}
filteredApplicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs().stream()
.filter(applicationReleaseDTO -> applicationReleaseDTO.getCurrentState().equals(state))
.collect(Collectors.toList());
List<ApplicationReleaseDTO> filteredApplicationReleaseDTOs = new ArrayList<>();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState()) && (
state == null || applicationReleaseDTO.getCurrentState().equals(state))) {
filteredApplicationReleaseDTOs.add(applicationReleaseDTO);
}
}
applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs);
if (applicationDTO.getApplicationReleaseDTOs().isEmpty()){
return null;
@ -674,9 +680,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ForbiddenException(msg);
}
return appDtoToAppResponse(applicationDTO);
} catch (LifecycleManagementException e){
String msg = "Error occurred when getting the last state of the application lifecycle flow";
log.error(msg);
throw new ApplicationManagementException(msg, e);
}catch (UserStoreException e) {
throw new ApplicationManagementException(
"User-store exception while getting application with the application id " + appId);
String msg = "User-store exception while getting application with the application id " + appId;
log.error(msg);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
@ -1438,15 +1449,37 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg);
throw new BadRequestException(msg);
}
if (!StringUtils.isEmpty(applicationWrapper.getDeviceType()) && !applicationDTO.getDeviceTypeName()
.equals(applicationWrapper.getDeviceType())) {
String msg = "You are trying to change the compatible device type of the application type and it is not "
+ "possible after you create an application for device type. " +
applicationWrapper.getDeviceType() + "Therefore please remove this application and publish "
+ "new application with device type: " + applicationWrapper.getDeviceType();
String deviceTypeName = applicationWrapper.getDeviceType();
if (!StringUtils.isEmpty(deviceTypeName)) {
DeviceType deviceType = getDeviceTypeData(deviceTypeName);
if (!deviceType.getName().equals(deviceTypeName)){
String msg = "You are trying to change the compatible device type of the application type and it is "
+ "not possible after you create an application for device type. " + deviceTypeName +
"Therefore please remove this application and publish new application with device type: " +
deviceTypeName;
log.error(msg);
throw new BadRequestException(msg);
}
}
if (!StringUtils.isEmpty(applicationWrapper.getName())){
Filter filter = new Filter();
filter.setFullMatch(true);
filter.setAppName(applicationWrapper.getName().trim());
filter.setOffset(0);
filter.setLimit(1);
List<ApplicationDTO> applicationList = applicationDAO
.getApplications(filter, applicationDTO.getDeviceTypeId(), tenantId);
if (!applicationList.isEmpty()) {
String msg = "Already an application registered with same name " + applicationWrapper.getName()
+ ". Hence you can't update the application name from " + applicationDTO.getName() + " to "
+ applicationWrapper.getName();
log.error(msg);
throw new BadRequestException(msg);
}
applicationDTO.setName(applicationWrapper.getName());
}
if (!StringUtils.isEmpty(applicationWrapper.getSubType()) && !applicationDTO.getSubType()
.equals(applicationWrapper.getSubType())) {
if (!ApplicationSubscriptionType.PAID.toString().equals(applicationWrapper.getSubType())
@ -1473,30 +1506,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationDTO.setSubType(applicationWrapper.getSubType());
applicationDTO.setPaymentCurrency(applicationWrapper.getPaymentCurrency());
}
if (!StringUtils.isEmpty(applicationWrapper.getName())){
Filter filter = new Filter();
filter.setFullMatch(true);
filter.setAppName(applicationWrapper.getName().trim());
filter.setOffset(0);
filter.setLimit(1);
List<ApplicationDTO> applicationList = applicationDAO
.getApplications(filter, applicationDTO.getDeviceTypeId(), tenantId);
if (!applicationList.isEmpty()) {
String msg = "Already an application registered with same name " + applicationWrapper.getName()
+ ". Hence you can't update the application name from " + applicationDTO.getName() + " to "
+ applicationWrapper.getName();
log.error(msg);
throw new BadRequestException(msg);
}
applicationDTO.setName(applicationWrapper.getName());
}
if (!StringUtils.isEmpty(applicationWrapper.getDescription())){
applicationDTO.setDescription(applicationWrapper.getDescription());
}
List<String> appUnrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(applicationId, tenantId);
List<String> appCategories = this.applicationDAO.getAppCategories(applicationId, tenantId);
boolean isExistingAppRestricted = !appUnrestrictedRoles.isEmpty();
boolean isUpdatingAppRestricted = !applicationWrapper.getUnrestrictedRoles().isEmpty();
@ -1533,15 +1548,48 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
applicationDTO.setUnrestrictedRoles(applicationWrapper.getUnrestrictedRoles());
String updatingAppCategory = applicationWrapper.getAppCategory();
if ( updatingAppCategory != null){
List<String> appCategories = this.applicationDAO.getAppCategories(applicationId, tenantId);
if (!appCategories.contains(updatingAppCategory)){
List<CategoryDTO> allCategories = this.applicationDAO.getAllCategories(tenantId);
List<Integer> categoryIds = allCategories.stream()
.filter(category -> category.getCategoryName().equals(updatingAppCategory))
.map(CategoryDTO::getId).collect(Collectors.toList());
if (!categoryIds.isEmpty()){
String msg =
"You are trying to update application category into invalid application category, "
+ "it is not registered in the system. Therefore please register the category "
+ updatingAppCategory + " and perform the action";
log.error(msg);
throw new BadRequestException(msg);
}
this.applicationDAO.addCategoryMapping(categoryIds, applicationId, tenantId);
}
}
List<String> updatingAppTags = applicationWrapper.getTags();
if ( updatingAppTags!= null){
List<String> appTags = this.applicationDAO.getAppTags(applicationId, tenantId);
List<String> addingTagList = getDifference(appTags, applicationWrapper.getTags());
List<String> removingTagList = getDifference(applicationWrapper.getTags(), appTags);
List<String> addingTagList = getDifference(appTags, updatingAppTags);
List<String> removingTagList = getDifference(updatingAppTags, appTags);
if (!addingTagList.isEmpty()) {
// applicationDAO.addTags(addingTags, application.getId(), tenantId);
List<TagDTO> allTags = this.applicationDAO.getAllTags(tenantId);
List<String> newTags = addingTagList.stream().filter(updatingTagName -> allTags.stream()
.noneMatch(tag -> tag.getTagName().equals(updatingTagName))).collect(Collectors.toList());
if (!newTags.isEmpty()){
this.applicationDAO.addTags(newTags, tenantId);
}
List<Integer> addingTagIds = this.applicationDAO.getTagIdsForTagNames(addingTagList, tenantId);
this.applicationDAO.addTagMapping(addingTagIds, applicationId, tenantId);
}
if (!removingTagList.isEmpty()) {
List<Integer> removingTagIds = this.applicationDAO.getTagIdsForTagNames(removingTagList, tenantId);
this.applicationDAO.deleteTagMapping(removingTagIds, applicationId, tenantId);
applicationDAO.deleteTags(removingTagList, applicationId, tenantId);
}
}
//todo
applicationDAO.editApplication(applicationDTO, tenantId);
} catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
@ -1904,7 +1952,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationDTO.setTags(applicationWrapper.getTags());
applicationDTO.setUnrestrictedRoles(applicationWrapper.getUnrestrictedRoles());
applicationDTO.setDeviceTypeId(deviceType.getId());
applicationDTO.setDeviceTypeName(deviceType.getName());
List<ApplicationReleaseDTO> applicationReleaseEntities = applicationWrapper.getApplicationReleaseWrappers()
.stream().map(this::releaseWrapperToReleaseDTO).collect(Collectors.toList());
applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities);
@ -1923,9 +1970,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
return applicationReleaseDTO;
}
private Application appDtoToAppResponse(ApplicationDTO applicationDTO) {
private Application appDtoToAppResponse(ApplicationDTO applicationDTO)
throws BadRequestException, UnexpectedServerErrorException {
Application application = new Application();
DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId());
application.setId(applicationDTO.getId());
application.setName(applicationDTO.getName());
application.setDescription(applicationDTO.getDescription());
@ -1935,7 +1984,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
application.setPaymentCurrency(applicationDTO.getPaymentCurrency());
application.setTags(applicationDTO.getTags());
application.setUnrestrictedRoles(applicationDTO.getUnrestrictedRoles());
application.setDeviceType(applicationDTO.getDeviceTypeName());
application.setDeviceType(deviceType.getName());
List<ApplicationRelease> applicationReleases = applicationDTO.getApplicationReleaseDTOs()
.stream().map(this::releaseDtoToRelease).collect(Collectors.toList());
application.setApplicationReleases(applicationReleases);
@ -1943,7 +1992,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
private ApplicationRelease releaseDtoToRelease(ApplicationReleaseDTO applicationReleaseDTO){
String artifactDownloadEndpoint = ConfigurationManager.getInstance().getConfiguration().getArtifactDownloadEndpoint();
String artifactDownloadEndpoint = ConfigurationManager.getInstance().getConfiguration()
.getArtifactDownloadEndpoint();
String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid();
ApplicationRelease applicationRelease = new ApplicationRelease();
applicationRelease.setDescription(applicationReleaseDTO.getDescription());
@ -1953,8 +2003,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationRelease.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants());
applicationRelease.setMetaData(applicationReleaseDTO.getMetaData());
applicationRelease.setUrl(applicationReleaseDTO.getUrl());
applicationRelease.setCurrentStatus(applicationReleaseDTO.getCurrentState());
applicationRelease.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants());
applicationRelease.setSupportedOsVersions(applicationReleaseDTO.getSupportedOsVersions());
applicationRelease.setInstallerPath(basePath + Constants.FORWARD_SLASH + applicationReleaseDTO.getInstallerName());
applicationRelease
.setInstallerPath(basePath + Constants.FORWARD_SLASH + applicationReleaseDTO.getInstallerName());
applicationRelease.setIconPath(basePath + Constants.FORWARD_SLASH + applicationReleaseDTO.getIconName());
applicationRelease.setBannerPath(basePath + Constants.FORWARD_SLASH + applicationReleaseDTO.getBannerName());

@ -176,8 +176,7 @@ public interface ApplicationManagementAPI {
@PathParam("appId") int appId,
@ApiParam(
name = "state",
value = "state",
defaultValue = "PUBLISHED")
value = "state")
@QueryParam("state") String state
);

@ -102,15 +102,14 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Path("/{appId}")
public Response getApplication(
@PathParam("appId") int appId,
@DefaultValue("PUBLISHED") @QueryParam("state") String state) {
@QueryParam("state") String state) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
Application application = applicationManager.getApplicationById(appId, state);
if (application == null){
String msg = "Couldn't found an application release which is in " + state + " state for application id "
+ appId;
String msg = "Could not found an application release which is in " + state + " state.";
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
return Response.status(Response.Status.OK).entity(msg).build();
}
return Response.status(Response.Status.OK).entity(application).build();
} catch (NotFoundException e) {
@ -317,6 +316,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
}
}
/*
//todo ----------------------
*/
@PUT
@Consumes("application/json")

Loading…
Cancel
Save