Implement Device Tagging Feature #492
Merged
tcdlpds
merged 5 commits from Gimhan-minion/device-mgt-core:master
into master
3 months ago
Loading…
Reference in new issue
There is no content yet.
Delete Branch 'Gimhan-minion/device-mgt-core:master'
Deleting a branch is permanent. It CANNOT be undone. Continue?
WIP: Implement Device Tagging Featureto Implement Device Tagging Feature 3 months agoproduces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting the List of Tags",
notes = "",
For this notes attribute, better to add an explanation about what the specific API endpoint does
@Override
public Response updateTag(@QueryParam("tagId") Integer tagId, @QueryParam("tagName") String tagName, TagInfo tagInfo) {
RequestValidationUtil.validateTagDetails(tagId, tagName, tagInfo);
try {
Don't we validate tagId,tagName and tagInfo here?
Here the update is possible by providing either the tagId or tagName. Therefore, for rechecking which type of parameter is provided we need to have a seperate null check here.
return Response.status(Response.Status.OK).entity(devices).build();
}
if (tags != null && !tags.isEmpty()) {
Can you explain the logic that you are going to achieve here?
tag = DeviceMgtAPIUtils.getTagManagementService().getTagByName(tagName);
}
if (tag == null) {
String msg = "Tag not found.";
Improve the message as you have done in the catch block.
public Response addDeviceTagMapping(TagMappingInfo tagMappingInfo) {
RequestValidationUtil.validateTagMappingDetails(tagMappingInfo);
try {
TagMappingDTO tagMappingDTO = new TagMappingDTO(tagMappingInfo.getDeviceIdentifiers(),
Better to move this part into service method.
}
}
public static void validateTagDetails(Integer tagId, String tagName, TagInfo tagInfo) {
Why didn't we use StringUtils to validate the tag name?
}
public static void validateTagDetails(Integer tagId, String tagName, TagInfo tagInfo) {
if (tagId == null && tagName == null) {
Shouldn't we handle negatives and 0?
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(msg).build());
}
if (tagInfo == null) {
throw new InputValidationException(
Log the error and throw.
public static void validateTagListDetails(List<TagInfo> tagInfo) {
if (tagInfo == null) {
throw new InputValidationException(
Log the error and throw.
}
}
public static void validateTagMappingDetails(TagMappingInfo tagMappingInfo) {
Add Java Doc comment, please check all the places and add.
public static void validateTagMappingDetails(TagMappingInfo tagMappingInfo) {
if (tagMappingInfo == null) {
throw new InputValidationException(
Log the error and throw.
new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Request body is empty").build());
} else if (tagMappingInfo.getDeviceIdentifiers() == null || tagMappingInfo.getDeviceType() == null
|| tagMappingInfo.getTags() == null) {
throw new InputValidationException(
Log the error and throw.
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
Use try with resources.
int[] updateCounts = preparedStatement.executeBatch();
for (int count : updateCounts) {
if (count == PreparedStatement.EXECUTE_FAILED) {
throw new TagManagementDAOException("Error occurred while adding tags, adding some tags failed.");
Log and throw the error
import io.entgra.device.mgt.core.device.mgt.common.exceptions.ConflictException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.*;
Don't use wild card imports
}
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.beginTransaction();
In the try block this should be the first line of a transaction handling logic.
}
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection();
Don't we need to start a transaction here?
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.Tag;
import io.entgra.device.mgt.core.device.mgt.core.dao.*;
import java.sql.*;
Importing specific classes individually is better.
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.authorization.GroupAccessAuthorizationServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.service.*;
Import specific classes individually
import io.entgra.device.mgt.core.device.mgt.common.exceptions.ConflictException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.*;
Import specific classes individually
This implementation does not have a way to retrieve a device with all tags it has. It only provide assigned tags along with a device, if filtered by tags.
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.BadRequestException;
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.*;
Remove * imports
} catch (TagManagementException e) {
String msg = "Error occurred while getting tags.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
As this API produces APPLICATION_JSON as result, entity should be an object which can parse to a valid json. Correct this in all other applicable places as well.
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (BadRequestException e) {
String msg = "Error occurred while adding tags. Please check the request" ;
log.error(msg, e);
No need to log, bad requests as an error. This is because, bad requests are made by clients and thus client needs to correct it from that end. It is sufficient to log validation failures as debug logs. Applicable for other places as well.
public static TagManagementProviderService getTagManagementService() {
TagManagementProviderService tagManagementService;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
tagManagementService =
This will retrieve TagManagementProviderService from OSGi registry every time the method getTagManagementService() is invoked. Since TagManagementProviderService instance is not changing over the server runtime, it is good to lazy load this and assign to a static variable for future use.
This implementation does not have a way to retrieve a device with all tags it has. It only provide assigned tags along with a device, if filtered by tags.
This implementation does not have a way to retrieve a device with all tags it has. It only provide assigned tags along with a device, if filtered by tags.
I'm assuming what mentioned above is that if we do not provide tags as a query parameter in the search filter, it will not provide the tags.
In the implementation, if you call the getDevices method without any params, it will return the list of all devices with a payload like this.
Scenario 2
If we provide a tag like 'tagss' in the query parameter, then it will filter by those.
So according to the requirement mentioned above, as I understand it, even if we provide a filter or not, it returns the attached tags within the payload.
Hoping i got this correct :)
PS: All the other requested changes except above are updated
c81b1fc0f8
toa2e9572681
3 months ago"FROM DM_DEVICE_TAG_MAPPING dtm " +
"JOIN DM_TAG t ON dtm.TAG_ID = t.ID " +
"WHERE t.NAME IN (";
for (int i = 0; i < tagList.size(); i++) {
For more readability you can use Collections.nCopies to construct an immutable list of same object, in this case the question mark is the object.
https://www.geeksforgeeks.org/collections-ncopies-java/
and use String.join to concatenate the the elements with your prefered delimter, which is the comma in this case.
https://www.geeksforgeeks.org/java-string-join-examples/
FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT(ID),
FOREIGN KEY (TAG_ID) REFERENCES DM_TAG(ID) ON DELETE CASCADE
);
-- END OF DM_DEVICE_TAG_MAPPING TABLE --
Please add these scripts as compatible for each database also in the dbscripts situated at /device-mgt-core/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm
a2e9572681
to295f1831d8
3 months agosb.append("]}\n");
return sb.toString();
}
Extra spaces
fc5716ddb4
to8d2fb5fa76
3 months agoimport io.entgra.device.mgt.core.apimgt.annotations.Scopes;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants;
import javax.ws.rs.*;
Don't use wild card imports
3a6cead8c9
tocfa40c0d2b
3 months agofa4d466798
into master 3 months agoReviewers
fa4d466798
.