Resolving merge conflicts

feature/appm-store/pbac
megala21 7 years ago
commit 8f42786749

@ -20,94 +20,82 @@ package org.wso2.carbon.device.application.mgt.api.services.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.api.services.ApplicationManagementService;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.ApplicationUser;
import org.wso2.carbon.device.application.mgt.common.*;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.api.APIUtil;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.Date;
@Produces({"application/json"})
@Consumes({"application/json"})
public class ApplicationManagementAPIImpl {
public static final int DEFAULT_LIMIT = 20;
@Path("/applications")
public class ApplicationManagementServiceImpl implements ApplicationManagementService {
private static final int DEFAULT_LIMIT = 20;
public static final String APPLICATION_UPLOAD_EXTENSION = "ApplicationUploadExtension";
private static Log log = LogFactory.getLog(ApplicationManagementServiceImpl.class);
private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class);
@GET
@Override
public Response getApplications(@HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("offset") int offset, @QueryParam("limit") int limit, @QueryParam("query") String searchQuery) {
@Consumes("application/json")
@Path("applications")
public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit,
@QueryParam("query") String searchQuery) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
if (log.isDebugEnabled()) {
log.debug("Received a query for getting applications : offset - " + offset + " limit - " + limit + " "
+ "searchQuery - " + searchQuery);
}
try {
if (limit == 0) {
limit = DEFAULT_LIMIT;
if (log.isDebugEnabled()) {
log.debug("Received a search query with the limit 0, hence using " + DEFAULT_LIMIT + " as limit "
+ "for getting applications");
}
}
Filter filter = new Filter();
filter.setOffset(offset);
filter.setLimit(limit);
filter.setSearchQuery(searchQuery);
ApplicationList applications = applicationManager.getApplications(filter);
return Response.status(Response.Status.OK).entity(applications).build();
} catch (ApplicationManagementException e) {
String msg = "Error occurred while getting the application list";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return Response.status(Response.Status.NOT_FOUND).build();
}
}
@POST
@Consumes("application/json")
@Path("applications")
public Response createApplication(@Valid Application application) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
//TODO : Get username and tenantId
ApplicationUser applicationUser = new ApplicationUser(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(),
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true));
application.setUser(applicationUser);
User user = new User("admin", -1234);
application.setUser(user);
if (log.isDebugEnabled()) {
log.debug("Create Application request received from the user : " + applicationUser.toString());
}
try {
application = applicationManager.createApplication(application);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
return Response.status(Response.Status.CREATED).entity(application).build();
return Response.status(Response.Status.OK).entity(application).build();
}
@PUT
@Consumes("application/json")
@Path("applications")
public Response editApplication(@Valid Application application) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
//TODO : Get username and tenantId
ApplicationUser user = new ApplicationUser("admin", -1234);
User user = new User("admin", -1234);
application.setUser(user);
try {
@ -120,5 +108,21 @@ public class ApplicationManagementServiceImpl implements ApplicationManagementSe
}
return Response.status(Response.Status.OK).entity(application).build();
}
}
@DELETE
@Path("applications/{appuuid}")
public Response deleteApplication(@PathParam("appuuid") String uuid) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
applicationManager.deleteApplication(uuid);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while deleting the application: " + uuid;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
String responseMsg = "Successfully deleted the application: " + uuid;
return Response.status(Response.Status.OK).entity(responseMsg).build();
}
}

