public RangerTagResourceMap preCreateTagResourceMap(String tagGuid, String resourceGuid)
      throws Exception {
    if (StringUtils.isBlank(resourceGuid) || StringUtils.isBlank(tagGuid)) {
      throw new Exception("Both resourceGuid and resourceId need to be non-empty");
    }

    RangerTagResourceMap existing =
        tagStore.getTagResourceMapForTagAndResourceGuid(tagGuid, resourceGuid);

    if (existing != null) {
      throw new Exception(
          "Attempt to create existing association between resourceId="
              + resourceGuid
              + " and tagId="
              + tagGuid);
    }

    RangerServiceResource existingServiceResource = tagStore.getServiceResourceByGuid(resourceGuid);

    if (existingServiceResource == null) {
      throw new Exception("No resource found for guid=" + resourceGuid);
    }

    RangerTag existingTag = tagStore.getTagByGuid(tagGuid);

    if (existingTag == null) {
      throw new Exception("No tag found for guid=" + tagGuid);
    }

    RangerTagResourceMap newTagResourceMap = new RangerTagResourceMap();
    newTagResourceMap.setResourceId(existingServiceResource.getId());
    newTagResourceMap.setTagId(existingTag.getId());

    return newTagResourceMap;
  }
  public RangerServiceResource preDeleteServiceResource(Long id) throws Exception {
    RangerServiceResource existing = tagStore.getServiceResource(id);

    if (existing == null) {
      throw new Exception("Attempt to delete nonexistent resource, id=" + id);
    }

    List<RangerTagResourceMap> associations =
        tagStore.getTagResourceMapsForResourceId(existing.getId());
    if (CollectionUtils.isNotEmpty(associations)) {
      throw new Exception(
          "Attempt to delete serviceResource which is associated with a tag, id=" + id);
    }

    return existing;
  }
  public RangerServiceResource preDeleteServiceResourceByGuid(String guid, boolean deleteReferences)
      throws Exception {
    RangerServiceResource existing = tagStore.getServiceResourceByGuid(guid);

    if (existing == null) {
      throw new Exception("Attempt to delete nonexistent resource, guid=" + guid);
    }

    List<RangerTagResourceMap> associations =
        tagStore.getTagResourceMapsForResourceId(existing.getId());
    if (CollectionUtils.isNotEmpty(associations) && !deleteReferences) {
      throw new Exception(
          "Attempt to delete serviceResource which is associated with a tag, guid=" + guid);
    }

    return existing;
  }
  public RangerServiceResource preCreateServiceResource(RangerServiceResource resource)
      throws Exception {
    RangerServiceResource ret = null;

    if (StringUtils.isBlank(resource.getServiceName())
        || MapUtils.isEmpty(resource.getResourceElements())) {
      throw new Exception("No serviceName or resource in RangerServiceResource");
    }

    String guid = resource.getGuid();
    if (!StringUtils.isBlank(guid)) {
      ret = tagStore.getServiceResourceByGuid(guid);
    }

    RangerServiceResourceSignature serializer = new RangerServiceResourceSignature(resource);

    resource.setResourceSignature(serializer.getSignature());

    return ret;
  }
  public void preUpdateServiceResource(Long id, RangerServiceResource resource) throws Exception {
    if (StringUtils.isBlank(resource.getServiceName())
        || MapUtils.isEmpty(resource.getResourceElements())) {
      throw new Exception("No serviceName or resource in RangerServiceResource");
    }

    if (id == null) {
      throw new Exception("Invalid/null id");
    }

    RangerServiceResource existing = tagStore.getServiceResource(id);
    if (existing == null) {
      throw new Exception("Attempt to update nonexistent resource, id=" + id);
    }

    RangerServiceResourceSignature serializer = new RangerServiceResourceSignature(resource);

    resource.setId(existing.getId());
    resource.setGuid(existing.getGuid());
    resource.setResourceSignature(serializer.getSignature());
  }