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