Add method comments and format code

4.x.x
Pahansith 4 years ago
parent 30f78f5ba9
commit e5dd609dc2

@ -75,7 +75,7 @@ public class GeofenceWrapper {
@ApiModelProperty(
name = "groupIds",
value = "Group ids to add geo fences",
value = "Group ids mapped with geo fences",
required = true)
private List<Integer> groupIds;

@ -40,11 +40,17 @@ 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;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.geo.service.*;
import org.wso2.carbon.device.mgt.common.geo.service.Alert;
import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException;
import org.wso2.carbon.device.mgt.common.geo.service.Event;
import org.wso2.carbon.device.mgt.common.geo.service.GeoFence;
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService;
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
@ -64,7 +70,15 @@ import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.ws.rs.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList;
@ -624,6 +638,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
}
}
/**
* Extract request event data from the payload and attach it to the DTO
* @param eventConfig request event payload
* @return generated event beans list according to the payload data
*/
private List<EventConfig> mapRequestEvent(List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventConfig) {
List<EventConfig> savingEventList = new ArrayList<>();
for (org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig event : eventConfig) {
@ -652,6 +671,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
GeofenceData geofenceData = geoService.getGeoFences(fenceId);
if (geofenceData == null) {
String msg = "No valid Geofence found for ID " + fenceId;
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
}
if (requireEventData) {
@ -661,6 +681,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
return Response.status(Response.Status.OK).entity(getMappedResponseBean(geofenceData)).build();
} catch (GeoLocationBasedServiceException e) {
String msg = "Server error occurred while retrieving Geofence for Id " + fenceId;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@ -692,6 +713,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
return geofenceWrapper;
}
/**
* Get event list to send with the response
* @param eventConfig event list retrieved
* @return list of response event beans
*/
private List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> getEventConfigBean(List<EventConfig> eventConfig) {
List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventList = new ArrayList<>();
org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig eventData;
@ -733,16 +759,16 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
if (!geoFences.isEmpty() && requireEventData) {
geoFences = geoService.attachEventObjects(geoFences);
}
return getResponse(geoFences);
return buildResponse(geoFences);
}
if (name != null && !name.isEmpty()) {
List<GeofenceData> geoFences = geoService.getGeoFences(name);
if (requireEventData) {
geoFences = geoService.attachEventObjects(geoFences);
}
return getResponse(geoFences);
return buildResponse(geoFences);
}
return getResponse(geoService.getGeoFences());
return buildResponse(geoService.getGeoFences());
} catch (GeoLocationBasedServiceException e) {
String msg = "Failed to retrieve geofence data";
log.error(msg, e);
@ -750,7 +776,12 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
}
}
private Response getResponse(List<GeofenceData> fencesList) {
/**
* Build the response payload from the data retrieved from the database
* @param fencesList retrieved geofence data to send in response
* @return HttpResponse object
*/
private Response buildResponse(List<GeofenceData> fencesList) {
List<GeofenceWrapper> geofenceList = new ArrayList<>();
for (GeofenceData geofenceData : fencesList) {
geofenceList.add(getMappedResponseBean(geofenceData));
@ -761,7 +792,6 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
return Response.status(Response.Status.OK).entity(paginationResult).build();
}
@DELETE
@Override
@Path("/geo-fence/{fenceId}")
@ -816,6 +846,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
}
}
/**
* Parse geofence data from the request payload to the GeofenceData DTO
* @param geofenceWrapper request payload data
* @return GeofenceData object built from the request data
*/
private GeofenceData mapRequestGeofenceData(GeofenceWrapper geofenceWrapper) {
GeofenceData geofenceData = new GeofenceData();
geofenceData.setFenceName(geofenceWrapper.getFenceName());

@ -684,6 +684,10 @@ public class RequestValidationUtil {
&& StringUtils.isEmpty(emailAddress);
}
/**
* Check the request payload attributes are correct for create a geofence
* @param geofenceWrapper request payload data
*/
public static void validateGeofenceData(GeofenceWrapper geofenceWrapper) {
boolean isGeoJsonExists = false;
if (geofenceWrapper.getFenceName() == null || geofenceWrapper.getFenceName().trim().isEmpty()) {
@ -721,6 +725,10 @@ public class RequestValidationUtil {
}
}
/**
* Check the request payload attributes are correct for create an event record
* @param eventConfig request payload data
*/
public static void validateEventConfigurationData(List<EventConfig> eventConfig) {
if (eventConfig == null ||eventConfig.isEmpty()) {
String msg = "Event configuration is mandatory, since should not be null or empty";

@ -476,7 +476,7 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"AND G.FENCE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, geofenceId);
return getEventConfigs(eventList, stmt);
return getEventConfigs(stmt);
}
} catch (SQLException e) {
String msg = "Error occurred while updating Geofence record with id ";
@ -521,7 +521,14 @@ public class GeofenceDAOImpl implements GeofenceDAO {
}
}
private List<EventConfig> getEventConfigs(List<EventConfig> eventList, PreparedStatement stmt) throws SQLException {
/**
* Retrieve the geofence event extracted from the DB
* @param stmt prepared statement to retrieve data from the DB
* @return Retrieved Event list from the DB
* @throws SQLException for the errors occur while accessing the DB
*/
private List<EventConfig> getEventConfigs(PreparedStatement stmt) throws SQLException {
List<EventConfig> eventList = new ArrayList<>();
ResultSet resultSet = stmt.executeQuery();
EventConfig event;
while (resultSet.next()) {

@ -131,7 +131,6 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
eventConfigDAO.updateEventRecords(eventsToUpdate);
}
if (!groupIdsToDelete.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Deleting event group mapping records of groups");
@ -160,6 +159,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed to start/open transaction to store device event configurations";
log.error(msg, e);
throw new EventConfigurationException(msg, e);
} catch (EventManagementDAOException e) {
String msg = "Error occurred while saving event records";
@ -227,6 +227,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed to start/open transaction to delete device event configurations";
log.error(msg, e);
throw new EventConfigurationException(msg, e);
} catch (EventManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();

@ -1330,8 +1330,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
* @param tenantId id of the fence owning tenant
*/
private void createEventRevokeTask(GeofenceData geofenceData, int tenantId) {
GeoFenceEventOperationManager eventManager = new GeoFenceEventOperationManager(OperationMgtConstants.OperationCodes.EVENT_REVOKE,
tenantId, values -> createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, geofenceData, tenantId));
GeoFenceEventOperationManager eventManager =
new GeoFenceEventOperationManager(OperationMgtConstants.OperationCodes.EVENT_REVOKE, tenantId,
values -> createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, geofenceData, tenantId));
ScheduledExecutorService eventOperationExecutor = Executors.newSingleThreadScheduledExecutor();
eventOperationExecutor.schedule(eventManager
.getEventOperationExecutor(geofenceData), 10, TimeUnit.SECONDS);

@ -607,6 +607,10 @@ public final class DeviceManagerUtil {
return eventsPublisherService;
}
/**
* Retrieve EventConfigurationProviderService osgi service component
* @return {@link EventConfigurationProviderService} service component
*/
public static EventConfigurationProviderService getEventConfigService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
EventConfigurationProviderService eventConfigService =
@ -657,6 +661,9 @@ public final class DeviceManagerUtil {
}
}
/**
* Enable Geofence caching according to the configurations proviced by cdm-config.xml
*/
public static void initializeGeofenceCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
int geoCacheExpiry = config.getGeoFenceCacheConfiguration().getExpiryTime();
@ -713,6 +720,10 @@ public final class DeviceManagerUtil {
return deviceCache;
}
/**
* Get geofence cache object
* @return {@link Cache<GeoCacheKey, GeofenceData>}
*/
public static Cache<GeoCacheKey, GeofenceData> getGeoCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
CacheManager manager = getCacheManager();

@ -1451,6 +1451,12 @@ public class PolicyManagerImpl implements PolicyManager {
Collections.sort(policyList);
}
/**
* Get the corrective action list of a specific policy
* @param allCorrectiveActions stored corrective actions of all policies
* @param policyId Id of the policy to get corrective action
* @return
*/
private List<CorrectiveAction> getSingleCorrectiveAction
(List<CorrectiveAction> allCorrectiveActions, int policyId) {
List<CorrectiveAction> correctiveActionsOfPolicy = new ArrayList<>();
@ -1464,6 +1470,13 @@ public class PolicyManagerImpl implements PolicyManager {
return correctiveActionsOfPolicy;
}
/**
* Set the corrective actions of a specific policy against with the policy profile.
* This method is using with the new implementation of corrective policies which is able to apply multiple corrective
* policies based on a feature code of a policy
* @param allCorrectiveActions corrective action list retrieved from the DB
* @param profile profile of the selected policy
*/
private void setMultipleCorrectiveActions(List<CorrectiveAction> allCorrectiveActions,
Profile profile) {
for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) {

@ -142,6 +142,12 @@ public class PolicyManagerUtil {
return buff.toString();
}
/**
* Attach policy specific data which has been not retrieved
* @param policy
* @return
* @throws PolicyTransformException
*/
public static Operation transformPolicy(Policy policy) throws PolicyTransformException {
List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList();
PolicyOperation policyOperation = new PolicyOperation();

Loading…
Cancel
Save