001: package org.openedit.store.links;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.HashSet;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Set;
009:
010: import org.openedit.store.CatalogArchive;
011: import org.openedit.store.Category;
012: import org.openedit.store.SearchFilter;
013:
014: import com.openedit.users.User;
015: import com.openedit.webui.tree.WebTreeModel;
016:
017: public class CatalogWebTreeModel implements WebTreeModel {
018: protected Category fieldRootCatalog;
019: protected User fieldUser;
020: protected Set fieldHiddenCatalogs;
021: protected Set fieldLimitToCatalogs;
022: protected CatalogArchive fieldCatalogArchive;
023: protected SearchFilter fieldSearchFilter;
024:
025: public CatalogWebTreeModel() {
026: }
027:
028: public CatalogWebTreeModel(Category inRootCatalog) {
029: fieldRootCatalog = inRootCatalog;
030: }
031:
032: /**
033: * @deprecated Use the list children method since it is faster
034: */
035: public Object getChild(Object inParent, int index) {
036: return listChildren(inParent).get(index);
037: }
038:
039: public List listChildren(Object inParent) {
040: if (inParent == null) {
041: return Collections.EMPTY_LIST;
042: }
043: Category parent = (Category) inParent;
044: List ok = new ArrayList(parent.getChildren().size());
045: for (Iterator iter = parent.getChildren().iterator(); iter
046: .hasNext();) {
047: //If this is slow then we might consider only checking the top level catalogs
048: Category cat = (Category) iter.next();
049: if (okToAdd(cat)) {
050: ok.add(cat);
051: }
052: }
053: return ok;
054: }
055:
056: protected boolean okToAdd(Category inCat) {
057: if (inCat.getParentCatalog() == null) {
058: return true;
059: }
060: if (getHiddenCatalogs().contains(inCat.getId())) {
061: return false;
062: }
063: if (getLimitToCatalogs().size() > 0) {
064: //Only worry about including these catalogs
065: for (Iterator iterator = getLimitToCatalogs().iterator(); iterator
066: .hasNext();) {
067: Category okid = (Category) iterator.next();
068: if (inCat.getId().equals(okid.getId())
069: || okid.hasParent(inCat.getId())) {
070: return true;
071: }
072: }
073: //This could be slow
074: for (Iterator iterator = getLimitToCatalogs().iterator(); iterator
075: .hasNext();) {
076: Category okid = (Category) iterator.next();
077: if (inCat.hasParent(okid.getId())) {
078: return true;
079: }
080: }
081:
082: //None found so cancel if at same level as included one
083: for (Iterator iterator = getLimitToCatalogs().iterator(); iterator
084: .hasNext();) {
085: Category okid = (Category) iterator.next();
086: if (inCat.getLevel() == okid.getLevel()) {
087: return false;
088: }
089: }
090: //index/photo2/stuff1 nostuff
091: return true;
092: }
093:
094: return true;
095: }
096:
097: public Set getHiddenCatalogs() {
098: if (fieldHiddenCatalogs == null) {
099: limitList();
100: }
101: return fieldHiddenCatalogs;
102: }
103:
104: public Set getLimitToCatalogs() {
105: if (fieldLimitToCatalogs == null) {
106: limitList();
107: }
108: return fieldLimitToCatalogs;
109: }
110:
111: protected void limitList() {
112: //look over this users permissions and see if there is a limit
113: fieldHiddenCatalogs = new HashSet();
114: fieldLimitToCatalogs = new HashSet();
115: for (Iterator iterator = getSearchFilter().listAllFilters()
116: .iterator(); iterator.hasNext();) {
117: String perm = (String) iterator.next();
118: if (perm.startsWith("limittocategory:")) {
119: String catid = perm.substring("limittocategory:"
120: .length());
121: Category cat = getCatalogArchive().getCatalog(catid);
122: if (cat != null) {
123: fieldLimitToCatalogs.add(cat);
124: }
125: // Category cat = getCatalogArchive().getCatalog(catid);
126: // while( cat != null )
127: // {
128: // fieldExclusiveCatalogs.add(cat.getId());
129: // cat = cat.getParentCatalog();
130: // }
131: }
132: //This is old way to do it
133: else if (perm.startsWith("hidecategory:")) //Aways exclude it
134: {
135: String catid = perm.substring("hidecategory:".length());
136: fieldHiddenCatalogs.add(catid);
137: } else if (perm.startsWith("hidecatalog:")) //Aways exclude it
138: {
139: String catid = perm.substring("hidecatalog:".length());
140: fieldHiddenCatalogs.add(catid);
141: } else if (perm.startsWith("backgroundcatalog:")) //Aways exclude it
142: {
143: String catid = perm.substring("backgroundcatalog:"
144: .length());
145: fieldHiddenCatalogs.add(catid);
146: }
147: }
148: }
149:
150: public List getChildren(Object inParent) {
151: return listChildren(inParent);
152: }
153:
154: public List getChildrenInRows(Object inParent, int inColCount) {
155: //Now break up the page into rows by dividing the count they wanted
156: List children = getChildren(inParent);
157: double rowscount = (double) children.size()
158: / (double) inColCount;
159:
160: List rows = new ArrayList();
161: for (int i = 0; i < rowscount; i++) {
162: int start = i * inColCount;
163: int end = i * inColCount + inColCount;
164: List sublist = children.subList(start, Math.min(children
165: .size(), end));
166: rows.add(sublist);
167: }
168: return rows;
169: }
170:
171: public int getChildCount(Object inParent) {
172: return listChildren(inParent).size();
173: }
174:
175: public int getIndexOfChild(Object inParent, Object inChild) {
176: return listChildren(inParent).indexOf(inChild);
177: }
178:
179: public boolean isLeaf(Object inNode) {
180: return !((Category) inNode).hasChildren();
181: }
182:
183: public Object getRoot() {
184: return fieldRootCatalog;
185: }
186:
187: public String getId(Object inNode) {
188: if (inNode == null) {
189: return null;
190: }
191: return ((Category) inNode).getId();
192: }
193:
194: public Object getParent(Object inNode) {
195: Category child = (Category) inNode;
196: return child.getParentCatalog();
197: }
198:
199: public User getUser() {
200: return fieldUser;
201: }
202:
203: public void setUser(User inUser) {
204: fieldUser = inUser;
205: }
206:
207: public Category getRootCatalog() {
208: return fieldRootCatalog;
209: }
210:
211: public void setRootCatalog(Category inRootCatalog) {
212: fieldRootCatalog = inRootCatalog;
213: }
214:
215: public Object getChildById(String inId) {
216: return findNodeById(getRoot(), inId);
217: }
218:
219: public Object findNodeById(Object inRoot, String inId) {
220: String test = getId(inRoot);
221: if (test.equals(inId)) {
222: return inRoot;
223: }
224: for (Iterator iterator = getChildren(inRoot).iterator(); iterator
225: .hasNext();) {
226: Object child = iterator.next();
227: child = findNodeById(child, inId);
228: if (child != null) {
229: return child;
230: }
231: }
232: return null;
233: }
234:
235: public CatalogArchive getCatalogArchive() {
236: return fieldCatalogArchive;
237: }
238:
239: public void setCatalogArchive(CatalogArchive inCatalogArchive) {
240: fieldCatalogArchive = inCatalogArchive;
241: }
242:
243: public SearchFilter getSearchFilter() {
244: return fieldSearchFilter;
245: }
246:
247: public void setSearchFilter(SearchFilter inSearchFilter) {
248: fieldSearchFilter = inSearchFilter;
249: }
250:
251: }
|