From 90dac0a10103e2a2a399ee3ec082a4876c36dd09 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 24 Sep 2020 08:53:35 +0530 Subject: [PATCH] Add location bean classes and format the source --- .../core/impl/SubscriptionManagerImpl.java | 6 +- .../impl/DeviceManagementServiceImpl.java | 82 +++++++++-------- .../common/device/details/LocationBean.java | 89 +++++++++++++++++++ .../device/details/LocationEventBean.java | 69 ++++++++++++++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 65 +++++++------- .../DeviceManagementProviderServiceImpl.java | 13 +-- .../src/main/resources/conf/mdm-ui-config.xml | 2 + 7 files changed, 243 insertions(+), 83 deletions(-) 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 diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 158b67ff10..c844ddaf08 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -108,7 +108,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { private LifecycleStateManager lifecycleStateManager; public SubscriptionManagerImpl() { - lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager(); + this.lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager(); this.subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); } @@ -549,7 +549,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { boolean isValidSubType = Arrays.stream(SubscriptionType.values()) .anyMatch(sub -> sub.name().equalsIgnoreCase(subType)); if (!isValidSubType) { - String msg = "Found invalid subscription type " + subType+ " to install application release" ; + String msg = "Found invalid subscription type " + subType+ " to subscribe application release" ; log.error(msg); throw new BadRequestException(msg); } @@ -579,8 +579,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ApplicationDTO applicationDTO, String subType, List subscribers, String action) throws ApplicationManagementException { + //Get app subscribing info of each device SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices, applicationDTO.getApplicationReleaseDTOs().get(0).getId()); + List activityList = new ArrayList<>(); List deviceIdentifiers = new ArrayList<>(); List ignoredDeviceIdentifiers = new ArrayList<>(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 73a07b7aad..bd04c963bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -504,15 +504,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.OK).entity(device).build(); } - @Path("/{deviceType}/{deviceId}/location-history") @GET - public Response getDeviceLocationInfo(@PathParam("deviceType") String deviceType, - @PathParam("deviceId") String deviceId, - @QueryParam("from") long from, @QueryParam("to") long to) { - - String errorMessage; - DeviceLocationHistory deviceLocationHistory = new DeviceLocationHistory(); - + @Path("/{deviceType}/{deviceId}/location-history") + public Response getDeviceLocationInfo( + @PathParam("deviceType") String deviceType, + @PathParam("deviceId") String deviceId, + @QueryParam("from") long from, + @QueryParam("to") long to) { try { RequestValidationUtil.validateDeviceIdentifier(deviceType, deviceId); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); @@ -520,81 +518,81 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(deviceType); if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, authorizedUser)) { String msg = "User '" + authorizedUser + "' is not authorized to retrieve the given device id '" + deviceId + "'"; log.error(msg); - return Response.status(Response.Status.UNAUTHORIZED).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(msg).build()).build(); + return Response.status(Response.Status.UNAUTHORIZED).entity(new ErrorResponse.ErrorResponseBuilder() + .setCode(Response.Status.UNAUTHORIZED.getStatusCode()).setMessage(msg).build()).build(); } if (from == 0 || to == 0) { - errorMessage = "Invalid values for from/to"; - log.error(errorMessage); - return Response.status(Response.Status.BAD_REQUEST).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage)).build(); + String msg = "Invalid values for from/to"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.ErrorResponseBuilder() + .setCode(Response.Status.BAD_REQUEST.getStatusCode()).setMessage(msg)).build(); } - List> locationHistorySnapshotList = new ArrayList<>(); // Get the location history snapshots for the given period - List deviceLocationHistorySnapshots = dms.getDeviceLocationInfo(deviceIdentifier, from, to); + List deviceLocationHistorySnapshots = dms + .getDeviceLocationInfo(deviceIdentifier, from, to); OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getDeviceMonitoringConfig(deviceType); int taskFrequency = operationMonitoringTaskConfig.getFrequency(); int operationRecurrentTimes = 0; List monitoringOperations = operationMonitoringTaskConfig.getMonitoringOperation(); - for (MonitoringOperation monitoringOperation : - monitoringOperations) { + for (MonitoringOperation monitoringOperation : monitoringOperations) { if (monitoringOperation.getTaskName().equals("DEVICE_LOCATION")) { operationRecurrentTimes = monitoringOperation.getRecurrentTimes(); break; } } + // Device Location operation frequency in milliseconds. Adding 100000 ms as an error long operationFrequency = taskFrequency * operationRecurrentTimes + 100000; + Queue deviceLocationHistorySnapshotsQueue = new LinkedList<>( + deviceLocationHistorySnapshots); + List> locationHistorySnapshotList = new ArrayList<>(); - Queue deviceLocationHistorySnapshotsQueue = new LinkedList<>(deviceLocationHistorySnapshots); - - while (deviceLocationHistorySnapshotsQueue.size() > 0) { + while (!deviceLocationHistorySnapshotsQueue.isEmpty()) { List snapshots = new ArrayList<>(); // Make a copy of remaining snapshots - List cachedSnapshots = new ArrayList<>(deviceLocationHistorySnapshotsQueue); + List cachedSnapshots = new ArrayList<>( + deviceLocationHistorySnapshotsQueue); for (int i = 0; i < cachedSnapshots.size(); i++) { DeviceLocationHistorySnapshot currentSnapshot = deviceLocationHistorySnapshotsQueue.poll(); snapshots.add(currentSnapshot); - if (deviceLocationHistorySnapshotsQueue.size() > 0) { + if (!deviceLocationHistorySnapshotsQueue.isEmpty()) { DeviceLocationHistorySnapshot nextSnapshot = deviceLocationHistorySnapshotsQueue.peek(); - if (nextSnapshot.getUpdatedTime().getTime() - currentSnapshot.getUpdatedTime().getTime() > operationFrequency) { + if (nextSnapshot.getUpdatedTime().getTime() - currentSnapshot.getUpdatedTime().getTime() + > operationFrequency) { break; } } } locationHistorySnapshotList.add(snapshots); } + DeviceLocationHistory deviceLocationHistory = new DeviceLocationHistory(); deviceLocationHistory.setLocationHistorySnapshots(locationHistorySnapshotList); - - + return Response.status(Response.Status.OK).entity(deviceLocationHistory).build(); } catch (DeviceManagementException e) { - errorMessage = "Error occurred while fetching the device information."; - log.error(errorMessage, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()).build(); + String msg = "Error occurred while fetching the device information."; + log.error(msg, e); + return Response.serverError().entity(new ErrorResponse.ErrorResponseBuilder() + .setCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).setMessage(msg).build()).build(); } catch (DeviceAccessAuthorizationException e) { - errorMessage = "Error occurred while checking the device authorization."; - log.error(errorMessage, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()).build(); - } catch (InputValidationException e){ - errorMessage = "Invalid device Id or device type"; - log.error(errorMessage, e); - return Response.status(Response.Status.BAD_REQUEST).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage)).build(); + String msg = "Error occurred while checking the device authorization."; + log.error(msg, e); + return Response.serverError().entity(new ErrorResponse.ErrorResponseBuilder() + .setCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).setMessage(msg).build()).build(); + } catch (InputValidationException e) { + String msg = "Invalid device Id or device type"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.ErrorResponseBuilder() + .setCode(Response.Status.BAD_REQUEST.getStatusCode()).setMessage(msg)).build(); } - return Response.status(Response.Status.OK).entity(deviceLocationHistory).build(); } @GET 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.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 825b684e0c..a23346ac39 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1830,40 +1830,45 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override - public List getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, long to) - throws DeviceManagementDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; + public List getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, + long to) throws DeviceManagementDAOException { List deviceLocationHistories = new ArrayList<>(); + String sql = "SELECT " + + "DEVICE_ID, " + + "TENANT_ID, " + + "DEVICE_ID_NAME, " + + "DEVICE_TYPE_NAME, " + + "LATITUDE, " + + "LONGITUDE, " + + "SPEED, " + + "HEADING, " + + "TIMESTAMP, " + + "GEO_HASH, " + + "DEVICE_OWNER, " + + "DEVICE_ALTITUDE, " + + "DISTANCE " + + "FROM DM_DEVICE_HISTORY_LAST_SEVEN_DAYS " + + "WHERE " + + "DEVICE_ID_NAME = ? AND " + + "DEVICE_TYPE_NAME = ? AND " + + "TIMESTAMP BETWEEN ? AND ?"; try { - conn = this.getConnection(); - - String sql = - "SELECT DEVICE_ID, TENANT_ID, DEVICE_ID_NAME, DEVICE_TYPE_NAME, LATITUDE, LONGITUDE, SPEED, " + - "HEADING, TIMESTAMP, GEO_HASH, DEVICE_OWNER, DEVICE_ALTITUDE, DISTANCE " + - "FROM DM_DEVICE_HISTORY_LAST_SEVEN_DAYS " + - "WHERE DEVICE_ID_NAME = ? " + - "AND DEVICE_TYPE_NAME = ? " + - "AND TIMESTAMP >= ? " + - "AND TIMESTAMP <= ?"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getId()); - stmt.setString(2, deviceIdentifier.getType()); - stmt.setLong(3, from); - stmt.setLong(4, to); - rs = stmt.executeQuery(); - while (rs.next()) { - deviceLocationHistories.add(DeviceManagementDAOUtil.loadDeviceLocation(rs)); + Connection conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, deviceIdentifier.getId()); + stmt.setString(2, deviceIdentifier.getType()); + stmt.setLong(3, from); + stmt.setLong(4, to); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + deviceLocationHistories.add(DeviceManagementDAOUtil.loadDeviceLocation(rs)); + } + } } } catch (SQLException e) { - String errMessage = "Error occurred while obtaining the DB connection to get device location information"; - log.error(errMessage, e); - throw new DeviceManagementDAOException(errMessage, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while obtaining the DB connection to get device location information"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } return deviceLocationHistories; } 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 5e28129390..f313477937 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 @@ -3051,26 +3051,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, long to) - throws DeviceManagementException { - + public List getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, + long to) throws DeviceManagementException { if (log.isDebugEnabled()) { log.debug("Get device location information"); } - List deviceLocationHistory; - String errMessage; - try { DeviceManagementDAOFactory.openConnection(); deviceLocationHistory = deviceDAO.getDeviceLocationInfo(deviceIdentifier, from, to); - } catch (DeviceManagementDAOException e) { - errMessage = "Error occurred in getDeviceLocationInfo"; + String errMessage = "Error occurred in getDeviceLocationInfo"; log.error(errMessage, e); throw new DeviceManagementException(errMessage, e); } catch (SQLException e) { - errMessage = "Error occurred while opening a connection to the data source"; + String errMessage = "Error occurred while opening a connection to the data source"; log.error(errMessage, e); throw new DeviceManagementException(errMessage, e); } finally { diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index e9c88e3321..e201cb9635 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -148,6 +148,8 @@ perm:user:permission-view perm:ios:view-configuration perm:ios:manage-configuration + perm:ios:dep-view + perm:ios:dep-add perm:windows:view-configuration perm:windows:manage-configuration perm:android:lock-devices