private boolean isDuplicateFile(
     JAXBResource jaxbResource, ResourceInterface resourcePrototype, boolean add) {
   if (null == jaxbResource.getBase64()) return false;
   boolean addError = true;
   String formFileName = jaxbResource.getFileName();
   try {
     resourcePrototype.setMainGroup(jaxbResource.getMainGroup());
     if (resourcePrototype.exists(formFileName)) {
       if (!add) {
         ResourceInterface masterResource =
             this.getResourceManager().loadResource(jaxbResource.getId());
         String masterFileName =
             (null != masterResource) ? masterResource.getMasterFileName() : null;
         if (null != masterFileName && masterFileName.equalsIgnoreCase(formFileName)) {
           addError = false;
         }
       }
     } else {
       addError = false;
     }
   } catch (Throwable t) {
     ApsSystemUtils.logThrowable(
         t,
         this,
         "isDuplicateFile",
         "Error while check duplicate file - master file name '" + formFileName + "'");
   }
   return addError;
 }
 private void checkType(JAXBResource jaxbResource, String expectedTypeCode) throws Throwable {
   if (!jaxbResource.getTypeCode().equals(expectedTypeCode)) {
     throw new ApiException(
         IApiErrorCodes.API_VALIDATION_ERROR,
         "Invalid resource type - '" + jaxbResource.getTypeCode() + "'",
         Response.Status.CONFLICT);
   }
 }
 public StringApiResponse updateResource(JAXBResource jaxbResource, Properties properties)
     throws Throwable {
   StringApiResponse response = new StringApiResponse();
   BaseResourceDataBean bean = null;
   try {
     UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
     this.check(jaxbResource, user, response, false);
     if (null != response.getErrors() && !response.getErrors().isEmpty()) {
       return response;
     }
     bean = jaxbResource.createBataBean(this.getCategoryManager());
     this.getResourceManager().updateResource(bean);
     response.setResult(IResponseBuilder.SUCCESS);
   } catch (Throwable t) {
     ApsSystemUtils.logThrowable(t, this, "updateResource");
     throw new ApsSystemException("Error into API method", t);
   } finally {
     if (null != bean && null != bean.getFile()) {
       bean.getFile().delete();
     }
   }
   return response;
 }
 public StringApiResponse addResource(JAXBResource jaxbResource, Properties properties)
     throws ApiException, Throwable {
   StringApiResponse response = new StringApiResponse();
   BaseResourceDataBean bean = null;
   try {
     UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
     this.check(jaxbResource, user, response, true);
     if (null != response.getErrors() && !response.getErrors().isEmpty()) {
       return response;
     }
     bean = jaxbResource.createBataBean(this.getCategoryManager());
     String id = bean.getResourceId();
     if (null != id && id.trim().length() > 0) {
       Pattern pattern = Pattern.compile("^[a-zA-Z]+$");
       Matcher matcher = pattern.matcher(id);
       if (!matcher.matches()) {
         throw new ApiException(
             IApiErrorCodes.API_PARAMETER_VALIDATION_ERROR,
             "The resourceId can contain only alphabetic characters",
             Response.Status.CONFLICT);
       }
     }
     this.getResourceManager().addResource(bean);
     response.setResult(IResponseBuilder.SUCCESS);
   } catch (ApiException ae) {
     throw ae;
   } catch (Throwable t) {
     ApsSystemUtils.logThrowable(t, this, "addResource");
     throw new ApsSystemException("Error into API method", t);
   } finally {
     if (null != bean && null != bean.getFile()) {
       bean.getFile().delete();
     }
   }
   return response;
 }
  private void check(
      JAXBResource jaxbResource, UserDetails user, StringApiResponse response, boolean add)
      throws Throwable {
    ResourceInterface resourcePrototype =
        this.getResourceManager().createResourceType(jaxbResource.getTypeCode());
    if (null == resourcePrototype) {
      this.addValidationError(
          "Invalid resource type - '" + jaxbResource.getTypeCode() + "'", response);
    }
    if (null == user) {
      user = this.getUserManager().getGuestUser();
    }
    String groupName = jaxbResource.getMainGroup();
    if (null == groupName || groupName.trim().length() == 0) {
      this.addValidationError("Group required", response);
    } else {
      if (null == this.getGroupManager().getGroup(groupName)) {
        // this.addValidationError("Group '" + groupName + "' does not exist", response);
      } else if (!this.getAuthorizationManager().isAuthOnGroup(user, groupName)) {
        // this.addValidationError("Group '" + groupName + "' not allowed", response);
      }
    }
    if (add) {
      if (null == jaxbResource.getBase64()) {
        this.addValidationError("Binary Image required", response);
      }
      if (null == jaxbResource.getFileName() || jaxbResource.getFileName().trim().length() == 0) {
        this.addValidationError("FileName required", response);
      }
    } else {
      ResourceInterface oldResource = this.getResourceManager().loadResource(jaxbResource.getId());
      if (null == oldResource) {
        this.addValidationError(
            "Resource with id '" + jaxbResource.getId() + "' does not exist", response);
      } else if (!oldResource.getMainGroup().equals(jaxbResource.getMainGroup())) {
        this.addValidationError(
            "Resource group has to be '" + oldResource.getMainGroup() + "'", response);
      }
    }
    if (this.isDuplicateFile(jaxbResource, resourcePrototype, add)) {
      this.addValidationError(
          "File '" + jaxbResource.getFileName() + "' is already present on Archive", response);
    }
    if (null == jaxbResource.getDescription()
        || jaxbResource.getDescription().trim().length() == 0) {
      this.addValidationError("Description required", response);
    }

    if (null == jaxbResource.getTypeCode() || jaxbResource.getTypeCode().trim().length() == 0) {
      this.addValidationError("TypeCode required", response);
    }
  }