Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ApplicationMappingDAO {

int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException;

List<Integer> addApplicationMappings(int deviceId, List<Integer> applicationIds, int tenantId)
void addApplicationMappings(int deviceId, List<Integer> applicationIds, int tenantId)
throws DeviceManagementDAOException;

void removeApplicationMapping(int deviceId, List<Integer> appIdList, int tenantId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int addDevice(int typeId, Device device, int tenantId) throws DeviceManag
conn = this.getConnection();
String sql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " +
"VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setString(1, device.getDescription());
stmt.setString(2, device.getName());
stmt.setInt(3, typeId);
Expand Down Expand Up @@ -77,7 +77,7 @@ public boolean updateDevice(int typeId, Device device, int tenantId) throws Devi
conn = this.getConnection();
String sql = "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " +
"DEVICE_TYPE_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setString(1, device.getDescription());
stmt.setString(2, device.getName());
stmt.setString(3, device.getDeviceIdentifier());
Expand Down Expand Up @@ -433,7 +433,7 @@ public int addEnrollment(Device device, int tenantId) throws DeviceManagementDAO
conn = this.getConnection();
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setInt(1, device.getId());
stmt.setString(2, device.getEnrolmentInfo().getOwner());
stmt.setString(3, device.getEnrolmentInfo().getOwnership().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public List<Integer> addApplications(List<Application> applications,
conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES,APP_IDENTIFIER) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)", new String[] {"id"});


for (Application application : applications) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public List<Integer> removeApplications(List<Application> apps, int tenantId) th
conn = this.getConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?",
Statement.RETURN_GENERATED_KEYS);
new String[] {"id"});

for (Application app : apps) {
stmt.setString(1, app.getApplicationIdentifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public int addApplicationMapping(int deviceId, int applicationId,
conn = this.getConnection();
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
"TENANT_ID) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setInt(1, deviceId);
stmt.setInt(2, applicationId);
stmt.setInt(3, tenantId);
Expand All @@ -68,17 +68,17 @@ public int addApplicationMapping(int deviceId, int applicationId,
}

@Override
public List<Integer> addApplicationMappings(int deviceId, List<Integer> applicationIds,
public void addApplicationMappings(int deviceId, List<Integer> applicationIds,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Integer> mappingIds = new ArrayList<>();

try {
conn = this.getConnection();
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
"TENANT_ID) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql);

for (int applicationId : applicationIds) {
stmt.setInt(1, deviceId);
Expand All @@ -88,11 +88,11 @@ public List<Integer> addApplicationMappings(int deviceId, List<Integer> applicat
}
stmt.executeBatch();

rs = stmt.getGeneratedKeys();
while (rs.next()) {
mappingIds.add(rs.getInt(1));
}
return mappingIds;
// rs = stmt.getGeneratedKeys();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 77c4c34

// while (rs.next()) {
// mappingIds.add(rs.getInt(1));
// }
// return mappingIds;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
} finally {
Expand All @@ -109,7 +109,7 @@ public void removeApplicationMapping(int deviceId, List<Integer> appIdList,
conn = this.getConnection();
String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
"APPLICATION_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql);

for (Integer appId : appIdList) {
stmt.setInt(1, deviceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public int addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
conn = this.getConnection();
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " +
"DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setInt(1, deviceId);
stmt.setString(2, enrolmentInfo.getOwner());
stmt.setString(3, enrolmentInfo.getOwnership().toString());
Expand Down Expand Up @@ -76,7 +76,7 @@ public int updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " +
"DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?" +
" AND ID = ?";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setString(1, enrolmentInfo.getOwnership().toString());
stmt.setString(2, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment()));
Expand Down Expand Up @@ -109,7 +109,7 @@ public int updateEnrollment(EnrolmentInfo enrolmentInfo) throws DeviceManagement
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " +
"DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ?";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setString(1, enrolmentInfo.getOwnership().toString());
stmt.setString(2, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment()));
Expand Down Expand Up @@ -140,7 +140,7 @@ public int removeEnrollment(int deviceId, String currentOwner,
try {
conn = this.getConnection();
String sql = "DELETE DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public int addOperation(Operation operation) throws OperationManagementDAOExcept
Connection connection = OperationManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE) " +
"VALUES (?, ?, ?, ?)";
stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt = connection.prepareStatement(sql, new String[] {"id"});
stmt.setString(1, operation.getType().toString());
stmt.setTimestamp(2, new Timestamp(new Date().getTime()));
stmt.setTimestamp(3, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId;
PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant we use try with resource here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #1275

operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
Expand All @@ -48,12 +50,33 @@ public int addOperation(Operation operation) throws OperationManagementDAOExcept
Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)");

bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(operation);

stmt.setInt(1, operationId);
stmt.setObject(2, policyOperation);
stmt.setBytes(2, bao.toByteArray());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding policy operation", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing policy operation object", e);
} finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
OperationManagementDAOUtil.cleanupResources(stmt);
}
return operationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,46 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {

public int addOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;

int operationId;
try {
operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
operation.setId(operationId);
operation.setEnabled(true);
ProfileOperation profileOp = (ProfileOperation) operation;
//ProfileOperation profileOp = (ProfileOperation) operation;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented line. And, can we change this to Java try with resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #1275

Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)");

bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(operation);

stmt.setInt(1, operationId);
stmt.setObject(2, profileOp);
stmt.setBytes(2, bao.toByteArray());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding profile operation", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
OperationManagementDAOUtil.cleanupResources(stmt);
}
return operationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface MonitoringDAO {

void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException;

void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures)
throws MonitoringDAOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, in
conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " +
"TENANT_ID) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, new String[] {"id"});

for (ProfileFeature feature : features) {
stmt.setInt(1, profileId);
Expand Down Expand Up @@ -113,13 +113,9 @@ public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features,
String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? AND FEATURE_CODE = ? AND" +
" TENANT_ID = ?";

stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query);
for (ProfileFeature feature : features) {
if (conn.getMetaData().getDriverName().contains("H2")) {
stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent()));
} else {
stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent()));
}
stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent()));
stmt.setInt(2, profileId);
stmt.setString(3, feature.getFeatureCode());
stmt.setInt(4, tenantId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) t
}

