/** * Set the field value, to a content according the content structure * * @param structure The content structure * @param contentlet The content * @param fieldName The field name * @param value The field value * @throws DotDataException */ private static void setField( Structure structure, Contentlet contentlet, String fieldName, String[] values) throws DotDataException { Field field = structure.getFieldVar(fieldName); String value = ""; if (UtilMethods.isSet(field) && APILocator.getFieldAPI().valueSettable(field)) { try { if (field.getFieldType().equals(Field.FieldType.HOST_OR_FOLDER.toString())) { value = VelocityUtil.cleanVelocity(values[0]); Host host = APILocator.getHostAPI().find(value, APILocator.getUserAPI().getSystemUser(), false); if (host != null && InodeUtils.isSet(host.getIdentifier())) { contentlet.setHost(host.getIdentifier()); contentlet.setFolder(FolderAPI.SYSTEM_FOLDER); } else { Folder folder = APILocator.getFolderAPI() .find(value, APILocator.getUserAPI().getSystemUser(), false); if (folder != null && InodeUtils.isSet(folder.getInode())) { contentlet.setHost(folder.getHostId()); contentlet.setFolder(folder.getInode()); } } } else if (field.getFieldType().equals(Field.FieldType.MULTI_SELECT.toString()) || field.getFieldType().equals(Field.FieldType.CHECKBOX.toString())) { if (field.getFieldContentlet().startsWith("float") || field.getFieldContentlet().startsWith("integer")) { value = values[0]; } else { for (String temp : values) { value = temp + "," + value; } } } else if (field.getFieldType().equals(Field.FieldType.DATE.toString())) { value = VelocityUtil.cleanVelocity(values[0]); if (value instanceof String) { value = value + " 00:00:00"; } } else { value = VelocityUtil.cleanVelocity(values[0]); } conAPI.setContentletProperty(contentlet, field, value); } catch (Exception e) { Logger.debug(SubmitContentUtil.class, e.getMessage()); } } }
public static FileAsset saveTempFile( User user, Host host, java.io.File uploadedFile, String folderPath, String title) throws Exception { Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host, user, false); byte[] bytes = FileUtil.getBytes(uploadedFile); if (bytes != null) { String name = UtilMethods.getFileName(title); int counter = 1; while (APILocator.getFileAPI().fileNameExists(folder, name)) { name = name + counter; counter++; } 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); APILocator.getContentletAPI().checkin(cont, user, false); APILocator.getVersionableAPI().setLive(cont); return APILocator.getFileAssetAPI().fromContentlet(cont); } return null; }
/** * 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; }