Initialize device tag feature updated req changes

Gimhan Wijayawardana 3 months ago
parent 5e1bfd6d9a
commit c81b1fc0f8

@ -107,7 +107,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting the List of Tags",
notes = "",
notes = "This endpoint is used to retrieve all tags",
tags = "Tag Management",
extensions = {
@Extension(properties = {
@ -153,7 +153,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Adding a Tag",
notes = "",
notes = "This endpoint is used to add new tags",
tags = "Tag Management",
extensions = {
@Extension(properties = {
@ -212,7 +212,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Updating a Tag",
notes = "",
notes = "This endpoint is used to update a specific tag",
tags = "Tag Management",
extensions = {
@Extension(properties = {
@ -260,7 +260,7 @@ public interface TagManagementService {
@ApiOperation(
httpMethod = "DELETE",
value = "Deleting a Tag",
notes = "",
notes = "This endpoint is used to delete a specific tag",
tags = "Tag Management",
extensions = {
@Extension(properties = {
@ -293,7 +293,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting a Tag by ID",
notes = "",
notes = "This endpoint is used to retrieve tag by id",
tags = "Tag Management",
extensions = {
@Extension(properties = {
@ -332,7 +332,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Adding a Device-Tag Mapping",
notes = "",
notes = "This endpoint is used to map devices with tags",
tags = "Device-Tag Management",
extensions = {
@Extension(properties = {
@ -372,7 +372,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "DELETE",
value = "Deleting a Device-Tag Mapping",
notes = "",
notes = "This endpoint is used to remove tag mappings from devices",
tags = "Device-Tag Management",
extensions = {
@Extension(properties = {

@ -228,17 +228,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
if (tags != null && !tags.isEmpty()) {
boolean isTagsEmpty = true;
for (String tagString : tags) {
if (StringUtils.isNotBlank(tagString)) {
isTagsEmpty = false;
break;
}
}
if (!isTagsEmpty) {
request.setTags(tags);
}
}
// this is the user who initiates the request
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();

@ -18,13 +18,16 @@
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.TagInfo;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.TagMappingInfo;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.TagManagementService;
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.*;
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.Tag;
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.TagManagementException;
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.TagNotFoundException;
import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger;
import io.entgra.device.mgt.core.notification.logger.impl.EntgraRoleMgtLoggerImpl;
@ -50,7 +53,8 @@ public class TagManagementServiceImpl implements TagManagementService {
} 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();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@ -69,11 +73,15 @@ public class TagManagementServiceImpl implements TagManagementService {
} catch (TagManagementException e) {
String msg = "Error occurred while adding tags." ;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity
(new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (BadRequestException e) {
String msg = "Error occurred while adding tags. Please check the request" ;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
if(log.isDebugEnabled()) {
log.debug(msg, e);
}
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
}
}
@ -89,9 +97,11 @@ public class TagManagementServiceImpl implements TagManagementService {
tag = DeviceMgtAPIUtils.getTagManagementService().getTagByName(tagName);
}
if (tag == null) {
String msg = "Tag not found.";
String msg = (tagId != null) ? "Tag with ID " + tagId + " is not found."
: "Tag with name " + tagName + " is not found.";
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
return Response.status(Response.Status.NOT_FOUND).entity(new ErrorResponse.ErrorResponseBuilder().
setMessage(msg).build()).build();
}
tag.setName(tagInfo.getName());
tag.setDescription(tagInfo.getDescription());
@ -101,17 +111,22 @@ public class TagManagementServiceImpl implements TagManagementService {
String msg = (tagId != null) ? "Error occurred while updating tag with ID " + tagId + "."
: "Error occurred while updating tag with name " + tagName + ".";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (TagNotFoundException e) {
String msg = (tagId != null) ? "Tag with ID " + tagId + " is not found."
: "Tag with name " + tagName + " is not found.";
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
return Response.status(Response.Status.NOT_FOUND).entity(new ErrorResponse.ErrorResponseBuilder().
setMessage(msg).build()).build();
} catch (BadRequestException e) {
String msg = (tagId != null) ? "Error occurred while updating tag with ID " + tagId
: "Error occurred while updating tag with name " + tagName;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
if(log.isDebugEnabled()) {
log.debug(msg, e);
}
return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.ErrorResponseBuilder().
setMessage(msg).build()).build();
}
}
@ -125,11 +140,13 @@ public class TagManagementServiceImpl implements TagManagementService {
} catch (TagManagementException e) {
String msg = "Error occurred while deleting tag with ID " + tagId + ".";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.ErrorResponseBuilder().
setMessage(msg).build()).build();
} catch (TagNotFoundException e) {
String msg = "Tag with ID " + tagId + " is not found.";
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
return Response.status(Response.Status.NOT_FOUND).entity(new ErrorResponse.ErrorResponseBuilder().
setMessage(e.getMessage()).build()).build();
}
}
@ -143,11 +160,13 @@ public class TagManagementServiceImpl implements TagManagementService {
} catch (TagManagementException e) {
String msg = "Error occurred while getting tag with ID " + tagId + ".";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (TagNotFoundException e) {
String msg = "Tag with ID " + tagId + " is not found.";
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
return Response.status(Response.Status.NOT_FOUND).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
}
}
@ -156,18 +175,21 @@ public class TagManagementServiceImpl implements TagManagementService {
public Response addDeviceTagMapping(TagMappingInfo tagMappingInfo) {
RequestValidationUtil.validateTagMappingDetails(tagMappingInfo);
try {
TagMappingDTO tagMappingDTO = new TagMappingDTO(tagMappingInfo.getDeviceIdentifiers(),
DeviceMgtAPIUtils.getTagManagementService().addDeviceTagMapping(tagMappingInfo.getDeviceIdentifiers(),
tagMappingInfo.getDeviceType(), tagMappingInfo.getTags());
DeviceMgtAPIUtils.getTagManagementService().addDeviceTagMapping(tagMappingDTO);
return Response.status(Response.Status.CREATED).entity(tagMappingDTO).build();
return Response.status(Response.Status.CREATED).entity(tagMappingInfo).build();
} catch (TagManagementException e) {
String msg = "Error occurred while adding device-tag mapping.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (BadRequestException e) {
String msg = "Error occurred while adding tag mappings.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
if(log.isDebugEnabled()) {
log.debug(msg, e);
}
return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
}
}
@ -176,18 +198,21 @@ public class TagManagementServiceImpl implements TagManagementService {
public Response deleteDeviceTagMapping(TagMappingInfo tagMappingInfo) {
RequestValidationUtil.validateTagMappingDetails(tagMappingInfo);
try {
TagMappingDTO tagMappingDTO = new TagMappingDTO(tagMappingInfo.getDeviceIdentifiers(),
DeviceMgtAPIUtils.getTagManagementService().deleteDeviceTagMapping(tagMappingInfo.getDeviceIdentifiers(),
tagMappingInfo.getDeviceType(), tagMappingInfo.getTags());
DeviceMgtAPIUtils.getTagManagementService().deleteDeviceTagMapping(tagMappingDTO);
return Response.status(Response.Status.NO_CONTENT).build();
return Response.status(Response.Status.NO_CONTENT).entity(tagMappingInfo).build();
} catch (TagManagementException e) {
String msg = "Error occurred while deleting tag mappings.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (BadRequestException e) {
String msg = "Error occurred while deleting tag mappings.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
if(log.isDebugEnabled()) {
log.debug(msg, e);
}
return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.
ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
}
}
}

@ -493,41 +493,70 @@ public class RequestValidationUtil {
}
}
/**
* Validates the provided tag details to ensure that either a valid tagId or a non-empty tagName is provided,
* and that the tagInfo is not null. Throws InputValidationException if any of these conditions are not met.
*
* @param tagId the ID of the tag (must be greater than 0)
* @param tagName the name of the tag (must be non-empty if tagId is not provided)
* @param tagInfo the TagInfo object to validate (must not be null)
* @throws InputValidationException if neither a valid tagId nor a tagName is provided, or if tagInfo is null
*/
public static void validateTagDetails(Integer tagId, String tagName, TagInfo tagInfo) {
if (tagId == null && tagName == null) {
String msg = "Either tagId or tagName must be provided.";
if ((tagId == null || tagId <= 0) && StringUtils.isBlank(tagName)) {
String msg = "Either valid tagId or tagName must be provided.";
log.error(msg);
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(msg).build());
}
if (tagInfo == null) {
String msg = "Provided request body is empty.";
log.error(msg);
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is "
+ "empty").build());
}
}
/**
* Validates the provided list of TagInfo objects to ensure that it is not null.
* Throws InputValidationException if the list is null.
*
* @param tagInfo the list of TagInfo objects to validate (must not be null)
* @throws InputValidationException if the list is null
*/
public static void validateTagListDetails(List<TagInfo> tagInfo) {
if (tagInfo == null) {
String msg = "Provided request body is empty.";
log.error(msg);
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is "
+ "empty").build());
}
}
/**
* Validates the provided TagMappingInfo object to ensure it is not null and contains valid
* device identifiers, device type, and tags. Throws InputValidationException if validation fails.
*
* @param tagMappingInfo the TagMappingInfo object to validate
* @throws InputValidationException if the tagMappingInfo or its required fields are invalid
*/
public static void validateTagMappingDetails(TagMappingInfo tagMappingInfo) {
if (tagMappingInfo == null) {
String msg = "Provided request body is empty.";
log.error(msg);
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Request body is empty").build());
} else if (tagMappingInfo.getDeviceIdentifiers() == null || tagMappingInfo.getDeviceType() == null
|| tagMappingInfo.getTags() == null) {
String msg = "Invalid tag mapping request body.";
log.error(msg);
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Invalid tag mapping request body").build());
}
}
public static void validateScopes(List<Scope> scopes) {
if (scopes == null || scopes.isEmpty()) {
throw new InputValidationException(

@ -21,12 +21,11 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.util;
import io.entgra.device.mgt.core.apimgt.webapp.publisher.APIPublisherService;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.TagManagementService;
import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuthorizationService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionManagerServiceImpl;
import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService;
import io.entgra.device.mgt.core.device.mgt.core.service.TagManagementProviderService;
import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.java.security.SSLProtocolSocketFactory;
@ -67,7 +66,6 @@ import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Operation;
import io.entgra.device.mgt.core.device.mgt.common.report.mgt.ReportManagementService;
import io.entgra.device.mgt.core.device.mgt.common.spi.DeviceTypeGeneratorService;
import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService;
import io.entgra.device.mgt.core.device.mgt.common.spi.TraccarManagementService;
import io.entgra.device.mgt.core.device.mgt.core.app.mgt.ApplicationManagementProviderService;
import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.DeviceInformationManager;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTypeVersion;
@ -167,6 +165,7 @@ public class DeviceMgtAPIUtils {
private static volatile APIPublisherService apiPublisher;
private static volatile TenantManagerAdminService tenantManagerAdminService;
private static volatile TagManagementProviderService tagManagementService;
static {
String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
@ -507,15 +506,24 @@ public class DeviceMgtAPIUtils {
return policyManagementService;
}
/**
* Initializing and accessing method for TagManagementService.
*
* @return TagManagementService instance
* @throws IllegalStateException if TagManagementService cannot be initialized
*/
public static TagManagementProviderService getTagManagementService() {
TagManagementProviderService tagManagementService;
if (tagManagementService == null) {
synchronized (DeviceMgtAPIUtils.class) {
if (tagManagementService == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
tagManagementService =
(TagManagementProviderService) ctx.getOSGiService(TagManagementProviderService.class, null);
tagManagementService = (TagManagementProviderService) ctx.getOSGiService(
TagManagementProviderService.class, null);
if (tagManagementService == null) {
String msg = "Tag Management service not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
throw new IllegalStateException("Tag Management service not initialized.");
}
}
}
}
return tagManagementService;
}

@ -18,7 +18,6 @@
package io.entgra.device.mgt.core.device.mgt.common.tag.mgt;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ -21,8 +21,15 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.DeviceTag;
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.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -33,6 +40,8 @@ import static io.entgra.device.mgt.core.device.mgt.core.dao.util.TagManagementDA
public class TagDAOImpl implements TagDAO {
private static final Log log = LogFactory.getLog(TagDAOImpl.class);
protected Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}
@ -62,11 +71,15 @@ public class TagDAOImpl implements TagDAO {
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.");
String msg = "Error occurred while adding tags, adding some tags failed.";
log.error(msg);
throw new TagManagementDAOException(msg);
}
}
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while adding tags", e);
String msg = "Error occurred while adding tags.";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, null);
}
@ -87,7 +100,9 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(4, tenantId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while updating tag", e);
String msg = "Error occurred while updating tag.";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, null);
}
@ -106,7 +121,9 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(2, tenantId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while deleting tag", e);
String msg = "Error occurred while deleting tag.";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, null);
}
@ -131,7 +148,9 @@ public class TagDAOImpl implements TagDAO {
tag = loadTag(resultSet);
}
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while retrieving tag", e);
String msg = "Error occurred while getting a specific tag." + tagId;
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, resultSet);
}
@ -157,7 +176,9 @@ public class TagDAOImpl implements TagDAO {
tag = loadTag(resultSet);
}
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while retrieving tag with name: " + tagName, e);
String msg = "Error occurred while retrieving tag with name: " + tagName;
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, resultSet);
}
@ -184,7 +205,9 @@ public class TagDAOImpl implements TagDAO {
tags.add(tag);
}
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while retrieving tags", e);
String msg = "Error occurred while retrieving tags";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, resultSet);
}
@ -237,9 +260,13 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(paramIndex, tenantId);
preparedStatement.executeUpdate();
} catch (SQLIntegrityConstraintViolationException e) {
throw new TagManagementDAOException("Tag is already mapped to this device", e, true);
String msg = "Tag is already mapped to this device";
log.error(msg, e);
throw new TagManagementDAOException(msg, e, true);
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while adding device tag", e);
String msg = "Error occurred while adding device tag mapping";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, null);
}
@ -287,7 +314,9 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(paramIndex, tenantId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while deleting device tag mapping", e);
String msg = "Error occurred while deleting device tag mapping";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, null);
}
@ -313,7 +342,9 @@ public class TagDAOImpl implements TagDAO {
deviceTags.add(deviceTag);
}
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while retrieving device tags", e);
String msg = "Error occurred while retrieving device tags";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, resultSet);
}
@ -340,7 +371,9 @@ public class TagDAOImpl implements TagDAO {
deviceTags.add(deviceTag);
}
} catch (SQLException e) {
throw new TagManagementDAOException("Error occurred while retrieving devices for tag", e);
String msg = "Error occurred while retrieving devices for tag";
log.error(msg, e);
throw new TagManagementDAOException(msg, e);
} finally {
cleanupResources(preparedStatement, resultSet);
}

@ -21,7 +21,8 @@ import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuth
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 io.entgra.device.mgt.core.device.mgt.core.service.TagManagementProviderService;
import io.entgra.device.mgt.core.device.mgt.core.service.TagManagementProviderServiceImpl;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ -22,7 +22,15 @@ import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
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.*;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceTypeDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.EnrollmentDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.ApplicationDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceStatusDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.TagDAO;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO;
import io.entgra.device.mgt.core.device.mgt.core.dto.OwnerWithDeviceDTO;
import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO;

@ -82,16 +82,22 @@ public interface TagManagementProviderService {
/**
* Method to add a device-tag mapping.
*
* @param tagMappingDto - TagMappingDTO object containing mapping information.
* @param deviceIdentifiers - List of device ids to map.
* @param deviceType - Device Type of that specific devices.
* @param tags - List of tags you want to attach.
* @throws TagManagementException if something goes wrong while adding the device-tag mapping.
*/
void addDeviceTagMapping(TagMappingDTO tagMappingDto) throws TagManagementException, BadRequestException;
void addDeviceTagMapping(List<String> deviceIdentifiers, String deviceType, List<String> tags)
throws TagManagementException, BadRequestException;
/**
* Method to delete a device-tag mapping.
*
* @param tagMappingDTO - TagMappingDTO object containing mapping information.
* @param deviceIdentifiers - List of device ids to map.
* @param deviceType - Device Type of that specific devices.
* @param tags - List of tags you want to attach.
* @throws TagManagementException if something goes wrong while deleting the device-tag mapping.
*/
void deleteDeviceTagMapping(TagMappingDTO tagMappingDTO) throws TagManagementException, BadRequestException;
void deleteDeviceTagMapping(List<String> deviceIdentifiers, String deviceType, List<String> tags)
throws TagManagementException, BadRequestException;
}

@ -47,11 +47,13 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
throw new BadRequestException(msg);
}
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
for (Tag tag : tags) {
if (tag.getName() == null) {
throw new BadRequestException("Tag name cannot be null");
String msg = "Tag name cannot be null";
log.error(msg);
throw new BadRequestException(msg);
}
}
if (log.isDebugEnabled()) {
@ -80,9 +82,11 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
@Override
public List<Tag> getAllTags() throws TagManagementException {
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.beginTransaction();
return tagDAO.getTags(tenantId);
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
List<Tag> tags = tagDAO.getTags(tenantId);
DeviceManagementDAOFactory.commitTransaction();
return tags;
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
@ -104,8 +108,8 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
@Override
public Tag getTagById(int tagId) throws TagManagementException, TagNotFoundException {
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Tag tag = tagDAO.getTagById(tagId, tenantId);
DeviceManagementDAOFactory.commitTransaction();
if (tag == null) {
@ -132,16 +136,14 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
}
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Tag tag = tagDAO.getTagByName(tagName, tenantId);
DeviceManagementDAOFactory.commitTransaction();
if (tag == null) {
String msg = "Tag with name " + tagName + " not found.";
throw new TagNotFoundException(msg);
}
return tag;
} catch (TransactionManagementException | TagManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
@ -162,8 +164,8 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
throw new BadRequestException(msg);
}
try {
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection();
Tag existingTag = tagDAO.getTagById(tag.getId(), tenantId);
if (existingTag == null) {
String msg = "Tag with ID: " + tag.getId() + " does not exist.";
@ -171,7 +173,8 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
throw new TagNotFoundException(msg);
}
tagDAO.updateTag(tag, tenantId);
} catch (TagManagementDAOException | SQLException e) {
DeviceManagementDAOFactory.commitTransaction();
} catch (TagManagementDAOException | TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating the tag with ID: " + tag.getId();
log.error(msg, e);
@ -185,8 +188,8 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
@Override
public void deleteTag(int tagId) throws TagManagementException, TagNotFoundException {
try {
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection();
Tag existingTag = tagDAO.getTagById(tagId, tenantId);
if (existingTag == null) {
String msg = "Tag with ID: " + tagId + " does not exist.";
@ -195,7 +198,7 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
}
tagDAO.deleteTag(tagId, tenantId);
DeviceManagementDAOFactory.commitTransaction();
} catch (TagManagementDAOException | SQLException e) {
} catch (TagManagementDAOException | TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting the tag with ID: " + tagId;
log.error(msg, e);
@ -206,17 +209,17 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
}
@Override
public void addDeviceTagMapping(TagMappingDTO tagMappingDto) throws TagManagementException, BadRequestException {
if (tagMappingDto == null || tagMappingDto.getDeviceIdentifiers() == null || tagMappingDto.getTags() == null
|| tagMappingDto.getDeviceType() == null) {
public void addDeviceTagMapping(List<String> deviceIdentifiers, String deviceType, List<String> tags)
throws TagManagementException, BadRequestException {
if (deviceIdentifiers == null || deviceType == null || tags == null) {
String msg = "Received incomplete data for device tag mapping.";
log.error(msg);
throw new BadRequestException(msg);
}
try {
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection();
TagMappingDTO tagMappingDto = new TagMappingDTO(deviceIdentifiers, deviceType, tags);
List<Tag> tagList = new ArrayList<>();
for (String tagName : tagMappingDto.getTags()) {
Tag tag = new Tag();
@ -227,7 +230,7 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
tagDAO.addTags(tagList, tenantId);
tagDAO.addDeviceTagMapping(tagMappingDto.getDeviceIdentifiers(), tagMappingDto.getDeviceType(),
tagMappingDto.getTags(), tenantId);
DeviceManagementDAOFactory.commitTransaction();
} catch (TagManagementDAOException e) {
if (e.isUniqueConstraintViolation()) {
String msg = "Tag is already mapped to this device.";
@ -239,7 +242,7 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
log.error(msg, e);
throw new TagManagementException(msg, e);
}
} catch (SQLException e) {
} catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding device tag mapping.";
log.error(msg, e);
@ -250,20 +253,20 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
}
@Override
public void deleteDeviceTagMapping(TagMappingDTO tagMappingDto) throws TagManagementException, BadRequestException {
if (tagMappingDto == null || tagMappingDto.getDeviceIdentifiers() == null || tagMappingDto.getTags() == null
|| tagMappingDto.getDeviceType() == null) {
public void deleteDeviceTagMapping(List<String> deviceIdentifiers, String deviceType, List<String> tags) throws TagManagementException, BadRequestException {
if (deviceIdentifiers == null || deviceType == null || tags == null) {
String msg = "Received incomplete data for device tag mapping.";
log.error(msg);
throw new BadRequestException(msg);
}
try {
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection();
TagMappingDTO tagMappingDto = new TagMappingDTO(deviceIdentifiers, deviceType, tags);
tagDAO.deleteDeviceTagMapping(tagMappingDto.getDeviceIdentifiers(), tagMappingDto.getDeviceType(),
tagMappingDto.getTags(), tenantId);
DeviceManagementDAOFactory.commitTransaction();
} catch (TagManagementDAOException | SQLException e) {
} catch (TagManagementDAOException | TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting device tag mappings.";
log.error(msg, e);

@ -133,84 +133,60 @@ public class TagManagementProviderServiceTest extends BaseDeviceManagementTest {
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "deleteTag")
public void createTagMappingsNull() throws TagManagementException, BadRequestException {
tagManagementProviderService.addDeviceTagMapping(null);
tagManagementProviderService.addDeviceTagMapping(null, null, null);
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "createTagMappingsNull")
public void createTagsMappingsNullDeviceIdentifiers() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(null);
tagMappingDTO.setDeviceType(DEVICE_TYPE);
tagMappingDTO.setTags(new ArrayList<>(Arrays.asList("tag1", "tag2")));
tagManagementProviderService.addDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.addDeviceTagMapping(null, DEVICE_TYPE,
new ArrayList<>(Arrays.asList("tag1", "tag2")));
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "createTagsMappingsNullDeviceIdentifiers")
public void createTagsMappingsNullDeviceType() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)));
tagMappingDTO.setDeviceType(null);
tagMappingDTO.setTags(new ArrayList<>(Arrays.asList("tag1", "tag2")));
tagManagementProviderService.addDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.addDeviceTagMapping(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)),
null, new ArrayList<>(Arrays.asList("tag1", "tag2")));
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "createTagsMappingsNullDeviceType")
public void createTagsMappingsNullTags() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)));
tagMappingDTO.setDeviceType(DEVICE_TYPE);
tagMappingDTO.setTags(null);
tagManagementProviderService.addDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.addDeviceTagMapping(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)),
DEVICE_TYPE, null);
}
@Test(dependsOnMethods = "createTagsMappingsNullTags")
public void createTagsMappings() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)));
tagMappingDTO.setDeviceType(DEVICE_TYPE);
tagMappingDTO.setTags(new ArrayList<>(Arrays.asList("tag1", "tag2")));
tagManagementProviderService.addDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.addDeviceTagMapping(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)),
DEVICE_TYPE, new ArrayList<>(Arrays.asList("tag1", "tag2")));
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "createTagsMappings")
public void deleteTagMappingsNull() throws TagManagementException, BadRequestException {
tagManagementProviderService.deleteDeviceTagMapping(null);
tagManagementProviderService.deleteDeviceTagMapping(null, null, null);
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "deleteTagMappingsNull")
public void deleteTagsMappingsNullDeviceIdentifiers() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(null);
tagMappingDTO.setDeviceType(DEVICE_TYPE);
tagMappingDTO.setTags(new ArrayList<>(Arrays.asList("tag1", "tag2")));
tagManagementProviderService.deleteDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.deleteDeviceTagMapping(null, DEVICE_TYPE,
new ArrayList<>(Arrays.asList("tag1", "tag2")));
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "deleteTagsMappingsNullDeviceIdentifiers")
public void deleteTagsMappingsNullDeviceType() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)));
tagMappingDTO.setDeviceType(null);
tagMappingDTO.setTags(new ArrayList<>(Arrays.asList("tag1", "tag2")));
tagManagementProviderService.deleteDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.deleteDeviceTagMapping(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)),
null, new ArrayList<>(Arrays.asList("tag1", "tag2")));
}
@Test(expectedExceptions = {BadRequestException.class}, dependsOnMethods = "deleteTagsMappingsNullDeviceType")
public void deleteTagsMappingsNullTags() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)));
tagMappingDTO.setDeviceType(DEVICE_TYPE);
tagMappingDTO.setTags(null);
tagManagementProviderService.deleteDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.deleteDeviceTagMapping(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)),
DEVICE_TYPE, null);
}
@Test(dependsOnMethods = "deleteTagsMappingsNullTags")
public void deleteTagsMappings() throws TagManagementException, DeviceManagementException {
TagMappingDTO tagMappingDTO = new TagMappingDTO();
tagMappingDTO.setDeviceIdentifiers(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)));
tagMappingDTO.setDeviceType(DEVICE_TYPE);
tagMappingDTO.setTags(new ArrayList<>(Arrays.asList("tag1", "tag2")));
tagManagementProviderService.deleteDeviceTagMapping(tagMappingDTO);
tagManagementProviderService.deleteDeviceTagMapping(new ArrayList<>(Arrays.asList(DEVICE_ID_1, DEVICE_ID_2)),
DEVICE_TYPE, new ArrayList<>(Arrays.asList("tag1", "tag2")));
}
}

Loading…
Cancel
Save