From 9c45f907e2ae40ba232e183db535919d7ce6ed67 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 24 May 2019 20:35:17 +0530 Subject: [PATCH 1/2] Improve APPM subscribing method --- .../common/ApplicationInstallResponse.java | 12 + .../mgt/common/SubscribingDeviceIdHolder.java | 56 +++ .../mgt/common/services/ReviewManager.java | 2 +- .../common/services/SubscriptionManager.java | 3 + .../mgt/core/impl/ApplicationManagerImpl.java | 27 +- .../impl/ApplicationStorageManagerImpl.java | 26 +- .../mgt/core/impl/AppmDataHandlerImpl.java | 2 +- .../mgt/core/impl/ReviewManagerImpl.java | 28 +- .../core/impl/SubscriptionManagerImpl.java | 458 +++++++++++------- .../application/mgt/core/util/Constants.java | 2 + .../api/services/ReviewManagementAPI.java | 4 + .../services/SubscriptionManagementAPI.java | 276 ++--------- .../impl/ReviewManagementAPIImpl.java | 9 +- .../impl/SubscriptionManagementAPIImpl.java | 128 ++--- .../carbon/device/mgt/core/dao/GroupDAO.java | 2 + .../core/dao/impl/AbstractGroupDAOImpl.java | 51 ++ .../operation/mgt/OperationManagerImpl.java | 21 - .../operation/mgt/OperationMgtConstants.java | 7 - .../GroupManagementProviderService.java | 3 + .../GroupManagementProviderServiceImpl.java | 24 + .../mgt/core/util/DeviceManagerUtil.java | 20 +- 21 files changed, 570 insertions(+), 591 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java index 5d3bc260bfe..6bc83c9a57e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java @@ -32,6 +32,14 @@ public class ApplicationInstallResponse { ) private List alreadyInstalledDevices; + @ApiModelProperty( + name = "errorDevices", + value = "List of devices that either device identity is not exist or device type doesn't compatible with the supported device type of the .", + dataType = "List[org.wso2.carbon.device.mgt.common.DeviceIdentifier]" + ) + private List errorDevices; + + @ApiModelProperty( name = "activity", value = "Activity corresponding to the operation" @@ -53,4 +61,8 @@ public class ApplicationInstallResponse { public void setAlreadyInstalledDevices(List alreadyInstalledDevices) { this.alreadyInstalledDevices = alreadyInstalledDevices; } + + public List getErrorDevices() { return errorDevices; } + + public void setErrorDevices(List errorDevices) { this.errorDevices = errorDevices; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java new file mode 100644 index 00000000000..6c7a3cb04b4 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java @@ -0,0 +1,56 @@ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.application.mgt.common; + +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class SubscribingDeviceIdHolder { + private List installedDevices = new ArrayList<>(); + private Map compatibleDevices = new HashMap<>(); + private Map deviceSubscriptions = new HashMap<>(); + + public List getInstalledDevices() { + return installedDevices; + } + + public void setInstalledDevices(List installedDevices) { + this.installedDevices = installedDevices; + } + + public Map getCompatibleDevices() { + return compatibleDevices; + } + + public void setCompatibleDevices(Map compatibleDevices) { + this.compatibleDevices = compatibleDevices; + } + + public Map getDeviceSubscriptions() { + return deviceSubscriptions; + } + + public void setDeviceSubscriptions(Map deviceSubscriptions) { + this.deviceSubscriptions = deviceSubscriptions; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java index 409bfe289aa..306b424494b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java @@ -84,5 +84,5 @@ public interface ReviewManager { * @return {@link Review}updated review * @throws ReviewManagementException Exceptions of the review management */ - Rating getRating(String appReleaseUuuid) throws ReviewManagementException; + Rating getRating(String appReleaseUuuid) throws ReviewManagementException, ApplicationManagementException; } \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 48dff00135f..1a21c601832 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -39,6 +39,9 @@ public interface SubscriptionManager { ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List deviceList) throws ApplicationManagementException; + ApplicationInstallResponse addBulkApInstallation(String applicationUUID, List subscriberList, + String subType) throws ApplicationManagementException; + /** * To install an application to given list of users. * @param applicationUUID ID of the application to install diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index e5bf96578b9..21f4331363f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -1,21 +1,20 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ + package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.io.IOUtils; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 053aa3bf73c..6264c803436 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.core.impl; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index 9f0ac6c8d23..d480dde109a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -1,4 +1,4 @@ -/* Copyright (c) 2018, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java index 440502102cc..8c0a41ac6f4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java @@ -1,20 +1,20 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - * */ + package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; @@ -49,6 +49,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.TreeMap; +import java.util.stream.IntStream; /** * 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); try { ConnectionManagerUtil.openDBConnection(); Rating rating = this.applicationReleaseDAO.getRating(appReleaseUuuid, tenantId); if (rating == null) { - throw new ReviewManagementException( + throw new NotFoundException( "Couldn't find rating for application release UUID: " + appReleaseUuuid + ". Please check the existence of the application release"); } List ratingValues = this.reviewDAO.getAllRatingValues(appReleaseUuuid, tenantId); TreeMap ratingVariety = new TreeMap<>(); - for (Integer ratingVal : ratingValues) { + ratingValues.forEach(ratingVal -> { if (ratingVariety.containsKey(ratingVal)) { ratingVariety.replace(ratingVal, ratingVariety.get(ratingVal) + 1); } else { ratingVariety.put(ratingVal, 1); } - } + }); + IntStream.rangeClosed(1, Constants.MAX_RATING).filter(i -> !ratingVariety.containsKey(i)) + .forEach(i -> ratingVariety.put(i, 0)); + rating.setRatingVariety(ratingVariety); return rating; } catch (ApplicationManagementDAOException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index c7c6c46a3de..ad026e13262 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1,22 +1,23 @@ -/* -* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.device.application.mgt.core.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -24,6 +25,7 @@ import org.wso2.carbon.device.application.mgt.common.AppOperation; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse; import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.SubsciptionType; +import org.wso2.carbon.device.application.mgt.common.SubscribingDeviceIdHolder; 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.dto.DeviceSubscriptionDTO; @@ -55,9 +57,9 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.util.DeviceIDHolder; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; @@ -85,59 +87,168 @@ public class SubscriptionManagerImpl implements SubscriptionManager { this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); } - @Override - public ApplicationInstallResponse installApplicationForDevices(String applicationUUID, + @Override public ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List deviceIdentifiers) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size() + "devices."); } + DeviceManagementProviderService deviceManagementProviderService = HelperUtil + .getDeviceManagementProviderService(); ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); - List operationTriggeredDeviceIds = new ArrayList<>(); - List filteredDevices = validateAppInstallingForDevicesRequest(applicationDTO, deviceIdentifiers); - List filteredDeviceIds = new ArrayList<>(); - List installedDeviceIdentifiers = new ArrayList<>(); - Map compatibleDevices = new HashMap<>(); - Map deviceSubscriptions; + DeviceIDHolder filteredDeviceHolder = filterDeviceIdentifiers(applicationDTO, deviceIdentifiers); + List errorDeviceIdentifiers = filteredDeviceHolder.getErrorDeviceIdList(); + List filteredDeviceObjs = new ArrayList<>(); - for (Device device : filteredDevices){ - filteredDeviceIds.add(device.getId()); + //getting device objects by using device identifiers + for (DeviceIdentifier deviceIdentifier : filteredDeviceHolder.getValidDeviceIDList()) { + try { + Device device = deviceManagementProviderService.getDevice(deviceIdentifier, false); + if (device == null) { + String msg = "Couldn't found an device for device identifier " + deviceIdentifier.getId() + + " and device type: " + deviceIdentifier.getType(); + log.warn(msg); + errorDeviceIdentifiers.add(deviceIdentifier); + } else { + filteredDeviceObjs.add(device); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred when getting device data for device identifier " + deviceIdentifier.getId() + + " and device type " + deviceIdentifier.getType(); + log.warn(msg); + errorDeviceIdentifiers.add(deviceIdentifier); + throw new ApplicationManagementException(msg, e); + } } + if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + String deviceTypeName = deviceType.getName(); + ApplicationInstallResponse applicationInstallResponse = installToDevices(deviceTypeName, filteredDeviceObjs, + 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; + } + } - deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds); - for (Device device : filteredDevices) { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), - device.getType()); - DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptions.get(device.getId()); - if (deviceSubscriptionDTO != null && !deviceSubscriptionDTO.isUnsubscribed() - && Operation.Status.COMPLETED.toString().equals(deviceSubscriptionDTO.getStatus())) { - installedDeviceIdentifiers.add(deviceIdentifier); + @Override + public ApplicationInstallResponse addBulkApInstallation(String applicationUUID, List subscriberList, + String subType) throws ApplicationManagementException { + if (log.isDebugEnabled()) { + log.debug("Install application release which has UUID " + applicationUUID + " to " + subscriberList.size() + + " users."); + } + + try { + if (subscriberList.isEmpty()) { + String msg = "In order to install application release which has UUID " + applicationUUID + ", you shou" + + "ld provide list of subscribers. But found an empty list of users."; + log.error(msg); + 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); + } + + ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil + .getDeviceManagementProviderService(); + GroupManagementProviderService groupManagementProviderService = HelperUtil + .getGroupManagementProviderService(); + List filteredDevices = new ArrayList<>(); + List 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)); + } + } + + if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + String deviceTypeName = deviceType.getName(); + for (Device device : subscriberDevices) { + if (deviceTypeName.equals(device.getType())) { + filteredDevices.add(device); + } + } + return installToDevices(deviceTypeName, filteredDevices, applicationDTO, subType, subscriberList); } else { - compatibleDevices.put(deviceIdentifier, device.getId()); + //todo improve this + return installToDevices(null, subscriberDevices, applicationDTO, subType, subscriberList); } + } 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); } - Activity activity = installToDevices(applicationDTO, deviceIdentifiers, deviceIdentifiers.get(0).getType()); + } - List activityStatuses = activity.getActivityStatus(); - for (ActivityStatus status : activityStatuses) { - if (status.getStatus().equals(ActivityStatus.Status.PENDING)){ - operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier())); - } + private ApplicationInstallResponse installToDevices(String deviceType, List devices, + ApplicationDTO applicationDTO, String subType, List subscribers) + throws ApplicationManagementException { + + SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices); + if (subscribingDeviceIdHolder.getCompatibleDevices().isEmpty()) { + ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); + applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices()); + return applicationInstallResponse; } + Activity activity = addAppInstallOperationToDevices(applicationDTO, + new ArrayList<>(subscribingDeviceIdHolder.getCompatibleDevices().keySet()), deviceType); + ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); applicationInstallResponse.setActivity(activity); - applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers); + applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices()); -// int operationId = Integer -// .parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]); - int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); - addDeviceSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), - operationTriggeredDeviceIds, new ArrayList<>(deviceSubscriptions.keySet()), null, operationId, - SubsciptionType.DEVICE.toString()); + addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity, + subscribingDeviceIdHolder.getCompatibleDevices(), + new ArrayList<>(subscribingDeviceIdHolder.getDeviceSubscriptions().keySet()), subscribers, subType); return applicationInstallResponse; } + private SubscribingDeviceIdHolder getSubscribingDeviceIdHolder(List devices) + throws ApplicationManagementException { + List installedDevices = new ArrayList<>(); + Map compatibleDevices = new HashMap<>(); + + List filteredDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); + Map deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds); + for (Device device : devices) { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptions.get(device.getId()); + if (deviceSubscriptionDTO != null && !deviceSubscriptionDTO.isUnsubscribed() && Operation.Status.COMPLETED + .toString().equals(deviceSubscriptionDTO.getStatus())) { + installedDevices.add(deviceIdentifier); + } else { + compatibleDevices.put(deviceIdentifier, device.getId()); + } + } + + SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); + subscribingDeviceIdHolder.setCompatibleDevices(compatibleDevices); + subscribingDeviceIdHolder.setInstalledDevices(installedDevices); + subscribingDeviceIdHolder.setDeviceSubscriptions(deviceSubscriptions); + return subscribingDeviceIdHolder; + } + private ApplicationDTO getApplicationDTO(String uuid) throws ApplicationManagementException { ApplicationDTO applicationDTO; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -173,117 +284,54 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - private List validateAppInstallingForDevicesRequest(ApplicationDTO applicationDTO, + private DeviceIDHolder filterDeviceIdentifiers(ApplicationDTO applicationDTO, List deviceIdentifiers) throws ApplicationManagementException { - DeviceType deviceType = null; - List existingDevices = new ArrayList<>(); - DeviceManagementProviderService deviceManagementProviderService = HelperUtil - .getDeviceManagementProviderService(); + List errorDeviceIdList = new ArrayList<>(); + List validDeviceIDList = new ArrayList<>(); if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { - deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - } - - for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType()) && deviceType != null - && !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 identifier which has different device" + " type is " + deviceIdentifier.getId(); - log.error(msg); - throw new BadRequestException(msg); - } - try { - Device device = deviceManagementProviderService.getDevice(deviceIdentifier, false); - if (device == null) { - String msg = "Couldn't found an device for device identifier " + deviceIdentifier.getId() - + " and device type: " + deviceIdentifier.getType(); - log.error(msg); + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { + if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) { + log.warn("Found a device identifier which has either empty identity of the device or empty device " + + "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 { - existingDevices.add(device); + validDeviceIDList.add(deviceIdentifier); } - } catch (DeviceManagementException e) { - String msg = "Error occuered when getting device data for divice identifier " + deviceIdentifier.getId() - + " and device type " + deviceIdentifier.getType(); - log.error(msg); - throw new ApplicationManagementException(msg, e); } - } - return existingDevices; - } - - @Override - public ApplicationInstallResponse installApplicationForUsers(String applicationUUID, - List 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 compatibleDevices = new HashMap<>(); - List operationTriggeredDeviceIds = new ArrayList<>(); - List installedDeviceIdentifiers = new ArrayList<>(); - Map deviceSubscriptions = new HashMap<>(); - - for (String user : userList) { - try { - List userDevices = HelperUtil.getDeviceManagementProviderService().getDevicesOfUser(user); - List filteredDeviceIds = new ArrayList<>(); - List filteredDevices = new ArrayList<>(); - - //todo improve for web clips - for (Device device : userDevices) { - if (appDeviceType.getName().equals(device.getType())) { - filteredDevices.add(device); - filteredDeviceIds.add(device.getId()); - } + } else { + for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { + if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) { + log.warn("Found a device identifier which has either empty identity of the device or empty device " + + "type. Hence ignoring the device identifier. "); + continue; } - deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds); - for (Device device : filteredDevices) { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), - device.getType()); - DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptions.get(device.getId()); - if (deviceSubscriptionDTO != null && !deviceSubscriptionDTO.isUnsubscribed() - && Operation.Status.COMPLETED.toString().equals(deviceSubscriptionDTO.getStatus())) { - installedDeviceIdentifiers.add(deviceIdentifier); - } else { - compatibleDevices.put(deviceIdentifier, device.getId()); - } - } - } catch (DeviceManagementException e) { - String msg = "Error occurred when extracting the device list of user[" + user + "]."; - log.error(msg); - throw new ApplicationManagementException(msg, e); + validDeviceIDList.add(deviceIdentifier); } } - Activity activity = installToDevices(applicationDTO, new ArrayList<>(compatibleDevices.keySet()), - appDeviceType.getName()); - List activityStatuses = activity.getActivityStatus(); - for (ActivityStatus status : activityStatuses) { - if (status.getStatus().equals(ActivityStatus.Status.PENDING)){ - operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier())); - } + if (validDeviceIDList.isEmpty()) { + String msg = "Couldn't found at least one valid device identifier to install the application."; + log.error(msg); + throw new BadRequestException(msg); } - ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setActivity(activity); - applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers); - -// int operationId = Integer -// .parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]); - int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); - - addDeviceSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), - operationTriggeredDeviceIds, new ArrayList<>(deviceSubscriptions.keySet()), userList, operationId, SubsciptionType.USER.toString()); - return applicationInstallResponse; + DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); + deviceIDHolder.setValidDeviceIDList(validDeviceIDList); + deviceIDHolder.setErrorDeviceIdList(errorDeviceIdList); + return deviceIDHolder; } - private void addDeviceSubscriptions(int applicationReleaseId, List deviceIds, - List subDeviceIds, List subscribers, int operationId, String subType) - throws ApplicationManagementException { + private void addSubscriptions(int applicationReleaseId, Activity activity, + Map compatibleDevices, List subDeviceIds, List subscribers, + String subType) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { @@ -291,7 +339,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List deviceResubscribingIds = new ArrayList<>(); List deviceSubscriptingIds; - if (SubsciptionType.USER.toString().equals(subType)){ + if (SubsciptionType.USER.toString().equals(subType)) { List subscribedUsers = subscriptionDAO.getSubscribedUsernames(subscribers, tenantId); if (!subscribedUsers.isEmpty()) { subscriptionDAO @@ -301,10 +349,20 @@ public class SubscriptionManagerImpl implements SubscriptionManager { subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, subscribers, applicationReleaseId); } + //todo add for other subscription types + + List deviceIds = new ArrayList<>(); + List activityStatuses = activity.getActivityStatus(); + for (ActivityStatus status : activityStatuses) { + if (status.getStatus().equals(ActivityStatus.Status.PENDING)) { + deviceIds.add(compatibleDevices.get(status.getDeviceIdentifier())); + } + } + + int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); if (!subDeviceIds.isEmpty()) { - deviceResubscribingIds = subscriptionDAO - .updateDeviceSubscription(subscriber, subDeviceIds, subType, Operation.Status.PENDING.toString(), - applicationReleaseId, tenantId); + deviceResubscribingIds = subscriptionDAO.updateDeviceSubscription(subscriber, subDeviceIds, subType, + Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); deviceIds.removeAll(subDeviceIds); } deviceSubscriptingIds = subscriptionDAO @@ -315,8 +373,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = - "Error occurred when adding subscription data for application release UUID: " + applicationReleaseId; + String msg = "Error occurred when adding subscription data for application release UUID: " + + applicationReleaseId; log.error(msg); throw new ApplicationManagementException(msg, e); } catch (DBConnectionException e) { @@ -334,14 +392,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - private Map getDeviceSubscriptions (List filteredDeviceIds) + private Map getDeviceSubscriptions(List filteredDeviceIds) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - return this.subscriptionDAO - .getDeviceSubscriptions(filteredDeviceIds, tenantId); + return this.subscriptionDAO.getDeviceSubscriptions(filteredDeviceIds, tenantId); } catch (ApplicationManagementDAOException e) { String msg = "Error occured when getting device subscriptions for given device IDs"; log.error(msg); @@ -356,9 +413,66 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } - @Override - public ApplicationInstallResponseTmp installApplicationForRoles(String applicationUUID, List roleList) - throws ApplicationManagementException { + //todo remove below methods + + @Override public ApplicationInstallResponse installApplicationForUsers(String applicationUUID, + List 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 compatibleDevices = new HashMap<>(); + List installedDeviceIdentifiers = new ArrayList<>(); + Map deviceSubscriptions = new HashMap<>(); + + for (String user : userList) { + try { + List userDevices = HelperUtil.getDeviceManagementProviderService().getDevicesOfUser(user); + List 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 + + @Override public ApplicationInstallResponseTmp installApplicationForRoles(String applicationUUID, + List roleList) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Install application: " + applicationUUID + " to " + roleList.size() + " roles."); } @@ -400,9 +514,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return response; } - @Override - public ApplicationInstallResponseTmp installApplicationForGroups(String applicationUUID, List deviceGroupList) - throws ApplicationManagementException { + @Override public ApplicationInstallResponseTmp installApplicationForGroups(String applicationUUID, + List deviceGroupList) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Install application: " + applicationUUID + " to " + deviceGroupList.size() + " groups."); } @@ -452,25 +565,23 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return null; } - private Activity installToDevices(ApplicationDTO application, + private Activity addAppInstallOperationToDevices(ApplicationDTO application, List deviceIdentifierList, String deviceType) throws ApplicationManagementException { DeviceManagementProviderService deviceManagementProviderService = HelperUtil .getDeviceManagementProviderService(); try { Operation operation = generateOperationPayloadByDeviceType(deviceType, application); //todo refactor add operation code to get successful operations - return deviceManagementProviderService - .addOperation(deviceType, operation, deviceIdentifierList); + return deviceManagementProviderService.addOperation(deviceType, operation, deviceIdentifierList); } catch (OperationManagementException e) { - throw new ApplicationManagementException("Error occurred while adding the application install " - + "operation to devices", e); + throw new ApplicationManagementException( + "Error occurred while adding the application install " + "operation to devices", e); } catch (InvalidDeviceException e) { //This exception should not occur because the validation has already been done. throw new ApplicationManagementException("The list of device identifiers are invalid"); } } - private ApplicationInstallResponseTmp installToDevicesTmp(ApplicationDTO application, List deviceIdentifierList) throws ApplicationManagementException { DeviceManagementProviderService deviceManagementProviderService = HelperUtil @@ -493,8 +604,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } catch (OperationManagementException e) { response.setSuccessfulDevices(null); response.setFailedDevices(deviceIdentifierList); - throw new ApplicationManagementException("Error occurred while adding the application install " - + "operation to devices", e); + throw new ApplicationManagementException( + "Error occurred while adding the application install " + "operation to devices", e); } catch (InvalidDeviceException e) { //This exception should not occur because the validation has already been done. throw new ApplicationManagementException("The list of device identifiers are invalid"); @@ -541,7 +652,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { /** * Validates the preconditions which is required to satisfy from the device which is required to install the * application. - * + *

* This method check two preconditions whether the application type is compatible to install in the device and * whether the device is enrolled in the system. * @@ -572,5 +683,4 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return applicationInstallResponseTmp; } - } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java index 4c705094a51..cbe0692336f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java @@ -65,4 +65,6 @@ public class Constants { public static final String RELEASE_ARTIFACT = "artifact"; public static final int REVIEW_PARENT_ID = -1; + + public static final int MAX_RATING = 5; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPI.java index 2428af42838..1776364de33 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPI.java @@ -364,6 +364,10 @@ public interface ReviewManagementAPI { message = "OK. \n Successfully retrieved ratings.", response = List.class, responseContainer = "List"), + @ApiResponse( + code = 404, + message = "Not Found. \n No Application release found for application release UUID.", + response = ErrorResponse.class), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while getting ratings", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index 39f23456539..53e84b37699 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -128,6 +128,48 @@ public interface SubscriptionManagementAPI { @Valid List 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 subscribers + ); + @POST @Path("/install/{uuid}/devices") @Produces(MediaType.APPLICATION_JSON) @@ -402,238 +444,4 @@ public interface SubscriptionManagementAPI { ) @Valid List 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); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java index 8b2d94a0aa3..931de5cd023 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java @@ -234,8 +234,13 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { Rating rating; try { rating = reviewManager.getRating(uuid); - } catch (ReviewManagementException e) { - log.error("Review Management Exception occurs", e); + } catch (NotFoundException 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.OK).entity(rating).build(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 59ec1bddeef..184ac2d87f8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -88,6 +88,39 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } } + @Override + @POST + @Path("/install/{uuid}/{subType}") + public Response addBulkAppInstalltion( + @PathParam("uuid") String uuid, + @PathParam("uuid") String subType, + @Valid List 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(); + } + } + @Override @POST @Path("/install/{uuid}/users") @@ -182,99 +215,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 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; - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java index 2536aa40e27..c00237389cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java @@ -190,6 +190,8 @@ public interface GroupDAO { List getDevices(int groupId, int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException; + List getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException; + /** * Get all user roles for device group. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 44b1384c098..dea2912776a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -18,11 +18,13 @@ 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.group.mgt.DeviceGroup; 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.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; import java.sql.Connection; @@ -619,4 +621,53 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override + public List getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException { + Connection conn; + List 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; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 353a37bf6b2..eba0477fc33 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -171,8 +171,6 @@ public class OperationManagerImpl implements OperationManager { + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain() + ", device count:" + deviceIds.size() + " operation type:" + operation.getCode()); } -// Map> ignoredDeviceIdentifierMap = new HashMap<>(); -// Map> authorizedDeviceIdentifierMap = new HashMap<>(); DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds); List authorizedDeviceIds = deviceAuthorizationResult.getValidDeviceIDList(); if (authorizedDeviceIds.isEmpty()) { @@ -221,17 +219,6 @@ public class OperationManagerImpl implements OperationManager { ignoredDevices.add(device); operation.setId(existingOperationID); this.sendNotification(operation, device); -// List deviceIdentifiers; -// if (ignoredDeviceIdentifierMap.containsKey(existingOperationID)) { -// deviceIdentifiers = ignoredDeviceIdentifierMap.get(existingOperationID); -// } else { -// deviceIdentifiers = new ArrayList<>(); -// } -// DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); -// deviceIdentifier.setType(device.getType()); -// deviceIdentifier.setId(device.getDeviceIdentifier()); -// deviceIdentifiers.add(deviceIdentifier); -// ignoredDeviceIdentifierMap.put(existingOperationID, deviceIdentifiers); } } } @@ -266,20 +253,12 @@ public class OperationManagerImpl implements OperationManager { } //TODO have to create a sql to load device details from deviceDAO using single query. - List authorizedDeviceIdentifiers = new ArrayList<>(); for (Device device : authorizedDevices) { enrolmentId = device.getEnrolmentInfo().getId(); //Do not repeat the task operations operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); -// DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); -// deviceIdentifier.setId(device.getDeviceIdentifier()); -// deviceIdentifier.setType(device.getType()); -// authorizedDeviceIdentifiers.add(deviceIdentifier); } - -// authorizedDeviceIdentifierMap.put(operationId, authorizedDeviceIdentifiers); OperationManagementDAOFactory.commitTransaction(); - if (!isScheduled) { for (Device device : authorizedDevices) { this.sendNotification(operation, device); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java index 8da340170a4..955fd506822 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java @@ -20,13 +20,6 @@ package org.wso2.carbon.device.mgt.core.operation.mgt; public class OperationMgtConstants { - public final class DeviceConstants { - private DeviceConstants() { - } - - public static final String DEVICE_ID_NOT_FOUND = "Device not found for device id: %s"; - } - public final class OperationCodes { private OperationCodes() { } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 27aa73f3584..731075a07ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -166,6 +166,9 @@ public interface GroupManagementProviderService { */ List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException; + List getAllDevicesOfGroup(String groupName) throws GroupManagementException; + + /** * This method is used to retrieve the device count of a given group. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index d2e4efdcb0b..df491dc5594 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -619,6 +619,30 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return devices; } + public List getAllDevicesOfGroup(String groupName) throws GroupManagementException { + + if (log.isDebugEnabled()) { + log.debug("Group devices of group: " + groupName); + } + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + List 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} */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 096c18a53d2..44701e6f47d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -463,25 +463,16 @@ public final class DeviceManagerUtil { } public static DeviceIDHolder validateDeviceIdentifiers(List deviceIDs) { - List errorDeviceIdList = new ArrayList<>(); List validDeviceIDList = new ArrayList<>(); - - int deviceIDCounter = 0; for (DeviceIdentifier deviceIdentifier : deviceIDs) { - - deviceIDCounter++; String deviceID = deviceIdentifier.getId(); - if (deviceID == null || deviceID.isEmpty()) { - log.warn(String.format(OperationMgtConstants.DeviceConstants.DEVICE_ID_NOT_FOUND, - deviceIDCounter)); - errorDeviceIdList.add(deviceIdentifier); + log.warn("When adding operation for devices, found a device identifiers which doesn't have defined " + + "the identity of the device, with the request. Hence ignoring the device identifier."); continue; } - try { - if (isValidDeviceIdentifier(deviceIdentifier)) { validDeviceIDList.add(deviceIdentifier); } else { @@ -491,7 +482,6 @@ public final class DeviceManagerUtil { errorDeviceIdList.add(deviceIdentifier); } } - DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); deviceIDHolder.setValidDeviceIDList(validDeviceIDList); deviceIDHolder.setErrorDeviceIdList(errorDeviceIdList); @@ -505,10 +495,8 @@ public final class DeviceManagerUtil { if (device == null || device.getDeviceIdentifier() == null || device.getDeviceIdentifier().isEmpty() || device.getEnrolmentInfo() == null) { return false; - } else if (EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) { - return false; - } - return true; + } else + return !EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus()); } public static boolean isDeviceExists(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { From 93d29e064b1259381bdb6c16f4d3cc1b1328c161 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 27 May 2019 17:57:36 +0530 Subject: [PATCH 2/2] Improve APP subscription API --- .../mgt/core/impl/SubscriptionManagerImpl.java | 7 +++++++ .../services/impl/SubscriptionManagementAPIImpl.java | 12 ++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index ad026e13262..f866b5b5a63 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -64,6 +64,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -93,6 +94,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size() + "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 .getDeviceManagementProviderService(); ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 184ac2d87f8..a009b83fa9f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -17,17 +17,13 @@ */ 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.LogFactory; 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.ForbiddenException; 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.common.InstallationDetails; 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.core.util.APIUtil; @@ -56,12 +52,6 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ public Response installApplicationForDevices( @PathParam("uuid") String uuid, @Valid List 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 { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); ApplicationInstallResponse response = subscriptionManager @@ -121,6 +111,8 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } } + //todo remove following unwanted APIs + @Override @POST @Path("/install/{uuid}/users")