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