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