deviceOrg: refactor with best practices

backup
Isuri Mendis 11 months ago
parent c551825a24
commit a45f4712ba

@ -17,6 +17,7 @@
*/
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util.DeviceOrgConstants;
import io.entgra.device.mgt.core.apimgt.annotations.Scope;
import io.entgra.device.mgt.core.apimgt.annotations.Scopes;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.beans.ErrorResponse;
@ -105,7 +106,7 @@ import javax.ws.rs.core.Response;
)
public interface DeviceOrganizationMgtService {
String SCOPE = "scope";
String SCOPE = DeviceOrgConstants.SCOPE;
/**
* Adds a new device organization.

@ -49,7 +49,7 @@ import java.util.List;
public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtService {
private static final Log log = LogFactory.getLog(DeviceOrganizationMgtServiceImpl.class);
Gson gson = new Gson();
private static final Gson gson = new Gson();
@POST
@Override
@ -72,7 +72,7 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe
boolean resp = deviceOrganizationService.addDeviceOrganization(deviceOrganizationRequest);
SuccessResponse response = new SuccessResponse();
response.setSuccess(resp);
return Response.status(Response.Status.OK).entity(response).build();
return Response.status(Response.Status.CREATED).entity(response).build();
} catch (DeviceOrganizationMgtPluginException e) {
String errorMessage = "device organization failed to be created";
log.error(errorMessage);

@ -36,6 +36,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@ -46,7 +47,6 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
public static final String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
private Gson gson;
private static final String UTF_8 = "UTF-8";
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return true;
@ -65,7 +65,7 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
InputStream entityStream)
throws IOException, WebApplicationException {
InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8");
InputStreamReader reader = new InputStreamReader(entityStream, StandardCharsets.UTF_8);
try {
return getGson().fromJson(reader, type);
@ -87,7 +87,7 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
OutputStream entityStream)
throws IOException, WebApplicationException {
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8);
try {
getGson().toJson(object, type, writer);
} finally {

@ -49,7 +49,9 @@ public class ValidationInterceptor extends AbstractPhaseInterceptor<Message> {
if (validator == null) {
log.warn("Bean Validation provider could not be found, no validation will be performed");
} else {
log.debug("Validation In-Interceptor initialized successfully");
if (log.isDebugEnabled()) {
log.debug("Validation In-Interceptor initialized successfully");
}
}
}

@ -0,0 +1,6 @@
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util;
public class DeviceOrgConstants {
public static final String SCOPE = "scope";
}

@ -132,8 +132,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
stmt.setInt(1, node.getDeviceId());
try (ResultSet rs = stmt.executeQuery()) {
DeviceNode child;
DeviceOrganization organization;
while (rs.next()) {
DeviceNode child = getDeviceFromResultSet(rs);
child = getDeviceFromResultSet(rs);
node.getChildren().add(child);
hasChildren = true;
if (includeDevice
@ -143,7 +145,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
parentAdded = true; // Set the flag to true after adding the parent device.
}
DeviceOrganization organization = loadDeviceOrganization(rs);
organization = loadDeviceOrganization(rs);
organizations.add(organization);
getChildrenRecursive(
@ -244,15 +246,17 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, node.getDeviceId());
try (ResultSet rs = stmt.executeQuery()) {
DeviceNode parent;
DeviceOrganization organization;
while (rs.next()) {
DeviceNode parent = getDeviceFromResultSet(rs);
parent = getDeviceFromResultSet(rs);
node.getParents().add(parent);
if (includeDevice && !childAdded) {
parentNodes.add(node);
childAdded = true;
}
DeviceOrganization organization = loadDeviceOrganization(rs);
organization = loadDeviceOrganization(rs);
organizations.add(organization);
getParentsRecursive(
@ -298,8 +302,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
DeviceOrganization deviceOrganization;
while (rs.next()) {
DeviceOrganization deviceOrganization = loadDeviceOrganization(rs);
deviceOrganization = loadDeviceOrganization(rs);
deviceOrganizations.add(deviceOrganization);
}
}
@ -339,8 +344,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
stmt.setInt(1, request.getLimit());
stmt.setInt(2, request.getOffSet());
try (ResultSet rs = stmt.executeQuery()) {
DeviceOrganization deviceOrganization;
while (rs.next()) {
DeviceOrganization deviceOrganization = loadDeviceOrganizationWithDeviceDetails(rs);
deviceOrganization = loadDeviceOrganizationWithDeviceDetails(rs);
deviceOrganizations.add(deviceOrganization);
}
}
@ -373,8 +379,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
stmt.setInt(1, request.getLimit());
stmt.setInt(2, request.getOffSet());
try (ResultSet rs = stmt.executeQuery()) {
DeviceOrganization deviceOrganization;
while (rs.next()) {
DeviceOrganization deviceOrganization = loadDeviceOrganization(rs);
deviceOrganization = loadDeviceOrganization(rs);
deviceOrganizations.add(deviceOrganization);
}
}

Loading…
Cancel
Save