Merge pull request #31 from milanperera/master

Fixed spelling mistakes in policy component and code cleanup
merge-requests/7/head
Prabath Abeysekara 9 years ago
commit b644bb43b8

@ -42,7 +42,15 @@ public interface DeviceDAO {
*/ */
int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to update a given device.
*
* @param typeId device type id.
* @param device device object.
* @param tenantId tenant id.
* @return returns the id of updated device.
* @throws DeviceManagementDAOException
*/
boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException;
/** /**
@ -104,6 +112,7 @@ public interface DeviceDAO {
/** /**
* This method is used to retrieve devices of a given user. * This method is used to retrieve devices of a given user.
*
* @param username user name. * @param username user name.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns list of devices. * @return returns list of devices.
@ -122,6 +131,7 @@ public interface DeviceDAO {
/** /**
* This method is used to retrieve devices of a given device name. * This method is used to retrieve devices of a given device name.
*
* @param deviceName device name. * @param deviceName device name.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns list of devices. * @return returns list of devices.
@ -197,5 +207,17 @@ public interface DeviceDAO {
*/ */
int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status,
int tenantId) throws DeviceManagementDAOException; int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the enrollment info of a given list of devices and status.
*
* @param deviceIds A list of device identifiers.
* @param status enrollment status.
* @param tenantId tenant id.
* @return returns a list of enrolment info objects.
* @throws DeviceManagementDAOException
*/
List<EnrolmentInfo> getEnrolmentsByStatus(List<DeviceIdentifier> deviceIds, Status status,
int tenantId) throws DeviceManagementDAOException;
} }

@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -516,6 +517,51 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} }
public List<EnrolmentInfo> getEnrolmentsByStatus(List<DeviceIdentifier> deviceIds, Status status,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<EnrolmentInfo> enrolments = new ArrayList<>();
try {
conn = this.getConnection();
StringBuilder sql = new StringBuilder();
sql.append("SELECT e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, " +
"e.STATUS FROM DM_ENROLMENT e WHERE e.DEVICE_ID IN (SELECT d.ID FROM DM_DEVICE d " +
"WHERE d.DEVICE_IDENTIFICATION IN (");
// adding arguments to the sql query
Iterator iterator = deviceIds.iterator();
while (iterator.hasNext()) {
iterator.next();
sql.append(" ?");
if (iterator.hasNext()) {
sql.append(",");
}
}
sql.append(") AND d.TENANT_ID = ?) AND e.STATUS = ? AND e.TENANT_ID = ?");
stmt = conn.prepareStatement(sql.toString());
int index = 1;
for (DeviceIdentifier id : deviceIds) {
stmt.setString(index++, id.getId());
}
stmt.setInt(index++, tenantId);
stmt.setString(index++, status.toString());
stmt.setInt(index, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolments.add(this.loadEnrolment(rs));
}
return enrolments;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"ids of devices", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private Device loadDevice(ResultSet rs) throws SQLException { private Device loadDevice(ResultSet rs) throws SQLException {
Device device = new Device(); Device device = new Device();
device.setId(rs.getInt("DEVICE_ID")); device.setId(rs.getInt("DEVICE_ID"));

@ -80,19 +80,11 @@ public class OperationManagerImpl implements OperationManager {
} }
} }
try { try {
OperationManagementDAOFactory.beginTransaction();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto =
OperationDAOUtil.convertOperation(operation);
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
int enrolmentId;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
for (DeviceIdentifier deviceId : deviceIds) { List<EnrolmentInfo> enrolments;
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); enrolments = deviceDAO.getEnrolmentsByStatus(deviceIds, EnrolmentInfo.Status.ACTIVE, tenantId);
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection the data " + throw new OperationManagementException("Error occurred while opening a connection the data " +
"source", e); "source", e);
@ -100,14 +92,14 @@ public class OperationManagerImpl implements OperationManager {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
if (enrolmentId < 0) { OperationManagementDAOFactory.beginTransaction();
String errorMsg = "The operation not added for device.The device not found for " + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto =
"device Identifier type -'" + deviceId.getType() + "' and device Id '" + OperationDAOUtil.convertOperation(operation);
deviceId.getId();
log.error(errorMsg); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
} else {
operationMappingDAO.addOperationMapping(operationId, enrolmentId); for (EnrolmentInfo enrolmentInfo : enrolments) {
} operationMappingDAO.addOperationMapping(operationId, enrolmentInfo.getId());
} }
OperationManagementDAOFactory.commitTransaction(); OperationManagementDAOFactory.commitTransaction();
return operationId; return operationId;

@ -246,6 +246,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
Device device = deviceDAO.getDevice(deviceId, tenantId); Device device = deviceDAO.getDevice(deviceId, tenantId);
if (device == null) {
if (log.isDebugEnabled()) {
log.debug("Device not found for id '" + deviceId.getId() + "'");
}
throw new DeviceManagementException("Device not found");
}
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());

@ -45,7 +45,7 @@ public class ComplianceFeature {
this.featureCode = featureCode; this.featureCode = featureCode;
} }
public boolean isCompliance() { public boolean isCompliant() {
return compliance; return compliance;
} }

@ -219,7 +219,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
for (ComplianceFeature feature : complianceFeatures) { for (ComplianceFeature feature : complianceFeatures) {
stmt.setInt(1, policyComplianceStatusId); stmt.setInt(1, policyComplianceStatusId);
stmt.setString(2, feature.getFeatureCode()); stmt.setString(2, feature.getFeatureCode());
if (feature.isCompliance()) { if (feature.isCompliant()) {
stmt.setInt(3, 1); stmt.setInt(3, 1);
} else { } else {
stmt.setInt(3, 0); stmt.setInt(3, 0);

@ -133,8 +133,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
triggerInfo.setIntervalMillis(0); triggerInfo.setRepeatCount(0);
triggerInfo.setRepeatCount(1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));

@ -107,12 +107,12 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest {
log.debug("Re-enforcing policy started...!"); log.debug("Re-enforcing policy started...!");
int sixe = policies.size(); int size = policies.size();
sortPolicies(policies); sortPolicies(policies);
int x = 0; int x = 0;
for (Policy policy : policies) { for (Policy policy : policies) {
policy.setPriorityId(sixe - x); policy.setPriorityId(size - x);
x++; x++;
} }

Loading…
Cancel
Save