Add improvement for user subscription method in APPM

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 8071d9262c
commit ab841939a4

@ -47,11 +47,11 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* To insert the ApplicationDTO Release Details. * To insert the ApplicationDTO Release Details.
* *
* @param appId Id of the application * @param appId Id of the application
* @param applicationRelease ApplicationDTO Release the properties of which that need to be inserted. * @param applicationReleaseDTO ApplicationDTO Release the properties of which that need to be inserted.
* @param tenantId Tenant Id * @param tenantId Tenant Id
* @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception.
*/ */
@Override public ApplicationReleaseDTO createRelease(ApplicationReleaseDTO applicationRelease, int appId, int tenantId) @Override public ApplicationReleaseDTO createRelease(ApplicationReleaseDTO applicationReleaseDTO, int appId, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
@ -83,31 +83,31 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
try { try {
connection = this.getDBConnection(); connection = this.getDBConnection();
statement = connection.prepareStatement(sql, generatedColumns); statement = connection.prepareStatement(sql, generatedColumns);
statement.setString(1, applicationRelease.getDescription()); statement.setString(1, applicationReleaseDTO.getDescription());
statement.setString(2, applicationRelease.getVersion()); statement.setString(2, applicationReleaseDTO.getVersion());
statement.setInt(3, tenantId); statement.setInt(3, tenantId);
statement.setString(4, applicationRelease.getUuid()); statement.setString(4, applicationReleaseDTO.getUuid());
statement.setString(5, String.valueOf(applicationRelease.getReleaseType())); statement.setString(5, String.valueOf(applicationReleaseDTO.getReleaseType()));
statement.setString(6, String.valueOf(applicationRelease.getPackageName())); statement.setString(6, String.valueOf(applicationReleaseDTO.getPackageName()));
statement.setDouble(7, applicationRelease.getPrice()); statement.setDouble(7, applicationReleaseDTO.getPrice());
statement.setString(8, applicationRelease.getInstallerName()); statement.setString(8, applicationReleaseDTO.getInstallerName());
statement.setString(9, applicationRelease.getIconName()); statement.setString(9, applicationReleaseDTO.getIconName());
statement.setString(10, applicationRelease.getBannerName()); statement.setString(10, applicationReleaseDTO.getBannerName());
statement.setString(11, applicationRelease.getScreenshotName1()); statement.setString(11, applicationReleaseDTO.getScreenshotName1());
statement.setString(12, applicationRelease.getScreenshotName2()); statement.setString(12, applicationReleaseDTO.getScreenshotName2());
statement.setString(13, applicationRelease.getScreenshotName3()); statement.setString(13, applicationReleaseDTO.getScreenshotName3());
statement.setString(14, applicationRelease.getAppHashValue()); statement.setString(14, applicationReleaseDTO.getAppHashValue());
statement.setBoolean(15, applicationRelease.getIsSharedWithAllTenants()); statement.setBoolean(15, applicationReleaseDTO.getIsSharedWithAllTenants());
statement.setString(16, applicationRelease.getMetaData()); statement.setString(16, applicationReleaseDTO.getMetaData());
statement.setString(17, applicationRelease.getSupportedOsVersions()); statement.setString(17, applicationReleaseDTO.getSupportedOsVersions());
statement.setString(18, applicationRelease.getCurrentState().toUpperCase()); statement.setString(18, applicationReleaseDTO.getCurrentState().toUpperCase());
statement.setInt(19, appId); statement.setInt(19, appId);
statement.executeUpdate(); statement.executeUpdate();
resultSet = statement.getGeneratedKeys(); resultSet = statement.getGeneratedKeys();
if (resultSet.next()) { if (resultSet.next()) {
applicationRelease.setId(resultSet.getInt(1)); applicationReleaseDTO.setId(resultSet.getInt(1));
} }
return applicationRelease; return applicationReleaseDTO;
} catch (SQLException e) { } catch (SQLException e) {
log.error("SQL Exception while trying to release an application by executing the query " + sql, e); log.error("SQL Exception while trying to release an application by executing the query " + sql, e);
throw new ApplicationManagementDAOException( throw new ApplicationManagementDAOException(

@ -92,13 +92,8 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -250,7 +245,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
//insert application data into databse //insert application data into database
ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager();
ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0);
try { try {
@ -1059,6 +1054,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
return roleList; return roleList;
} }
//todo no usage
public ApplicationDTO getApplication(String appType, String appName) throws ApplicationManagementException { public ApplicationDTO getApplication(String appType, String appName) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -1159,33 +1155,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
// return filterAppReleaseByCurrentState(applicationReleases, releaseState); // return filterAppReleaseByCurrentState(applicationReleases, releaseState);
} }
// private List<ApplicationReleaseDTO> filterAppReleaseByCurrentState(List<ApplicationReleaseDTO> applicationReleases,
// String state) {
// List<ApplicationReleaseDTO> filteredReleases = new ArrayList<>();
//
// if (state != null && !state.isEmpty()) {
// for (ApplicationReleaseDTO applicationRelease : applicationReleases) {
// if (state.equals(applicationRelease.getLifecycleStateChangeFlow().getCurrentState())) {
// filteredReleases.add(applicationRelease);
// }
// }
//
// if (AppLifecycleState.PUBLISHED.toString().equals(state) && filteredReleases.size() > 1) {
// log.warn("There are more than one application releases is found which is in PUBLISHED state");
// filteredReleases.sort((r1, r2) -> {
// if (r1.getLifecycleStateChangeFlow().getUpdatedAt().after(r2.getLifecycleStateChangeFlow().getUpdatedAt())) {
// return -1;
// } else if (r2.getLifecycleStateChangeFlow().getUpdatedAt().after(r1.getLifecycleStateChangeFlow().getUpdatedAt())) {
// return 1;
// }
// return 0;
// });
// }
// return filteredReleases;
// }
// return applicationReleases;
// }
@Override @Override
public void deleteApplication(int applicationId) throws ApplicationManagementException { public void deleteApplication(int applicationId) throws ApplicationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -1283,53 +1252,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
private List<String> searchLifecycleStateFlow(String start, String finish) throws ApplicationManagementException {
Map<String, String> nextNodeMap = new HashMap<>();
List<String> directions = new LinkedList<>();
Queue<String> queue = new LinkedList<>();
String currentNode = start;
queue.add(currentNode);
Set<String> visitedNodes = new HashSet<>();
visitedNodes.add(currentNode);
while (!queue.isEmpty()) {
currentNode = queue.remove();
if (currentNode.equals(finish)) {
break;
} else {
List<String> nextStates = lifecycleStateManager.getNextLifecycleStates(currentNode);
if (nextStates.contains(finish)) {
queue = new LinkedList<>();
queue.add(finish);
nextNodeMap.put(currentNode, finish);
} else {
for (String node : nextStates) {
if (!visitedNodes.contains(node)) {
queue.add(node);
visitedNodes.add(node);
nextNodeMap.put(currentNode, node);
}
}
}
}
}
//If all nodes are explored and the destination node hasn't been found.
if (!currentNode.equals(finish)) {
String errorMsg = "can't found a feasible path from " + start + " to " + finish;
throw new ApplicationManagementException(errorMsg);
}
//Reconstruct path
for (String node = start; node != null; node = nextNodeMap.get(node)) {
if (!node.equals(start)) {
directions.add(node);
}
}
return directions;
}
@Override @Override
public void deleteApplicationRelease(String releaseUuid) public void deleteApplicationRelease(String releaseUuid)
throws ApplicationManagementException { throws ApplicationManagementException {
@ -1547,49 +1469,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
/**
* To get role restricted application list.
*
* @param applicationList list of applications.
* @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 (hasUserRole(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
* satisfied.
*
* @param applicationRelease ApplicationReleaseDTO that need to be created.
* @throws ApplicationManagementException ApplicationDTO Management Exception.
*/
private void validateAppReleasePayload(ApplicationReleaseDTO applicationRelease)
throws ApplicationManagementException {
if (applicationRelease.getVersion() == null) {
throw new ApplicationManagementException("ApplicationReleaseDTO version name is a mandatory parameter for "
+ "creating release. It cannot be found.");
}
}
@Override @Override
public List<LifecycleState> getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException { public List<LifecycleState> getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -2430,10 +2309,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new RequestValidatingException(msg); throw new RequestValidatingException(msg);
} }
} }
@Override @Override
public void validateReleaseCreatingRequest(ApplicationReleaseWrapper applicationReleaseWrapper, public void validateReleaseCreatingRequest(ApplicationReleaseWrapper applicationReleaseWrapper,
String applicationType) throws RequestValidatingException { String applicationType) throws RequestValidatingException {

@ -160,7 +160,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
+ " users."); + " users.");
} }
//todo check valid user list //todo check valid user list - throw BadRequest exception
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
List<DeviceIdentifier> operationTriggeredDeviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> operationTriggeredDeviceIdentifiers = new ArrayList<>();

@ -28,6 +28,8 @@ import io.swagger.annotations.Info;
import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag; import io.swagger.annotations.Tag;
import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.application.mgt.common.ErrorResponse;
import org.wso2.carbon.device.application.mgt.common.PaginationResult;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponseTmp; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponseTmp;
import org.wso2.carbon.device.application.mgt.common.EnterpriseInstallationDetails; import org.wso2.carbon.device.application.mgt.common.EnterpriseInstallationDetails;
@ -181,6 +183,26 @@ public interface SubscriptionManagementAPI {
) )
@ApiResponses( @ApiResponses(
value = { value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully add an operation to install application for user devices..",
response = PaginationResult.class,
responseContainer = "PaginationResult"),
@ApiResponse(
code = 400,
message =
"Bad Request. \n Found invalid payload with the request."),
@ApiResponse(
code = 403,
message = "Don't have permission to install application release."),
@ApiResponse(
code = 404,
message = "Not Found. \n Not found an application release for requested UUID."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while adding operation to install "
+ "application for users.",
response = ErrorResponse.class)
}) })
Response installApplicationForRoles ( Response installApplicationForRoles (

@ -94,7 +94,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
public Response installApplicationForUsers( public Response installApplicationForUsers(
@PathParam("uuid") String uuid, @PathParam("uuid") String uuid,
@Valid List<String> users) { @Valid List<String> users) {
if (users.isEmpty()){ if (users.isEmpty()) {
String msg = "In order to install application release which has UUID " + uuid + ", you should provide list " String msg = "In order to install application release which has UUID " + uuid + ", you should provide list "
+ "of users. But found an empty list of users."; + "of users. But found an empty list of users.";
log.error(msg); log.error(msg);
@ -104,23 +104,27 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
ApplicationInstallResponse response = subscriptionManager.installApplicationForUsers(uuid, users); ApplicationInstallResponse response = subscriptionManager.installApplicationForUsers(uuid, users);
return Response.status(Response.Status.OK).entity(response).build(); return Response.status(Response.Status.OK).entity(response).build();
} catch (NotFoundException e) {
//todo String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload";
} catch(BadRequestException e){ log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (BadRequestException e) {
String msg = "Found invalid payload for installing application which has UUID: " + uuid String msg = "Found invalid payload for installing application which has UUID: " + uuid
+ ". Hence verify the payload"; + ". Hence verify the payload";
log.error(msg); log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch(ForbiddenException e){ } catch (ForbiddenException e) {
String msg = "Application release is not in the installable state. Hence you are not permitted to install the aplication."; String msg = "Application release is not in the installable state. Hence you are not permitted to install "
+ "the application.";
log.error(msg); log.error(msg);
return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); return Response.status(Response.Status.FORBIDDEN).entity(msg).build();
}catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = String msg = "Error occurred while installing the application release which has UUID: " + uuid
"Error occurred while installing the application release which has UUID: " + uuid + " for devices"; + " for user devices";
log.error(msg); log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} } }
}
@Override @Override
@POST @POST

Loading…
Cancel
Save