Add Application updating functionality

feature/appm-store/pbac
lasanthaDLPDS 7 years ago
parent 6c65aef26c
commit c755dc018a

@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.common;
import org.wso2.carbon.device.application.mgt.common.jaxrs.Exclude;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.util.List;
/**
@ -95,13 +96,6 @@ public class Application {
public ImageArtifact iconOfLatestRelease;
public List<UnrestrictedRole> getUnrestrictedRoles() {
return unrestrictedRoles;
}
public void setUnrestrictedRoles(List<UnrestrictedRole> unrestrictedRoles) {
this.unrestrictedRoles = unrestrictedRoles;
}
public String getType() {
return type;
@ -166,4 +160,12 @@ public class Application {
public void setApplicationReleases(List<ApplicationRelease> applicationReleases) {
this.applicationReleases = applicationReleases;
}
public List<UnrestrictedRole> getUnrestrictedRoles() {
return unrestrictedRoles;
}
public void setUnrestrictedRoles(List<UnrestrictedRole> unrestrictedRoles) {
this.unrestrictedRoles = unrestrictedRoles;
}
}

@ -143,14 +143,15 @@ public interface ApplicationDAO {
*/
int getApplicationCount(Filter filter, int tenantId) throws ApplicationManagementDAOException;
/**
* To delete the tags of a application.
*
* @param tags Tags which are going to delete.
* @param applicationId ID of the application to delete the tags.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
void deleteTags(int applicationId) throws ApplicationManagementDAOException;
void deleteTags(List<Tag> tags, int applicationId, int tenantId) throws ApplicationManagementDAOException;
/**
* To get an {@link Application} associated with the given release

@ -93,36 +93,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public void addTags(List<Tag> tags, int applicationId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to add tags");
}
Connection conn;
PreparedStatement stmt = null;
String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
try {
conn = this.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
for (Tag tag : tags) {
stmt.setString(1, tag.getTagName());
stmt.setInt(2, tenantId);
stmt.setInt(3, applicationId);
stmt.addBatch();
}
stmt.executeBatch();
} 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);
} finally {
Util.cleanupResources(stmt, null);
}
}
@Override
public int isExistApplication(String appName, String type, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
@ -531,15 +501,55 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
@Override
public void deleteTags(int applicationId) throws ApplicationManagementDAOException {
public void addTags(List<Tag> tags, int applicationId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to add tags");
}
Connection conn;
PreparedStatement stmt = null;
String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
try {
conn = this.getDBConnection();
String sql = "DELETE FROM AP_APP_TAG WHERE ID = ?";
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId);
stmt.executeUpdate();
for (Tag tag : tags) {
stmt.setString(1, tag.getTagName());
stmt.setInt(2, tenantId);
stmt.setInt(3, applicationId);
stmt.addBatch();
}
stmt.executeBatch();
} 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);
} finally {
Util.cleanupResources(stmt, null);
}
}
@Override
public void deleteTags(List<Tag> tags, int applicationId, int tenantId) throws ApplicationManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
String sql = "DELETE FROM AP_APP_TAG WHERE ID = ? AND AP_APP_ID = ? AND TENANT_ID = ?;";
try{
conn = this.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
for (Tag tag : tags) {
stmt.setInt(1, tag.getId());
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.", e);

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

@ -31,6 +31,7 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationType;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.SortingOrder;
import org.wso2.carbon.device.application.mgt.common.Tag;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
import org.wso2.carbon.device.application.mgt.common.User;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
@ -56,6 +57,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.sql.Timestamp;
import java.util.Date;
@ -223,7 +225,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
private boolean isRoleExists(List<UnrestrictedRole> unrestrictedRoleList, String userName)
private boolean isRoleExists(Collection<UnrestrictedRole> unrestrictedRoleList, String userName)
throws UserStoreException {
String[] roleList;
roleList = getRolesOfUser(userName);
@ -811,7 +813,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override
public Application updateApplication(Application application) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Application existingApplication = validateApplication(application.getId());
ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
VisibilityDAO visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO();
List<UnrestrictedRole> addingRoleList;
List<UnrestrictedRole> removingRoleList;
List<Tag> addingTags;
List<Tag> removingTags;
if (existingApplication == null) {
throw new NotFoundException("Tried to update Application which is not in the publisher, " +
"Please verify application details");
@ -837,25 +849,51 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
}
//todo get restricted roles and assign for application
if (existingApplication.getIsRestricted() != application.getIsRestricted()) {
if (existingApplication.getIsRestricted() == 1) {
if (existingApplication.getIsRestricted() == 0 && existingApplication.getUnrestrictedRoles() == null) {
if (application.getUnrestrictedRoles() == null || application.getUnrestrictedRoles().isEmpty()) {
throw new ApplicationManagementException("If you are going to add role restriction for non role " +
"restricted Application, Unrestricted role list " +
"won't be empty or null");
}
} else if (existingApplication.getIsRestricted() == 0) {
visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), application.getId(), tenantId);
} else if (existingApplication.getIsRestricted() == 1 && existingApplication.getUnrestrictedRoles() !=
null) {
if (application.getUnrestrictedRoles() != null || !application.getUnrestrictedRoles().isEmpty()) {
throw new ApplicationManagementException("If you are going to remove role restriction from role " +
"restricted Application, Unrestricted role list should be empty or null");
"restricted Application, Unrestricted role list " +
"should be empty or null");
}
visibilityDAO.deleteUnrestrictedRoles(existingApplication.getUnrestrictedRoles(), application.getId(),
tenantId);
}
} else if (existingApplication.getIsRestricted() == application.getIsRestricted()) {
if (existingApplication.getIsRestricted() == 1) {
addingRoleList = getDifference(application.getUnrestrictedRoles(), existingApplication
.getUnrestrictedRoles());
removingRoleList = getDifference(existingApplication
.getUnrestrictedRoles(), application.getUnrestrictedRoles());
if (!addingRoleList.isEmpty()) {
visibilityDAO.addUnrestrictedRoles(addingRoleList, application.getId(), tenantId);
}
if (!removingRoleList.isEmpty()) {
visibilityDAO.deleteUnrestrictedRoles(removingRoleList, application.getId(), tenantId);
}
}
//todo update role restriction
}
//todo get tags and assign for application verify
//todo update application
return application;
addingTags = getDifference(existingApplication.getTags(), application.getTags());
removingTags = getDifference(application.getTags(), existingApplication.getTags());
if (!addingTags.isEmpty()) {
applicationDAO.addTags(addingTags, application.getId(), tenantId);
}
if (!removingTags.isEmpty()) {
applicationDAO.deleteTags(removingTags, application.getId(), tenantId);
}
return applicationDAO.editApplication(application, tenantId);
}
private Filter validateFilter(Filter filter) {
@ -879,4 +917,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
return filter;
}
private <T> List<T> getDifference(List<T> list1, Collection<T> list2) {
List<T> list = new ArrayList<>();
for (T t : list1) {
if(!list2.contains(t)) {
list.add(t);
}
}
return list;
}
}

@ -26,6 +26,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import java.util.Collection;
import java.util.List;
//todo need to work on business logic

Loading…
Cancel
Save