|
|
|
@ -166,13 +166,20 @@ public class TagManagementProviderServiceImpl implements TagManagementProviderSe
|
|
|
|
|
try {
|
|
|
|
|
DeviceManagementDAOFactory.beginTransaction();
|
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
Tag existingTag = tagDAO.getTagById(tag.getId(), tenantId);
|
|
|
|
|
if (existingTag == null) {
|
|
|
|
|
Tag tagWithName = tagDAO.getTagByName(tag.getName(), tenantId);
|
|
|
|
|
if (tagWithName != null && tagWithName.getId() != tag.getId()) {
|
|
|
|
|
String msg = "Tag with name: " + tag.getName() + " already exists.";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
}
|
|
|
|
|
if (tagWithName == null || tagWithName.getId() == tag.getId()) {
|
|
|
|
|
tagDAO.updateTag(tag, tenantId);
|
|
|
|
|
DeviceManagementDAOFactory.commitTransaction();
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Tag with ID: " + tag.getId() + " does not exist.";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new TagNotFoundException(msg);
|
|
|
|
|
}
|
|
|
|
|
tagDAO.updateTag(tag, tenantId);
|
|
|
|
|
DeviceManagementDAOFactory.commitTransaction();
|
|
|
|
|
} catch (TagManagementDAOException | TransactionManagementException e) {
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
|