return forward;
}
public ActionForward saveComments(ActionMapping mapping, ActionForm lf, HttpServletRequest request, HttpServletResponse response) {
CommentsForm commentsForm = (CommentsForm) lf;
request.getSession().setAttribute("commentsForm", commentsForm);
ActionErrors ae = commentsForm.validate(mapping, request);
HashMap<String,String> commentsOptions = (HashMap<String,String>) request.getSession().getAttribute("commentsOptions");
try {
HibernateUtil.startTransaction();
if ((ae != null) && (ae.size() > 0)) {
String referrer = UtilMethods.isSet(commentsOptions.get("referrer"))?commentsOptions.get("referrer"):"";
if(referrer != null){
referrer=referrer.replaceAll("#comments", "#comments");
if(referrer.indexOf("?") > -1){
referrer = referrer + "&dotcache=no";
}else{
referrer = referrer + "?dotcache=no";
}
}
referrer = (referrer.indexOf("#comments") == -1 ? referrer + "#comments" : referrer);
saveMessages(request, ae);
saveMessages(request.getSession(), ae);
ActionForward forward = new ActionForward(SecurityUtils.stripReferer(request, referrer));
forward.setRedirect(true);
return forward;
}
User user = APILocator.getUserAPI().getSystemUser();
if (request.getSession().getAttribute(WebKeys.CMS_USER) != null) {
user = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
}
String userId = user.getUserId();
// Contentlet Data
Contentlet contentlet = new Contentlet();
try{
contentlet = conAPI.find(commentsOptions.get("contentInode"), user, true);
}catch(DotDataException e){
Logger.error(this, "Unable to look up comment with inode " + commentsOptions.get("contentInode"), e);
}
Structure contentletStructure = StructureCache.getStructureByInode(contentlet.getStructureInode());
Identifier contentletIdentifier = APILocator.getIdentifierAPI().find(contentlet);
/*make sure we have a structure in place before saving */
CommentsWebAPI cAPI = new CommentsWebAPI();
cAPI.validateComments(contentlet.getInode());
Structure commentsStructure = StructureCache.getStructureByVelocityVarName(CommentsWebAPI.commentsVelocityStructureName);
Contentlet contentletComment = new Contentlet();
// set the default language
com.dotmarketing.portlets.contentlet.business.Contentlet beanContentlet = (com.dotmarketing.portlets.contentlet.business.Contentlet) InodeFactory.getInode(contentlet.getInode(), com.dotmarketing.portlets.contentlet.business.Contentlet.class);
contentletComment.setLanguageId(beanContentlet.getLanguageId());
// Add the default fields
contentletComment.setStructureInode(commentsStructure.getInode());
Field field;
/* Set the title if we have one*/
if(UtilMethods.isSet(commentsOptions.get("commentTitle"))){
field = commentsStructure.getFieldVar("title");
conAPI.setContentletProperty(contentletComment, field, commentsOptions.get("commentTitle"));
}
/* Validate if a CommentsCount field exists in the contentlet structure
if not, then create it and populate it.*/
field = contentletStructure.getFieldVar("commentscount");
if (field==null || !InodeUtils.isSet(field.getInode())) {
List<Field> fields = new ArrayList<Field>();
field = new Field("CommentsCount", Field.FieldType.TEXT, Field.DataType.INTEGER, contentletStructure,
false, false, true, Integer.MAX_VALUE, "0", "0", "",true, true, true);
FieldFactory.saveField(field);
for(Field structureField: contentletStructure.getFields()){
fields.add(structureField);
}
fields.add(field);
FieldsCache.removeFields(contentletStructure);
FieldsCache.addFields(contentletStructure,fields);
}
/* Get the value from the CommentsCount field for this contentlet, if the value
* is null, then the contentlet has no comments, otherwise increment its value by one
* and set it to the contentlet.
*/
String velVar = field.getVelocityVarName();
int comentsCount = -1;
try {
Long countValue = contentlet.getLongProperty(velVar);
comentsCount = countValue.intValue();
} catch (Exception e) {
Logger.debug(this, e.toString());
}
if (comentsCount == -1) {
try {
String countValue = (contentlet.getStringProperty(velVar) == null) ? field.getDefaultValue() : contentlet.getStringProperty(velVar);
comentsCount = countValue.equals("") ? 0 : new Integer(countValue).intValue();
} catch (Exception e) {
Logger.debug(this, e.toString());
}
}
++comentsCount;
conAPI.setContentletProperty(contentlet, field, comentsCount);
//Update the contentlet with the new comment count
/*List<Category> cats = catAPI.getParents(contentlet, user, true);
Map<Relationship, List<Contentlet>> contentRelationships = new HashMap<Relationship, List<Contentlet>>();
List<Relationship> rels = RelationshipFactory.getAllRelationshipsByStructure(contentlet.getStructure());
for (Relationship r : rels) {
if(!contentRelationships.containsKey(r)){
contentRelationships.put(r, new ArrayList<Contentlet>());
}
List<Contentlet> cons = conAPI.getRelatedContent(contentlet, r, user, true);
for (Contentlet co : cons) {
List<Contentlet> l2 = contentRelationships.get(r);
l2.add(co);
}
}
conAPI.checkinWithoutVersioning(contentlet, contentRelationships, cats, APILocator.getPermissionAPI().getPermissions(contentlet), user, true);
*/
// Date
field = commentsStructure.getFieldVar("datePublished");
conAPI.setContentletProperty(contentletComment, field, new Date());
// User Id
field = commentsStructure.getFieldVar("userid");
conAPI.setContentletProperty(contentletComment, field, userId);
// Author
field = commentsStructure.getFieldVar("author");
conAPI.setContentletProperty(contentletComment, field, VelocityUtil.cleanVelocity(commentsForm.getName()));
// Email
field = commentsStructure.getFieldVar("email");
conAPI.setContentletProperty(contentletComment, field, VelocityUtil.cleanVelocity(commentsForm.getEmail()));
// WebSite
field = commentsStructure.getFieldVar("website");
conAPI.setContentletProperty(contentletComment, field, VelocityUtil.cleanVelocity(commentsForm.getWebsite()));
// EmailResponse
field = commentsStructure.getFieldVar("emailResponse");
conAPI.setContentletProperty(contentletComment, field, (commentsForm.isNotify()?"yes":"no"));
// IP Address
field = commentsStructure.getFieldVar("ipAddress");
conAPI.setContentletProperty(contentletComment, field, request.getRemoteAddr());
// Comment
field = commentsStructure.getFieldVar("comment");
String comment = commentsForm.getComment();
comment=VelocityUtil.cleanVelocity(comment);
if (UtilMethods.isSet(commentsOptions.get("commentStripHtml")) && commentsOptions.get("commentStripHtml").equalsIgnoreCase("true")) {
comment = Html.stripHtml(comment);
}
conAPI.setContentletProperty(contentletComment, field, comment);
// Add the permission
PermissionAPI perAPI = APILocator.getPermissionAPI();
List<Permission> pers = perAPI.getPermissions(commentsStructure);
// new workflows
if(UtilMethods.isSet(commentsOptions.get("commentsModeration"))){
if(!UtilMethods.isSet(contentletComment.getStringProperty(Contentlet.WORKFLOW_ACTION_KEY)))
contentletComment.setStringProperty(Contentlet.WORKFLOW_ACTION_KEY, APILocator.getWorkflowAPI().findEntryAction(contentletComment, user).getId());
contentletComment.setStringProperty(Contentlet.WORKFLOW_COMMENTS_KEY, commentsForm.getComment());
}
// Save the comment
contentletComment = conAPI.checkin(contentletComment, new HashMap<Relationship, List<Contentlet>>(), new ArrayList<Category>(), pers, user, true);
// If live I have to publish the asset
if (UtilMethods.isSet(commentsOptions.get("commentAutoPublish")) && commentsOptions.get("commentAutoPublish").equalsIgnoreCase("true")) {
APILocator.getVersionableAPI().setLive(contentletComment);
}
// Set the relation between the content and the comments
Identifier contentletCommentIdentifier = APILocator.getIdentifierAPI().find(contentletComment);
String commentRelationStructureName = commentsStructure.getName().replaceAll("\\s", "_").replaceAll("[^a-zA-Z0-9\\_]", "");
String contentletRelationStructureName = contentletStructure.getName().replaceAll("\\s", "_").replaceAll("[^a-zA-Z0-9\\_]", "");
String relationName = contentletRelationStructureName + "-" + commentRelationStructureName;
/* get the next in the order */
int order = RelationshipFactory.getMaxInSortOrder(contentletIdentifier.getInode(), relationName);
contentletIdentifier.addChild(contentletCommentIdentifier, relationName, ++order);
ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.comment.success"));
/* If the comment has been sennt successfully, activeDiv is set to its initial value*/
commentsForm.setActiveDiv("");
beanContentlet = (com.dotmarketing.portlets.contentlet.business.Contentlet) InodeFactory.getInode(contentletComment.getInode(), com.dotmarketing.portlets.contentlet.business.Contentlet.class);
beanContentlet = conAPI.convertContentletToFatContentlet(contentletComment, beanContentlet);
HibernateUtil.saveOrUpdate(beanContentlet);
if (UtilMethods.isSet(commentsOptions.get("commentAutoPublish")) && commentsOptions.get("commentAutoPublish").equalsIgnoreCase("true"))
{
conAPI.publish(contentletComment, user, true);
}
saveMessages(request, ae);
saveMessages(request.getSession(), ae);
String referrer = UtilMethods.isSet(commentsOptions.get("referrer"))?commentsOptions.get("referrer"):"";
referrer = (referrer.indexOf("#comments") == -1 ? referrer + "#comments" : referrer);
String commentQuestion = commentsOptions.get("commentTitle");
//The send comment emails should be send when there is a new comment (ALWAYS)
String userName = commentsForm.getName();
String userEmail = commentsForm.getEmail();
String userComment = commentsForm.getComment();
HibernateUtil.commitTransaction();
if(!conAPI.isInodeIndexed(contentletComment.getInode())){
Logger.error(this, "Problem indexing comment content");
}
sendCommentEmails(contentletIdentifier, relationName, request, referrer,userName,userEmail,userComment,commentQuestion, commentsOptions);
ActionForward forward = new ActionForward(referrer);
forward.setRedirect(true);
/* reset the form and reset the captcha */
commentsForm.reset();
request.setAttribute("commentsForm", commentsForm);
return forward;
} catch (Exception ex) {
try {