Merge branch 'corrective-policy' of https://gitlab.com/entgra/carbon-device-mgt into corrective-policy

corrective-policy
Pahansith 4 years ago
commit 08a96a2345

@ -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",

@ -294,14 +294,9 @@ class ReleaseView extends React.Component {
<Divider />
<ReviewContainer uuid={release.uuid} />
</TabPane>
<Authorized
permission="/permission/admin/app-mgt/store/admin/subscription/view"
yes={
<TabPane tab="Subscription Details" key="2">
<SubscriptionDetails uuid={release.uuid} />
</TabPane>
}
/>
</Tabs>
</div>
</div>

@ -96,10 +96,10 @@ class Login extends React.Component {
const config = this.props.context;
return (
<div className="login">
<div className="background"></div>
<div className="background" />
<div className="content">
<Row>
<Col xs={3} sm={3} md={10}></Col>
<Col xs={3} sm={3} md={10} />
<Col xs={18} sm={18} md={4}>
<Row style={{ marginBottom: 20 }}>
<Col
@ -174,7 +174,7 @@ class Login extends React.Component {
</Col>
</Row>
<Row>
<Col span={4} offset={10}></Col>
<Col span={4} offset={10} />
</Row>
</div>
</div>

@ -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

@ -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,25 +655,37 @@ 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<GeofenceData> geofence = geoService.getGeofence(request);
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));
}
return getResponse(geoService.getGeoFences());
} catch (GeoLocationBasedServiceException e) {
String msg = "Failed to retrieve geofence data";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
private Response getResponse(List<GeofenceData> fencesList) {
List<GeofenceWrapper> geofenceList = new ArrayList<>();
for (GeofenceData geofenceData : geofence) {
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();
} catch (GeoLocationBasedServiceException e) {
String msg = "Failed to retrieve geofence data";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}

@ -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 {

@ -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<GeofenceData> getGeofence(PaginationRequest request) throws GeoLocationBasedServiceException;
List<GeofenceData> 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<GeofenceData> 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<GeofenceData> 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;
}

@ -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<Activity> activityList;
private List<Integer> largeResponseIDs;
public List<Activity> getActivityList() {
return activityList;
}
public void setActivityList(List<Activity> activityList) {
this.activityList = activityList;
}
public List<Integer> getLargeResponseIDs() {
return largeResponseIDs;
}
public void setLargeResponseIDs(List<Integer> largeResponseIDs) {
this.largeResponseIDs = largeResponseIDs;
}
}

@ -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<Integer, ActivityStatus> activityStatusMap;
public Activity getActivity() {
return activity;
}
public void setActivity(Activity activity) {
this.activity = activity;
}
public Map<Integer, ActivityStatus> getActivityStatusMap() {
return activityStatusMap;
}
public void setActivityStatusMap(Map<Integer, ActivityStatus> activityStatusMap) {
this.activityStatusMap = activityStatusMap;
}
}

@ -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 = "/";

@ -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;
}
}

@ -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);
}

@ -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<GeoCacheKey, GeofenceData> 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<GeoCacheKey, GeofenceData> 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<GeoCacheKey, GeofenceData> 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<GeoCacheKey, GeofenceData> 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;
}
}

@ -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;

@ -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;
}
}

