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