001/*
002 * (C) Copyright 2010 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 * Contributors:
014 *     Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.platform.shibboleth.web.tree;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023import org.nuxeo.ecm.core.api.DocumentModel;
024
025/**
026 * Tree node class handling node information and Nodes operation
027 *
028 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
029 */
030public class UserTreeNode {
031
032    protected DocumentModel document;
033
034    protected List<UserTreeNode> childrens;
035
036    protected String name = "";
037
038    public UserTreeNode(String name) {
039        this.name = name;
040    }
041
042    public UserTreeNode(DocumentModel document) {
043        this.document = document;
044    }
045
046    public List<UserTreeNode> getChildrens() {
047        if (childrens == null) {
048            childrens = new ArrayList<UserTreeNode>();
049        }
050        return childrens;
051    }
052
053    public void addChildren(UserTreeNode child) {
054        getChildrens().add(child);
055    }
056
057    public String getId() {
058        return document == null ? name : document.getId();
059    }
060
061    /**
062     * Get the displayed name, if instantiate with a documentModel it will be the document Id
063     *
064     * @return name defined with the constructor, or Document Id
065     */
066    public String getDisplayedName() {
067        if (name.equals("") && document != null) {
068            String id = document.getId();
069            int pos = id.lastIndexOf(UserTreeNodeHelper.getParseString());
070
071            if (pos > 0) {
072                name = id.substring(pos + UserTreeNodeHelper.getParseStringLength());
073            } else {
074                name = id;
075            }
076        }
077        return name;
078    }
079
080    /**
081     * Factory method to build a collection of UserTReeNode.
082     *
083     * @return empty list if no docs passed
084     */
085    public static List<UserTreeNode> constructNodes(Collection<DocumentModel> docs) {
086        List<UserTreeNode> ret = new ArrayList<UserTreeNode>();
087
088        if (docs != null) {
089            for (DocumentModel doc : docs) {
090                ret.add(new UserTreeNode(doc));
091            }
092        }
093
094        return ret;
095    }
096}