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 *     ${user}
016 *
017 * $$Id: SummaryImpl.java 28482 2008-01-04 15:33:39Z sfermigier $$
018 */
019package org.nuxeo.ecm.webapp.clipboard;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Set;
027
028import org.nuxeo.ecm.core.api.IdRef;
029
030/**
031 * This class is an implementation of the interface Summary. It intends to build and display a summary, thanks to
032 * summary items in the HashMap.
033 *
034 * @author <a href="bchaffangeon@nuxeo.com">Brice Chaffangeon</a>
035 */
036public class SummaryImpl extends HashMap<String, SummaryEntry> implements Summary, Serializable {
037
038    private static final long serialVersionUID = -7791997708511426604L;
039
040    public boolean hasChild(SummaryEntry parentEntry) {
041        Set<String> key = keySet();
042
043        for (String pathRef : key) {
044            SummaryEntry summaryEntry = get(pathRef);
045            if (summaryEntry.getParent() != null && summaryEntry.getParent().equals(parentEntry)) {
046                return true;
047            }
048        }
049        return false;
050    }
051
052    public List<SummaryEntry> getChildren(SummaryEntry parentEntry) {
053        List<SummaryEntry> children = null;
054        Set<String> key = keySet();
055
056        for (String pathRef : key) {
057            SummaryEntry summaryEntry = get(pathRef);
058
059            if (summaryEntry.getParent() != null && summaryEntry.getParent().equals(parentEntry)) {
060                if (children == null) {
061                    children = new ArrayList<SummaryEntry>();
062                }
063                children.add(get(pathRef));
064            }
065        }
066        return children;
067    }
068
069    public String displayEntry(StringBuffer sb, SummaryEntry parentEntry) {
070        if (sb == null) {
071            sb = new StringBuffer();
072        }
073        if (parentEntry != null) {
074            if (hasChild(parentEntry)) {
075                parentEntry.setBullet("+ ");
076            }
077            sb.append(parentEntry.toTreeString());
078            List<SummaryEntry> childrens = getChildren(parentEntry);
079            if (childrens != null && !childrens.isEmpty()) {
080                for (SummaryEntry child : childrens) {
081                    // Force the marker to increment the blank space for a child
082                    child.setMarker(parentEntry.getMarker() + child.getMarker() + child.getMarker());
083                    displayEntry(sb, child);
084                }
085            }
086        }
087        return sb.toString();
088    }
089
090    public String toFlatList() {
091        StringBuilder sb = new StringBuilder();
092        List<SummaryEntry> entryList = new ArrayList<SummaryEntry>();
093        Set<String> key = keySet();
094        for (String docRef : key) {
095            entryList.add(get(docRef));
096        }
097        Collections.sort(entryList);
098
099        for (SummaryEntry summaryEntry : entryList) {
100            sb.append(summaryEntry.toFlatString());
101        }
102        return sb.toString();
103    }
104
105    /**
106     * @return the hierarchical view by default.
107     */
108    @Override
109    public String toString() {
110        return toTreeString();
111    }
112
113    public String toTreeString() {
114        return displayEntry(null, getSummaryRoot());
115    }
116
117    /**
118     * Gets the root SummaryEntry in the map, usually identified by a key in the map set to 0.
119     *
120     * @return the root SummaryEntry in the map
121     */
122    public SummaryEntry getSummaryRoot() {
123        return get(new IdRef("0").toString());
124    }
125
126}