@ -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<GeofenceData> getGeofencesOfTenant(PaginationRequest request, int tenantId)
List<GeofenceData> 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<GeofenceData> 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<GeofenceData> getGeoFencesOfTenant(int tenantId)
throws DeviceManagementDAOException;
/**

@ -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<GeofenceData> getGeofencesOfTenant(PaginationRequest request, int tenantId)
public List<GeofenceData> getGeoFencesOfTenant(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
boolean isNameProvided = false;
List<GeofenceData> 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<GeofenceData> getGeoFencesOfTenant(String fenceName, int tenantId)
throws DeviceManagementDAOException {
return null;
}
@Override
public List<GeofenceData> getGeoFencesOfTenant(int tenantId)
throws DeviceManagementDAOException {
try {
List<GeofenceData> 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);
}

@ -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<GeofenceData> getGeofence(PaginationRequest request) throws GeoLocationBasedServiceException {
public List<GeofenceData> 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<GeofenceData> 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<GeofenceData> 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<GeofenceData> 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);

@ -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,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
for (Map.Entry<String, List<OperationResponse>> deviceOpRes : deviceOpResponseMap.entrySet()) {
for (ActivityStatus status : activityStatuses) {
if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) {
if (status.getResponses() == null) {
status.setResponses(new ArrayList<>());
}
status.getResponses().addAll(deviceOpRes.getValue());
}
}
@ -942,349 +946,117 @@ public class GenericOperationDAOImpl implements OperationDAO {
@Override
public List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> 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<Integer> largeResponseIDs = new ArrayList<>();
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> 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<OperationResponse> 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<OperationResponse> 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<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> activities = activityHolder.getActivityList();
if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs);
}
return activities;
}
}
} 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<Activity> getActivitiesUpdatedAfter(long timestamp, int limit,
int offset) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> largeResponseIDs = new ArrayList<>();
public List<Activity> 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<ActivityStatus> 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<OperationResponse> 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<OperationResponse> 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<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> activities = activityHolder.getActivityList();
if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs);
}
return activities;
}
}
} 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 +1065,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);
@ -1317,7 +1089,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 " +
@ -1490,11 +1262,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 +1459,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 {

@ -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,17 +201,11 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl {
}
@Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit,
int offset)
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " +
" eom.ENROLMENT_ID," +
" eom.CREATED_TIMESTAMP," +
@ -229,142 +224,46 @@ 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 = ? " +
"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<Integer> largeResponseIDs = new ArrayList<>();
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> statusList = new ArrayList<>();
activityStatus = new ActivityStatus();
operationId = rs.getInt("OPERATION_ID");
enrolmentId = rs.getInt("ENROLMENT_ID");
"ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
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"));
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, offset);
stmt.setInt(2, limit);
stmt.setLong(3, timestamp);
stmt.setInt(4, tenantId);
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<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
try (ResultSet rs = stmt.executeQuery()) {
ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> activities = activityHolder.getActivityList();
if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs);
}
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<OperationResponse> 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));
}
}
}
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<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> 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 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.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<ActivityStatus> 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<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
"ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
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);
List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> activities = activityHolder.getActivityList();
if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs);
}
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<OperationResponse> 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));
}
}
}
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;
}
}

@ -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<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException {
@ -185,192 +187,56 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
@Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> 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<ActivityStatus> 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<OperationResponse> 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<OperationResponse> 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<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> 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;
}
}

@ -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<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
@ -162,192 +162,59 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
}
@Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> largeResponseIDs = new ArrayList<>();
public List<Activity> 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<ActivityStatus> 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<OperationResponse> 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<OperationResponse> 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");
}
" 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";
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<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> 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

@ -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<Integer, ActivityMapper> activityInfo = new TreeMap<>();
List<Integer> largeResponseIDs = new ArrayList<>();
List<Activity> 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<OperationResponse> 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<ActivityStatus> 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<OperationResponse> 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<Integer, ActivityStatus> 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<ActivityStatus> 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);
}
}
}

@ -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,7 +614,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
}
try {
DeviceCacheKey deviceCacheKey;
for (Device device : existingDevices) {
if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) {
@ -633,11 +633,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (deviceIdentifierMap.containsKey(device.getType())) {
deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier());
} else {
deviceIdentifierMap.put(device.getType(), Collections.singletonList(device.getDeviceIdentifier()));
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'");
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);
@ -651,6 +652,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
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);

@ -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.<GeoCacheKey, GeofenceData>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.<GeoCacheKey, GeofenceData>getCache(DeviceManagementConstants.GEOFENCE_CACHE)).
setCapacity(geoCacheCapacity);
}
} else {
manager.<GeoCacheKey, GeofenceData>getCache(DeviceManagementConstants.GEOFENCE_CACHE);
}
} else {
if (geoCacheExpiry > 0) {
Caching.getCacheManager().
<GeoCacheKey, GeofenceData>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.<GeoCacheKey, GeofenceData>getCache(DeviceManagementConstants.GEOFENCE_CACHE))).
setCapacity(geoCacheCapacity);
} else {
Caching.getCacheManager().<GeoCacheKey, GeofenceData>getCache(DeviceManagementConstants.GEOFENCE_CACHE);
}
}
}
}
}
public static Cache<DeviceCacheKey, Device> getDeviceCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
CacheManager manager = getCacheManager();
@ -659,6 +700,24 @@ public final class DeviceManagerUtil {
return deviceCache;
}
public static Cache<GeoCacheKey, GeofenceData> getGeoCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
CacheManager manager = getCacheManager();
Cache<GeoCacheKey, GeofenceData> 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
*

@ -89,6 +89,14 @@
server environment-->
<Capacity>10000</Capacity>
</DeviceCacheConfiguration>
<GeoFenceCacheConfiguration>
<Enable>false</Enable>
<ExpiryTime>600</ExpiryTime>
<!--This configuration specifies the number of cache entries in device cache. default capacity is 10000 entries.
This can be configured to higher number if cache eviction happens due to large number of devices in the
server environment-->
<Capacity>10000</Capacity>
</GeoFenceCacheConfiguration>
<CertificateCacheConfiguration>
<Enable>false</Enable>
<ExpiryTime>86400</ExpiryTime>

@ -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());

@ -89,6 +89,14 @@
server environment-->
<Capacity>10000</Capacity>
</DeviceCacheConfiguration>
<GeoFenceCacheConfiguration>
<Enable>true</Enable>
<ExpiryTime>600</ExpiryTime>
<!--This configuration specifies the number of cache entries in device cache. default capacity is 10000 entries.
This can be configured to higher number if cache eviction happens due to large number of devices in the
server environment-->
<Capacity>10000</Capacity>
</GeoFenceCacheConfiguration>
<CertificateCacheConfiguration>
<Enable>true</Enable>
<ExpiryTime>86400</ExpiryTime>

Loading…
Cancel
Save