Exemplo n.º 1
0
  /**
   * Save the file uploaded
   *
   * @param user the user that save the file
   * @param host Current host
   * @param uploadedFile
   * @param folder The folder where the file is going to be save
   * @param title The filename
   * @return File
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  private static FileAsset saveFile(
      User user, Host host, java.io.File uploadedFile, String folderPath, String title)
      throws Exception {

    Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host, user, false);
    if (!UtilMethods.isSet(folder.getInode())) {
      User systemUser = APILocator.getUserAPI().getSystemUser();
      folder =
          APILocator.getFolderAPI()
              .createFolders(folderPath, host, APILocator.getUserAPI().getSystemUser(), false);
    }

    byte[] bytes = FileUtil.getBytes(uploadedFile);

    if (bytes != null) {
      String newFileName = "";
      String name = UtilMethods.getFileName(title);
      int counter = 1;
      String fileName = name + "." + UtilMethods.getFileExtension(title);
      while (APILocator.getFileAPI().fileNameExists(folder, fileName)) {
        newFileName = name + "(" + counter + ")";
        fileName = newFileName + "." + UtilMethods.getFileExtension(title);
        counter++;
      }
      while (APILocator.getFileAssetAPI().fileNameExists(host, folder, name, "")) {
        newFileName = name + "(" + counter + ")";
        fileName = newFileName + "." + UtilMethods.getFileExtension(title);
        counter++;
      }
      if (UtilMethods.isSet(newFileName)) {
        name = newFileName;
      }
      Contentlet cont = new Contentlet();
      cont.setStructureInode(folder.getDefaultFileType());
      cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(name));
      cont.setFolder(folder.getInode());
      cont.setHost(host.getIdentifier());
      cont.setBinary(FileAssetAPI.BINARY_FIELD, uploadedFile);
      if (StructureCache.getStructureByInode(cont.getStructureInode()).getStructureType()
          == Structure.STRUCTURE_TYPE_FILEASSET) cont.setStringProperty("fileName", title);
      cont = APILocator.getContentletAPI().checkin(cont, user, false);
      if (APILocator.getPermissionAPI()
          .doesUserHavePermission(cont, PermissionAPI.PERMISSION_PUBLISH, user))
        APILocator.getVersionableAPI().setLive(cont);
      return APILocator.getFileAssetAPI().fromContentlet(cont);
    }

    return null;
  }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public static Contentlet createContent(
      Structure st,
      ArrayList<Category> cats,
      String userId,
      List<String> parametersName,
      List<String[]> values,
      String options,
      List<Map<String, Object>> fileParameters,
      boolean autoPublish,
      Host formHost,
      String moderatorRole)
      throws DotContentletStateException, DotDataException, DotSecurityException {

    Contentlet contentlet = null;

    /*try {*/
    /** Get the current user */
    User user = getUserFromId(userId);

    /** Content inherit structure permissions */
    List<Permission> permissionList = perAPI.getPermissions(st);

    /** Set the content values */
    contentlet = SubmitContentUtil.setAllFields(st.getName(), parametersName, values);

    /** Get the required relationships */
    Map<Relationship, List<Contentlet>> relationships =
        SubmitContentUtil.getRelationships(st, contentlet, options, user);

    /** Validating content fields */
    // conAPI.validateContentlet(contentlet,relationships,cats);

    /** Set the binary field values http://jira.dotmarketing.net/browse/DOTCMS-3463 */
    if (fileParameters.size() > 0) {
      for (Map<String, Object> value : fileParameters) {
        Field field = (Field) value.get("field");
        java.io.File file = (java.io.File) value.get(field.getVelocityVarName());
        if (file != null) {
          try {
            contentlet.setBinary(field.getVelocityVarName(), file);
          } catch (IOException e) {

          }
        }
      }
    }

    if (st.getStructureType() == Structure.STRUCTURE_TYPE_FORM) {
      contentlet.setHost(formHost.getIdentifier());
      Host host =
          APILocator.getHostAPI()
              .find(formHost.getIdentifier(), APILocator.getUserAPI().getSystemUser(), false);
      if (!perAPI.doesUserHavePermissions(
          host,
          "PARENT:"
              + PermissionAPI.PERMISSION_READ
              + ", CONTENTLETS:"
              + PermissionAPI.PERMISSION_WRITE
              + "",
          user)) {
        throw new DotSecurityException("User doesn't have write permissions to Contentlet");
      }
    }

    /** If the moderator field is set, a work flow task is created */
    if (UtilMethods.isSet(moderatorRole)) {

      if (!UtilMethods.isSet(contentlet.getStringProperty(Contentlet.WORKFLOW_ACTION_KEY)))
        contentlet.setStringProperty(
            Contentlet.WORKFLOW_ACTION_KEY,
            APILocator.getWorkflowAPI().findEntryAction(contentlet, user).getId());

      String contentletTitle = "";

      List<Field> fields = FieldsCache.getFieldsByStructureInode(contentlet.getStructureInode());

      for (Field fld : fields) {
        if (fld.isListed()) {
          contentletTitle = contentlet.getMap().get(fld.getVelocityVarName()).toString();
          contentletTitle =
              contentletTitle.length() > 250 ? contentletTitle.substring(0, 250) : contentletTitle;
        }
      }
      contentlet.setStringProperty(
          Contentlet.WORKFLOW_COMMENTS_KEY,
          "A new content titled \""
              + UtilHTML.escapeHTMLSpecialChars(contentletTitle.trim())
              + "\" has been posted by "
              + UtilHTML.escapeHTMLSpecialChars(user.getFullName())
              + " ("
              + user.getEmailAddress()
              + ")");

      contentlet.setStringProperty(
          Contentlet.WORKFLOW_ASSIGN_KEY, roleAPI.loadRoleByKey(moderatorRole).getId());
    }

    /** Saving Content */
    contentlet = conAPI.checkin(contentlet, relationships, cats, permissionList, user, true);

    APILocator.getVersionableAPI().setWorking(contentlet);
    if (autoPublish) APILocator.getVersionableAPI().setLive(contentlet);

    /** Saving file and images */
    if (fileParameters.size() > 0) {

      for (Map<String, Object> value : fileParameters) {
        Field field = (Field) value.get("field");
        // http://jira.dotmarketing.net/browse/DOTCMS-3463
        if (field.getFieldType().equals(Field.FieldType.IMAGE.toString())
            || field.getFieldType().equals(Field.FieldType.FILE.toString())) {
          java.io.File uploadedFile = (java.io.File) value.get("file");
          try {
            if (!UtilMethods.isSet(FileUtil.getBytes(uploadedFile))) continue;
          } catch (IOException e) {
            Logger.error(SubmitContentUtil.class, e.getMessage());
          }
          String title = (String) value.get("title");
          Host host = (Host) value.get("host");
          contentlet = addFileToContentlet(contentlet, field, host, uploadedFile, user, title);
        }
      }
      if (autoPublish) { // DOTCMS-5188
        contentlet =
            conAPI.checkinWithoutVersioning(
                contentlet, relationships, cats, permissionList, user, true);
        conAPI.publish(contentlet, APILocator.getUserAPI().getSystemUser(), false);
      } else {
        contentlet =
            conAPI.checkinWithoutVersioning(
                contentlet, relationships, cats, permissionList, user, true);
        conAPI.unpublish(contentlet, APILocator.getUserAPI().getSystemUser(), false);
      }
    }

    /*}catch(Exception e){

    	Logger.error(SubmitContentUtil.class, e.getMessage());
    	throw new DotContentletStateException("Unable to perform checkin. "+e.getMessage());

    }*/

    return contentlet;
  }