Initialize device tag feature updated req changes

secure-folder-policy
Gimhan Wijayawardana 3 months ago
parent 5e1bfd6d9a
commit 295f1831d8

@ -17,7 +17,10 @@
*/ */
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api; package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.*; 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.TagInfoList;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.TagMappingInfo;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import io.entgra.device.mgt.core.apimgt.annotations.Scope; import io.entgra.device.mgt.core.apimgt.annotations.Scope;
import io.entgra.device.mgt.core.apimgt.annotations.Scopes; import io.entgra.device.mgt.core.apimgt.annotations.Scopes;
@ -107,7 +110,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
value = "Getting the List of Tags", value = "Getting the List of Tags",
notes = "", notes = "This endpoint is used to retrieve all tags",
tags = "Tag Management", tags = "Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -153,7 +156,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "POST", httpMethod = "POST",
value = "Adding a Tag", value = "Adding a Tag",
notes = "", notes = "This endpoint is used to add new tags",
tags = "Tag Management", tags = "Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -212,7 +215,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT", httpMethod = "PUT",
value = "Updating a Tag", value = "Updating a Tag",
notes = "", notes = "This endpoint is used to update a specific tag",
tags = "Tag Management", tags = "Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -260,7 +263,7 @@ public interface TagManagementService {
@ApiOperation( @ApiOperation(
httpMethod = "DELETE", httpMethod = "DELETE",
value = "Deleting a Tag", value = "Deleting a Tag",
notes = "", notes = "This endpoint is used to delete a specific tag",
tags = "Tag Management", tags = "Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -293,7 +296,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
value = "Getting a Tag by ID", value = "Getting a Tag by ID",
notes = "", notes = "This endpoint is used to retrieve tag by id",
tags = "Tag Management", tags = "Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -332,7 +335,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "POST", httpMethod = "POST",
value = "Adding a Device-Tag Mapping", value = "Adding a Device-Tag Mapping",
notes = "", notes = "This endpoint is used to map devices with tags",
tags = "Device-Tag Management", tags = "Device-Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -372,7 +375,7 @@ public interface TagManagementService {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "DELETE", httpMethod = "DELETE",
value = "Deleting a Device-Tag Mapping", value = "Deleting a Device-Tag Mapping",
notes = "", notes = "This endpoint is used to remove tag mappings from devices",
tags = "Device-Tag Management", tags = "Device-Tag Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {

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

@ -18,13 +18,16 @@
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; 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.TagInfo;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.TagMappingInfo; 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.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.service.impl.util.RequestValidationUtil;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; 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.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.device.mgt.extensions.logger.spi.EntgraLogger;
import io.entgra.device.mgt.core.notification.logger.impl.EntgraRoleMgtLoggerImpl; import io.entgra.device.mgt.core.notification.logger.impl.EntgraRoleMgtLoggerImpl;
@ -50,7 +53,8 @@ public class TagManagementServiceImpl implements TagManagementService {
} catch (TagManagementException e) { } catch (TagManagementException e) {
String msg = "Error occurred while getting tags."; String msg = "Error occurred while getting tags.";
log.error(msg, e); 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) { } catch (TagManagementException e) {
String msg = "Error occurred while adding tags." ; String msg = "Error occurred while adding tags." ;
log.error(msg, e); 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) { } catch (BadRequestException e) {
String msg = "Error occurred while adding tags. Please check the request" ; String msg = "Error occurred while adding tags. Please check the request" ;
log.error(msg, e); if(log.isDebugEnabled()) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); 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); tag = DeviceMgtAPIUtils.getTagManagementService().getTagByName(tagName);
} }
if (tag == null) { 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); 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.setName(tagInfo.getName());
tag.setDescription(tagInfo.getDescription()); 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 + "." String msg = (tagId != null) ? "Error occurred while updating tag with ID " + tagId + "."
: "Error occurred while updating tag with name " + tagName + "."; : "Error occurred while updating tag with name " + tagName + ".";
log.error(msg, e); 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) { } catch (TagNotFoundException e) {
String msg = (tagId != null) ? "Tag with ID " + tagId + " is not found." String msg = (tagId != null) ? "Tag with ID " + tagId + " is not found."
: "Tag with name " + tagName + " is not found."; : "Tag with name " + tagName + " is not found.";
log.error(msg, e); 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) { } catch (BadRequestException e) {
String msg = (tagId != null) ? "Error occurred while updating tag with ID " + tagId String msg = (tagId != null) ? "Error occurred while updating tag with ID " + tagId
: "Error occurred while updating tag with name " + tagName; : "Error occurred while updating tag with name " + tagName;
log.error(msg, e); if(log.isDebugEnabled()) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); 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) { } catch (TagManagementException e) {
String msg = "Error occurred while deleting tag with ID " + tagId + "."; String msg = "Error occurred while deleting tag with ID " + tagId + ".";
log.error(msg, e); 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) { } catch (TagNotFoundException e) {
String msg = "Tag with ID " + tagId + " is not found."; String msg = "Tag with ID " + tagId + " is not found.";
log.error(msg, e); 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) { } catch (TagManagementException e) {
String msg = "Error occurred while getting tag with ID " + tagId + "."; String msg = "Error occurred while getting tag with ID " + tagId + ".";
log.error(msg, e); 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) { } catch (TagNotFoundException e) {
String msg = "Tag with ID " + tagId + " is not found."; String msg = "Tag with ID " + tagId + " is not found.";
log.error(msg, e); 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) { public Response addDeviceTagMapping(TagMappingInfo tagMappingInfo) {
RequestValidationUtil.validateTagMappingDetails(tagMappingInfo); RequestValidationUtil.validateTagMappingDetails(tagMappingInfo);
try { try {
TagMappingDTO tagMappingDTO = new TagMappingDTO(tagMappingInfo.getDeviceIdentifiers(), DeviceMgtAPIUtils.getTagManagementService().addDeviceTagMapping(tagMappingInfo.getDeviceIdentifiers(),
tagMappingInfo.getDeviceType(), tagMappingInfo.getTags()); tagMappingInfo.getDeviceType(), tagMappingInfo.getTags());
DeviceMgtAPIUtils.getTagManagementService().addDeviceTagMapping(tagMappingDTO); return Response.status(Response.Status.CREATED).entity(tagMappingInfo).build();
return Response.status(Response.Status.CREATED).entity(tagMappingDTO).build();
} catch (TagManagementException e) { } catch (TagManagementException e) {
String msg = "Error occurred while adding device-tag mapping."; String msg = "Error occurred while adding device-tag mapping.";
log.error(msg, e); 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) { } catch (BadRequestException e) {
String msg = "Error occurred while adding tag mappings."; String msg = "Error occurred while adding tag mappings.";
log.error(msg, e); if(log.isDebugEnabled()) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); 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) { public Response deleteDeviceTagMapping(TagMappingInfo tagMappingInfo) {
RequestValidationUtil.validateTagMappingDetails(tagMappingInfo); RequestValidationUtil.validateTagMappingDetails(tagMappingInfo);
try { try {
TagMappingDTO tagMappingDTO = new TagMappingDTO(tagMappingInfo.getDeviceIdentifiers(), DeviceMgtAPIUtils.getTagManagementService().deleteDeviceTagMapping(tagMappingInfo.getDeviceIdentifiers(),
tagMappingInfo.getDeviceType(), tagMappingInfo.getTags()); tagMappingInfo.getDeviceType(), tagMappingInfo.getTags());
DeviceMgtAPIUtils.getTagManagementService().deleteDeviceTagMapping(tagMappingDTO); return Response.status(Response.Status.NO_CONTENT).entity(tagMappingInfo).build();
return Response.status(Response.Status.NO_CONTENT).build();
} catch (TagManagementException e) { } catch (TagManagementException e) {
String msg = "Error occurred while deleting tag mappings."; String msg = "Error occurred while deleting tag mappings.";
log.error(msg, e); 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) { } catch (BadRequestException e) {
String msg = "Error occurred while deleting tag mappings."; String msg = "Error occurred while deleting tag mappings.";
log.error(msg, e); if(log.isDebugEnabled()) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); 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) { public static void validateTagDetails(Integer tagId, String tagName, TagInfo tagInfo) {
if (tagId == null && tagName == null) { if ((tagId == null || tagId <= 0) && StringUtils.isBlank(tagName)) {
String msg = "Either tagId or tagName must be provided."; String msg = "Either valid tagId or tagName must be provided.";
log.error(msg); log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(msg).build());
} }
if (tagInfo == null) { if (tagInfo == null) {
String msg = "Provided request body is empty.";
log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is " new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is "
+ "empty").build()); + "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) { public static void validateTagListDetails(List<TagInfo> tagInfo) {
if (tagInfo == null) { if (tagInfo == null) {
String msg = "Provided request body is empty.";
log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is " new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is "
+ "empty").build()); + "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) { public static void validateTagMappingDetails(TagMappingInfo tagMappingInfo) {
if (tagMappingInfo == null) { if (tagMappingInfo == null) {
String msg = "Provided request body is empty.";
log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Request body is empty").build()); new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Request body is empty").build());
} else if (tagMappingInfo.getDeviceIdentifiers() == null || tagMappingInfo.getDeviceType() == null } else if (tagMappingInfo.getDeviceIdentifiers() == null || tagMappingInfo.getDeviceType() == null
|| tagMappingInfo.getTags() == null) { || tagMappingInfo.getTags() == null) {
String msg = "Invalid tag mapping request body.";
log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Invalid tag mapping request body").build()); new ErrorResponse.ErrorResponseBuilder().setCode(400L).setMessage("Invalid tag mapping request body").build());
} }
} }
public static void validateScopes(List<Scope> scopes) { public static void validateScopes(List<Scope> scopes) {
if (scopes == null || scopes.isEmpty()) { if (scopes == null || scopes.isEmpty()) {
throw new InputValidationException( 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.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.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; 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.authorization.GroupAccessAuthorizationService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; 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.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.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.AxisFault;
import org.apache.axis2.client.Options; import org.apache.axis2.client.Options;
import org.apache.axis2.java.security.SSLProtocolSocketFactory; 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.report.mgt.ReportManagementService;
import io.entgra.device.mgt.core.device.mgt.common.spi.DeviceTypeGeneratorService; 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.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.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.device.details.mgt.DeviceInformationManager;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTypeVersion; 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 APIPublisherService apiPublisher;
private static volatile TenantManagerAdminService tenantManagerAdminService; private static volatile TenantManagerAdminService tenantManagerAdminService;
private static volatile TagManagementProviderService tagManagementService;
static { static {
String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password"); String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
@ -507,15 +506,24 @@ public class DeviceMgtAPIUtils {
return policyManagementService; return policyManagementService;
} }
/**
* Initializing and accessing method for TagManagementService.
*
* @return TagManagementService instance
* @throws IllegalStateException if TagManagementService cannot be initialized
*/
public static TagManagementProviderService getTagManagementService() { public static TagManagementProviderService getTagManagementService() {
TagManagementProviderService tagManagementService;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
tagManagementService =
(TagManagementProviderService) ctx.getOSGiService(TagManagementProviderService.class, null);
if (tagManagementService == null) { if (tagManagementService == null) {
String msg = "Tag Management service not initialized."; synchronized (DeviceMgtAPIUtils.class) {
log.error(msg); if (tagManagementService == null) {
throw new IllegalStateException(msg); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
tagManagementService = (TagManagementProviderService) ctx.getOSGiService(
TagManagementProviderService.class, null);
if (tagManagementService == null) {
throw new IllegalStateException("Tag Management service not initialized.");
}
}
}
} }
return tagManagementService; return tagManagementService;
} }

@ -18,7 +18,6 @@
package io.entgra.device.mgt.core.device.mgt.common.tag.mgt; 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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

@ -1091,13 +1091,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"SELECT dtm.ENROLMENT_ID " + "SELECT dtm.ENROLMENT_ID " +
"FROM DM_DEVICE_TAG_MAPPING dtm " + "FROM DM_DEVICE_TAG_MAPPING dtm " +
"JOIN DM_TAG t ON dtm.TAG_ID = t.ID " + "JOIN DM_TAG t ON dtm.TAG_ID = t.ID " +
"WHERE t.NAME IN ("; "WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )"; sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )";
isTagsProvided = true; isTagsProvided = true;
} }
@ -1372,13 +1366,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"SELECT dtm.ENROLMENT_ID " + "SELECT dtm.ENROLMENT_ID " +
"FROM DM_DEVICE_TAG_MAPPING dtm " + "FROM DM_DEVICE_TAG_MAPPING dtm " +
"JOIN DM_TAG t ON dtm.TAG_ID = t.ID " + "JOIN DM_TAG t ON dtm.TAG_ID = t.ID " +
"WHERE t.NAME IN ("; "WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )"; sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )";
isTagsProvided = true; isTagsProvided = true;
} }
@ -2988,6 +2976,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return joiner.toString(); return joiner.toString();
} }
protected String buildTagQuery(List<String> tagList)
throws DeviceManagementDAOException {
return String.join(", ", Collections.nCopies(tagList.size(), "?"));
}
public int getFunctioningDevicesInSystem() throws DeviceManagementDAOException { public int getFunctioningDevicesInSystem() throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;

@ -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.DeviceTag;
import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.Tag; import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.Tag;
import io.entgra.device.mgt.core.device.mgt.core.dao.*; import io.entgra.device.mgt.core.device.mgt.core.dao.*;
import org.apache.commons.logging.Log;
import java.sql.*; 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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 { public class TagDAOImpl implements TagDAO {
private static final Log log = LogFactory.getLog(TagDAOImpl.class);
protected Connection getConnection() throws SQLException { protected Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }
@ -62,11 +71,15 @@ public class TagDAOImpl implements TagDAO {
int[] updateCounts = preparedStatement.executeBatch(); int[] updateCounts = preparedStatement.executeBatch();
for (int count : updateCounts) { for (int count : updateCounts) {
if (count == PreparedStatement.EXECUTE_FAILED) { 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) { } 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 { } finally {
cleanupResources(preparedStatement, null); cleanupResources(preparedStatement, null);
} }
@ -87,7 +100,9 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(4, tenantId); preparedStatement.setInt(4, tenantId);
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, null); cleanupResources(preparedStatement, null);
} }
@ -106,7 +121,9 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(2, tenantId); preparedStatement.setInt(2, tenantId);
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, null); cleanupResources(preparedStatement, null);
} }
@ -131,7 +148,9 @@ public class TagDAOImpl implements TagDAO {
tag = loadTag(resultSet); tag = loadTag(resultSet);
} }
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, resultSet); cleanupResources(preparedStatement, resultSet);
} }
@ -157,7 +176,9 @@ public class TagDAOImpl implements TagDAO {
tag = loadTag(resultSet); tag = loadTag(resultSet);
} }
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, resultSet); cleanupResources(preparedStatement, resultSet);
} }
@ -184,7 +205,9 @@ public class TagDAOImpl implements TagDAO {
tags.add(tag); tags.add(tag);
} }
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, resultSet); cleanupResources(preparedStatement, resultSet);
} }
@ -237,9 +260,13 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(paramIndex, tenantId); preparedStatement.setInt(paramIndex, tenantId);
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} catch (SQLIntegrityConstraintViolationException e) { } 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) { } 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 { } finally {
cleanupResources(preparedStatement, null); cleanupResources(preparedStatement, null);
} }
@ -287,7 +314,9 @@ public class TagDAOImpl implements TagDAO {
preparedStatement.setInt(paramIndex, tenantId); preparedStatement.setInt(paramIndex, tenantId);
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, null); cleanupResources(preparedStatement, null);
} }
@ -313,7 +342,9 @@ public class TagDAOImpl implements TagDAO {
deviceTags.add(deviceTag); deviceTags.add(deviceTag);
} }
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, resultSet); cleanupResources(preparedStatement, resultSet);
} }
@ -340,7 +371,9 @@ public class TagDAOImpl implements TagDAO {
deviceTags.add(deviceTag); deviceTags.add(deviceTag);
} }
} catch (SQLException e) { } 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 { } finally {
cleanupResources(preparedStatement, resultSet); cleanupResources(preparedStatement, resultSet);
} }