@Override
public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
public void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
Expand All @@ -215,7 +215,7 @@ public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " +
"TENANT_ID) VALUES (?, ?, ?, ?) ";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query);
for (ComplianceFeature feature : complianceFeatures) {
stmt.setInt(1, policyComplianceStatusId);
stmt.setString(2, feature.getFeatureCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public Criterion addCriterion(Criterion criteria) throws PolicyManagerDAOExcepti
try {
conn = this.getConnection();
String query = "INSERT INTO DM_CRITERIA (TENANT_ID, NAME) VALUES (?, ?)";
stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, new String[] {"id"});
stmt.setInt(1, tenantId);
stmt.setString(2, criteria.getName());
stmt.executeUpdate();
Expand Down Expand Up @@ -622,7 +622,7 @@ public Policy addPolicyCriteria(Policy policy) throws PolicyManagerDAOException
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_CRITERIA (CRITERIA_ID, POLICY_ID) VALUES (?, ?)";
stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, new String[] {"id"});

List<PolicyCriterion> criteria = policy.getPolicyCriterias();
for (PolicyCriterion criterion : criteria) {
Expand Down Expand Up @@ -1348,7 +1348,7 @@ private Policy persistPolicy(Policy policy) throws PolicyManagerDAOException {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," +
"UPDATED, ACTIVE, DESCRIPTION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, new String[] {"id"});

stmt.setString(1, policy.getPolicyName());
stmt.setInt(2, policy.getProfile().getProfileId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Profile addProfile(Profile profile) throws ProfileManagerDAOException {
conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE " +
"(PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt = conn.prepareStatement(query, new String[] {"id"});

stmt.setString(1, profile.getProfileName());
stmt.setInt(2, tenantId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public List<ComplianceFeature> checkPolicyCompliance(DeviceIdentifier deviceIden
if (log.isDebugEnabled()) {
log.debug("Compliance status primary key " + complianceData.getId());
}
monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(),
monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(),
complianceFeatures);

PolicyManagementDAOFactory.commitTransaction();
Expand Down
Loading