001/*
002 * (C) Copyright 2006-2007 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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.webapp.documentsLists;
021
022import static org.jboss.seam.ScopeType.SESSION;
023
024import java.security.Principal;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.jboss.seam.annotations.Create;
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.annotations.Name;
031import org.jboss.seam.annotations.Observer;
032import org.jboss.seam.annotations.Scope;
033import org.jboss.seam.core.Events;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentRef;
037import org.nuxeo.ecm.webapp.helpers.EventNames;
038
039@Name("documentsListsManager")
040@Scope(SESSION)
041public class DocumentsListsManagerBean extends BaseDocumentsListsManager implements DocumentsListsManager {
042
043    private static final long serialVersionUID = 2895324573454635971L;
044
045    private Boolean initialized = false;
046
047    @In(create = true)
048    private ConversationDocumentsListsManager conversationDocumentsListsManager;
049
050    @In(create = true, required = false)
051    private transient CoreSession documentManager;
052
053    @In(create = true)
054    private transient Principal currentUser;
055
056    @Override
057    protected void notifyListUpdated(String listName) {
058        Events.instance().raiseEvent(listName + "Updated");
059    }
060
061    private DocumentRef lastDocumentRef;
062
063    @Create
064    public void initListManager() {
065        if (!initialized) {
066            super.setUserName(currentUser.getName());
067            List<String> listContribNames = getService().getDocumentsListDescriptorsName();
068            for (String listName : listContribNames) {
069                DocumentsListDescriptor desc = getService().getDocumentsListDescriptor(listName);
070
071                if (desc.getIsSession()) {
072                    super.createWorkingList(listName, desc, documentManager, currentUser.getName());
073                } else {
074                    // just store the descriptor
075                    documentsLists_descriptors.put(listName, desc);
076                }
077            }
078            initialized = true;
079        }
080    }
081
082    // Forward API
083    @Override
084    public void createWorkingList(String listName, DocumentsListDescriptor descriptor) {
085        if (descriptor.getIsSession()) {
086            super.createWorkingList(listName, descriptor);
087        } else {
088            conversationDocumentsListsManager.createWorkingList(listName, descriptor);
089            documentsLists_descriptors.put(listName, descriptor);
090        }
091    }
092
093    @Override
094    public List<DocumentModel> getWorkingList(String listName) {
095        if (isSessionOrIsNull(listName)) {
096            return super.getWorkingList(listName);
097        } else {
098            return conversationDocumentsListsManager.getWorkingList(listName);
099        }
100    }
101
102    @Override
103    public List<String> getWorkingListTypes(String listName) {
104        if (isSessionOrIsNull(listName)) {
105            return super.getWorkingListTypes(listName);
106        } else {
107            return conversationDocumentsListsManager.getWorkingListTypes(listName);
108        }
109    }
110
111    @Override
112    public void setWorkingList(String listName, List<DocumentModel> docList) {
113        if (isSessionOrIsNull(listName)) {
114            super.setWorkingList(listName, docList);
115        } else {
116            conversationDocumentsListsManager.setWorkingList(listName, docList);
117        }
118    }
119
120    @Override
121    public List<DocumentModel> addToWorkingList(String listName, DocumentModel doc) {
122        if (isSessionOrIsNull(listName)) {
123            return super.addToWorkingList(listName, doc);
124        } else {
125            return conversationDocumentsListsManager.addToWorkingList(listName, doc);
126        }
127    }
128
129    @Override
130    public List<DocumentModel> addToWorkingList(String listName, List<DocumentModel> docList) {
131        if (isSessionOrIsNull(listName)) {
132            return super.addToWorkingList(listName, docList);
133        } else {
134            return conversationDocumentsListsManager.addToWorkingList(listName, docList);
135        }
136    }
137
138    @Override
139    public List<DocumentModel> addToWorkingList(String listName, List<DocumentModel> docList, Boolean forceAppend) {
140        if (isSessionOrIsNull(listName)) {
141            return super.addToWorkingList(listName, docList, forceAppend);
142        } else {
143            return conversationDocumentsListsManager.addToWorkingList(listName, docList, forceAppend);
144        }
145    }
146
147    @Override
148    public List<DocumentModel> removeFromWorkingList(String listName, DocumentModel doc) {
149        if (isSessionOrIsNull(listName)) {
150            return super.removeFromWorkingList(listName, doc);
151        } else {
152            return conversationDocumentsListsManager.removeFromWorkingList(listName, doc);
153        }
154    }
155
156    @Override
157    public List<DocumentModel> removeFromWorkingList(String listName, List<DocumentModel> lst) {
158        if (isSessionOrIsNull(listName)) {
159            return super.removeFromWorkingList(listName, lst);
160        } else {
161            return conversationDocumentsListsManager.removeFromWorkingList(listName, lst);
162        }
163    }
164
165    @Override
166    public List<DocumentModel> resetWorkingList(String listName) {
167        if (isSessionOrIsNull(listName)) {
168            return super.resetWorkingList(listName);
169        } else {
170            return conversationDocumentsListsManager.resetWorkingList(listName);
171        }
172    }
173
174    @Override
175    public List<DocumentModel> resetWorkingList(String listName, List<DocumentModel> newDocList) {
176        if (isSessionOrIsNull(listName)) {
177            return super.resetWorkingList(listName, newDocList);
178        } else {
179            return conversationDocumentsListsManager.resetWorkingList(listName, newDocList);
180        }
181    }
182
183    @Override
184    public boolean isWorkingListEmpty(String listName) {
185        if (isSessionOrIsNull(listName)) {
186            return super.isWorkingListEmpty(listName);
187        } else {
188            return conversationDocumentsListsManager.isWorkingListEmpty(listName);
189        }
190    }
191
192    @Override
193    public void removeFromAllLists(List<DocumentModel> documentsToRemove) {
194        super.removeFromAllLists(documentsToRemove);
195        conversationDocumentsListsManager.removeFromAllLists(documentsToRemove);
196    }
197
198    @Override
199    public List<String> getWorkingListNamesForCategory(String categoryName) {
200        List<String> result = new ArrayList<String>();
201
202        result.addAll(super.getWorkingListNamesForCategory(categoryName));
203        result.addAll(conversationDocumentsListsManager.getWorkingListNamesForCategory(categoryName));
204        return result;
205    }
206
207    @Override
208    public DocumentsListDescriptor getWorkingListDescriptor(String listName) {
209        // Session level contains all the descriptors
210        return super.getWorkingListDescriptor(listName);
211    }
212
213    // Shortcut API
214    public List<DocumentModel> getWorkingList() {
215        return getWorkingList(DEFAULT_WORKING_LIST);
216    }
217
218    public DocumentsListDescriptor getWorkingListDescriptor() {
219        return getWorkingListDescriptor(DEFAULT_WORKING_LIST);
220    }
221
222    public List<String> getWorkingListTypes() {
223        return getWorkingListTypes(DEFAULT_WORKING_LIST);
224    }
225
226    public void setWorkingList(List<DocumentModel> docList) {
227        setWorkingList(DEFAULT_WORKING_LIST, docList);
228    }
229
230    public List<DocumentModel> addToWorkingList(DocumentModel doc) {
231        return addToWorkingList(DEFAULT_WORKING_LIST, doc);
232    }
233
234    public List<DocumentModel> addToWorkingList(List<DocumentModel> docList) {
235        return addToWorkingList(DEFAULT_WORKING_LIST, docList, false);
236    }
237
238    public List<DocumentModel> removeFromWorkingList(DocumentModel doc) {
239        return removeFromWorkingList(DEFAULT_WORKING_LIST, doc);
240    }
241
242    public List<DocumentModel> resetWorkingList() {
243        return resetWorkingList(DEFAULT_WORKING_LIST);
244    }
245
246    public List<DocumentModel> resetWorkingList(List<DocumentModel> newDocList) {
247        resetWorkingList();
248        return addToWorkingList(newDocList);
249    }
250
251    public boolean isWorkingListEmpty() {
252        return isWorkingListEmpty(DEFAULT_WORKING_LIST);
253    }
254
255    // Event listener
256    @Observer(value = { EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED }, create = false)
257    public void refreshLists(DocumentModel currentDocument) {
258
259        if (lastDocumentRef != null && lastDocumentRef.equals(currentDocument.getRef())) {
260            return;
261        }
262
263        if (!documentsLists_events.containsKey(EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED)) {
264            return;
265        }
266
267        for (String listName : documentsLists_events.get(EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED)) {
268            if (getWorkingList(listName) != null) {
269                documentsLists.get(listName).clear();
270                notifyListUpdated(listName);
271            }
272        }
273
274        lastDocumentRef = currentDocument.getRef();
275    }
276
277    /**
278     * Refresh lists when a search is performed
279     */
280    @Observer(value = { EventNames.SEARCH_PERFORMED }, create = false)
281    public void refreshListsOnSearch() {
282        if (!documentsLists_events.containsKey(EventNames.SEARCH_PERFORMED)) {
283            return;
284        }
285        for (String listName : documentsLists_events.get(EventNames.SEARCH_PERFORMED)) {
286            List<DocumentModel> docList = documentsLists.get(listName);
287            if (!docList.isEmpty()) {
288                docList.clear();
289                notifyListUpdated(listName);
290            }
291        }
292    }
293
294    private boolean isSessionOrIsNull(String listName) {
295        DocumentsListDescriptor desc = documentsLists_descriptors.get(listName);
296        return desc == null || desc.getIsSession();
297    }
298
299    private boolean isPersistent(String listName) {
300        DocumentsListDescriptor desc = documentsLists_descriptors.get(listName);
301        if (desc == null) {
302            return false;
303        } else {
304            return desc.getPersistent();
305        }
306    }
307
308}