From bc7e39c8833571f75ab037d3fac25987050a5013 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Thu, 15 Oct 2020 10:05:37 +0530 Subject: [PATCH 1/8] Add geo fence search and geo cache --- .../service/api/GeoLocationBasedService.java | 13 +- .../impl/GeoLocationBasedServiceImpl.java | 39 ++++-- .../mgt/common/DeviceManagementConstants.java | 1 + .../service/GeoLocationProviderService.java | 23 ++- .../mgt/core/DeviceManagementConstants.java | 1 + .../device/mgt/core/cache/GeoCacheKey.java | 73 ++++++++++ .../mgt/core/cache/GeoCacheManager.java | 54 ++++++++ .../core/cache/impl/GeoCacheManagerImpl.java | 95 +++++++++++++ .../core/config/DeviceManagementConfig.java | 11 ++ .../cache/GeoFenceCacheConfiguration.java | 57 ++++++++ .../device/mgt/core/dao/GeofenceDAO.java | 23 ++- .../mgt/core/dao/impl/GeofenceDAOImpl.java | 73 ++++++++-- .../GeoLocationProviderServiceImpl.java | 131 ++++++++++++++++-- .../mgt/core/util/DeviceManagerUtil.java | 59 ++++++++ .../repository/conf/cdm-config.xml | 8 ++ .../mgt/core/util/PolicyManagerUtil.java | 3 +- .../src/main/resources/conf/cdm-config.xml | 8 ++ 17 files changed, 631 insertions(+), 41 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java index d0e87c6c40..a8358ad831 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java @@ -965,9 +965,18 @@ public interface GeoLocationBasedService { response = Response.class) }) Response getGeofence( + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.") @QueryParam("offset") int offset, - @DefaultValue("10") - @QueryParam("limit") int limit); + @ApiParam( + name = "limit", + value = "Provide how many device details you require from the starting pagination index/offset.") + @QueryParam("limit") int limit, + @ApiParam( + name = "name", + value = "Geo Fence name") + @QueryParam("name") String name); @DELETE diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 726afa5129..aa41922d5a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -29,9 +29,9 @@ import org.wso2.carbon.analytics.dataservice.commons.SortType; import org.wso2.carbon.analytics.datasource.commons.Record; import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -45,7 +45,6 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.GeoHashLength import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.ZoomGeoHashLengthStrategy; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.device.mgt.jaxrs.beans.GeofenceList; import org.wso2.carbon.device.mgt.jaxrs.beans.GeofenceWrapper; import org.wso2.carbon.device.mgt.jaxrs.service.api.GeoLocationBasedService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; @@ -621,7 +620,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { public Response getGeofence(@PathParam("fenceId") int fenceId) { try { GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); - GeofenceData geofenceData = geoService.getGeofence(fenceId); + GeofenceData geofenceData = geoService.getGeoFences(fenceId); if (geofenceData == null) { String msg = "No valid Geofence found for ID " + fenceId; return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); @@ -656,20 +655,21 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { @Consumes("application/json") @Produces("application/json") public Response getGeofence(@QueryParam("offset") int offset, - @DefaultValue("10") - @QueryParam("limit") int limit) { + @QueryParam("limit") int limit, + @QueryParam("name") String name) { try { - PaginationRequest request = new PaginationRequest(offset, limit); GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); - List geofence = geoService.getGeofence(request); - List geofenceList = new ArrayList<>(); - for (GeofenceData geofenceData : geofence) { - geofenceList.add(getMappedResponseBean(geofenceData)); + if (offset != 0 && limit != 0) { + PaginationRequest request = new PaginationRequest(offset, limit); + if (name != null && !name.isEmpty()) { + request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name); + } + return getResponse(geoService.getGeoFences(request)); + } + if (name != null && !name.isEmpty()) { + return getResponse(geoService.getGeoFences(name)); } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(geofenceList); - paginationResult.setRecordsTotal(geofenceList.size()); - return Response.status(Response.Status.OK).entity(paginationResult).build(); + return getResponse(geoService.getGeoFences()); } catch (GeoLocationBasedServiceException e) { String msg = "Failed to retrieve geofence data"; log.error(msg, e); @@ -677,6 +677,17 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { } } + private Response getResponse(List fencesList) { + List geofenceList = new ArrayList<>(); + for (GeofenceData geofenceData : fencesList) { + geofenceList.add(getMappedResponseBean(geofenceData)); + } + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(geofenceList); + paginationResult.setRecordsTotal(geofenceList.size()); + return Response.status(Response.Status.OK).entity(paginationResult).build(); + } + @DELETE @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 492288febb..95cc1a0267 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -124,6 +124,7 @@ public final class DeviceManagementConstants { public static final String DAS_HOST_NAME = "${iot.analytics.host}"; public static final String DEFAULT_HTTP_PROTOCOL = "https"; public static final String DAS_URL = DEFAULT_HTTP_PROTOCOL + "://" + DAS_HOST_NAME + ":" + DAS_PORT; + public static final String FENCE_NAME = "name"; } public static final class OTPProperties { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java index 1449ca6b04..60bb198a24 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java @@ -86,7 +86,7 @@ public interface GeoLocationProviderService { * @return Extracted geofence data * @throws GeoLocationBasedServiceException error occurs while retrieving a geofence */ - GeofenceData getGeofence(int fenceId) throws GeoLocationBasedServiceException; + GeofenceData getGeoFences(int fenceId) throws GeoLocationBasedServiceException; /** * Get paginated geofence list @@ -94,13 +94,28 @@ public interface GeoLocationProviderService { * @return List of Geofences retrieved * @throws GeoLocationBasedServiceException error occurs while retrieving geofences */ - List getGeofence(PaginationRequest request) throws GeoLocationBasedServiceException; + List getGeoFences(PaginationRequest request) throws GeoLocationBasedServiceException; + + /** + * Search geo fences using the fence name + * @param name searching name of the fence + * @return list of fences found for the specific name + * @throws GeoLocationBasedServiceException for errors occur while querying geo fences + */ + List getGeoFences(String name) throws GeoLocationBasedServiceException; + + /** + * Get all geo fences of the tenant + * @return list of the all geo fences of the tenant + * @throws GeoLocationBasedServiceException for errors occur while querying geo fences + */ + List getGeoFences() throws GeoLocationBasedServiceException; /** * Delete Geofence with ID * @param fenceId Id of the fence which should be deleted * @return true if deletion success. false if not record found for the used Id - * @throws GeoLocationBasedServiceException + * @throws GeoLocationBasedServiceException for errors occur while deleting geo fences */ boolean deleteGeofenceData(int fenceId) throws GeoLocationBasedServiceException; @@ -109,7 +124,7 @@ public interface GeoLocationProviderService { * @param geofenceData Bean with updated geofence data * @param fenceId Id of the fence which should be updated * @return true if update success. false if not a record found for the used Id - * @throws GeoLocationBasedServiceException + * @throws GeoLocationBasedServiceException for errors occur while updating geo fences */ boolean updateGeofence(GeofenceData geofenceData, int fenceId) throws GeoLocationBasedServiceException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index d8b4ac5cb0..3f309a001b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -42,6 +42,7 @@ public final class DeviceManagementConstants { public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DEVICE_CACHE = "DEVICE_CACHE"; + public static final String GEOFENCE_CACHE = "GEOFENCE_CACHE"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; public static final String URL_SEPERATOR = "/"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java new file mode 100644 index 0000000000..dcac42c7b0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020, 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.mgt.core.cache; + +import java.util.Objects; + +public class GeoCacheKey { + private int fenceId; + private int tenantId; + private volatile int hashCode; + + public GeoCacheKey(int fenceId, int tenantId) { + this.fenceId = fenceId; + this.tenantId = tenantId; + } + + public GeoCacheKey() { + } + + public int getFenceId() { + return fenceId; + } + + public void setFenceId(int fenceId) { + this.fenceId = fenceId; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (!GeoCacheKey.class.isAssignableFrom(obj.getClass())) { + return false; + } + final GeoCacheKey other = (GeoCacheKey) obj; + String thisId = this.fenceId + "-" + "_" + this.tenantId; + String otherId = other.fenceId + "-" + "_" + other.tenantId; + return thisId.equals(otherId); + } + + @Override + public int hashCode() { + if (hashCode == 0) { + hashCode = Objects.hash(fenceId, tenantId); + } + return hashCode; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java new file mode 100644 index 0000000000..1b3bd9397d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020, 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.mgt.core.cache; + +import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; + +public interface GeoCacheManager { + /** + * Add geo fences to the cache + * @param geofenceData adding fence object + * @param fenceId id of the fence + * @param tenantId id of the tenant + */ + void addFenceToCache(GeofenceData geofenceData, int fenceId, int tenantId); + + /** + * Update geo fences already in cache + * @param geofenceData updating geo fence object + * @param fenceId id of the fence + * @param tenantId id of the tenant + */ + void updateGeoFenceInCache(GeofenceData geofenceData, int fenceId, int tenantId); + + /** + * Remove geo fence from cache + * @param fenceId id of the fence + * @param tenantId id of the tenant + */ + void removeFenceFromCache(int fenceId, int tenantId); + + /** + * Get geo fence data from the cache + * @param fenceId id of the retrieving fence object + * @param tenantId tenant id of the fence created + * @return GeofenceData object if the cache have the specific object or null if there is no entry + */ + GeofenceData getGeoFenceFromCache(int fenceId, int tenantId); +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java new file mode 100644 index 0000000000..770e31bdcc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020, 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.mgt.core.cache.impl; + +import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; +import org.wso2.carbon.device.mgt.core.cache.GeoCacheKey; +import org.wso2.carbon.device.mgt.core.cache.GeoCacheManager; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; + +import javax.cache.Cache; + +public class GeoCacheManagerImpl implements GeoCacheManager { + + private static GeoCacheManager geoCacheManager; + private GeoCacheManagerImpl() {} + + public static GeoCacheManager getInstance() { + if (geoCacheManager == null) { + synchronized (GeoCacheManagerImpl.class) { + if (geoCacheManager == null) { + geoCacheManager = new GeoCacheManagerImpl(); + } + } + } + return geoCacheManager; + } + + @Override + public void addFenceToCache(GeofenceData geofenceData, int fenceId, int tenantId) { + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + GeoCacheKey cacheKey = getCacheKey(fenceId, tenantId); + if (lCache.containsKey(cacheKey)) { + this.updateGeoFenceInCache(geofenceData, fenceId, tenantId); + } else { + lCache.put(cacheKey, geofenceData); + } + } + } + + @Override + public void removeFenceFromCache(int fenceId, int tenantId) { + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + GeoCacheKey cacheKey = getCacheKey(fenceId, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.remove(cacheKey); + } + } + } + + @Override + public void updateGeoFenceInCache(GeofenceData geofenceData, int fenceId, int tenantId) { + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + GeoCacheKey cacheKey = getCacheKey(fenceId, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.replace(cacheKey, geofenceData); + } + } + } + + @Override + public GeofenceData getGeoFenceFromCache(int fenceId, int tenantId) { + GeofenceData geofenceData = null; + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + geofenceData = lCache.get(getCacheKey(fenceId, tenantId)); + } + return geofenceData; + } + + private GeoCacheKey getCacheKey(int fenceId, int tenantId) { + GeoCacheKey geoCacheKey = new GeoCacheKey(); + geoCacheKey.setFenceId(fenceId); + geoCacheKey.setTenantId(tenantId); + return geoCacheKey; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index cf58967988..2a967238e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.core.config.analytics.OperationAnalyticsConfig import org.wso2.carbon.device.mgt.core.config.archival.ArchivalConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.CertificateCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration; +import org.wso2.carbon.device.mgt.core.config.cache.GeoFenceCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.geo.location.GeoLocationConfiguration; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.config.keymanager.KeyManagerConfigurations; @@ -54,6 +55,7 @@ public final class DeviceManagementConfig { private PullNotificationConfiguration pullNotificationConfiguration; private DeviceStatusTaskConfig deviceStatusTaskConfig; private DeviceCacheConfiguration deviceCacheConfiguration; + private GeoFenceCacheConfiguration geoFenceCacheConfiguration; private CertificateCacheConfiguration certificateCacheConfiguration; private OperationAnalyticsConfiguration operationAnalyticsConfiguration; private GeoLocationConfiguration geoLocationConfiguration; @@ -154,6 +156,15 @@ public final class DeviceManagementConfig { this.deviceCacheConfiguration = deviceCacheConfiguration; } + @XmlElement(name = "GeoFenceCacheConfiguration", required = true) + public GeoFenceCacheConfiguration getGeoFenceCacheConfiguration() { + return geoFenceCacheConfiguration; + } + + public void setGeoFenceCacheConfiguration(GeoFenceCacheConfiguration geoFenceCacheConfiguration) { + this.geoFenceCacheConfiguration = geoFenceCacheConfiguration; + } + @XmlElement(name = "CertificateCacheConfiguration", required = true) public CertificateCacheConfiguration getCertificateCacheConfiguration() { return certificateCacheConfiguration; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java new file mode 100644 index 0000000000..5a27eb7430 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020, 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.mgt.core.config.cache; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "GeoFenceCacheConfiguration") +public class GeoFenceCacheConfiguration { + + private boolean isEnabled; + private int expiryTime; + private long capacity; + + @XmlElement(name = "Enable", required = true) + public boolean isEnabled() { + return isEnabled; + } + + public void setEnabled(boolean enabled) { + isEnabled = enabled; + } + + @XmlElement(name = "ExpiryTime", required = true) + public int getExpiryTime() { + return expiryTime; + } + + public void setExpiryTime(int expiryTime) { + this.expiryTime = expiryTime; + } + + @XmlElement(name = "Capacity", required = true) + public long getCapacity() { + return capacity; + } + + public void setCapacity(long capacity) { + this.capacity = capacity; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java index 27e8d72f1b..0801edd6d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java @@ -34,7 +34,7 @@ public interface GeofenceDAO { * @return created row count * @throws DeviceManagementDAOException error occurs while saving the data */ - int saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException; + GeofenceData saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException; /** * Retrieve a geofence record for specified Id @@ -51,7 +51,26 @@ public interface GeofenceDAO { * @return List of geofences retrieved * @throws DeviceManagementDAOException error occurs while reading the data */ - List getGeofencesOfTenant(PaginationRequest request, int tenantId) + List getGeoFencesOfTenant(PaginationRequest request, int tenantId) + throws DeviceManagementDAOException; + + /** + * Search geofence by fence name of a specific tenant + * @param fenceName searching name + * @param tenantId searching tenant + * @return list of found fences + * @throws DeviceManagementDAOException + */ + List getGeoFencesOfTenant(String fenceName, int tenantId) + throws DeviceManagementDAOException; + + /** + * Get all fences of the specific tenant + * @param tenantId tenant id of the fences + * @return list of the fences owned by the tenant + * @throws DeviceManagementDAOException + */ + List getGeoFencesOfTenant(int tenantId) throws DeviceManagementDAOException; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java index 68223d9806..452c2ecc3a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; @@ -31,6 +32,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; @@ -39,7 +41,7 @@ import java.util.List; public class GeofenceDAOImpl implements GeofenceDAO { private static final Log log = LogFactory.getLog(GeofenceDAOImpl.class); @Override - public int saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException { + public GeofenceData saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException { try { Connection conn = this.getConnection(); String sql = "INSERT INTO DM_GEOFENCE(" + @@ -54,7 +56,7 @@ public class GeofenceDAOImpl implements GeofenceDAO { "OWNER, " + "TENANT_ID) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - try (PreparedStatement stmt = conn.prepareStatement(sql)) { + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { stmt.setString(1, geofenceData.getFenceName()); stmt.setString(2, geofenceData.getDescription()); stmt.setDouble(3, geofenceData.getLatitude()); @@ -65,7 +67,13 @@ public class GeofenceDAOImpl implements GeofenceDAO { stmt.setTimestamp(8, new Timestamp(new Date().getTime())); stmt.setString(9, geofenceData.getOwner()); stmt.setInt(10, geofenceData.getTenantId()); - return stmt.executeUpdate(); + if (stmt.executeUpdate() > 0) { + ResultSet generatedKeys = stmt.getGeneratedKeys(); + if (generatedKeys.next()) { + geofenceData.setId(generatedKeys.getInt(1)); + } + } + return geofenceData; } } catch (SQLException e) { String msg = "Error occurred while creating Geofence for the tenant id "+geofenceData.getTenantId(); @@ -110,7 +118,59 @@ public class GeofenceDAOImpl implements GeofenceDAO { } @Override - public List getGeofencesOfTenant(PaginationRequest request, int tenantId) + public List getGeoFencesOfTenant(PaginationRequest request, int tenantId) + throws DeviceManagementDAOException { + try { + boolean isNameProvided = false; + List geofenceData; + Connection conn = this.getConnection(); + String sql = "SELECT " + + "ID, " + + "FENCE_NAME, " + + "DESCRIPTION, " + + "LATITUDE, " + + "LONGITUDE, " + + "RADIUS, " + + "GEO_JSON, " + + "FENCE_SHAPE, " + + "OWNER, " + + "TENANT_ID " + + "FROM DM_GEOFENCE " + + "WHERE TENANT_ID = ? "; + + if (request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME) != null) { + sql += "AND FENCE_NAME LIKE ?"; + isNameProvided = true; + } + sql += "LIMIT ? OFFSET ?"; + int index = 1; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(index++, tenantId); + if (isNameProvided) { + stmt.setString(index++, request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME).toString() + "%"); + } + stmt.setInt(index++, request.getRowCount()); + stmt.setInt(index, request.getStartIndex()); + try (ResultSet rst = stmt.executeQuery()) { + geofenceData = extractGeofenceData(rst); + } + } + return geofenceData; + } catch (SQLException e) { + String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getGeoFencesOfTenant(String fenceName, int tenantId) + throws DeviceManagementDAOException { + return null; + } + + @Override + public List getGeoFencesOfTenant(int tenantId) throws DeviceManagementDAOException { try { List geofenceData; @@ -127,12 +187,9 @@ public class GeofenceDAOImpl implements GeofenceDAO { "OWNER, " + "TENANT_ID " + "FROM DM_GEOFENCE " + - "WHERE TENANT_ID = ? " + - "LIMIT ? OFFSET ?"; + "WHERE TENANT_ID = ? "; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); - stmt.setInt(2, request.getRowCount()); - stmt.setInt(3, request.getStartIndex()); try (ResultSet rst = stmt.executeQuery()) { geofenceData = extractGeofenceData(rst); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index d54818ca9c..d8409ef112 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -38,7 +38,6 @@ import org.wso2.carbon.core.util.Utils; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices; import org.wso2.carbon.device.mgt.common.PaginationRequest; -import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.geo.service.Alert; import org.wso2.carbon.device.mgt.common.geo.service.GeoFence; @@ -46,6 +45,7 @@ import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService; import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; +import org.wso2.carbon.device.mgt.core.cache.impl.GeoCacheManagerImpl; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO; @@ -1249,8 +1249,10 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean createGeofence(GeofenceData geofenceData) throws GeoLocationBasedServiceException { + int tenantId; try { - geofenceData.setTenantId(DeviceManagementDAOUtil.getTenantId()); + tenantId = DeviceManagementDAOUtil.getTenantId(); + geofenceData.setTenantId(tenantId); geofenceData.setOwner(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving tenant Id"; @@ -1260,7 +1262,10 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic try { DeviceManagementDAOFactory.beginTransaction(); - return geofenceDAO.saveGeofence(geofenceData) > 0; + geofenceData = geofenceDAO.saveGeofence(geofenceData); + GeoCacheManagerImpl.getInstance() + .addFenceToCache(geofenceData, geofenceData.getId(), tenantId); + return true; } catch (TransactionManagementException e) { String msg = "Failed to begin transaction for saving geofence"; log.error(msg, e); @@ -1276,10 +1281,28 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } @Override - public GeofenceData getGeofence(int fenceId) throws GeoLocationBasedServiceException { + public GeofenceData getGeoFences(int fenceId) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + GeofenceData geofenceData = GeoCacheManagerImpl.getInstance() + .getGeoFenceFromCache(fenceId, tenantId); + if (geofenceData != null) { + return geofenceData; + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant Id"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + try { DeviceManagementDAOFactory.openConnection(); - return geofenceDAO.getGeofence(fenceId); + GeofenceData geofence = geofenceDAO.getGeofence(fenceId); + if (geofence != null) { + GeoCacheManagerImpl.getInstance().addFenceToCache(geofence, fenceId, tenantId); + } + return geofence; } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving Geofence data with ID "+fenceId; log.error(msg, e); @@ -1294,7 +1317,37 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } @Override - public List getGeofence(PaginationRequest request) throws GeoLocationBasedServiceException { + public List getGeoFences(PaginationRequest request) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + + try { + if (log.isDebugEnabled()) { + log.debug("Retrieving geofence data for the tenant " + tenantId); + } + DeviceManagementDAOFactory.openConnection(); + return geofenceDAO.getGeoFencesOfTenant(request, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } catch (SQLException e) { + String msg = "Failed to open the DB connection to retrieve Geofence"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public List getGeoFences(String fenceName) throws GeoLocationBasedServiceException { int tenantId; try { tenantId = DeviceManagementDAOUtil.getTenantId(); @@ -1309,7 +1362,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic log.debug("Retrieving geofence data for the tenant " + tenantId); } DeviceManagementDAOFactory.openConnection(); - return geofenceDAO.getGeofencesOfTenant(request, tenantId); + return geofenceDAO.getGeoFencesOfTenant(fenceName, tenantId); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId; log.error(msg, e); @@ -1323,17 +1376,64 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } } + @Override + public List getGeoFences() throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + + try { + if (log.isDebugEnabled()) { + log.debug("Retrieving all fence data for the tenant " + tenantId); + } + DeviceManagementDAOFactory.openConnection(); + List geoFencesOfTenant = geofenceDAO.getGeoFencesOfTenant(tenantId); + for (GeofenceData geofenceData : geoFencesOfTenant) { + GeoCacheManagerImpl.getInstance() + .addFenceToCache(geofenceData, geofenceData.getId(), tenantId); + } + return geoFencesOfTenant; + + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } catch (SQLException e) { + String msg = "Failed to open the DB connection to retrieve Geofence"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + } + @Override public boolean deleteGeofenceData(int fenceId) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + try { DeviceManagementDAOFactory.beginTransaction(); if (geofenceDAO.deleteGeofenceById(fenceId) > 0) { DeviceManagementDAOFactory.commitTransaction(); + GeoCacheManagerImpl.getInstance().removeFenceFromCache(fenceId, tenantId); return true; } - DeviceManagementDAOFactory.rollbackTransaction(); return false; } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while deleting geofence"; log.error(msg, e); throw new GeoLocationBasedServiceException(msg, e); @@ -1349,9 +1449,22 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean updateGeofence(GeofenceData geofenceData, int fenceId) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + try { DeviceManagementDAOFactory.beginTransaction(); - return geofenceDAO.updateGeofence(geofenceData, fenceId) > 0; + if (geofenceDAO.updateGeofence(geofenceData, fenceId) > 0) { + GeoCacheManagerImpl.getInstance().updateGeoFenceInCache(geofenceData, fenceId, tenantId); + return true; + } + return false; } catch (TransactionManagementException e) { String msg = "Failed to begin transaction for saving geofence"; log.error(msg, e); 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 9404e5de86..6a64c5b3d9 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 @@ -67,6 +67,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; @@ -75,6 +76,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey; +import org.wso2.carbon.device.mgt.core.cache.GeoCacheKey; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; @@ -132,6 +134,7 @@ public final class DeviceManagerUtil { public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; private static boolean isDeviceCacheInitialized = false; + private static boolean isGeoFenceCacheInitialized = false; public static Document convertToDocument(File file) throws DeviceManagementException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); @@ -641,6 +644,44 @@ public final class DeviceManagerUtil { } } + public static void initializeGeofenceCache() { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + int geoCacheExpiry = config.getGeoFenceCacheConfiguration().getExpiryTime(); + long geoCacheCapacity = config.getGeoFenceCacheConfiguration().getCapacity(); + CacheManager manager = getCacheManager(); + if (config.getGeoFenceCacheConfiguration().isEnabled()) { + if(!isGeoFenceCacheInitialized) { + isGeoFenceCacheInitialized = true; + if (manager != null) { + if (geoCacheExpiry > 0) { + manager.createCacheBuilder(DeviceManagementConstants.GEOFENCE_CACHE). + setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, + geoCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration. + Duration(TimeUnit.SECONDS, geoCacheExpiry)).setStoreByValue(true).build(); + if(geoCacheCapacity > 0 ) { + ((CacheImpl) manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE)). + setCapacity(geoCacheCapacity); + } + } else { + manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } + } else { + if (geoCacheExpiry > 0) { + Caching.getCacheManager(). + createCacheBuilder(DeviceManagementConstants.GEOFENCE_CACHE). + setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, + geoCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration. + Duration(TimeUnit.SECONDS, geoCacheExpiry)).setStoreByValue(true).build(); + ((CacheImpl)(manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE))). + setCapacity(geoCacheCapacity); + } else { + Caching.getCacheManager().getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } + } + } + } + } + public static Cache getDeviceCache() { DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); CacheManager manager = getCacheManager(); @@ -659,6 +700,24 @@ public final class DeviceManagerUtil { return deviceCache; } + public static Cache getGeoCache() { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + CacheManager manager = getCacheManager(); + Cache geoCache = null; + if (config.getGeoFenceCacheConfiguration().isEnabled()) { + if(!isGeoFenceCacheInitialized) { + initializeGeofenceCache(); + } + if (manager != null) { + geoCache = manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } else { + geoCache = Caching.getCacheManager(DeviceManagementConstants.GEOFENCE_CACHE) + .getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } + } + return geoCache; + } + /** * Create an app and get app registration token from the application registration endpoint * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml index 7b57ee7414..723662a8cd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml @@ -89,6 +89,14 @@ server environment--> 10000 + + false + 600 + + 10000 + false 86400 diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 4ca080d7f2..51ab59b4eb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -40,7 +40,6 @@ import org.apache.commons.lang.StringUtils; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; @@ -205,7 +204,7 @@ public class PolicyManagerUtil { if (log.isDebugEnabled()) { log.debug("Retrieving geofence with ID " + fenceId); } - GeofenceData geofence = geoLocationProviderService.getGeofence(fenceId); + GeofenceData geofence = geoLocationProviderService.getGeoFences(fenceId); if (geofence != null) { JsonObject operationPayload = new JsonObject(); operationPayload.addProperty("fenceId", geofence.getId()); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml index bef658a615..cbb8cc6905 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml @@ -89,6 +89,14 @@ server environment--> 10000 + + true + 600 + + 10000 + true 86400 From 2ae8c126e88c001d5c6f5c7096d5f4ce50e14f14 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Oct 2020 21:01:24 +0530 Subject: [PATCH 2/8] fixing issues with activity retrieval --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 429 ++++-------------- .../impl/operation/MySQLOperationDAOImpl.java | 341 +++----------- .../operation/OracleOperationDAOImpl.java | 230 ++-------- .../operation/SQLServerOperationDAOImpl.java | 223 ++------- .../mgt/dao/util/OperationDAOUtil.java | 117 ++++- 5 files changed, 375 insertions(+), 965 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 37984c8709..caa2a86601 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -23,6 +23,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; @@ -375,6 +376,10 @@ public class GenericOperationDAOImpl implements OperationDAO { for (Map.Entry> deviceOpRes : deviceOpResponseMap.entrySet()) { for (ActivityStatus status : activityStatuses) { if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) { + if(status.getResponses()==null){ + List responses = new ArrayList<>(); + status.setResponses(responses); + } status.getResponses().addAll(deviceOpRes.getValue()); } } @@ -942,349 +947,117 @@ public class GenericOperationDAOImpl implements OperationDAO { @Override public List getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.ID AS OP_RES_ID, " + - " ops.OPERATION_RESPONSE , " + - " ops.IS_LARGE_RESPONSE , " + - " opr.INITIATED_BY " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.INITIATED_BY, " + - " op.TYPE AS OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME AS DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM" + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? AND op.INITIATED_BY = ?" + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " LIMIT ? OFFSET ?) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE " + - " opr.UPDATED_TIMESTAMP > ? AND opr.INITIATED_BY = ?" + - " AND opr.TENANT_ID = ? "; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setString(2, user); - stmt.setInt(3, tenantId); - stmt.setInt(4, limit); - stmt.setInt(5, offset); - stmt.setLong(6, timestamp); - stmt.setString(7, user); - stmt.setInt(8, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - List largeResponseIDs = new ArrayList<>(); - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " opr.ID AS OP_RES_ID," + + " opr.RECEIVED_TIMESTAMP," + + " opr.OPERATION_RESPONSE," + + " opr.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING eom " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + " AND eom.INITIATED_BY = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + stmt.setString(5, user); + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if (!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database. " + e.getMessage(), e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database. "; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - - return activities; - } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, - int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) + throws OperationManagementDAOException { try { Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.IS_LARGE_RESPONSE, " + - " ops.ID AS OP_RES_ID, " + - " ops.OPERATION_RESPONSE " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.TYPE AS OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME AS DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM" + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? " + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " LIMIT ? OFFSET ?) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE " + - " opr.UPDATED_TIMESTAMP > ? " + - " AND opr.TENANT_ID = ? "; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, limit); - stmt.setInt(4, offset); - stmt.setLong(5, timestamp); - stmt.setInt(6, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + String sql = "SELECT " + + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if (!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database. " + e.getMessage(), e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg,e); + throw new OperationManagementDAOException(msg, e); } - return activities; } @Override @@ -1293,7 +1066,7 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE " + + String sql = "SELECT COUNT(DISTINCT(OPERATION_ID)) AS COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE " + "UPDATED_TIMESTAMP > ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setLong(1, timestamp); @@ -1490,11 +1263,6 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setId(rs.getInt("ID")); operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } if (rs.getLong("UPDATED_TIMESTAMP") == 0) { operation.setReceivedTimeStamp(""); } else { @@ -1692,11 +1460,6 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setId(rs.getInt("ID")); operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } if (rs.getLong("UPDATED_TIMESTAMP") == 0) { operation.setReceivedTimeStamp(""); } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java index d65f7a49d5..ec097133c5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -200,171 +201,69 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, - int offset) + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " eom.ENROLMENT_ID," + - " eom.CREATED_TIMESTAMP," + - " eom.UPDATED_TIMESTAMP," + - " eom.OPERATION_ID," + - " eom.OPERATION_CODE," + - " eom.INITIATED_BY," + - " eom.TYPE," + - " eom.STATUS," + - " eom.DEVICE_ID," + - " eom.DEVICE_IDENTIFICATION," + - " eom.DEVICE_TYPE," + - " opr.ID AS OP_RES_ID," + - " opr.RECEIVED_TIMESTAMP," + - " opr.OPERATION_RESPONSE," + - " opr.IS_LARGE_RESPONSE " + - "FROM " + - " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + - " LEFT JOIN " + - " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + - "WHERE " + - " eom.UPDATED_TIMESTAMP > ? " + - " AND eom.TENANT_ID = ? " + - "ORDER BY eom.UPDATED_TIMESTAMP " + - "LIMIT ? OFFSET ?"; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, limit); - stmt.setInt(4, offset); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - List largeResponseIDs = new ArrayList<>(); - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " opr.ID AS OP_RES_ID," + + " opr.RECEIVED_TIMESTAMP," + + " opr.OPERATION_RESPONSE," + + " opr.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } @Override public List getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + " eom.ENROLMENT_ID," + " eom.CREATED_TIMESTAMP," + @@ -383,138 +282,38 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { " opr.IS_LARGE_RESPONSE " + "FROM " + " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + - " LEFT JOIN " + + "LEFT JOIN " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + "WHERE " + " eom.UPDATED_TIMESTAMP > ? " + " AND eom.TENANT_ID = ? " + " AND eom.INITIATED_BY = ? " + - "ORDER BY eom.UPDATED_TIMESTAMP " + - "LIMIT ? OFFSET ?"; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setString(3, user); - stmt.setInt(4, limit); - stmt.setInt(5, offset); - -// stmt.setLong(1, timestamp); -// stmt.setString(2, user); -// stmt.setInt(3, tenantId); -// stmt.setInt(4, limit); -// stmt.setInt(5, offset); -// stmt.setLong(6, timestamp); -// stmt.setString(7, user); -// stmt.setInt(8, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + stmt.setString(5, user); + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } - -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java index 45d656e194..37de111d8a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java @@ -18,12 +18,13 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; 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.OperationResponse; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -32,7 +33,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; -import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -48,6 +48,8 @@ import java.util.Map; */ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { + private static final Log log = LogFactory.getLog(OracleOperationDAOImpl.class); + @Override public List getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException { @@ -185,192 +187,56 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { @Override public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - /*String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" - + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" - + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" - + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" - + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" - + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" - + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" - + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" - + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" - + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" - + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; - - if (timestamp == 0) { - sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } else { - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } - */ - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.ID OP_RES_ID, " + - " ops.IS_LARGE_RESPONSE, " + - " ops.OPERATION_RESPONSE " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.TYPE OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM " + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? " + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE " + - " opr.UPDATED_TIMESTAMP > ? " + - " AND opr.TENANT_ID = ? "; - - - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, offset); - stmt.setInt(4, limit); - stmt.setLong(5, timestamp); - stmt.setInt(6, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while getting the operation details from " + "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java index 5c486bac29..a3520084f6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java @@ -41,6 +41,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; @@ -51,7 +52,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; -import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -67,7 +67,7 @@ import java.util.Map; */ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { - private static final Log log = LogFactory.getLog(GenericOperationDAOImpl.class); + private static final Log log = LogFactory.getLog(SQLServerOperationDAOImpl.class); @Override public List getOperationsForDevice(int enrolmentId, PaginationRequest request) @@ -162,192 +162,59 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) + throws OperationManagementDAOException { try { Connection conn = OperationManagementDAOFactory.getConnection(); - /*String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" - + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" - + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" - + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" - + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" - + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" - + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" - + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" - + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" - + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" - + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; - - if (timestamp == 0) { - sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } else { - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - }*/ - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.ID OP_RES_ID, " + - " ops.IS_LARGE_RESPONSE, " + - " ops.OPERATION_RESPONSE " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.TYPE OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM" + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? " + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE" + - " opr.UPDATED_TIMESTAMP > ? " + - " AND opr.TENANT_ID = ? "; - - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, offset); - stmt.setInt(4, limit); - stmt.setLong(5, timestamp); - stmt.setInt(6, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while getting the operation details from " + "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java index 65fe7b99f3..c58b75e745 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java @@ -20,6 +20,11 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityMapper; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation; @@ -27,12 +32,17 @@ import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; public class OperationDAOUtil { private static final Log log = LogFactory.getLog(OperationDAOUtil.class); @@ -178,8 +188,113 @@ public class OperationDAOUtil { operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); } - public static String getActivityId(int operationId) { return DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId; } + + public static ActivityHolder getActivityHolder(ResultSet rs) throws OperationManagementDAOException { + Map activityInfo = new TreeMap<>(); + List largeResponseIDs = new ArrayList<>(); + List activities = new ArrayList<>(); + try { + while (rs.next()) { + ActivityMapper activityMapper = activityInfo.get(rs.getInt("OPERATION_ID")); + if (activityMapper != null) { + ActivityStatus activityStatus = activityMapper.getActivityStatusMap() + .get(rs.getInt("ENROLMENT_ID")); + if (activityStatus == null) { + + activityStatus = new ActivityStatus(); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); + + activityStatus.setDeviceIdentifier(deviceIdentifier); + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + if (rs.getBoolean("IS_LARGE_RESPONSE")) { + largeResponseIDs.add(rs.getInt("OP_RES_ID")); + } else { + List operationResponses = new ArrayList<>(); + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + activityStatus.setResponses(operationResponses); + } + } + activityMapper.getActivityStatusMap().put(rs.getInt("ENROLMENT_ID"), activityStatus); + } else { + if (rs.getInt("OP_RES_ID") != 0 && rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + if (rs.getBoolean("IS_LARGE_RESPONSE")) { + largeResponseIDs.add(rs.getInt("OP_RES_ID")); + } else { + activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); + } + } + } + } else { + Activity activity = new Activity(); + List statusList = new ArrayList<>(); + ActivityStatus activityStatus = new ActivityStatus(); + + activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); + activity.setCreatedTimeStamp( + new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + activity.setInitiatedBy(rs.getString("INITIATED_BY")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + if (rs.getBoolean("IS_LARGE_RESPONSE")) { + largeResponseIDs.add(rs.getInt("OP_RES_ID")); + } else { + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + } + } + activityStatus.setResponses(operationResponses); + statusList.add(activityStatus); + activity.setActivityStatus(statusList); + activity.setActivityId(getActivityId(rs.getInt("OPERATION_ID"))); + + ActivityMapper newActivityMapper = new ActivityMapper(); + Map activityStatusMap = new TreeMap<>(); + activityStatusMap.put(rs.getInt("ENROLMENT_ID"), activityStatus); + + newActivityMapper.setActivity(activity); + newActivityMapper.setActivityStatusMap(activityStatusMap); + activityInfo.put(rs.getInt("OPERATION_ID"), newActivityMapper); + } + } + + activityInfo.values().forEach(holder -> { + List activityStatuses = new ArrayList<>(holder.getActivityStatusMap().values()); + holder.getActivity().setActivityStatus(activityStatuses); + activities.add(holder.getActivity()); + }); + + ActivityHolder activityHolder = new ActivityHolder(); + activityHolder.setActivityList(activities); + activityHolder.setLargeResponseIDs(largeResponseIDs); + return activityHolder; + } catch (SQLException e) { + String msg = "Error occurred while getting activity data from the result set."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); + } + } } From 22eba43aa9fa81d858063c5f9af51b2cd8796d43 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Oct 2020 21:05:33 +0530 Subject: [PATCH 3/8] including missing files --- .../common/device/details/LocationBean.java | 89 +++++++++++++++++++ .../device/details/LocationEventBean.java | 69 ++++++++++++++ .../common/operation/mgt/ActivityHolder.java | 42 +++++++++ .../common/operation/mgt/ActivityMapper.java | 42 +++++++++ 4 files changed, 242 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java new file mode 100644 index 0000000000..261d387e03 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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.mgt.common.device.details; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.List; + +@ApiModel( + value = "LocationBean", + description = "This class carries all information related IOS Device location." +) +public class LocationBean { + + @ApiModelProperty( + name = "latitude", + value = "Latitude of the IOS device Location.", + required = true + ) + private float latitude; + @ApiModelProperty( + name = "longitude", + value = "Longitude of the IOS device Location.", + required = true + ) + private float longitude; + @ApiModelProperty( + name = "operationId", + value = "Specific Id of the Location operation.", + required = true + ) + private String operationId; + + @ApiModelProperty( + name = "locationEvents", + value = "If this is a device initiated location publishing." + ) + private List locations; + + public List getLocationEvents() { + return locations; + } + + public void setLocationEvents(List locationEvents) { + this.locations = locationEvents; + } + + public String getOperationId() { + return operationId; + } + + public void setOperationId(String operationId) { + this.operationId = operationId; + } + + + public float getLatitude() { + return latitude; + } + + public void setLatitude(float latitude) { + this.latitude = latitude; + } + + public float getLongitude() { + return longitude; + } + + public void setLongitude(float longitude) { + this.longitude = longitude; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java new file mode 100644 index 0000000000..6e1ef6c976 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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.mgt.common.device.details; + +import io.swagger.annotations.ApiModelProperty; + +public class LocationEventBean { + + @ApiModelProperty( + name = "latitude", + value = "Latitude of the IOS device Location.", + required = true + ) + private String latitude; + + @ApiModelProperty( + name = "longitude", + value = "Longitude of the IOS device Location.", + required = true + ) + private String longitude; + + @ApiModelProperty( + name = "timestamp", + value = "Device Location time.", + required = true + ) + private String timestamp; + + public String getLatitude() { + return latitude; + } + + public void setLatitude(String latitude) { + this.latitude = latitude; + } + + public String getLongitude() { + return longitude; + } + + public void setLongitude(String longitude) { + this.longitude = longitude; + } + + public String getTimestamp() { + return timestamp; + } + + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java new file mode 100644 index 0000000000..6ab26276b0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java @@ -0,0 +1,42 @@ +/* Copyright (c) 2020, 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.mgt.common.operation.mgt; + +import java.util.List; + +public class ActivityHolder { + + private List activityList; + private List largeResponseIDs; + + public List getActivityList() { + return activityList; + } + + public void setActivityList(List activityList) { + this.activityList = activityList; + } + + public List getLargeResponseIDs() { + return largeResponseIDs; + } + + public void setLargeResponseIDs(List largeResponseIDs) { + this.largeResponseIDs = largeResponseIDs; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java new file mode 100644 index 0000000000..544611453e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java @@ -0,0 +1,42 @@ +/* Copyright (c) 2020, 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.mgt.common.operation.mgt; + +import java.util.Map; + +public class ActivityMapper { + + private Activity activity; + private Map activityStatusMap; + + public Activity getActivity() { + return activity; + } + + public void setActivity(Activity activity) { + this.activity = activity; + } + + public Map getActivityStatusMap() { + return activityStatusMap; + } + + public void setActivityStatusMap(Map activityStatusMap) { + this.activityStatusMap = activityStatusMap; + } +} From 61c05143c4dd5410fcc9810edfa62f9074d76dd9 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 23 Oct 2020 16:59:35 +0530 Subject: [PATCH 4/8] Fixing activity retrieval with intiatedBy --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 2 +- .../dao/impl/operation/MySQLOperationDAOImpl.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index caa2a86601..caa4de580a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -1090,7 +1090,7 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(*) AS COUNT " + + String sql = "SELECT COUNT(DISTINCT(OPERATION_ID)) AS COUNT " + "FROM DM_ENROLMENT_OP_MAPPING AS m " + " INNER JOIN " + " DM_OPERATION dp ON dp.ID = m.OPERATION_ID " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java index ec097133c5..532f0ca364 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -285,20 +285,20 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { "LEFT JOIN " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + "INNER JOIN " + - " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE INITIATED_BY = ? " + + " ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + "WHERE " + " eom.UPDATED_TIMESTAMP > ? " + " AND eom.TENANT_ID = ? " + - " AND eom.INITIATED_BY = ? " + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, offset); - stmt.setInt(2, limit); - stmt.setLong(3, timestamp); - stmt.setInt(4, tenantId); - stmt.setString(5, user); + stmt.setString(1, user); + stmt.setInt(2, offset); + stmt.setInt(3, limit); + stmt.setLong(4, timestamp); + stmt.setInt(5, tenantId); try (ResultSet rs = stmt.executeQuery()) { ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); From ad299e676c3589914f01bda780c17d3a74b0d775 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 23 Oct 2020 17:21:23 +0530 Subject: [PATCH 5/8] removing un-necessary files --- .../common/device/details/LocationBean.java | 89 ------------------- .../device/details/LocationEventBean.java | 69 -------------- 2 files changed, 158 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java deleted file mode 100644 index 261d387e03..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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.mgt.common.device.details; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; - -import java.util.List; - -@ApiModel( - value = "LocationBean", - description = "This class carries all information related IOS Device location." -) -public class LocationBean { - - @ApiModelProperty( - name = "latitude", - value = "Latitude of the IOS device Location.", - required = true - ) - private float latitude; - @ApiModelProperty( - name = "longitude", - value = "Longitude of the IOS device Location.", - required = true - ) - private float longitude; - @ApiModelProperty( - name = "operationId", - value = "Specific Id of the Location operation.", - required = true - ) - private String operationId; - - @ApiModelProperty( - name = "locationEvents", - value = "If this is a device initiated location publishing." - ) - private List locations; - - public List getLocationEvents() { - return locations; - } - - public void setLocationEvents(List locationEvents) { - this.locations = locationEvents; - } - - public String getOperationId() { - return operationId; - } - - public void setOperationId(String operationId) { - this.operationId = operationId; - } - - - public float getLatitude() { - return latitude; - } - - public void setLatitude(float latitude) { - this.latitude = latitude; - } - - public float getLongitude() { - return longitude; - } - - public void setLongitude(float longitude) { - this.longitude = longitude; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java deleted file mode 100644 index 6e1ef6c976..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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.mgt.common.device.details; - -import io.swagger.annotations.ApiModelProperty; - -public class LocationEventBean { - - @ApiModelProperty( - name = "latitude", - value = "Latitude of the IOS device Location.", - required = true - ) - private String latitude; - - @ApiModelProperty( - name = "longitude", - value = "Longitude of the IOS device Location.", - required = true - ) - private String longitude; - - @ApiModelProperty( - name = "timestamp", - value = "Device Location time.", - required = true - ) - private String timestamp; - - public String getLatitude() { - return latitude; - } - - public void setLatitude(String latitude) { - this.latitude = latitude; - } - - public String getLongitude() { - return longitude; - } - - public void setLongitude(String longitude) { - this.longitude = longitude; - } - - public String getTimestamp() { - return timestamp; - } - - public void setTimestamp(String timestamp) { - this.timestamp = timestamp; - } -} From d831ef32844ed5daba349a849e86265970f0cbe4 Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 26 Oct 2020 09:40:50 +0530 Subject: [PATCH 6/8] code improvements --- .../core/operation/mgt/dao/impl/GenericOperationDAOImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index caa4de580a..7094afdcd3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -376,9 +376,8 @@ public class GenericOperationDAOImpl implements OperationDAO { for (Map.Entry> deviceOpRes : deviceOpResponseMap.entrySet()) { for (ActivityStatus status : activityStatuses) { if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) { - if(status.getResponses()==null){ - List responses = new ArrayList<>(); - status.setResponses(responses); + if (status.getResponses() == null) { + status.setResponses(new ArrayList<>()); } status.getResponses().addAll(deviceOpRes.getValue()); } From c88ec6077bad25700d0a468c733b85bc7237e7a8 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Fri, 30 Oct 2020 00:59:43 +0530 Subject: [PATCH 7/8] Fixes multiple device deleting issue Fixes entgra/product-iots#656 --- .../DeviceManagementProviderServiceImpl.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 20bb832532..8aa0390820 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -150,6 +150,7 @@ import java.io.IOException; import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; @@ -613,44 +614,46 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } - try { - DeviceCacheKey deviceCacheKey; - for (Device device : existingDevices) { - if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) { - String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType() - + " is not dis-enrolled to permanently delete the device"; - log.error(msg); - throw new InvalidDeviceException(msg); - } - deviceCacheKey = new DeviceCacheKey(); - deviceCacheKey.setDeviceId(device.getDeviceIdentifier()); - deviceCacheKey.setDeviceType(device.getType()); - deviceCacheKey.setTenantId(tenantId); - deviceCacheKeyList.add(deviceCacheKey); - deviceIds.add(device.getId()); - validDeviceIdentifiers.add(device.getDeviceIdentifier()); - enrollmentIds.add(device.getEnrolmentInfo().getId()); - if (deviceIdentifierMap.containsKey(device.getType())) { - deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier()); - } else { - deviceIdentifierMap.put(device.getType(), Collections.singletonList(device.getDeviceIdentifier())); - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - log.error("Device Manager associated with the device type '" + device.getType() + - "' is null. Therefore, not attempting method 'deleteDevice'"); - return false; - } - deviceManagerMap.put(device.getType(), deviceManager); - } - } - if (deviceIds.isEmpty()) { - String msg = "No device IDs found for the device identifiers '" + deviceIdentifiers + "'"; + DeviceCacheKey deviceCacheKey; + for (Device device : existingDevices) { + if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) { + String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType() + + " is not dis-enrolled to permanently delete the device"; log.error(msg); throw new InvalidDeviceException(msg); } - if (log.isDebugEnabled()) { - log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers); + deviceCacheKey = new DeviceCacheKey(); + deviceCacheKey.setDeviceId(device.getDeviceIdentifier()); + deviceCacheKey.setDeviceType(device.getType()); + deviceCacheKey.setTenantId(tenantId); + deviceCacheKeyList.add(deviceCacheKey); + deviceIds.add(device.getId()); + validDeviceIdentifiers.add(device.getDeviceIdentifier()); + enrollmentIds.add(device.getEnrolmentInfo().getId()); + if (deviceIdentifierMap.containsKey(device.getType())) { + deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier()); + } else { + deviceIdentifierMap.put(device.getType(), + new ArrayList<>(Collections.singletonList(device.getDeviceIdentifier()))); + DeviceManager deviceManager = this.getDeviceManager(device.getType()); + if (deviceManager == null) { + log.error("Device Manager associated with the device type '" + device.getType() + + "' is null. Therefore, not attempting method 'deleteDevice'"); + return false; + } + deviceManagerMap.put(device.getType(), deviceManager); } + } + if (deviceIds.isEmpty()) { + String msg = "No device IDs found for the device identifiers '" + deviceIdentifiers + "'"; + log.error(msg); + throw new InvalidDeviceException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers); + } + + try { DeviceManagementDAOFactory.beginTransaction(); //deleting device from the core deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); From b344e9d93b81f3ce5b948b4f473be4b07da4d91c Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Fri, 30 Oct 2020 15:49:06 +0530 Subject: [PATCH 8/8] Fix app subscription detail showing issue --- .../admin/SubscriptionManagementAdminAPI.java | 6 ++---- .../ReleasePage/components/ReleaseView/index.js | 11 +++-------- .../react-app/src/scenes/Login/index.js | 6 +++--- 3 files changed, 8 insertions(+), 15 deletions(-) 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/admin/SubscriptionManagementAdminAPI.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/admin/SubscriptionManagementAdminAPI.java index 7fb357e4fe..7c47b1f7cf 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/admin/SubscriptionManagementAdminAPI.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/admin/SubscriptionManagementAdminAPI.java @@ -27,14 +27,12 @@ import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.Info; import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; +import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.application.mgt.common.ErrorResponse; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.GET; -import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -64,7 +62,7 @@ import java.util.List; ) @Scopes( scopes = { - @org.wso2.carbon.apimgt.annotations.api.Scope( + @Scope( name = "View Application Subscriptions", description = "View Application Subscriptions.", key = "perm:admin:app:subscription:view", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Home/scenes/Apps/scenes/Release/components/ReleasePage/components/ReleaseView/index.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Home/scenes/Apps/scenes/Release/components/ReleasePage/components/ReleaseView/index.js index fa6307d6b0..3b0a02f92e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Home/scenes/Apps/scenes/Release/components/ReleasePage/components/ReleaseView/index.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Home/scenes/Apps/scenes/Release/components/ReleasePage/components/ReleaseView/index.js @@ -294,14 +294,9 @@ class ReleaseView extends React.Component { - - - - } - /> + + + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Login/index.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Login/index.js index 3d0f11d4a8..e7d4ccf679 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Login/index.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/scenes/Login/index.js @@ -96,10 +96,10 @@ class Login extends React.Component { const config = this.props.context; return (
-
+
- + - +