001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.cms.repository.comment.generators; 018 019import java.io.IOException; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.generation.ServiceableGenerator; 028import org.apache.cocoon.xml.AttributesImpl; 029import org.apache.cocoon.xml.XMLUtils; 030import org.apache.commons.lang.StringUtils; 031import org.xml.sax.SAXException; 032 033import org.ametys.cms.content.ContentHelper; 034import org.ametys.cms.repository.Content; 035import org.ametys.cms.repository.comment.Comment; 036import org.ametys.cms.repository.comment.CommentableContent; 037import org.ametys.core.right.RightManager; 038import org.ametys.core.right.RightManager.RightResult; 039import org.ametys.core.user.CurrentUserProvider; 040import org.ametys.core.user.UserIdentity; 041import org.ametys.core.util.DateUtils; 042import org.ametys.plugins.repository.AmetysObjectResolver; 043 044/** 045 * Generates the comments of the given contents 046 */ 047public class CommentsGenerator extends ServiceableGenerator 048{ 049 private AmetysObjectResolver _resolver; 050 private RightManager _rightManager; 051 private CurrentUserProvider _currentUserProvider; 052 private ContentHelper _contentHelper; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 061 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 062 _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE); 063 } 064 065 @Override 066 public void generate() throws IOException, SAXException, ProcessingException 067 { 068 @SuppressWarnings("unchecked") 069 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 070 @SuppressWarnings("unchecked") 071 List<String> contentIds = (List<String>) jsParameters.get("contentIds"); 072 073 String commentId = (String) jsParameters.get("commentId"); 074 075 UserIdentity user = _currentUserProvider.getUser(); 076 077 contentHandler.startDocument(); 078 XMLUtils.startElement(contentHandler, "comments"); 079 080 for (String contentId : contentIds) 081 { 082 Content content = _resolver.resolveById(contentId); 083 084 if (content instanceof CommentableContent) 085 { 086 if (_rightManager.hasRight(user, "CMS_Rights_CommentModerate", content) == RightResult.RIGHT_ALLOW) 087 { 088 CommentableContent commentableContent = (CommentableContent) content; 089 List<Comment> comments = commentableContent.getComments(true, true); 090 091 for (Comment comment : comments) 092 { 093 if (StringUtils.isNotEmpty(commentId) && !commentId.equals(comment.getId())) 094 { 095 continue; 096 } 097 098 _saxComment(comment, content); 099 } 100 } 101 } 102 } 103 104 XMLUtils.endElement(contentHandler, "comments"); 105 contentHandler.endDocument(); 106 } 107 108 /** 109 * SAX a comment 110 * @param comment The comment 111 * @param content The content 112 * @throws SAXException if an error occurred while saxing 113 */ 114 protected void _saxComment (Comment comment, Content content) throws SAXException 115 { 116 AttributesImpl attrs = new AttributesImpl(); 117 attrs.addCDATAAttribute("id", comment.getId()); 118 attrs.addCDATAAttribute("validated", Boolean.toString(comment.isValidated())); 119 120 XMLUtils.startElement(contentHandler, "comment", attrs); 121 122 _saxContent (content); 123 124 XMLUtils.createElement(contentHandler, "creationDate", DateUtils.zonedDateTimeToString(comment.getCreationDate())); 125 126 String authorName = comment.getAuthorName(); 127 if (authorName != null) 128 { 129 XMLUtils.createElement(contentHandler, "author-name", authorName); 130 } 131 132 String authorEmail = comment.getAuthorEmail(); 133 if (authorEmail != null) 134 { 135 AttributesImpl emailAttrs = new AttributesImpl(); 136 emailAttrs.addCDATAAttribute("hidden", Boolean.toString(comment.isEmailHidden())); 137 XMLUtils.createElement(contentHandler, "author-email", emailAttrs, authorEmail); 138 } 139 140 String authorUrl = comment.getAuthorURL(); 141 if (authorUrl != null) 142 { 143 XMLUtils.createElement(contentHandler, "author-url", authorUrl); 144 } 145 146 String text = comment.getContent(); 147 if (text != null) 148 { 149 XMLUtils.createElement(contentHandler, "text", comment.getContent()); 150 } 151 XMLUtils.endElement(contentHandler, "comment"); 152 } 153 154 /** 155 * SAX a content 156 * @param content The content 157 * @throws SAXException if an error occurred while saxing 158 */ 159 protected void _saxContent (Content content) throws SAXException 160 { 161 AttributesImpl attrs = new AttributesImpl(); 162 attrs.addCDATAAttribute("id", content.getId()); 163 attrs.addCDATAAttribute("title", _contentHelper.getTitle(content)); 164 attrs.addCDATAAttribute("name", content.getName()); 165 166 XMLUtils.createElement(contentHandler, "content", attrs); 167 } 168}