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     * Refresh lists when main tab is changed
088     *
089     * @since 10.3
090     */
091    @Observer(value = { EventNames.MAIN_TABS_CHANGED }, create = false)
092    public void refreshListsOnMainTabsChanged() {
093        refreshLists(EventNames.MAIN_TABS_CHANGED);
094    }
095
096    /**
097     * @since 5.6
098     */
099    public void refreshLists(String eventName, DocumentModel selectedDocument) {
100
101        if (lastDocumentRef != null && lastDocumentRef.equals(selectedDocument.getRef())) {
102            return;
103        }
104
105        refreshLists(eventName);
106
107        lastDocumentRef = selectedDocument.getRef();
108    }
109
110    /**
111     * Refresh lists for the event in parameter
112     *
113     * @since 10.3
114     */
115    protected void refreshLists(String eventName) {
116        if (!documentsLists_events.containsKey(eventName)) {
117            return;
118        }
119        for (String listName : documentsLists_events.get(eventName)) {
120
121            List<DocumentModel> docList = documentsLists.get(listName);
122            if (!docList.isEmpty()) {
123                docList.clear();
124                notifyListUpdated(listName);
125            }
126        }
127    }
128
129    /**
130     * Refresh lists when a search is performed
131     */
132    @Observer(value = { EventNames.SEARCH_PERFORMED }, create = false)
133    public void refreshListsOnSearch() {
134        if (!documentsLists_events.containsKey(EventNames.SEARCH_PERFORMED)) {
135            return;
136        }
137        for (String listName : documentsLists_events.get(EventNames.SEARCH_PERFORMED)) {
138            List<DocumentModel> docList = documentsLists.get(listName);
139            if (!docList.isEmpty()) {
140                docList.clear();
141                notifyListUpdated(listName);
142            }
143        }
144    }
145
146}