@ -73,7 +73,7 @@ public class Application {
private Visibility visibility;
private ApplicationUser user;
private User user;
public int getId() {
return id;
@ -243,11 +243,11 @@ public class Application {
this.visibility = visibility;
}
public ApplicationUser getUser() {
public User getUser() {
return user;
}
public void setUser(ApplicationUser user) {
public void setUser(User user) {
this.user = user;
}
}

@ -21,13 +21,13 @@ package org.wso2.carbon.device.application.mgt.common;
/**
* Represents an user of {@link Application}.
*/
public class ApplicationUser {
public class User {
private String userName;
private int tenantId;
public ApplicationUser(String userName, int tenantId) {
public User(String userName, int tenantId) {
this.userName = userName;
this.tenantId = tenantId;
}
@ -50,6 +50,6 @@ public class ApplicationUser {
@Override
public String toString() {
return "ApplicationUser-name : " + userName + "\t Tenant-ID : " + tenantId;
return "User-name : " + userName + "\t Tenant-ID : " + tenantId;
}
}

@ -21,8 +21,9 @@ package org.wso2.carbon.device.application.mgt.common.exception;
/**
* Represents the exception thrown during application management.
*/
public abstract class ApplicationManagementException extends Exception {
private String message;
public class ApplicationManagementException extends Exception {
String message;
public ApplicationManagementException(String message, Throwable throwable) {
super(message, throwable);
@ -37,7 +38,6 @@ public abstract class ApplicationManagementException extends Exception {
public ApplicationManagementException() {
}
@Override
public String getMessage() {
return message;

@ -49,7 +49,7 @@ public interface ApplicationManager {
* @param uuid Unique ID for tha application
* @throws ApplicationManagementException Application Management Exception
*/
public void deleteApplication(int uuid) throws ApplicationManagementException;
public void deleteApplication(String uuid) throws ApplicationManagementException;
/**
* To get the applications based on the search filter.

@ -36,7 +36,7 @@ public interface ApplicationDAO {
Application editApplication(Application application) throws ApplicationManagementDAOException;
void deleteApplication(Application application) throws ApplicationManagementDAOException;
void deleteApplication(String uuid) throws ApplicationManagementDAOException;
int getApplicationCount(Filter filter) throws ApplicationManagementDAOException;
@ -44,7 +44,9 @@ public interface ApplicationDAO {
void editProperties(Map<String, String> properties) throws ApplicationManagementDAOException;
void deleteProperties(List<String> propertyKeys) throws ApplicationManagementDAOException;
void deleteProperties(int applicationId) throws ApplicationManagementDAOException;
void deleteTags(int applicationId) throws ApplicationManagementDAOException;
void changeLifeCycle(LifecycleState lifecycleState) throws ApplicationManagementDAOException;

@ -43,6 +43,8 @@ public class Util {
application.setId(rs.getInt("ID"));
application.setName(rs.getString("NAME"));
application.setUuid(rs.getString("UUID"));
application.setIdentifier(rs.getString("IDENTIFIER"));
application.setShortDescription(rs.getString("SHORT_DESCRIPTION"));
application.setDescription(rs.getString("DESCRIPTION"));
application.setIconName(rs.getString("ICON_NAME"));
application.setBannerName(rs.getString("BANNER_NAME"));

@ -37,11 +37,6 @@ public abstract class AbstractApplicationDAOImpl extends AbstractDAOImpl impleme
private static final Log log = LogFactory.getLog(AbstractApplicationDAOImpl.class);
@Override
public void deleteApplication(Application application) throws ApplicationManagementDAOException {
}
@Override
public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException {
if(log.isDebugEnabled()){

@ -145,6 +145,21 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl {
return 0;
}
@Override
public void deleteApplication(String uuid) throws ApplicationManagementDAOException {
}
@Override
public void deleteProperties(int applicationId) throws ApplicationManagementDAOException {
}
@Override
public void deleteTags(int applicationId) throws ApplicationManagementDAOException {
}
@Override
public Application editApplication(Application application) throws ApplicationManagementDAOException {
return null;
@ -160,11 +175,6 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl {
}
@Override
public void deleteProperties(List<String> propertyKeys) throws ApplicationManagementDAOException {
}
@Override
public void changeLifeCycle(LifecycleState lifecycleState) throws ApplicationManagementDAOException {

@ -189,6 +189,27 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
return application;
}
@Override
public void deleteApplication(String uuid) throws ApplicationManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, uuid);
stmt.executeUpdate();
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while deleting the application: " + uuid, e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public int getApplicationId(String uuid) throws ApplicationManagementDAOException {
Connection conn = null;
@ -216,7 +237,6 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
}
return id;
}
@Override
@ -298,6 +318,22 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
stmt.executeBatch();
}
// delete existing properties and add new ones. if no properties are set, existing ones will be deleted.
sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, application.getId());
stmt.executeUpdate();
sql = "INSERT INTO APPM_APPLICATION_PROPERTY (PROP_KEY, PROP_VAL, APPLICATION_ID) VALUES (?, ?, ?); ";
stmt = conn.prepareStatement(sql);
for (String propKey : application.getProperties().keySet()) {
stmt.setString(1, propKey);
stmt.setString(2, application.getProperties().get(propKey));
stmt.setInt(3, application.getId());
stmt.addBatch();
}
stmt.executeBatch();
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException e) {
@ -318,8 +354,47 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
}
@Override
public void deleteProperties(List<String> propertyKeys) throws ApplicationManagementDAOException {
public void deleteProperties(int applicationId) throws ApplicationManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId);
stmt.executeUpdate();
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while deleting properties of application: " + applicationId, e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public void deleteTags(int applicationId) throws ApplicationManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId);
stmt.executeUpdate();
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while deleting tags of application: " + applicationId, e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
@ -350,29 +425,30 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl {
try {
conn = this.getConnection();
sql += "INSERT INTO APPM_APPLICATION (UUID, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, BANNER_NAME, " +
sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, BANNER_NAME, " +
"VIDEO_NAME, SCREENSHOTS, CREATED_BY, CREATED_AT, MODIFIED_AT, APPLICATION_CATEGORY_ID, " + "" +
"PLATFORM_ID, TENANT_ID, LIFECYCLE_STATE_ID, LIFECYCLE_STATE_MODIFIED_AT, " +
"LIFECYCLE_STATE_MODIFIED_BY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
"LIFECYCLE_STATE_MODIFIED_BY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, application.getUuid());
stmt.setString(2, application.getName());
stmt.setString(3, application.getShortDescription());
stmt.setString(4, application.getDescription());
stmt.setString(5, application.getIconName());
stmt.setString(6, application.getBannerName());
stmt.setString(7, application.getVideoName());
stmt.setString(8, JSONUtil.listToJsonArrayString(application.getScreenshots()));
stmt.setString(9, application.getUser().getUserName());
stmt.setDate(10, new Date(application.getCreatedAt().getTime()));
stmt.setDate(11, new Date(application.getModifiedAt().getTime()));
stmt.setInt(12, application.getCategory().getId());
stmt.setInt(13, application.getPlatform().getId());
stmt.setInt(14, application.getUser().getTenantId());
stmt.setInt(15, application.getCurrentLifecycle().getLifecycleState().getId());
stmt.setDate(16, new Date(application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime()));
stmt.setString(17, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy());
stmt.setString(2, application.getIdentifier());
stmt.setString(3, application.getName());
stmt.setString(4, application.getShortDescription());
stmt.setString(5, application.getDescription());
stmt.setString(6, application.getIconName());
stmt.setString(7, application.getBannerName());
stmt.setString(8, application.getVideoName());
stmt.setString(9, JSONUtil.listToJsonArrayString(application.getScreenshots()));
stmt.setString(10, application.getUser().getUserName());
stmt.setDate(11, new Date(application.getCreatedAt().getTime()));
stmt.setDate(12, new Date(application.getModifiedAt().getTime()));
stmt.setInt(13, application.getCategory().getId());
stmt.setInt(14, application.getPlatform().getId());
stmt.setInt(15, application.getUser().getTenantId());
stmt.setInt(16, application.getCurrentLifecycle().getLifecycleState().getId());
stmt.setDate(17, new Date(application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime()));
stmt.setString(18, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy());
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();

@ -18,6 +18,7 @@
*/
package org.wso2.carbon.device.application.mgt.core.impl;
import com.sun.corba.se.spi.legacy.connection.Connection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.*;
@ -28,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO;
import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.exception.ValidationException;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
@ -36,37 +38,33 @@ import org.wso2.carbon.device.application.mgt.core.util.HelperUtil;
import java.util.Date;
public class ApplicationManagerImpl implements ApplicationManager {
private static final Log log = LogFactory.getLog(ApplicationManagerImpl.class);
public static final String CREATED = "created";
private static Log log = LogFactory.getLog(ApplicationManagerImpl.class);
@Override
public Application createApplication(Application application) throws ApplicationManagementException {
validateApplication(application, false);
try {
ConnectionManagerUtil.openConnection();
ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
application.setUuid(HelperUtil.generateApplicationUuid());
application.setCreatedAt(new Date());
application.setModifiedAt(new Date());
if (log.isDebugEnabled()) {
log.debug("Creating Application " + application.getName() + " with UUID " + application.getUuid());
}
LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO();
LifecycleState lifecycleState = lifecycleStateDAO.getLifeCycleStateByIdentifier(CREATED);
if (lifecycleState == null) {
throw new NotFoundException("Invalid lifecycle state. There is no lifecycle state connected with "
+ "'CREATED'");
throw new NotFoundException("Invalid lifecycle state.");
}
if (log.isDebugEnabled()) {
log.debug("Life cycle state of the application " + application.getName() + " set as name - " +
lifecycleState.getName() + " id - " + lifecycleState.getId() + " identifier - " +
lifecycleState.getIdentifier());
}
Lifecycle lifecycle = new Lifecycle();
lifecycle.setLifecycleState(lifecycleState);
lifecycle.setLifecycleState(lifecycleState);
lifecycle.setLifecycleStateModifiedAt(new Date());
lifecycle.setGetLifecycleStateModifiedBy(application.getUser().getUserName());
application.setCurrentLifecycle(lifecycle);
@ -74,14 +72,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
PlatformDAO platformDAO = DAOFactory.getPlatformDAO();
Platform platform = platformDAO.getPlatform(application.getUser().getTenantId(), application.getPlatform().getIdentifier());
if (platform == null) {
throw new NotFoundException("Invalid platform. No platform details found for " + application
.getPlatform().getName());
}
if (log.isDebugEnabled()) {
log.debug("Application '" + application.getName() + "' platform is set to (platform name , platform "
+ "id)- ( " + platform.getName() + ", " + platform.getIdentifier() + ", " + platform.getId());
throw new NotFoundException("Invalid platform");
}
application.setPlatform(platform);
return applicationDAO.createApplication(application);
} finally {
ConnectionManagerUtil.closeConnection();
@ -121,8 +115,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public void deleteApplication(int uuid) throws ApplicationManagementException {
public void deleteApplication(String uuid) throws ApplicationManagementException {
try {
ConnectionManagerUtil.openConnection();
ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
int appId = applicationDAO.getApplicationId(uuid);
ConnectionManagerUtil.beginTransaction();
applicationDAO.deleteTags(appId);
applicationDAO.deleteProperties(appId);
applicationDAO.deleteApplication(uuid);
ConnectionManagerUtil.commitTransaction();
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackTransaction();
String msg = "Failed to delete application: " + uuid;
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeConnection();
}
}
@Override

Loading…
Cancel
Save