Merge branch 'application-mgt-new' into 'application-mgt-new'

Improve APPM subscribing method

See merge request entgra/carbon-device-mgt!107
feature/appm-store/pbac
Saad Sahibjan 5 years ago
commit 44287d4b7f

@ -84,5 +84,5 @@ public interface ReviewManager {
* @return {@link Review}updated review * @return {@link Review}updated review
* @throws ReviewManagementException Exceptions of the review management * @throws ReviewManagementException Exceptions of the review management
*/ */
Rating getRating(String appReleaseUuuid) throws ReviewManagementException; Rating getRating(String appReleaseUuuid) throws ReviewManagementException, ApplicationManagementException;
} }

@ -39,6 +39,9 @@ public interface SubscriptionManager {
ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList) ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList)
throws ApplicationManagementException; throws ApplicationManagementException;
ApplicationInstallResponse addBulkApInstallation(String applicationUUID, List<String> subscriberList,
String subType) throws ApplicationManagementException;
/** /**
* To install an application to given list of users. * To install an application to given list of users.
* @param applicationUUID ID of the application to install * @param applicationUUID ID of the application to install

@ -49,6 +49,7 @@ import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.stream.IntStream;
/** /**
* This class is the default implementation for the Managing the reviews. * This class is the default implementation for the Managing the reviews.
@ -467,26 +468,29 @@ public class ReviewManagerImpl implements ReviewManager {
} }
} }
@Override public Rating getRating(String appReleaseUuuid) throws ReviewManagementException { @Override public Rating getRating(String appReleaseUuuid) throws ReviewManagementException, ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
Rating rating = this.applicationReleaseDAO.getRating(appReleaseUuuid, tenantId); Rating rating = this.applicationReleaseDAO.getRating(appReleaseUuuid, tenantId);
if (rating == null) { if (rating == null) {
throw new ReviewManagementException( throw new NotFoundException(
"Couldn't find rating for application release UUID: " + appReleaseUuuid "Couldn't find rating for application release UUID: " + appReleaseUuuid
+ ". Please check the existence of the application release"); + ". Please check the existence of the application release");
} }
List<Integer> ratingValues = this.reviewDAO.getAllRatingValues(appReleaseUuuid, tenantId); List<Integer> ratingValues = this.reviewDAO.getAllRatingValues(appReleaseUuuid, tenantId);
TreeMap<Integer, Integer> ratingVariety = new TreeMap<>(); TreeMap<Integer, Integer> ratingVariety = new TreeMap<>();
for (Integer ratingVal : ratingValues) { ratingValues.forEach(ratingVal -> {
if (ratingVariety.containsKey(ratingVal)) { if (ratingVariety.containsKey(ratingVal)) {
ratingVariety.replace(ratingVal, ratingVariety.get(ratingVal) + 1); ratingVariety.replace(ratingVal, ratingVariety.get(ratingVal) + 1);
} else { } else {
ratingVariety.put(ratingVal, 1); ratingVariety.put(ratingVal, 1);
} }
} });
IntStream.rangeClosed(1, Constants.MAX_RATING).filter(i -> !ratingVariety.containsKey(i))
.forEach(i -> ratingVariety.put(i, 0));
rating.setRatingVariety(ratingVariety); rating.setRatingVariety(ratingVariety);
return rating; return rating;
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {

@ -87,20 +87,26 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
} }
@Override @Override public ApplicationInstallResponse installApplicationForDevices(String applicationUUID,
public ApplicationInstallResponse installApplicationForDevices(String applicationUUID,
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException { List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size() log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size()
+ "devices."); + "devices.");
} }
if (deviceIdentifiers.isEmpty()){
String msg = "In order to install application release which has UUID " + applicationUUID + ", you should "
+ "provide list of device identifiers. But found an empty list of identifiers.";
log.error(msg);
throw new BadRequestException(msg);
}
DeviceManagementProviderService deviceManagementProviderService = HelperUtil DeviceManagementProviderService deviceManagementProviderService = HelperUtil
.getDeviceManagementProviderService(); .getDeviceManagementProviderService();
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
DeviceIDHolder filteredDeviceHolder = filterDeviceIdentifiers(applicationDTO, deviceIdentifiers); DeviceIDHolder filteredDeviceHolder = filterDeviceIdentifiers(applicationDTO, deviceIdentifiers);
List<DeviceIdentifier> errorDeviceIdentifierList = filteredDeviceHolder.getErrorDeviceIdList(); List<DeviceIdentifier> errorDeviceIdentifiers = filteredDeviceHolder.getErrorDeviceIdList();
List<Device> filteredDeviceObjs = new ArrayList<>(); List<Device> filteredDeviceObjs = new ArrayList<>();
//getting device objects by using device identifiers
for (DeviceIdentifier deviceIdentifier : filteredDeviceHolder.getValidDeviceIDList()) { for (DeviceIdentifier deviceIdentifier : filteredDeviceHolder.getValidDeviceIDList()) {
try { try {
Device device = deviceManagementProviderService.getDevice(deviceIdentifier, false); Device device = deviceManagementProviderService.getDevice(deviceIdentifier, false);
@ -108,7 +114,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
String msg = "Couldn't found an device for device identifier " + deviceIdentifier.getId() String msg = "Couldn't found an device for device identifier " + deviceIdentifier.getId()
+ " and device type: " + deviceIdentifier.getType(); + " and device type: " + deviceIdentifier.getType();
log.warn(msg); log.warn(msg);
errorDeviceIdentifierList.add(deviceIdentifier); errorDeviceIdentifiers.add(deviceIdentifier);
} else { } else {
filteredDeviceObjs.add(device); filteredDeviceObjs.add(device);
} }
@ -116,86 +122,111 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
String msg = "Error occurred when getting device data for device identifier " + deviceIdentifier.getId() String msg = "Error occurred when getting device data for device identifier " + deviceIdentifier.getId()
+ " and device type " + deviceIdentifier.getType(); + " and device type " + deviceIdentifier.getType();
log.warn(msg); log.warn(msg);
errorDeviceIdentifierList.add(deviceIdentifier); errorDeviceIdentifiers.add(deviceIdentifier);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
} }
SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(filteredDeviceObjs); if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) {
if (subscribingDeviceIdHolder.getCompatibleDevices().isEmpty()){ DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); String deviceTypeName = deviceType.getName();
applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices()); ApplicationInstallResponse applicationInstallResponse = installToDevices(deviceTypeName, filteredDeviceObjs,
applicationInstallResponse.setErrorDevices(errorDeviceIdentifierList); applicationDTO, SubsciptionType.DEVICE.toString(), null);
applicationInstallResponse.setErrorDevices(errorDeviceIdentifiers);
return applicationInstallResponse;
} else {
//todo improve thos
ApplicationInstallResponse applicationInstallResponse = installToDevices(null, filteredDeviceObjs,
applicationDTO, SubsciptionType.DEVICE.toString(), null);
applicationInstallResponse.setErrorDevices(errorDeviceIdentifiers);
return applicationInstallResponse; return applicationInstallResponse;
} }
Activity activity = installToDevices(applicationDTO,
new ArrayList<>(subscribingDeviceIdHolder.getCompatibleDevices().keySet()),
deviceIdentifiers.get(0).getType());
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setActivity(activity);
applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices());
applicationInstallResponse.setErrorDevices(errorDeviceIdentifierList);
addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity,
subscribingDeviceIdHolder.getCompatibleDevices(),
new ArrayList<>(subscribingDeviceIdHolder.getDeviceSubscriptions().keySet()), null,
SubsciptionType.DEVICE.toString());
return applicationInstallResponse;
} }
@Override @Override
public ApplicationInstallResponse installApplicationForUsers(String applicationUUID, public ApplicationInstallResponse addBulkApInstallation(String applicationUUID, List<String> subscriberList,
List<String> userList) throws ApplicationManagementException { String subType) throws ApplicationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Install application release which has UUID " + applicationUUID + " to " + userList.size() log.debug("Install application release which has UUID " + applicationUUID + " to " + subscriberList.size()
+ " users."); + " users.");
} }
//todo check valid user list - throw BadRequest exception try {
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); if (subscriberList.isEmpty()) {
DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); String msg = "In order to install application release which has UUID " + applicationUUID + ", you shou"
Map<DeviceIdentifier, Integer> compatibleDevices = new HashMap<>(); + "ld provide list of subscribers. But found an empty list of users.";
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>(); log.error(msg);
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions = new HashMap<>(); throw new BadRequestException(msg);
}
if (SubsciptionType.USER.toString().equals(subType) && !SubsciptionType.ROLE.toString().equals(subType)
&& SubsciptionType.GROUP.toString().equals(subType)) {
String msg = "Found invalid subscription type to install application release witch has UUID: "
+ applicationUUID + ". Subscription type is " + subType;
log.error(msg);
throw new BadRequestException(msg);
}
for (String user : userList) { ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
try { DeviceManagementProviderService deviceManagementProviderService = HelperUtil
List<Device> userDevices = HelperUtil.getDeviceManagementProviderService().getDevicesOfUser(user); .getDeviceManagementProviderService();
List<Device> filteredDevices = new ArrayList<>(); GroupManagementProviderService groupManagementProviderService = HelperUtil
.getGroupManagementProviderService();
List<Device> filteredDevices = new ArrayList<>();
List<Device> subscriberDevices = new ArrayList<>();
for (String subscriber : subscriberList) {
if (SubsciptionType.USER.toString().equals(subType)) {
subscriberDevices.addAll(deviceManagementProviderService.getDevicesOfUser(subscriber));
} else if (SubsciptionType.ROLE.toString().equals(subType)) {
subscriberDevices.addAll(deviceManagementProviderService.getAllDevicesOfRole(subscriber));
} else if (SubsciptionType.GROUP.toString().equals(subType)) {
subscriberDevices.addAll(groupManagementProviderService.getAllDevicesOfGroup(subscriber));
}
}
//todo improve for web clips if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) {
for (Device device : userDevices) { DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
if (appDeviceType.getName().equals(device.getType())) { String deviceTypeName = deviceType.getName();
for (Device device : subscriberDevices) {
if (deviceTypeName.equals(device.getType())) {
filteredDevices.add(device); filteredDevices.add(device);
} }
} }
return installToDevices(deviceTypeName, filteredDevices, applicationDTO, subType, subscriberList);
SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(filteredDevices); } else {
compatibleDevices.putAll(subscribingDeviceIdHolder.getCompatibleDevices()); //todo improve this
deviceSubscriptions.putAll(subscribingDeviceIdHolder.getDeviceSubscriptions()); return installToDevices(null, subscriberDevices, applicationDTO, subType, subscriberList);
installedDeviceIdentifiers.addAll(subscribingDeviceIdHolder.getInstalledDevices());
} catch (DeviceManagementException e) {
String msg = "Error occurred when extracting the device list of user[" + user + "].";
log.error(msg);
throw new ApplicationManagementException(msg, e);
} }
} catch (DeviceManagementException e) {
String msg = "Error occurred while getting devices of given users or given roles.";
log.error(msg);
throw new ApplicationManagementException(msg, e);
} catch (GroupManagementException e) {
String msg = "Error occurred while getting devices of given groups";
log.error(msg);
throw new ApplicationManagementException(msg, e);
} }
}
private ApplicationInstallResponse installToDevices(String deviceType, List<Device> devices,
ApplicationDTO applicationDTO, String subType, List<String> subscribers)
throws ApplicationManagementException {
if (compatibleDevices.keySet().isEmpty()){ SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices);
if (subscribingDeviceIdHolder.getCompatibleDevices().isEmpty()) {
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers); applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices());
return applicationInstallResponse; return applicationInstallResponse;
} }
Activity activity = installToDevices(applicationDTO, new ArrayList<>(compatibleDevices.keySet()), Activity activity = addAppInstallOperationToDevices(applicationDTO,
appDeviceType.getName()); new ArrayList<>(subscribingDeviceIdHolder.getCompatibleDevices().keySet()), deviceType);
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setActivity(activity); applicationInstallResponse.setActivity(activity);
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers); applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices());
addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity, compatibleDevices, addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity,
new ArrayList<>(deviceSubscriptions.keySet()), userList, SubsciptionType.USER.toString()); subscribingDeviceIdHolder.getCompatibleDevices(),
new ArrayList<>(subscribingDeviceIdHolder.getDeviceSubscriptions().keySet()), subscribers, subType);
return applicationInstallResponse; return applicationInstallResponse;
} }
@ -261,28 +292,34 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
private DeviceIDHolder filterDeviceIdentifiers(ApplicationDTO applicationDTO, private DeviceIDHolder filterDeviceIdentifiers(ApplicationDTO applicationDTO,
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException { List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
DeviceType deviceType = null;
List<DeviceIdentifier> errorDeviceIdList = new ArrayList<>(); List<DeviceIdentifier> errorDeviceIdList = new ArrayList<>();
List<DeviceIdentifier> validDeviceIDList = new ArrayList<>(); List<DeviceIdentifier> validDeviceIDList = new ArrayList<>();
if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) {
deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
} for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) {
for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { log.warn("Found a device identifier which has either empty identity of the device or empty device "
if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) { + "type. Hence ignoring the device identifier. ");
log.warn("Found a device identifier which has either empty identity of the device or empty device " continue;
+ "type. Hence ignoring the device identifier. "); }
continue; if (!deviceType.getName().equals(deviceIdentifier.getType())) {
String msg =
"Found a device identifier which is not matched with the application device Type. Application "
+ "device type is " + deviceType.getName() + " and the identifier of which has a "
+ "different device type is " + deviceIdentifier.getId();
log.warn(msg);
errorDeviceIdList.add(deviceIdentifier);
} else {
validDeviceIDList.add(deviceIdentifier);
}
} }
if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType()) && deviceType != null } else {
&& !deviceType.getName().equals(deviceIdentifier.getType())) { for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
String msg = if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) {
"Found a device identifier which is not matched with the application device Type. Application " log.warn("Found a device identifier which has either empty identity of the device or empty device "
+ "device type is " + deviceType.getName() + " and the identifier of which has a " + "type. Hence ignoring the device identifier. ");
+ "different device type is " + deviceIdentifier.getId(); continue;
log.warn(msg); }
errorDeviceIdList.add(deviceIdentifier);
} else {
validDeviceIDList.add(deviceIdentifier); validDeviceIDList.add(deviceIdentifier);
} }
} }
@ -318,6 +355,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, subscribers, applicationReleaseId); subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, subscribers, applicationReleaseId);
} }
//todo add for other subscription types
List<Integer> deviceIds = new ArrayList<>(); List<Integer> deviceIds = new ArrayList<>();
List<ActivityStatus> activityStatuses = activity.getActivityStatus(); List<ActivityStatus> activityStatuses = activity.getActivityStatus();
for (ActivityStatus status : activityStatuses) { for (ActivityStatus status : activityStatuses) {
@ -380,6 +419,62 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
//todo remove below methods
@Override public ApplicationInstallResponse installApplicationForUsers(String applicationUUID,
List<String> userList) throws ApplicationManagementException {
if (log.isDebugEnabled()) {
log.debug("Install application release which has UUID " + applicationUUID + " to " + userList.size()
+ " users.");
}
//todo check valid user list - throw BadRequest exception
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
Map<DeviceIdentifier, Integer> compatibleDevices = new HashMap<>();
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>();
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions = new HashMap<>();
for (String user : userList) {
try {
List<Device> userDevices = HelperUtil.getDeviceManagementProviderService().getDevicesOfUser(user);
List<Device> filteredDevices = new ArrayList<>();
//todo improve for web clips
for (Device device : userDevices) {
if (appDeviceType.getName().equals(device.getType())) {
filteredDevices.add(device);
}
}
SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(filteredDevices);
compatibleDevices.putAll(subscribingDeviceIdHolder.getCompatibleDevices());
deviceSubscriptions.putAll(subscribingDeviceIdHolder.getDeviceSubscriptions());
installedDeviceIdentifiers.addAll(subscribingDeviceIdHolder.getInstalledDevices());
} catch (DeviceManagementException e) {
String msg = "Error occurred when extracting the device list of user[" + user + "].";
log.error(msg);
throw new ApplicationManagementException(msg, e);
}
}
if (compatibleDevices.keySet().isEmpty()) {
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
return applicationInstallResponse;
}
Activity activity = addAppInstallOperationToDevices(applicationDTO, new ArrayList<>(compatibleDevices.keySet()),
appDeviceType.getName());
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setActivity(activity);
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity, compatibleDevices,
new ArrayList<>(deviceSubscriptions.keySet()), userList, SubsciptionType.USER.toString());
return applicationInstallResponse;
}
//todo not implemented below code properly //todo not implemented below code properly
@Override public ApplicationInstallResponseTmp installApplicationForRoles(String applicationUUID, @Override public ApplicationInstallResponseTmp installApplicationForRoles(String applicationUUID,
@ -476,8 +571,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return null; return null;
} }
private Activity installToDevices(ApplicationDTO application, List<DeviceIdentifier> deviceIdentifierList, private Activity addAppInstallOperationToDevices(ApplicationDTO application,
String deviceType) throws ApplicationManagementException { List<DeviceIdentifier> deviceIdentifierList, String deviceType) throws ApplicationManagementException {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil DeviceManagementProviderService deviceManagementProviderService = HelperUtil
.getDeviceManagementProviderService(); .getDeviceManagementProviderService();
try { try {

@ -65,4 +65,6 @@ public class Constants {
public static final String RELEASE_ARTIFACT = "artifact"; public static final String RELEASE_ARTIFACT = "artifact";
public static final int REVIEW_PARENT_ID = -1; public static final int REVIEW_PARENT_ID = -1;
public static final int MAX_RATING = 5;
} }

@ -364,6 +364,10 @@ public interface ReviewManagementAPI {
message = "OK. \n Successfully retrieved ratings.", message = "OK. \n Successfully retrieved ratings.",
response = List.class, response = List.class,
responseContainer = "List"), responseContainer = "List"),
@ApiResponse(
code = 404,
message = "Not Found. \n No Application release found for application release UUID.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while getting ratings", message = "Internal Server Error. \n Error occurred while getting ratings",

@ -128,6 +128,48 @@ public interface SubscriptionManagementAPI {
@Valid List<DeviceIdentifier> deviceIdentifiers @Valid List<DeviceIdentifier> deviceIdentifiers
); );
@POST
@Path("/install/{uuid}/{subType}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Install an application for subscription type.",
notes = "This will install an application to a given subscription type and this is bulk app installation.",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:app:subscription:install")
})
}
)
@ApiResponses(
value = {
})
Response addBulkAppInstalltion(
@ApiParam(
name = "uuid",
value = "The application release UUID",
required = true
)
@PathParam("uuid") String uuid,
@ApiParam(
name = "subType",
value = "Subscription type of the app installing operation.",
required = true
)
@PathParam("subType") String subType,
@ApiParam(
name = "subscribers",
value = "Subscriber list of the application release.",
required = true
)
@Valid List<String> subscribers
);
@POST @POST
@Path("/install/{uuid}/devices") @Path("/install/{uuid}/devices")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -402,238 +444,4 @@ public interface SubscriptionManagementAPI {
) )
@Valid List<String> groups @Valid List<String> groups
); );
// ----------------------------------------------
@POST
@Path("/install-application")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Install an application",
notes = "This will install an application to a given list of devices",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:install")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully sent the install application operation.",
response = ApplicationInstallResponseTmp.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the application is already installed."
),
@ApiResponse(
code = 404,
message = "Not Found. \n ApplicationDTO cannot be found to install."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while installing the application."
)
})
Response installApplication(
@ApiParam(
name = "installationDetails",
value = "The application ID and list of devices/users/roles",
required = true
)
@Valid InstallationDetails installationDetails);
@POST
@Path("/enterprise-install-application")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Install an application to the devices belong to an enterprise entity",
notes = "This will install an application to a given list of groups/users/roles",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:install")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully sent the install application operation.",
response = ApplicationInstallResponseTmp.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the application is already installed."
),
@ApiResponse(
code = 404,
message = "Not Found. \n ApplicationDTO cannot be found to install."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while installing the application."
)
})
Response enterpriseInstallApplication(
@ApiParam(
name = "enterpriseInstallationDetails",
value = "The application ID and list of devices/users/roles",
required = true)
@Valid EnterpriseInstallationDetails enterpriseInstallationDetails);
@POST
@Path("/uninstall-application")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Uninstall an application",
notes = "This will uninstall an application from given list of devices",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:uninstall")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully uninstalled the application.",
response = ApplicationDTO.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the application is already uninstalled."
),
@ApiResponse(
code = 404,
message = "Not Found. \n ApplicationDTO cannot be found to uninstall."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while uninstalling the application."
)
})
Response uninstallApplication(
@ApiParam(
name = "installationDetails",
value = "The application ID and list of devices",
required = true)
@Valid InstallationDetails installationDetails);
@POST
@Path("/enterprise-uninstall-application")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Uninstall an application from the devices belong to an enterprise entity",
notes = "This will uninstall an application from devices belong to given list of groups/users/roles",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:uninstall")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully uninstalled the application.",
response = ApplicationDTO.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the application is already uninstalled."
),
@ApiResponse(
code = 404,
message = "Not Found. \n ApplicationDTO cannot be found to uninstall."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while uninstalling the application."
)
})
Response enterpriseUninstallApplication(
@ApiParam(
name = "enterpriseInstallationDetails",
value = "The application ID and list of groups/users/roles",
required = true
)
@Valid EnterpriseInstallationDetails enterpriseInstallationDetails);
@GET
@Path("/application/{applicationUUID}/device/{deviceId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Get an application",
notes = "This will return an application to a given valid token",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:getApplication")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully installed the application.",
response = ApplicationDTO.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n " +
"Empty body because the application is already installed."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while fetching the application."
)
})
Response getApplication(
@ApiParam(
name = "applicationUUID",
value = "ApplicationDTO ID"
)
@QueryParam("applicationUUID") String applicationUUID,
@ApiParam(
name = "deviceId",
value = "The device ID"
)
@QueryParam("deviceId") String deviceId);
} }

@ -234,8 +234,13 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
Rating rating; Rating rating;
try { try {
rating = reviewManager.getRating(uuid); rating = reviewManager.getRating(uuid);
} catch (ReviewManagementException e) { } catch (NotFoundException e) {
log.error("Review Management Exception occurs", e); String msg = "Couldn't found an application release for UUID: " + uuid;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (ReviewManagementException | ApplicationManagementException e) {
String msg = "Error occured while getting review data for application release UUID: " + uuid;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} }
return Response.status(Response.Status.OK).entity(rating).build(); return Response.status(Response.Status.OK).entity(rating).build();

@ -17,17 +17,13 @@
*/ */
package org.wso2.carbon.device.application.mgt.store.api.services.impl; package org.wso2.carbon.device.application.mgt.store.api.services.impl;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse;
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.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.store.api.services.SubscriptionManagementAPI; import org.wso2.carbon.device.application.mgt.store.api.services.SubscriptionManagementAPI;
import org.wso2.carbon.device.application.mgt.common.InstallationDetails;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
@ -56,12 +52,6 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
public Response installApplicationForDevices( public Response installApplicationForDevices(
@PathParam("uuid") String uuid, @PathParam("uuid") String uuid,
@Valid List<DeviceIdentifier> deviceIdentifiers) { @Valid List<DeviceIdentifier> deviceIdentifiers) {
if (deviceIdentifiers.isEmpty()){
String msg = "In order to install application release which has UUID " + uuid + ", you should provide list "
+ "of device identifiers. But found an empty list of identifiers.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
try { try {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
ApplicationInstallResponse response = subscriptionManager ApplicationInstallResponse response = subscriptionManager
@ -88,6 +78,41 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
} }
} }
@Override
@POST
@Path("/install/{uuid}/{subType}")
public Response addBulkAppInstalltion(
@PathParam("uuid") String uuid,
@PathParam("uuid") String subType,
@Valid List<String> subscribers) {
try {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
ApplicationInstallResponse response = subscriptionManager.addBulkApInstallation(uuid, subscribers, subType);
return Response.status(Response.Status.OK).entity(response).build();
} catch (NotFoundException e) {
String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload";
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
+ ". Hence verify the payload";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (ForbiddenException e) {
String msg = "Application release is not in the installable state. Hence you are not permitted to install "
+ "the application.";
log.error(msg);
return Response.status(Response.Status.FORBIDDEN).entity(msg).build();
} catch (ApplicationManagementException e) {
String msg = "Error occurred while installing the application release which has UUID: " + uuid
+ " for user devices";
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
//todo remove following unwanted APIs
@Override @Override
@POST @POST
@Path("/install/{uuid}/users") @Path("/install/{uuid}/users")
@ -182,99 +207,4 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
} }
@Override
@POST
@Path("/install-application")
public Response installApplication(@ApiParam(name = "installationDetails", value = "ApplicationDTO ID and list of" +
"devices", required = true) @Valid InstallationDetails installationDetails) {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
String applicationUUID = installationDetails.getApplicationUUID();
if (applicationUUID.isEmpty() || installationDetails.getDeviceIdentifiers().isEmpty()) {
String msg = "Some or all data in the incoming request is empty. Therefore unable to proceed with the "
+ "installation.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
try {
ApplicationInstallResponse response = subscriptionManager.installApplicationForDevices(applicationUUID,
installationDetails.getDeviceIdentifiers());
return Response.status(Response.Status.OK).entity(response).build();
} catch (ApplicationManagementException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Error occurred while installing the application for devices" + ": " + e.getMessage())
.build();
}
}
@Override
public Response enterpriseInstallApplication(EnterpriseInstallationDetails enterpriseInstallationDetails) {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
String msg;
String applicationUUID = enterpriseInstallationDetails.getApplicationUUID();
EnterpriseInstallationDetails.EnterpriseEntity enterpriseEntity = enterpriseInstallationDetails.getEntityType();
List<String> entityValueList = enterpriseInstallationDetails.getEntityValueList();
ApplicationInstallResponseTmp response = null;
if (applicationUUID.isEmpty()) {
msg = "ApplicationDTO UUID is empty in the incoming request. Therefore unable to proceed with the "
+ "installation.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
if (enterpriseEntity == null || entityValueList.isEmpty()) {
msg = "Some or all details of the entity is empty in the incoming request. Therefore unable to proceed "
+ "with the installation.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
try {
if (EnterpriseInstallationDetails.EnterpriseEntity.USER.equals(enterpriseEntity)) {
// response = subscriptionManager.installApplicationForUsers(applicationUUID, entityValueList);
} else if (EnterpriseInstallationDetails.EnterpriseEntity.ROLE.equals(enterpriseEntity)) {
response = subscriptionManager.installApplicationForRoles(applicationUUID, entityValueList);
} else if (EnterpriseInstallationDetails.EnterpriseEntity.DEVICE_GROUP.equals(enterpriseEntity)) {
response = subscriptionManager.installApplicationForGroups(applicationUUID, entityValueList);
} else {
msg = "Entity type does not match either USER, ROLE or DEVICE_GROUP. Therefore unable to proceed with "
+ "the installation";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
return Response.status(Response.Status.OK).entity(response).build();
} catch (ApplicationManagementException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Error occurred while installing the application for devices" + ": " + e.getMessage())
.build();
}
}
@Override
public Response uninstallApplication(@ApiParam(name = "installationDetails", value = "The application ID and list" +
" of devices/users/roles", required = true) @Valid InstallationDetails installationDetails) {
return null;
}
@Override
public Response enterpriseUninstallApplication(
EnterpriseInstallationDetails enterpriseInstallationDetails) {
return null;
}
@Override
public Response getApplication(@ApiParam(name = "applicationUUID", value = "ApplicationDTO ID") String
applicationUUID, @ApiParam(name = "deviceId", value = "The device ID")
String deviceId) {
return null;
}
} }

@ -190,6 +190,8 @@ public interface GroupDAO {
List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId) List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId)
throws GroupManagementDAOException; throws GroupManagementDAOException;
List<Device> getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException;
/** /**
* Get all user roles for device group. * Get all user roles for device group.
* *

@ -18,11 +18,13 @@
package org.wso2.carbon.device.mgt.core.dao.impl; package org.wso2.carbon.device.mgt.core.dao.impl;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupDAO;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil;
import java.sql.Connection; import java.sql.Connection;
@ -619,4 +621,53 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
} }
@Override
public List<Device> getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException {
Connection conn;
List<Device> devices;
try {
conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT "
+ "d1.DEVICE_ID, "
+ "d1.DESCRIPTION, "
+ "d1.NAME AS DEVICE_NAME, "
+ "d1.DEVICE_TYPE, "
+ "d1.DEVICE_IDENTIFICATION, "
+ "e.OWNER, "
+ "e.OWNERSHIP, "
+ "e.STATUS, "
+ "e.DATE_OF_LAST_UPDATE, "
+ "e.DATE_OF_ENROLMENT, "
+ "e.ID AS ENROLMENT_ID "
+ "FROM "
+ "DM_ENROLMENT e, "
+ "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE "
+ "FROM "
+ "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID "
+ "FROM DM_DEVICE d, "
+ "(SELECT dgm.DEVICE_ID "
+ "FROM DM_DEVICE_GROUP_MAP dgm "
+ "WHERE dgm.GROUP_ID = (SELECT ID FROM DM_GROUP WHERE GROUP_NAME = ? )) dgm1 "
+ "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t "
+ "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 "
+ "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setString(1, groupName);
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
try (ResultSet rs = stmt.executeQuery() ){
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
}
}
} catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while retrieving information of all registered devices"
+ " which belongs to the given group name.", e);
}
return devices;
}
} }

@ -166,6 +166,9 @@ public interface GroupManagementProviderService {
*/ */
List<Device> getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException; List<Device> getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException;
List<Device> getAllDevicesOfGroup(String groupName) throws GroupManagementException;
/** /**
* This method is used to retrieve the device count of a given group. * This method is used to retrieve the device count of a given group.
* *

@ -619,6 +619,30 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return devices; return devices;
} }
public List<Device> getAllDevicesOfGroup(String groupName) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Group devices of group: " + groupName);
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
List<Device> devices;
try {
GroupManagementDAOFactory.openConnection();
devices = this.groupDAO.getAllDevicesOfGroup(groupName, tenantId);
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while getting devices in group.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getDevices for group name: " + groupName;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
return devices;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

Loading…
Cancel
Save