@ -174,13 +174,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID "; "LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID ";
if (tagList != null && !tagList.isEmpty()) { if (tagList != null && !tagList.isEmpty()) {
sql += " WHERE t.NAME IN ("; sql += " WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? "; sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ";
isTagsProvided = true; isTagsProvided = true;
} else { } else {
@ -800,13 +794,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"FROM DM_ENROLMENT e " + "FROM DM_ENROLMENT e " +
"LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " +
"LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID " + "LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID " +
"WHERE t.NAME IN ("; "WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) "; sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) ";
isTagsProvided = true; isTagsProvided = true;
} else { } else {

@ -163,13 +163,7 @@ public class PostgreSQLDeviceDAOImpl extends GenericDeviceDAOImpl {
if (tagList != null && !tagList.isEmpty()) { if (tagList != null && !tagList.isEmpty()) {
sql += " AND e.ID IN (SELECT e.ID FROM DM_ENROLMENT e " + sql += " AND e.ID IN (SELECT e.ID FROM DM_ENROLMENT e " +
"LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " +
"LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID WHERE t.NAME IN ("; "LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) "; sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) ";
isTagsProvided = true; isTagsProvided = true;
} }
@ -590,13 +584,7 @@ public class PostgreSQLDeviceDAOImpl extends GenericDeviceDAOImpl {
"FROM DM_ENROLMENT e " + "FROM DM_ENROLMENT e " +
"LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " +
"LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID " + "LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID " +
"WHERE t.NAME IN ("; "WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) "; sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) ";
isTagsProvided = true; isTagsProvided = true;
} else { } else {

@ -169,13 +169,7 @@ public class SQLServerDeviceDAOImpl extends GenericDeviceDAOImpl {
if (tagList!= null && !tagList.isEmpty()) { if (tagList!= null && !tagList.isEmpty()) {
sql += " AND e.ID IN (SELECT e.ID FROM DM_ENROLMENT e " + sql += " AND e.ID IN (SELECT e.ID FROM DM_ENROLMENT e " +
"LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " +
"LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID WHERE t.NAME IN ("; "LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) "; sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ? ) ";
isTagsProvided = true; isTagsProvided = true;
} }
@ -609,13 +603,7 @@ public class SQLServerDeviceDAOImpl extends GenericDeviceDAOImpl {
"SELECT e.ID FROM DM_ENROLMENT e " + "SELECT e.ID FROM DM_ENROLMENT e " +
"LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_TAG_MAPPING dtm ON e.ID = dtm.ENROLMENT_ID " +
"LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID " + "LEFT JOIN DM_TAG t ON dtm.TAG_ID = t.ID " +
"WHERE t.NAME IN ("; "WHERE t.NAME IN (" + buildTagQuery(tagList);
for (int i = 0; i < tagList.size(); i++) {
if (i > 0) {
sql += ", ";
}
sql += "?";
}
sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ?)"; sql += ") GROUP BY e.ID HAVING COUNT(DISTINCT t.NAME) = ?)";
isTagsProvided = true; isTagsProvided = true;
} else { } else {

@ -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.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.authorization.GroupAccessAuthorizationServiceImpl; 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.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 io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;

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

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

@ -875,3 +875,24 @@ CREATE TABLE IF NOT EXISTS DM_CEA_POLICIES (
-- END OF DM_CEA_POLICIES TABLE -- -- END OF DM_CEA_POLICIES TABLE --
-- DM_TAG TABLE --
CREATE TABLE IF NOT EXISTS DM_TAG (
ID BIGINT AUTO_INCREMENT,
NAME VARCHAR(255) NOT NULL,
DESCRIPTION VARCHAR(255) NULL,
TENANT_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME)
);
-- END OF DM_TAG TABLE --
-- DM_DEVICE_TAG_MAPPING TABLE --
CREATE TABLE IF NOT EXISTS DM_DEVICE_TAG_MAPPING (
ENROLMENT_ID BIGINT NOT NULL,
TAG_ID BIGINT NOT NULL,
TENANT_ID INTEGER NOT NULL,
PRIMARY KEY (ENROLMENT_ID, TAG_ID, TENANT_ID),
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 --

@ -951,3 +951,25 @@ BEGIN
END; END;
-- END OF DM_CEA_POLICIES TABLE -- -- END OF DM_CEA_POLICIES TABLE --
-- DM_TAG TABLE --
CREATE TABLE IF NOT EXISTS DM_TAG (
ID BIGINT IDENTITY(1,1) PRIMARY KEY,
NAME NVARCHAR(255) NOT NULL,
DESCRIPTION NVARCHAR(255) NULL,
TENANT_ID INT NOT NULL,
CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME)
);
-- END OF DM_TAG TABLE --
-- DM_DEVICE_TAG_MAPPING TABLE --
CREATE TABLE IF NOT EXISTS DM_DEVICE_TAG_MAPPING (
ENROLMENT_ID BIGINT NOT NULL,
TAG_ID BIGINT NOT NULL,
TENANT_ID INT NOT NULL,
PRIMARY KEY (ENROLMENT_ID, TAG_ID, TENANT_ID),
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 --

@ -950,3 +950,25 @@ CREATE TABLE IF NOT EXISTS DM_CEA_POLICIES (
); );
-- END OF DM_CEA_POLICIES TABLE -- -- END OF DM_CEA_POLICIES TABLE --
-- DM_TAG TABLE --
CREATE TABLE IF NOT EXISTS DM_TAG (
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(255) NOT NULL,
DESCRIPTION VARCHAR(255) NULL,
TENANT_ID INT NOT NULL,
CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME)
);
-- END OF DM_TAG TABLE --
-- DM_DEVICE_TAG_MAPPING TABLE --
CREATE TABLE IF NOT EXISTS DM_DEVICE_TAG_MAPPING (
ENROLMENT_ID BIGINT NOT NULL,
TAG_ID BIGINT NOT NULL,
TENANT_ID INT NOT NULL,
PRIMARY KEY (ENROLMENT_ID, TAG_ID, TENANT_ID),
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 --

@ -1255,3 +1255,25 @@ END;
/ /
-- END OF DM_CEA_POLICIES TABLE -- -- END OF DM_CEA_POLICIES TABLE --
-- DM_TAG TABLE --
CREATE TABLE DM_TAG (
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
NAME VARCHAR2(255) NOT NULL,
DESCRIPTION VARCHAR2(255) NULL,
TENANT_ID NUMBER(10) NOT NULL,
CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME)
);
-- END OF DM_TAG TABLE --
-- DM_DEVICE_TAG_MAPPING TABLE --
CREATE TABLE DM_DEVICE_TAG_MAPPING (
ENROLMENT_ID NUMBER(19) NOT NULL,
TAG_ID NUMBER(19) NOT NULL,
TENANT_ID NUMBER(10) NOT NULL,
PRIMARY KEY (ENROLMENT_ID, TAG_ID, TENANT_ID),
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 --

@ -881,3 +881,25 @@ CREATE TABLE IF NOT EXISTS DM_CEA_POLICIES (
); );
-- END OF DM_CEA_POLICIES TABLE -- -- END OF DM_CEA_POLICIES TABLE --
-- DM_TAG TABLE --
CREATE TABLE IF NOT EXISTS DM_TAG (
ID BIGSERIAL PRIMARY KEY,
NAME VARCHAR(255) NOT NULL,
DESCRIPTION VARCHAR(255) NULL,
TENANT_ID INTEGER NOT NULL,
CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME)
);
-- END OF DM_TAG TABLE --
-- DM_DEVICE_TAG_MAPPING TABLE --
CREATE TABLE IF NOT EXISTS DM_DEVICE_TAG_MAPPING (
ENROLMENT_ID BIGINT NOT NULL,
TAG_ID BIGINT NOT NULL,
TENANT_ID INTEGER NOT NULL,
PRIMARY KEY (ENROLMENT_ID, TAG_ID, TENANT_ID),
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 --

Loading…
Cancel
Save