}
@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,parametersName,values, 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("file");
if(file!=null){
try {
contentlet.setBinary(field.getVelocityVarName(), file);
} catch (IOException e) {
}
}
else if(field.isRequired()) {
DotContentletValidationException cve = new DotContentletValidationException("Contentlet's fields are not valid");
cve.addRequiredField(field);
throw cve;
}
}
}
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 file and images
*/