001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.forum.web.api;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.GregorianCalendar;
025import java.util.List;
026
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.platform.comment.api.CommentableDocument;
029import org.nuxeo.ecm.platform.forum.workflow.ForumConstants;
030
031public class ThreadAdapterImpl implements ThreadAdapter, Serializable {
032
033    private static final long serialVersionUID = 1876878787587L;
034
035    private final DocumentModel threadDoc;
036
037    private List<DocumentModel> posts;
038
039    private List<DocumentModel> publishedPosts;
040
041    private List<DocumentModel> pendingPosts;
042
043    private DocumentModel lastPublishedPost;
044
045    public ThreadAdapterImpl(DocumentModel threadDoc) {
046        this.threadDoc = threadDoc;
047    }
048
049    private void fetchAllPosts() {
050        posts = getSubComments(threadDoc);
051    }
052
053    public List<DocumentModel> getAllPosts() {
054        if (posts == null) {
055            fetchAllPosts();
056        }
057        return posts;
058    }
059
060    public List<DocumentModel> getPublishedPosts() {
061        if (publishedPosts == null) {
062            publishedPosts = new ArrayList<DocumentModel>();
063            for (DocumentModel doc : getAllPosts()) {
064                if (ForumConstants.PUBLISHED_STATE.equals(doc.getCurrentLifeCycleState())) {
065                    publishedPosts.add(doc);
066                }
067            }
068        }
069        return publishedPosts;
070    }
071
072    public List<DocumentModel> getPendingPosts() {
073        if (pendingPosts == null) {
074            pendingPosts = new ArrayList<DocumentModel>();
075            for (DocumentModel doc : getAllPosts()) {
076                if (ForumConstants.PENDING_STATE.equals(doc.getCurrentLifeCycleState())) {
077                    pendingPosts.add(doc);
078                }
079            }
080        }
081        return pendingPosts;
082    }
083
084    public DocumentModel getLastPublishedPost() {
085        if (lastPublishedPost == null) {
086            GregorianCalendar lastPostDate = null;
087            for (DocumentModel post : getPublishedPosts()) {
088                GregorianCalendar postDate = (GregorianCalendar) post.getProperty("post", "creationDate");
089
090                if (lastPostDate == null || postDate.after(lastPostDate)) {
091                    lastPostDate = postDate;
092                    lastPublishedPost = post;
093                }
094            }
095        }
096        return lastPublishedPost;
097    }
098
099    protected List<DocumentModel> getSubComments(DocumentModel doc) {
100        List<DocumentModel> allSubPosts = new ArrayList<DocumentModel>();
101        CommentableDocument commentDoc = doc.getAdapter(CommentableDocument.class);
102
103        if (commentDoc != null) {
104            List<DocumentModel> childComments = commentDoc.getComments();
105            for (DocumentModel childComment : childComments) {
106                allSubPosts.add(childComment);
107                allSubPosts.addAll(getSubComments(childComment));
108            }
109        }
110        return allSubPosts;
111    }
112
113    public DocumentModel getThreadDoc() {
114        return threadDoc;
115    }
116
117}