001: package org.emforge.comment;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Date;
006:
007: import javax.jws.WebService;
008:
009: import org.acegisecurity.AccessDeniedException;
010: import org.apache.commons.collections.CollectionUtils;
011: import org.emforge.BpmService;
012: import org.emforge.CommentService;
013: import org.emforge.EmForgeException;
014: import org.emforge.xfer.CommentTO;
015: import org.emforge.xfer.TaskTO;
016: import org.springframework.transaction.annotation.Propagation;
017: import org.springframework.transaction.annotation.Transactional;
018:
019: import ru.emdev.EmForge.security.UserFactory;
020:
021: import com.ecyrd.jspwiki.WikiEngine;
022:
023: /** Implementation of Comment Service
024: *
025: * @todo Move it into org.emforge package
026: */
027: @WebService(endpointInterface="org.emforge.CommentService")
028: @Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=EmForgeException.class)
029: public class CommentServiceImpl implements CommentService {
030:
031: protected CommentDAOHinernateImpl m_commentDao;
032: protected BpmService m_bpmService;
033: protected WikiEngine m_wikiEngine;
034: protected UserFactory m_userFactory;
035:
036: public void setWikiEngine(WikiEngine engine) {
037: m_wikiEngine = engine;
038: }
039:
040: public void setCommentDao(CommentDAOHinernateImpl dao) {
041: m_commentDao = dao;
042: }
043:
044: public void setBpmService(BpmService service) {
045: m_bpmService = service;
046: }
047:
048: public void setUserFactory(UserFactory i_userFactory) {
049: m_userFactory = i_userFactory;
050: }
051:
052: /** Store Comment */
053: public boolean addComment(String parentName, String commentText)
054: throws EmForgeException {
055: if (!canAddComment(parentName)) {
056: throw new AccessDeniedException(
057: "You are not allowed to add comment");
058: }
059:
060: TaskTO task = null;
061: try {
062: task = m_bpmService.getTask(Long.parseLong(parentName));
063: } catch (NumberFormatException nfe) {
064: // just ignore it
065: }
066:
067: if (task != null) {
068: m_bpmService.addComment(task.getId(), commentText);
069: } else {
070: // check- is page exists (we may already pass it into this method)
071: if (m_wikiEngine.getPage(parentName) != null) {
072:
073: CommentDO comment = new CommentDO();
074: comment.setMessage(commentText);
075: comment.setActorId(m_userFactory.getCurrentUser()
076: .getUsername());
077: comment.setTime(new Date());
078: comment.setPageName(parentName);
079:
080: m_commentDao.saveComment(comment);
081: m_commentDao.getHibernateTemplate().flush();
082: } else {
083: throw new IllegalArgumentException(
084: "There is no such wiki page: " + parentName);
085: }
086: }
087:
088: return true;
089: }
090:
091: public CommentTO[] getComments(String parentName)
092: throws EmForgeException {
093: // first - try - is it Task name?
094: Long taskId = null;
095: try {
096: taskId = Long.parseLong(parentName);
097: } catch (NumberFormatException nfe) {
098: }
099:
100: if (taskId != null && m_bpmService.hasTask(taskId) != null) {
101: // yes, it is task id - get comments from BPM service
102: return m_bpmService.getTaskComments(taskId);
103: } else {
104: // no, it is not task - get comments from database
105: Collection<CommentDO> comments = m_commentDao
106: .getCommentsByPageName(parentName);
107:
108: // convert TO to DO
109: CommentTransformer transformer = new CommentTransformerImpl(
110: m_wikiEngine, parentName);
111: Collection<CommentTO> outputCollection = new ArrayList<CommentTO>();
112: CollectionUtils.collect(comments, transformer,
113: outputCollection);
114:
115: // return result
116: return outputCollection
117: .toArray(new CommentTO[outputCollection.size()]);
118: }
119: }
120:
121: /** Is current user allowed to add comment?
122: *
123: **/
124: public boolean canAddComment(String parentName) {
125: Long taskId = null;
126: try {
127: taskId = Long.parseLong(parentName);
128: } catch (NumberFormatException nfe) {
129: }
130:
131: if (taskId != null && m_bpmService.hasTask(taskId) != null) {
132: return m_bpmService.canCommentTask(taskId);
133: }
134:
135: // non-anonymous can add comments
136: if (m_userFactory.getCurrentUser() != null
137: && !m_userFactory.getCurrentUser().isAnonymous()) {
138: return true;
139: } else {
140: return false;
141: }
142: }
143: }
|