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 java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.platform.ui.web.util.DocumentsListsUtils;
032import org.nuxeo.runtime.api.Framework;
033
034public abstract class BaseDocumentsListsManager implements Serializable {
035
036    private static final long serialVersionUID = 98757690654316L;
037
038    private static DocumentsListsService dlService;
039
040    private transient DocumentsListsPersistenceManager persistenceManager;
041
042    // ListName => DocumentModel List
043    protected final Map<String, List<DocumentModel>> documentsLists = new HashMap<String, List<DocumentModel>>();
044
045    protected final Map<String, List<DocumentModel>> documentsListsPerConversation = new HashMap<String, List<DocumentModel>>();
046
047    // EventName => ListName
048    protected final Map<String, List<String>> documentsLists_events = new HashMap<String, List<String>>();
049
050    // ListName => List Descriptor
051    protected final Map<String, DocumentsListDescriptor> documentsLists_descriptors = new HashMap<String, DocumentsListDescriptor>();
052
053    protected DocumentsListsService getService() {
054        if (dlService == null) {
055            dlService = (DocumentsListsService) Framework.getRuntime().getComponent(DocumentsListsService.NAME);
056        }
057        return dlService;
058    }
059
060    protected String userName;
061
062    protected String getUserName() {
063        return userName;
064    }
065
066    protected void setUserName(String userName) {
067        this.userName = userName;
068    }
069
070    protected abstract void notifyListUpdated(String listName);
071
072    protected DocumentsListsPersistenceManager getPersistenceManager() {
073        if (persistenceManager == null) {
074            persistenceManager = new DocumentsListsPersistenceManager();
075        }
076
077        return persistenceManager;
078    }
079
080    public List<DocumentModel> resetWorkingList(String listName) {
081        if (!documentsLists.containsKey(listName)) {
082            return null;
083        }
084        List<DocumentModel> docList = getWorkingList(listName);
085        DocumentsListDescriptor desc = getWorkingListDescriptor(listName);
086        if (desc.getPersistent()) {
087            if (getPersistenceManager().clearPersistentList(userName, listName)) {
088                docList.clear();
089            }
090        } else {
091            docList.clear();
092        }
093        notifyListUpdated(listName);
094        return docList;
095    }
096
097    public boolean isWorkingListEmpty(String listName) {
098        if (!documentsLists.containsKey(listName)) {
099            return true;
100        }
101        List<DocumentModel> docList = getWorkingList(listName);
102
103        return docList.isEmpty();
104    }
105
106    public void removeFromAllLists(List<DocumentModel> documentsToRemove) {
107        for (String listName : documentsLists.keySet()) {
108            removeFromWorkingList(listName, documentsToRemove);
109            // DocumentsListsUtils.removeDocumentsFromList(
110            // getWorkingList(listName), documentsToRemove);
111            notifyListUpdated(listName);
112        }
113    }
114
115    public void createWorkingList(String listName, DocumentsListDescriptor descriptor) {
116        createWorkingList(listName, descriptor, null, null);
117    }
118
119    public void createWorkingList(String listName, DocumentsListDescriptor descriptor, CoreSession session,
120            String userName) {
121
122        if (documentsLists.containsKey(listName)) {
123            return;
124        }
125
126        if (descriptor != null && descriptor.getPersistent() && session != null && userName != null) {
127            // load persistent list
128            documentsLists.put(listName,
129                    getPersistenceManager().loadPersistentDocumentsLists(session, userName, listName));
130        } else {
131            // create empty list
132            documentsLists.put(listName, new ArrayList<DocumentModel>());
133        }
134
135        // create the descriptor
136        if (descriptor == null) {
137            descriptor = new DocumentsListDescriptor(listName);
138        }
139
140        documentsLists_descriptors.put(listName, descriptor);
141
142        // manage events subscriptions
143        for (String eventName : descriptor.getEventsName()) {
144            if (documentsLists_events.containsKey(eventName)) {
145                documentsLists_events.get(eventName).add(listName);
146            } else {
147                List<String> suscribersList = new ArrayList<String>();
148                suscribersList.add(listName);
149                documentsLists_events.put(eventName, suscribersList);
150            }
151        }
152    }
153
154    public List<String> getWorkingListNamesForCategory(String categoryName) {
155        List<String> res = new ArrayList<String>();
156
157        for (String listName : documentsLists_descriptors.keySet()) {
158            if (documentsLists_descriptors.get(listName).getCategory().equals(categoryName)) {
159                // default list in category is returned at start of the list !
160                if (documentsLists_descriptors.get(listName).getDefaultInCategory()) {
161                    res.add(0, listName);
162                } else {
163                    res.add(listName);
164                }
165            }
166        }
167        return res;
168    }
169
170    public List<DocumentModel> resetWorkingList(String listName, List<DocumentModel> newDocList) {
171        resetWorkingList(listName);
172        return addToWorkingList(listName, newDocList);
173    }
174
175    public List<DocumentModel> removeFromWorkingList(String listName, List<DocumentModel> lst) {
176        if (!documentsLists.containsKey(listName)) {
177            return null;
178        }
179        List<DocumentModel> docList = getWorkingList(listName);
180        DocumentsListDescriptor desc = getWorkingListDescriptor(listName);
181
182        // FIXME needs to be checked
183        for (DocumentModel doc : lst) {
184
185            if (desc.getPersistent()) {
186                if (getPersistenceManager().removeDocumentFromPersistentList(userName, listName, doc));
187                docList.remove(doc);
188            } else
189                docList.remove(doc);
190        }
191        notifyListUpdated(listName);
192        return docList;
193    }
194
195    public List<DocumentModel> removeFromWorkingList(String listName, DocumentModel doc) {
196        if (!documentsLists.containsKey(listName)) {
197            return null;
198        }
199        List<DocumentModel> docList = getWorkingList(listName);
200        DocumentsListDescriptor desc = getWorkingListDescriptor(listName);
201
202        // FIXME needs to be checked
203        if (desc.getPersistent()) {
204            if (getPersistenceManager().removeDocumentFromPersistentList(userName, listName, doc));
205            docList.remove(doc);
206        } else
207            docList.remove(doc);
208        notifyListUpdated(listName);
209        return docList;
210    }
211
212    public List<DocumentModel> addToWorkingList(String listName, List<DocumentModel> docList) {
213        return addToWorkingList(listName, docList, false);
214    }
215
216    public List<DocumentModel> addToWorkingList(String listName, List<DocumentModel> docList, Boolean forceAppend) {
217        if (!documentsLists.containsKey(listName)) {
218            return null;
219        }
220
221        List<DocumentModel> currentDocList = getWorkingList(listName);
222        DocumentsListDescriptor currentDescriptor = getWorkingListDescriptor(listName);
223        Boolean currentListIsPersistent = false;
224
225        if (currentDescriptor != null) {
226            if (!forceAppend && !getWorkingListDescriptor(listName).getSupportAppends()) {
227                currentDocList.clear();
228            }
229
230            currentListIsPersistent = currentDescriptor.getPersistent();
231        }
232
233        // filter for duplicate
234        List<DocumentRef> docRefList = DocumentsListsUtils.getDocRefs(currentDocList);
235        for (DocumentModel doc : docList) {
236            if (!docRefList.contains(doc.getRef())) {
237                if (currentListIsPersistent) {
238                    if (getPersistenceManager().addDocumentToPersistentList(userName, listName, doc)) {
239                        // Strange, CHECKME;
240                    }
241                    currentDocList.add(doc);
242                } else {
243                    currentDocList.add(doc);
244                }
245            }
246        }
247        notifyListUpdated(listName);
248        return currentDocList;
249    }
250
251    public List<DocumentModel> addToWorkingList(String listName, DocumentModel doc) {
252        if (!documentsLists.containsKey(listName)) {
253            return null;
254        }
255
256        List<DocumentModel> docList = getWorkingList(listName);
257        DocumentsListDescriptor currentDescriptor = getWorkingListDescriptor(listName);
258        boolean currentListIsPersistent = false;
259
260        if (currentDescriptor != null) {
261            currentListIsPersistent = currentDescriptor.getPersistent();
262        }
263
264        List<DocumentRef> docRefList = DocumentsListsUtils.getDocRefs(docList);
265        if (!docRefList.contains(doc.getRef())) {
266            if (currentListIsPersistent) {
267                if (getPersistenceManager().addDocumentToPersistentList(userName, listName, doc)) {
268                    // Strange, CHECKME;
269                }
270                docList.add(doc);
271            } else {
272                docList.add(doc);
273            }
274        }
275        notifyListUpdated(listName);
276        return docList;
277    }
278
279    public void setWorkingList(String listName, List<DocumentModel> docList) {
280        if (documentsLists.containsKey(listName)) {
281            documentsLists.put(listName, docList);
282        }
283    }
284
285    public List<String> getWorkingListTypes(String listName) {
286        if (!documentsLists.containsKey(listName)) {
287            return null;
288        }
289
290        List<String> res = new ArrayList<String>();
291        for (DocumentModel doc : documentsLists.get(listName)) {
292            String dt = doc.getType();
293            if (!res.contains(dt)) {
294                res.add(dt);
295            }
296        }
297        return res;
298    }
299
300    public DocumentsListDescriptor getWorkingListDescriptor(String listName) {
301        if (!documentsLists.containsKey(listName)) {
302            return null;
303        }
304        return documentsLists_descriptors.get(listName);
305    }
306
307    public List<DocumentModel> getWorkingList(String listName) {
308        if (documentsLists.containsKey(listName)) {
309            return documentsLists.get(listName);
310        } else {
311            return null;
312        }
313    }
314
315}