001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (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
016 *     Antoine Taillefer
017 */
018
019package org.nuxeo.ecm.webapp.documentsLists;
020
021import static org.jboss.seam.ScopeType.CONVERSATION;
022
023import java.io.Serializable;
024import java.util.List;
025
026import org.jboss.seam.annotations.Create;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Observer;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.core.Events;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentRef;
033import org.nuxeo.ecm.webapp.helpers.EventNames;
034
035@Name("conversationDocumentsListsManager")
036@Scope(CONVERSATION)
037public class ConversationDocumentsListsManager extends BaseDocumentsListsManager implements Serializable {
038
039    private static final long serialVersionUID = 9876098763432L;
040
041    private Boolean initialized = false;
042
043    private DocumentRef lastDocumentRef;
044
045    @Override
046    protected void notifyListUpdated(String listName) {
047        Events.instance().raiseEvent(listName + "Updated");
048    }
049
050    @Create
051    public void initListManager() {
052        if (!initialized) {
053            List<String> listContribNames = getService().getDocumentsListDescriptorsName();
054            for (String listName : listContribNames) {
055                DocumentsListDescriptor desc = getService().getDocumentsListDescriptor(listName);
056                if (!desc.getIsSession()) {
057                    createWorkingList(listName, desc);
058                }
059            }
060            initialized = true;
061        }
062    }
063
064    // Event listeners
065    @Observer(value = { EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED }, create = false)
066    public void refreshLists(DocumentModel selectedDocument) {
067
068        if (selectedDocument != null) {
069            refreshLists(EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED, selectedDocument);
070        }
071    }
072
073    /**
074     * @since 5.6
075     */
076    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED }, create = false)
077    public void refreshListsOnDocumentSelectionChanged(DocumentModel selectedDocument) {
078
079        if (selectedDocument != null) {
080            refreshLists(EventNames.DOCUMENT_SELECTION_CHANGED, selectedDocument);
081        }
082    }
083
084    /**
085     * @since 5.6
086     */
087    public void refreshLists(String eventName, DocumentModel selectedDocument) {
088
089        if (lastDocumentRef != null && lastDocumentRef.equals(selectedDocument.getRef())) {
090            return;
091        }
092
093        if (!documentsLists_events.containsKey(eventName)) {
094            return;
095        }
096        for (String listName : documentsLists_events.get(eventName)) {
097
098            List<DocumentModel> docList = documentsLists.get(listName);
099            if (!docList.isEmpty()) {
100                docList.clear();
101                notifyListUpdated(listName);
102            }
103        }
104
105        lastDocumentRef = selectedDocument.getRef();
106    }
107
108    /**
109     * Refresh lists when a search is performed
110     */
111    @Observer(value = { EventNames.SEARCH_PERFORMED }, create = false)
112    public void refreshListsOnSearch() {
113        if (!documentsLists_events.containsKey(EventNames.SEARCH_PERFORMED)) {
114            return;
115        }
116        for (String listName : documentsLists_events.get(EventNames.SEARCH_PERFORMED)) {
117            List<DocumentModel> docList = documentsLists.get(listName);
118            if (!docList.isEmpty()) {
119                docList.clear();
120                notifyListUpdated(listName);
121            }
122        }
123    }
124
125}