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.platform.ui.web.util;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029
030/**
031 * Simple utility class to provide DocumentList related functions.
032 *
033 * @author tiry
034 */
035public final class DocumentsListsUtils {
036
037    private static final Log log = LogFactory.getLog(DocumentsListsUtils.class);
038
039    // Utility class.
040    private DocumentsListsUtils() {
041    }
042
043    /**
044     * Returns list of the document types contained in the list of document.
045     */
046    public static List<String> getTypesInList(List<DocumentModel> documentsList) {
047        List<String> res = new ArrayList<String>();
048        for (DocumentModel doc : documentsList) {
049            String dt = doc.getType();
050            if (!res.contains(dt)) {
051                res.add(dt);
052            }
053        }
054        return res;
055    }
056
057    /**
058     * Returns list of DocumentRef corresponding to the list of documents.
059     */
060    public static List<DocumentRef> getDocRefs(List<DocumentModel> documentsList) {
061        List<DocumentRef> references = new ArrayList<DocumentRef>();
062
063        for (DocumentModel docModel : documentsList) {
064            references.add(docModel.getRef());
065        }
066
067        return references;
068    }
069
070    /**
071     * Removes one document from a list.
072     * <p>
073     * Removal is based on DocumentRef.
074     *
075     * @return <code>true</code> if the given list contains specified document and it has been removed
076     */
077    public static boolean removeDocumentFromList(List<DocumentModel> documentList, DocumentModel documentToRemove) {
078        if (null == documentToRemove) {
079            return false;
080        }
081        try {
082            boolean found = false;
083            for (int i = 0; i < documentList.size(); i++) {
084                if (documentList.get(i).getRef().equals(documentToRemove.getRef())) {
085                    documentList.remove(i);
086                    found = true;
087                }
088            }
089            return found;
090        } catch (UnsupportedOperationException e) {
091            // XXX: maybe throw a checked exception
092            log.error("immutable list, cannot remove document: " + documentToRemove, e);
093            return false;
094        }
095    }
096
097    /**
098     * Removes some documents from a list.
099     * <p>
100     * Removal is based on DocumentRef.
101     */
102    public static void removeDocumentsFromList(List<DocumentModel> documentList, List<DocumentModel> documentsToRemove) {
103        if (null == documentsToRemove || documentsToRemove.isEmpty()) {
104            return;
105        }
106
107        if (null == documentList || documentList.isEmpty()) {
108            return;
109        }
110
111        for (DocumentModel documentToRemove : documentsToRemove) {
112            for (int i = 0; i < documentList.size(); i++) {
113                if (documentList.get(i).getRef().equals(documentToRemove.getRef())) {
114                    documentList.remove(i);
115                }
116            }
117        }
118    }
119
120    /**
121     * Returns the list of parents documentRef.
122     */
123    public static List<DocumentRef> getParentRefFromDocumentList(List<DocumentModel> documentList) {
124        List<DocumentRef> parentRefs = new ArrayList<DocumentRef>();
125
126        for (DocumentModel doc : documentList) {
127            if (!parentRefs.contains(doc.getParentRef())) {
128                parentRefs.add(doc.getParentRef());
129            }
130        }
131
132        return parentRefs;
133    }
134
135    public static boolean isDocumentInList(DocumentModel doc, List<DocumentModel> list) {
136        String strDocRef = doc.getRef().toString();
137        for (DocumentModel d : list) {
138            if (strDocRef.equals(d.getRef().toString())) {
139                return true;
140            }
141        }
142        return false;
143    }
144
145}