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