@SuppressWarnings({ "unchecked", "deprecation" })
public void _saveWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user) throws Exception, DotContentletValidationException {
ActionErrors ae = new ActionErrors();
// wraps request to get session object
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
HttpServletRequest httpReq = reqImpl.getHttpServletRequest();
String subcmd = req.getParameter("subcmd");
// Getting the contentlets variables to work
Contentlet currentContentlet = (Contentlet) req.getAttribute(WebKeys.CONTENTLET_EDIT);
String currentContentident = currentContentlet.getIdentifier();
boolean isNew = false;
if(!(InodeUtils.isSet(currentContentlet.getInode()))){
isNew = true;
}
if(!isNew){
try{
currentContentlet = conAPI.checkout(currentContentlet.getInode(), user, false);
}catch (DotSecurityException dse) {
SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save");
throw new DotSecurityException("User cannot checkout contentlet : ", dse);
}
}
req.setAttribute(WebKeys.CONTENTLET_FORM_EDIT, currentContentlet);
req.setAttribute(WebKeys.CONTENTLET_EDIT, currentContentlet);
ContentletForm contentletForm = (ContentletForm) form;
String owner = contentletForm.getOwner();
try{
_populateContent(req, res, config, form, user, currentContentlet);
//http://jira.dotmarketing.net/browse/DOTCMS-1450
//The form doesn't have the identifier in it. so the populate content was setting it to 0
currentContentlet.setIdentifier(currentContentident);
}catch (DotContentletValidationException ve) {
ae.add(Globals.ERROR_KEY, new ActionMessage("message.contentlet.invalid.form"));
req.setAttribute(Globals.ERROR_KEY, ae);
throw new DotContentletValidationException(ve.getMessage());
}
//Saving interval review properties
if (contentletForm.isReviewContent()) {
currentContentlet.setReviewInterval(contentletForm.getReviewIntervalNum() + contentletForm.getReviewIntervalSelect());
} else {
currentContentlet.setReviewInterval(null);
}
// saving the review dates
currentContentlet.setLastReview(new Date ());
if (currentContentlet.getReviewInterval() != null) {
currentContentlet.setNextReview(conAPI.getNextReview(currentContentlet, user, false));
}
ArrayList<Category> cats = new ArrayList<Category>();
// Getting categories that come from the entity
String[] arr = req.getParameterValues("categories") == null?new String[0]:req.getParameterValues("categories");
if (arr != null && arr.length > 0) {
for (int i = 0; i < arr.length; i++) {
Category category = catAPI.find(arr[i], user, false);
if(!cats.contains(category))
{
cats.add(category);
}
}
}
try{
currentContentlet.setInode(null);
ContentletRelationships contRel = retrieveRelationshipsData(currentContentlet,user, req );
// http://jira.dotmarketing.net/browse/DOTCMS-65
// Coming from other contentlet to relate it automatically
String relateWith = req.getParameter("relwith");
String relationType = req.getParameter("reltype");
String relationHasParent = req.getParameter("relisparent");
if(relateWith != null){
try {
List<ContentletRelationshipRecords> recordsList = contRel.getRelationshipsRecords();
for(ContentletRelationshipRecords records : recordsList) {
if(!records.getRelationship().getRelationTypeValue().equals(relationType))
continue;
if(RelationshipFactory.isSameStructureRelationship(records.getRelationship()) &&
((!records.isHasParent() && relationHasParent.equals("no")) ||
(records.isHasParent() && relationHasParent.equals("yes"))))
continue;
records.getRecords().add(conAPI.find(relateWith, user, false));
}
} catch (Exception e) {
Logger.error(this,"Contentlet failed while creating new relationship",e);
}
}
//Checkin in the content
currentContentlet = conAPI.checkin(currentContentlet, contRel, cats, _getSelectedPermissions(req, currentContentlet), user, false);
if ((subcmd != null) && subcmd.equals(com.dotmarketing.util.Constants.PUBLISH)) {
Logger.debug(this, "publishing after checkin");
ActivityLogger.logInfo(this.getClass(), "Publishing Contentlet "," User "+user.getFirstName()+" published content titled '"+currentContentlet.getTitle(), HostUtil.hostNameUtil(req, user));
APILocator.getVersionableAPI().setLive(currentContentlet);
}
}catch(DotContentletValidationException ve) {
if(ve.hasRequiredErrors()){
List<Field> reqs = ve.getNotValidFields().get(DotContentletValidationException.VALIDATION_FAILED_REQUIRED);
for (Field field : reqs) {
ae.add(Globals.ERROR_KEY, new ActionMessage("message.contentlet.required", field.getFieldName()));
}
}
if(ve.hasLengthErrors()){
List<Field> reqs = ve.getNotValidFields().get(DotContentletValidationException.VALIDATION_FAILED_MAXLENGTH);
for (Field field : reqs) {
ae.add(Globals.ERROR_KEY, new ActionMessage("message.contentlet.maxlength", field.getFieldName(),"255"));
}
}
if(ve.hasPatternErrors()){
List<Field> reqs = ve.getNotValidFields().get(DotContentletValidationException.VALIDATION_FAILED_PATTERN);
for (Field field : reqs) {
ae.add(Globals.ERROR_KEY, new ActionMessage("message.contentlet.format", field.getFieldName()));
}
}
if(ve.hasRelationshipErrors()){
StringBuffer sb = new StringBuffer("<br>");
Map<String,Map<Relationship,List<Contentlet>>> notValidRelationships = ve.getNotValidRelationship();
Set<String> auxKeys = notValidRelationships.keySet();
for(String key : auxKeys)
{
String errorMessage = "";
if(key.equals(DotContentletValidationException.VALIDATION_FAILED_REQUIRED_REL))
{
errorMessage = "<b>Required Relationship</b>";
}
else if(key.equals(DotContentletValidationException.VALIDATION_FAILED_INVALID_REL_CONTENT))
{
errorMessage = "<b>Invalid Relationship-Contentlet</b>";
}
else if(key.equals(DotContentletValidationException.VALIDATION_FAILED_BAD_REL))
{
errorMessage = "<b>Bad Relationship</b>";
}
sb.append(errorMessage + ":<br>");
Map<Relationship,List<Contentlet>> relationshipContentlets = notValidRelationships.get(key);
for(Entry<Relationship,List<Contentlet>> relationship : relationshipContentlets.entrySet())
{
sb.append(relationship.getKey().getRelationTypeValue() + ", ");
}
sb.append("<br>");
}
sb.append("<br>");
//need to update message to support multiple relationship validation errors
ae.add(Globals.ERROR_KEY, new ActionMessage("message.relationship.required_ext",sb.toString()));
}
if(ve.hasUniqueErrors()){
List<Field> reqs = ve.getNotValidFields().get(DotContentletValidationException.VALIDATION_FAILED_UNIQUE);
for (Field field : reqs) {
ae.add(Globals.ERROR_KEY, new ActionMessage("message.contentlet.unique", field.getFieldName()));
}
}
req.setAttribute(com.dotmarketing.util.WebKeys.CONTENTLET_RELATIONSHIPS_EDIT, getCurrentContentletRelationships(req, user));
throw ve;
}catch (Exception e) {
ae.add(Globals.ERROR_KEY, new ActionMessage("message.contentlet.save.error"));
Logger.error(this,"Contentlet failed during checkin",e);
throw e;
}finally{
if (ae != null && ae.size() > 0){
req.setAttribute(Globals.ERROR_KEY, ae);
}
}
req.setAttribute("inodeToWaitOn", currentContentlet.getInode());
req.setAttribute(WebKeys.CONTENTLET_EDIT, currentContentlet);
req.setAttribute(WebKeys.CONTENTLET_FORM_EDIT, currentContentlet);
if (Config.getBooleanProperty("CONTENT_CHANGE_NOTIFICATIONS") && !isNew)
_sendContentletPublishNotification(currentContentlet, reqImpl.getHttpServletRequest());
SessionMessages.add(httpReq, "message", "message.contentlet.save");
if( subcmd != null && subcmd.equals(com.dotmarketing.util.Constants.PUBLISH) ) {
SessionMessages.add(httpReq, "message", "message.contentlet.published");
}