@Override
  @Transactional
  public void save(ResourceEntity entity, final String requestorId) {
    if (entity.getResourceType() != null) {
      entity.setResourceType(resourceTypeDao.findById(entity.getResourceType().getId()));
    }

    /* admin resource can't have an admin resource - do this check here */
    boolean isAdminResource =
        StringUtils.equals(entity.getResourceType().getId(), adminResourceTypeId);

    if (entity.getType() != null && StringUtils.isNotBlank(entity.getType().getId())) {
      entity.setType(typeDAO.findById(entity.getType().getId()));
    } else {
      entity.setType(null);
    }

    if (StringUtils.isNotBlank(entity.getId())) {
      final ResourceEntity dbObject = resourceDao.findById(entity.getId());
      entity.setAdminResource(dbObject.getAdminResource());
      entity.setApproverAssociations(dbObject.getApproverAssociations());

      if (isAdminResource) {
        entity.setAdminResource(null);
      } else if (entity.getAdminResource() == null) {
        final ResourceEntity adminResource = getNewAdminResource(entity, requestorId);
        entity.setAdminResource(adminResource);
        if (CollectionUtils.isEmpty(dbObject.getApproverAssociations())) {
          entity.addApproverAssociation(createDefaultApproverAssociations(entity, requestorId));
        }
      }
      entity.setChildResources(dbObject.getChildResources());
      entity.setParentResources(dbObject.getParentResources());
      entity.setUsers(dbObject.getUsers());
      entity.setGroups(dbObject.getGroups());
      entity.setRoles(dbObject.getRoles());

      // elementDAO.flush();
      mergeAttribute(entity, dbObject);

    } else {
      boolean addApproverAssociation = false;
      if (isAdminResource) {
        entity.setAdminResource(null);
      } else {
        entity.setAdminResource(getNewAdminResource(entity, requestorId));
        addApproverAssociation = true;
      }
      resourceDao.save(entity);

      if (addApproverAssociation) {
        entity.addApproverAssociation(createDefaultApproverAssociations(entity, requestorId));
      }

      addRequiredAttributes(entity);
    }

    resourceDao.merge(entity);
  }
 @Override
 @Transactional
 public void addRequiredAttributes(ResourceEntity resource) {
   if (resource != null
       && resource.getType() != null
       && StringUtils.isNotBlank(resource.getType().getId())) {
     MetadataElementSearchBean sb = new MetadataElementSearchBean();
     sb.addTypeId(resource.getType().getId());
     List<MetadataElementEntity> elementList = elementDAO.getByExample(sb, -1, -1);
     if (CollectionUtils.isNotEmpty(elementList)) {
       for (MetadataElementEntity element : elementList) {
         if (element.isRequired()) {
           resourcePropDao.save(AttributeUtil.buildResAttribute(resource, element));
         }
       }
     }
   }
 }