/** * Check if a para is tupe file or image * * @param structure * @param paramName * @return boolean */ public static boolean imageOrFileParam(Structure structure, String paramName) { Field field = structure.getFieldVar(paramName); if (UtilMethods.isSet(field) && (field.getFieldType().equals(Field.FieldType.FILE.toString()) || field.getFieldType().equals(Field.FieldType.IMAGE.toString()))) { return true; } return false; }
/** * 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()); } } }
/** * @param folder This is the folder where the assets are stored * @param fileAssetsList This is the list of all the file assets * @return */ private static List<String> findFileAssetsList(File folder, List<String> fileAssetsList) { File[] files = folder.listFiles(); if (0 < files.length) { List<Structure> structures = StructureFactory.getStructures(); List<Field> binaryFields = new ArrayList<Field>(); List<Field> fields; for (Structure structure : structures) { fields = FieldsCache.getFieldsByStructureInode(structure.getInode()); for (Field field : fields) { if (field.getFieldType().equals(Field.FieldType.BINARY.toString())) binaryFields.add(field); } } boolean isBinaryField; for (int j = 0; j < files.length; j++) { fileAssetsList.add(files[j].getPath()); if (files[j].isDirectory()) { isBinaryField = false; for (Field field : binaryFields) { if (field.getVelocityVarName().equals(files[j].getName())) { isBinaryField = true; break; } } if (!isBinaryField) findFileAssetsList(files[j], fileAssetsList); } } } return fileAssetsList; }
@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; }