001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.forum.web.api; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.GregorianCalendar; 027import java.util.List; 028 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.platform.comment.api.CommentableDocument; 031import org.nuxeo.ecm.platform.forum.workflow.ForumConstants; 032 033public class ThreadAdapterImpl implements ThreadAdapter, Serializable { 034 035 private static final long serialVersionUID = 1876878787587L; 036 037 private final DocumentModel threadDoc; 038 039 private List<DocumentModel> posts; 040 041 private List<DocumentModel> publishedPosts; 042 043 private List<DocumentModel> pendingPosts; 044 045 private DocumentModel lastPublishedPost; 046 047 public ThreadAdapterImpl(DocumentModel threadDoc) { 048 this.threadDoc = threadDoc; 049 } 050 051 private void fetchAllPosts() { 052 posts = getSubComments(threadDoc); 053 } 054 055 public List<DocumentModel> getAllPosts() { 056 if (posts == null) { 057 fetchAllPosts(); 058 } 059 return posts; 060 } 061 062 public List<DocumentModel> getPublishedPosts() { 063 if (publishedPosts == null) { 064 publishedPosts = new ArrayList<DocumentModel>(); 065 for (DocumentModel doc : getAllPosts()) { 066 if (ForumConstants.PUBLISHED_STATE.equals(doc.getCurrentLifeCycleState())) { 067 publishedPosts.add(doc); 068 } 069 } 070 } 071 return publishedPosts; 072 } 073 074 public List<DocumentModel> getPendingPosts() { 075 if (pendingPosts == null) { 076 pendingPosts = new ArrayList<DocumentModel>(); 077 for (DocumentModel doc : getAllPosts()) { 078 if (ForumConstants.PENDING_STATE.equals(doc.getCurrentLifeCycleState())) { 079 pendingPosts.add(doc); 080 } 081 } 082 } 083 return pendingPosts; 084 } 085 086 public DocumentModel getLastPublishedPost() { 087 if (lastPublishedPost == null) { 088 GregorianCalendar lastPostDate = null; 089 for (DocumentModel post : getPublishedPosts()) { 090 GregorianCalendar postDate = (GregorianCalendar) post.getProperty("post", "creationDate"); 091 092 if (lastPostDate == null || postDate.after(lastPostDate)) { 093 lastPostDate = postDate; 094 lastPublishedPost = post; 095 } 096 } 097 } 098 return lastPublishedPost; 099 } 100 101 protected List<DocumentModel> getSubComments(DocumentModel doc) { 102 List<DocumentModel> allSubPosts = new ArrayList<DocumentModel>(); 103 CommentableDocument commentDoc = doc.getAdapter(CommentableDocument.class); 104 105 if (commentDoc != null) { 106 List<DocumentModel> childComments = commentDoc.getComments(); 107 for (DocumentModel childComment : childComments) { 108 allSubPosts.add(childComment); 109 allSubPosts.addAll(getSubComments(childComment)); 110 } 111 } 112 return allSubPosts; 113 } 114 115 public DocumentModel getThreadDoc() { 116 return threadDoc; 117 } 118 119}