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