@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; }
private void _saveStructure(ActionForm form, ActionRequest req, ActionResponse res) { try { boolean newStructure = false; StructureForm structureForm = (StructureForm) form; Structure structure = (Structure) req.getAttribute(WebKeys.Structure.STRUCTURE); User user = _getUser(req); HttpServletRequest httpReq = ((ActionRequestImpl) req).getHttpServletRequest(); if (!UtilMethods.isSet(structureForm.getHost()) && (!UtilMethods.isSet(structureForm.getFolder()) || structureForm.getFolder().equals("SYSTEM_FOLDER"))) { throw new DotDataException(LanguageUtil.get(user, "Host-or-folder-is-required")); } // Checking permissions _checkWritePermissions(structure, user, httpReq); // Check if another structure with the same name exist String auxStructureName = structureForm.getName(); auxStructureName = (auxStructureName != null ? auxStructureName.trim() : ""); @SuppressWarnings("deprecation") Structure auxStructure = StructureCache.getStructureByType(auxStructureName); if (InodeUtils.isSet(auxStructure.getInode()) && !auxStructure.getInode().equalsIgnoreCase(structure.getInode())) { throw new DotDataException( LanguageUtil.get(user, "There-is-another-structure-with-the-same-name")); } Arrays.sort(reservedStructureNames); if (!InodeUtils.isSet(structureForm.getInode()) && (Arrays.binarySearch(reservedStructureNames, auxStructureName) >= 0)) { throw new DotDataException("Invalid Reserved Structure Name : " + auxStructureName); } // Validate if is a new structure and if the name hasn't change if (!InodeUtils.isSet(structure.getInode())) { newStructure = true; } else { String structureName = structure.getName(); String structureFormName = structureForm.getName(); if (UtilMethods.isSet(structureName) && UtilMethods.isSet(structureFormName) && !structureName.equals(structureFormName) && !structure.isFixed()) { StructureCache.removeStructure(structure); } } // If the structure is fixed the name cannot be changed if (structure.isFixed()) { structureForm.setName(structure.getName()); } // if I'm editing a structure the structureType couldn't not be // change if (UtilMethods.isSet(structure.getInode()) && InodeUtils.isSet(structure.getInode())) { // reset the structure type to it's original value structureForm.setStructureType(structure.getStructureType()); } if (UtilMethods.isSet(structure.getVelocityVarName())) { structureForm.setVelocityVarName(structure.getVelocityVarName()); } if (UtilMethods.isSet(structureForm.getHost())) { if (!structureForm.getHost().equals(Host.SYSTEM_HOST) && hostAPI.findSystemHost().getIdentifier().equals(structureForm.getHost())) { structureForm.setHost(Host.SYSTEM_HOST); } structureForm.setFolder("SYSTEM_FOLDER"); } else if (UtilMethods.isSet(structureForm.getFolder())) { structureForm.setHost(folderAPI.find(structureForm.getFolder(), user, false).getHostId()); } if (UtilMethods.isSet(structureForm.getHost()) && (!UtilMethods.isSet(structureForm.getFolder()) || structureForm.getFolder().equals("SYSTEM_FOLDER"))) { Host host = hostAPI.find(structureForm.getHost(), user, false); if (host != null) { if (structure.getStructureType() == Structure.STRUCTURE_TYPE_FORM) { if (!perAPI.doesUserHavePermissions( host, "PARENT:" + PermissionAPI.PERMISSION_CAN_ADD_CHILDREN + ", STRUCTURES:" + PermissionAPI.PERMISSION_PUBLISH, user)) { throw new DotDataException( LanguageUtil.get( user, "User-does-not-have-add-children-permission-on-host-folder")); } } else { if (!perAPI.doesUserHavePermission( host, PermissionAPI.PERMISSION_CAN_ADD_CHILDREN, user)) { throw new DotDataException( LanguageUtil.get( user, "User-does-not-have-add-children-permission-on-host-folder")); } } } } if (UtilMethods.isSet(structureForm.getFolder()) && !structureForm.getFolder().equals("SYSTEM_FOLDER")) { Folder folder = folderAPI.find(structureForm.getFolder(), user, false); if (folder != null) { if (structure.getStructureType() == Structure.STRUCTURE_TYPE_FORM) { if (!perAPI.doesUserHavePermissions( folder, "PARENT:" + PermissionAPI.PERMISSION_CAN_ADD_CHILDREN + ", STRUCTURES:" + PermissionAPI.PERMISSION_PUBLISH, user)) { throw new DotDataException( LanguageUtil.get( user, "User-does-not-have-add-children-permission-on-host-folder")); } } else { if (!perAPI.doesUserHavePermission( folder, PermissionAPI.PERMISSION_CAN_ADD_CHILDREN, user)) { throw new DotDataException( LanguageUtil.get( user, "User-does-not-have-add-children-permission-on-host-folder")); } } } } BeanUtils.copyProperties(structure, structureForm); // if htmlpage doesn't exist page id should be an identifier. Should // be refactored once we get identifierAPI/HTMLPage API done String pageDetail = structureForm.getDetailPage(); if (newStructure) { String structureVelocityName = VelocityUtil.convertToVelocityVariable(structure.getName(), true); List<String> velocityvarnames = StructureFactory.getAllVelocityVariablesNames(); int found = 0; if (VelocityUtil.isNotAllowedVelocityVariableName(structureVelocityName)) { found++; } for (String velvar : velocityvarnames) { if (velvar != null) { if (structureVelocityName.equalsIgnoreCase(velvar)) { found++; } else if (velvar.toLowerCase().contains(structureVelocityName.toLowerCase())) { String number = velvar.substring(structureVelocityName.length()); if (RegEX.contains(number, "^[0-9]+$")) { found++; } } } } if (found > 0) { structureVelocityName = structureVelocityName + Integer.toString(found); } structure.setVelocityVarName(structureVelocityName); } if (UtilMethods.isSet(pageDetail)) { structure.setDetailPage(pageDetail); } // Saving interval review properties if (structureForm.isReviewContent()) { structure.setReviewInterval( structureForm.getReviewIntervalNum() + structureForm.getReviewIntervalSelect()); } else { structure.setReviewInterval(null); structure.setReviewerRole(null); } // If there is no default structure this would be Structure defaultStructure = StructureFactory.getDefaultStructure(); if (!InodeUtils.isSet(defaultStructure.getInode())) { structure.setDefaultStructure(true); } if (newStructure) { structure.setFixed(false); structure.setOwner(user.getUserId()); } // validate iit is a form structure set it as system by default if (structureForm.getStructureType() == Structure.STRUCTURE_TYPE_FORM) { structure.setSystem(true); } StructureFactory.saveStructure(structure); structureForm.setUrlMapPattern(structure.getUrlMapPattern()); WorkflowScheme scheme = APILocator.getWorkflowAPI().findSchemeForStruct(structure); String schemeId = req.getParameter("workflowScheme"); if (scheme != null && UtilMethods.isSet(schemeId) && !schemeId.equals(scheme.getId())) { scheme = APILocator.getWorkflowAPI().findScheme(schemeId); APILocator.getWorkflowAPI().saveSchemeForStruct(structure, scheme); } // if the structure is a widget we need to add the base fields. if (newStructure && structureForm.getStructureType() == Structure.STRUCTURE_TYPE_WIDGET) { wAPI.createBaseWidgetFields(structure); } // if the structure is a form we need to add the base fields. if (newStructure && structureForm.getStructureType() == Structure.STRUCTURE_TYPE_FORM) { fAPI.createBaseFormFields(structure); } // if the structure is a form we need to add the base fields. if (newStructure && structureForm.getStructureType() == Structure.STRUCTURE_TYPE_FILEASSET) { APILocator.getFileAssetAPI().createBaseFileAssetFields(structure); } if (!newStructure) { perAPI.resetPermissionReferences(structure); } // Saving the structure in cache StructureCache.removeStructure(structure); StructureCache.addStructure(structure); StructureServices.removeStructureFile(structure); String message = "message.structure.savestructure"; if (structure.getStructureType() == 3) { message = "message.form.saveform"; } SessionMessages.add(req, "message", message); AdminLogger.log( EditStructureAction.class, "_saveStructure", "Structure saved : " + structure.getName(), user); } catch (Exception ex) { Logger.error(this.getClass(), ex.toString()); String message = ex.toString(); SessionMessages.add(req, "error", message); } }