001: /*
002: * Created on Mar 2, 2004
003: */
004: package org.openedit.store;
005:
006: import java.util.ArrayList;
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.LinkedList;
011: import java.util.List;
012: import java.util.Map;
013:
014: /**
015: * @author cburkey
016: *
017: */
018: public class Category {
019: protected String fieldName;
020: protected String fieldId;
021: protected String fieldDescription;
022: protected String fieldShortDecription;
023: // protected Map fieldItemMap;
024: protected boolean fieldUserSelected; //Deprecated?
025: protected int fieldItemCount;
026: protected List fieldChildren;
027: protected Category fieldParentCatalog;
028: protected Map fieldProperties;
029: protected Map fieldImages;
030: protected Map fieldLinkedFiles; //these are links to files that this catalog might include (such as PDF's)
031: protected String fieldBrochure;
032: protected String fieldSpecSheet;
033: protected List fieldOptions;
034: protected List fieldRelatedCategoryIds;
035: protected String fieldLinkedToCategoryId;
036:
037: public List getRelatedCategoryIds() {
038: if (fieldRelatedCategoryIds == null) {
039: fieldRelatedCategoryIds = new ArrayList();
040:
041: }
042:
043: return fieldRelatedCategoryIds;
044:
045: }
046:
047: public void setRelatedCategoryIds(List fieldRelatedCategoryIds) {
048: this .fieldRelatedCategoryIds = fieldRelatedCategoryIds;
049: }
050:
051: public Category() {
052: }
053:
054: public Category(String inName) {
055: fieldName = inName;
056: }
057:
058: public Category(String inId, String inName) {
059: setId(inId);
060: if (inName != null) {
061: setName(inName.trim());
062: }
063: }
064:
065: /* This is old, we now use Lucene to find the items
066: public Collection getItems()
067: {
068: return getItemMap().values();
069: }
070:
071: public Map getItemMap()
072: {
073: if ( fieldItemMap == null )
074: {
075: fieldItemMap = new HashMap();
076: }
077: return fieldItemMap;
078: }
079:
080: public int getNumItems()
081: {
082: return getItemMap().size();
083: }
084:
085: public void addItem( Product inItem )
086: {
087: getItemMap().put( inItem.getId(), inItem );
088: }
089:
090: public void removeItem( Product inItem )
091: {
092: getItemMap().remove( inItem );
093: }
094:
095: public Product getItem( String inSkuNumber )
096: {
097: return (Product) getItemMap().get( inSkuNumber );
098: }
099: */
100: public String getName() {
101: return fieldName;
102: }
103:
104: public void setName(String inString) {
105: fieldName = inString;
106: }
107:
108: public String getId() {
109: return fieldId;
110: }
111:
112: public void setId(String inId) {
113: fieldId = inId;
114: }
115:
116: public String toString() {
117: return getName();
118: }
119:
120: public boolean isUserSelected() {
121: return fieldUserSelected;
122: }
123:
124: public void setUserSelected(boolean inUserSelected) {
125: fieldUserSelected = inUserSelected;
126: }
127:
128: public int getItemCount() {
129: return fieldItemCount;
130: }
131:
132: public void setItemCount(int inItemCount) {
133: fieldItemCount = inItemCount;
134: }
135:
136: public boolean isContainsItems() {
137: return getItemCount() > 0;
138: }
139:
140: /**
141: * @return Returns the children.
142: */
143: public List getChildren() {
144: if (fieldChildren == null) {
145: fieldChildren = new ArrayList();
146: }
147: return fieldChildren;
148: }
149:
150: /**
151: * @param children The children to set.
152: */
153: public void setChildren(List inChildren) {
154: fieldChildren = inChildren;
155: for (Iterator iter = inChildren.iterator(); iter.hasNext();) {
156: Category cat = (Category) iter.next();
157: cat.setParentCatalog(this );
158: }
159: }
160:
161: public Category addChild(Category inNewChild) {
162: inNewChild.setParentCatalog(this );
163:
164: for (int i = 0; i < getChildren().size(); i++) {
165: Category element = (Category) getChildren().get(i);
166: if (element.getId().equals(inNewChild.getId())) {
167: getChildren().set(i, inNewChild);
168: return inNewChild;
169: }
170: }
171: getChildren().add(inNewChild);
172: return inNewChild;
173: }
174:
175: public Category getChild(String inId) {
176: for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
177: Category element = (Category) iter.next();
178: if (element.getId().equals(inId)) {
179: return element;
180: }
181: }
182: return null;
183: }
184:
185: public void removeChild(Category inChild) {
186: Category child = getChild(inChild.getId());
187: if (child != null) {
188: getChildren().remove(child);
189: child.setParentCatalog(null);
190: }
191:
192: inChild.setParentCatalog(null);
193: }
194:
195: public boolean hasParent(String inId) {
196: Category parent = this ;
197: while (parent != null) {
198: if (parent.getId().equals(inId)) {
199: return true;
200: }
201: parent = parent.getParentCatalog();
202: }
203: return false;
204: }
205:
206: /**
207: * @return
208: */
209: public boolean hasChildren() {
210: return fieldChildren != null && fieldChildren.size() > 0;
211: }
212:
213: public boolean hasCatalog(String inId) {
214: if (getId().equals(inId)) {
215: return true;
216: }
217: if (hasChildren()) {
218: for (Iterator iter = getChildren().iterator(); iter
219: .hasNext();) {
220: Category child = (Category) iter.next();
221: if (child.hasCatalog(inId)) {
222: return true;
223: }
224: }
225: }
226: return false;
227: }
228:
229: public boolean hasChild(String inId) {
230: if (hasChildren()) {
231: for (Iterator iter = getChildren().iterator(); iter
232: .hasNext();) {
233: Category child = (Category) iter.next();
234: if (child.getId().equals(inId)) {
235: return true;
236: }
237: }
238: }
239: return false;
240: }
241:
242: public boolean isAncestorOf(Category inCatalog) {
243: for (Iterator children = getChildren().iterator(); children
244: .hasNext();) {
245: Category child = (Category) children.next();
246: if (child == inCatalog) {
247: return true;
248: } else if (child.hasChildren()
249: && child.isAncestorOf(inCatalog)) {
250: return true;
251: }
252: }
253: return false;
254: }
255:
256: public Category getParentCatalog() {
257: return fieldParentCatalog;
258: }
259:
260: public void setParentCatalog(Category parentCatalog) {
261: fieldParentCatalog = parentCatalog;
262: }
263:
264: /**
265: * Returns a list of all the ancestors of this catalog, starting at the
266: * catalog at the given level and ending at this catalog itself.
267: *
268: * @param inStartLevel The level at which to start listing ancestors (0 is
269: * the root, 1 is the first-level children, etc.)
270: *
271: * @return The list of ancestors of this catalog
272: */
273: public List listAncestorsAndSelf(int inStartLevel) {
274: LinkedList result = new LinkedList();
275: Category catalog = this ;
276: while (catalog != null) {
277: result.addFirst(catalog);
278: catalog = catalog.getParentCatalog();
279: }
280: return result.subList(inStartLevel, result.size());
281: }
282:
283: public List getChildrenInRows(int inColCount) {
284: //Now break up the page into rows by dividing the count they wanted
285: List children = getChildren();
286: double rowscount = (double) children.size()
287: / (double) inColCount;
288:
289: List rows = new ArrayList();
290: for (int i = 0; i < rowscount; i++) {
291: int start = i * inColCount;
292: int end = i * inColCount + inColCount;
293: List sublist = children.subList(start, Math.min(children
294: .size(), end));
295: rows.add(sublist);
296: }
297: return rows;
298: }
299:
300: public int getLevel() {
301: int i = 1;
302: Category parent = this ;
303: while (parent != null) {
304: parent = parent.getParentCatalog();
305: }
306: return i;
307: }
308:
309: public String getDescription() {
310: return fieldDescription;
311: }
312:
313: public void setDescription(String inDescription) {
314: fieldDescription = inDescription;
315: }
316:
317: public String get(String inKey) {
318: String val = getProperty(inKey);
319: if (val != null) {
320: return val;
321: }
322: Category parent = getParentCatalog();
323: if (parent != null) {
324: return parent.get(inKey);
325: }
326: return null;
327: }
328:
329: public Map getProperties() {
330: if (fieldProperties == null) {
331: fieldProperties = new HashMap();
332: }
333: return fieldProperties;
334: }
335:
336: public void setProperties(Map inProperties) {
337: fieldProperties = inProperties;
338: }
339:
340: public void setProperty(String inKey, String inValue) {
341: if (inValue != null) {
342: getProperties().put(inKey, inValue);
343: } else {
344: getProperties().remove(inKey);
345: }
346: }
347:
348: public void setProperty(String inKey, boolean inValue) {
349: setProperty(inKey, String.valueOf(inValue));
350: }
351:
352: public boolean isPropertyTrue(String inKey) {
353: String val = getProperty(inKey);
354: if ("true".equalsIgnoreCase(val)) {
355: return true;
356: }
357: return false;
358: }
359:
360: public String getProperty(String inKey) {
361: return (String) getProperties().get(inKey);
362: }
363:
364: /**
365: * @param inKey
366: */
367: public void removeProperty(String inKey) {
368: getProperties().remove(inKey);
369: }
370:
371: public Image getImage(String inKey) {
372: return (Image) getImages().get(inKey);
373: }
374:
375: public Map getImages() {
376: if (fieldImages == null) {
377: fieldImages = new HashMap(4);
378: }
379: return fieldImages;
380: }
381:
382: public List loadImages(String inType) {
383: List list = new ArrayList(getImages().keySet());
384: Collections.sort(list);
385: List images = new ArrayList();
386: for (Iterator iter = list.iterator(); iter.hasNext();) {
387: Image image = getImage((String) iter.next());
388: if (image.getType().equals(inType)) {
389: images.add(image);
390: }
391: }
392: return images;
393:
394: }
395:
396: public List getImageList() {
397: List list = new ArrayList(getImages().keySet());
398: Collections.sort(list);
399: List images = new ArrayList();
400: for (Iterator iter = list.iterator(); iter.hasNext();) {
401: Image image = getImage((String) iter.next());
402: images.add(image);
403:
404: }
405: return images;
406: }
407:
408: /**
409: * Adds the given image to the list of actual images for this catalog, as
410: * returned by {@link #getImageList()}. If there is already an image with
411: * the given image's ID, it will be replaced (in memory) with the new image.
412: *
413: * @param inImage The image to add
414: */
415: public void addImage(Image inImage) {
416: getImages().put(inImage.getId(), inImage);
417: }
418:
419: public String getShortDescription() {
420: return fieldShortDecription;
421: }
422:
423: public void setShortDescription(String inShortDecription) {
424: fieldShortDecription = inShortDecription;
425: }
426:
427: /**
428: * This is a list of links associated with this catalog
429: * @deprecated This seems useless
430: * @return
431: */
432: public List getLinkedFileList() {
433: List list = new ArrayList(getLinkedFiles().keySet());
434: Collections.sort(list);
435: List files = new ArrayList();
436: for (Iterator iter = list.iterator(); iter.hasNext();) {
437: LinkedFile file = getLinkedFile((String) iter.next());
438: files.add(file);
439:
440: }
441: return files;
442: }
443:
444: public LinkedFile getLinkedFile(String inFilename) {
445: return (LinkedFile) getLinkedFiles().get(inFilename);
446: }
447:
448: public void putLinkedFile(String inFilename, LinkedFile inFile) {
449: getLinkedFiles().put(inFilename, inFile);
450: }
451:
452: public Map getLinkedFiles() {
453: if (fieldLinkedFiles == null) {
454: fieldLinkedFiles = new HashMap(4);
455: }
456: return fieldLinkedFiles;
457: }
458:
459: public void clearImages() {
460: getImages().clear();
461: }
462:
463: public void clearLinkedFiles() {
464: getLinkedFiles().clear();
465: }
466:
467: public String getBrochure() {
468: return fieldBrochure;
469: }
470:
471: public void setBrochure(String inBrochure) {
472: fieldBrochure = inBrochure;
473: }
474:
475: public String getSpecSheet() {
476: return fieldSpecSheet;
477: }
478:
479: public void setSpecSheet(String inSpecSheet) {
480: fieldSpecSheet = inSpecSheet;
481: }
482:
483: public List getOptions() {
484: if (fieldOptions == null) {
485: fieldOptions = new ArrayList();
486: }
487: return fieldOptions;
488: }
489:
490: public List getAllOptions() {
491: Map optionsMap = new HashMap();
492:
493: Category parent = this ;
494: while (parent != null) {
495: if (parent.fieldOptions != null) {
496: List catalogOptions = parent.getOptions();
497:
498: for (Iterator iter = catalogOptions.iterator(); iter
499: .hasNext();) {
500: Option option = (Option) iter.next();
501: if (!optionsMap.containsKey(option.getId())) {
502: optionsMap.put(option.getId(), option);
503: }
504: }
505: }
506: parent = parent.getParentCatalog();
507: }
508:
509: return new ArrayList(optionsMap.values());
510: }
511:
512: public void setOptions(List inOptions) {
513: fieldOptions = inOptions;
514: }
515:
516: public void addOption(Option inOption) {
517: removeOption(inOption.getId());
518: getOptions().add(inOption);
519: }
520:
521: public void removeOption(String id) {
522: List options = getOptions();
523: for (int i = 0; i < options.size(); i++) {
524: Option option = (Option) options.get(i);
525: if (option.getId().equals(id)) {
526: getOptions().remove(i);
527: }
528: }
529: }
530:
531: public void clearOptions() {
532: getOptions().clear();
533: }
534:
535: public Option getOption(String inOptionId) {
536: for (Iterator it = getOptions().iterator(); it.hasNext();) {
537: Option option = (Option) it.next();
538: if (inOptionId.equals(option.getId())) {
539: return option;
540: }
541: }
542:
543: if (getParentCatalog() != null) {
544: return getParentCatalog().getOption(inOptionId);
545: }
546:
547: return null;
548: /*
549: if ( getParentCatalog() != null)
550: {
551: return getParentCatalog().getOption(inOptionId);
552: }
553: */
554:
555: }
556:
557: public void clearChildren() {
558: getChildren().clear();
559: }
560:
561: public Category getChildByName(String inCatName) {
562: for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
563: Category cat = (Category) iter.next();
564: if (cat.getName().equals(inCatName)) {
565: return cat;
566: }
567: }
568: return null;
569: }
570:
571: public String getLink() {
572: String path = getProperty("path");
573: if (path != null) {
574: return path;
575: }
576: String root = get("categoryhome");
577: if (root == null) {
578: root = "/store/categories/";
579: }
580: return root + getId() + ".html";
581: }
582:
583: public List getParentCategories() {
584: List paths = new ArrayList();
585: Category parent = getParentCatalog();
586: paths.add(this );
587: while (parent != null) {
588: paths.add(0, parent);
589: parent = parent.getParentCatalog();
590: }
591: return paths;
592: }
593:
594: public void clearRelatedCategoryIds() {
595: fieldRelatedCategoryIds = new ArrayList();
596:
597: }
598:
599: public void addRelatedCategoryId(String inId) {
600: getRelatedCategoryIds().add(inId);
601:
602: }
603:
604: public String getLinkedToCategoryId() {
605: return fieldLinkedToCategoryId;
606: }
607:
608: public void setLinkedToCategoryId(String inLinkedToCategoryId) {
609: fieldLinkedToCategoryId = inLinkedToCategoryId;
610: }
611:
612: }
|