KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > Category


1 /*
2  * Created on Mar 2, 2004
3  */

4 package com.openedit.store;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.LinkedList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.dom4j.Element;
15
16 /**
17  * @author cburkey
18  *
19  */

20 public class Category
21 {
22     protected String JavaDoc fieldName;
23     protected String JavaDoc fieldId;
24     protected String JavaDoc fieldDescription;
25     protected String JavaDoc fieldShortDecription;
26 // protected Map fieldItemMap;
27
protected boolean fieldUserSelected;
28     protected String JavaDoc fieldCatalogHome;
29     protected int fieldItemCount;
30     protected List JavaDoc fieldChildren;
31     protected Category fieldParentCatalog;
32     protected Map JavaDoc fieldProperties;
33     protected Map JavaDoc fieldImages;
34     protected Map JavaDoc fieldLinkedFiles; //these are links to files that this catalog might include (such as PDF's)
35
protected String JavaDoc fieldBrochure;
36     protected String JavaDoc fieldSpecSheet;
37     protected List JavaDoc fieldOptions;
38     
39     public Category()
40     {
41     }
42
43     public Category( String JavaDoc inName )
44     {
45         fieldName = inName;
46     }
47     
48 public Category(String JavaDoc inId, String JavaDoc inName)
49     {
50         setId(inId);
51         if( inName != null)
52         {
53             setName(inName.trim());
54         }
55     }
56
57     /* This is old, we now use Lucene to find the items
58     public Collection getItems()
59     {
60         return getItemMap().values();
61     }
62
63     public Map getItemMap()
64     {
65         if ( fieldItemMap == null )
66         {
67             fieldItemMap = new HashMap();
68         }
69         return fieldItemMap;
70     }
71
72     public int getNumItems()
73     {
74         return getItemMap().size();
75     }
76
77     public void addItem( Product inItem )
78     {
79         getItemMap().put( inItem.getId(), inItem );
80     }
81
82     public void removeItem( Product inItem )
83     {
84         getItemMap().remove( inItem );
85     }
86
87     public Product getItem( String inSkuNumber )
88     {
89         return (Product) getItemMap().get( inSkuNumber );
90     }
91 */

92     public String JavaDoc getName()
93     {
94         return fieldName;
95     }
96
97     public void setName( String JavaDoc inString )
98     {
99         fieldName = inString;
100     }
101
102     public void loadData( Element inElement )
103     {
104         setName( inElement.attributeValue( "name" ) );
105         setId( inElement.attributeValue( "id" ) );
106         setCatalogHome(inElement.attributeValue( "catalogHome" ) );
107         String JavaDoc val = inElement.attributeValue( "itemcount" );
108         if ( val != null && val.length() > 0)
109         {
110             setItemCount( Integer.parseInt(val));
111         }
112         setUserSelected("true".equalsIgnoreCase(inElement.attributeValue("userselected")));
113     }
114     public String JavaDoc getCatalogHome()
115     {
116         return fieldCatalogHome;
117     }
118     public void setCatalogHome(String JavaDoc inCatalogHome)
119     {
120         fieldCatalogHome = inCatalogHome;
121     }
122     public String JavaDoc getId()
123     {
124         return fieldId;
125     }
126     public void setId(String JavaDoc inId)
127     {
128         fieldId = inId;
129     }
130     public String JavaDoc toString()
131     {
132         return getName();
133     }
134     public boolean isUserSelected()
135     {
136         return fieldUserSelected;
137     }
138     public void setUserSelected(boolean inUserSelected)
139     {
140         fieldUserSelected = inUserSelected;
141     }
142     public int getItemCount() {
143         return fieldItemCount;
144     }
145     public void setItemCount(int inItemCount) {
146         fieldItemCount = inItemCount;
147     }
148     public boolean isContainsItems()
149     {
150         return getItemCount() > 0;
151     }
152     /**
153      * @return Returns the children.
154      */

155     public List JavaDoc getChildren()
156     {
157         if( fieldChildren == null )
158         {
159             fieldChildren = new ArrayList JavaDoc();
160         }
161         return fieldChildren;
162     }
163     /**
164      * @param children The children to set.
165      */

166     public void setChildren( List JavaDoc inChildren )
167     {
168         fieldChildren = inChildren;
169         for ( Iterator JavaDoc iter = inChildren.iterator(); iter.hasNext(); )
170         {
171             Category cat = (Category) iter.next();
172             cat.setParentCatalog(this);
173         }
174     }
175     public Category addChild( Category inNewChild )
176     {
177         inNewChild.setParentCatalog(this);
178
179         for (int i = 0; i < getChildren().size(); i++)
180         {
181             Category element = (Category) getChildren().get(i);
182             if ( element.getId().equals(inNewChild.getId()))
183             {
184                 getChildren().set(i, inNewChild);
185                 return inNewChild;
186             }
187         }
188         getChildren().add(inNewChild);
189         return inNewChild;
190     }
191     public Category getChild(String JavaDoc inId)
192     {
193         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
194         {
195             Category element = (Category) iter.next();
196             if ( element.getId().equals(inId))
197             {
198                 return element;
199             }
200         }
201         return null;
202     }
203     
204     public void removeChild( Category inChild )
205     {
206         Category child = getChild(inChild.getId());
207         if ( child != null)
208         {
209             getChildren().remove(child);
210             child.setParentCatalog(null);
211         }
212             
213         inChild.setParentCatalog( null );
214     }
215     public boolean hasParent(String JavaDoc inId)
216     {
217         Category parent = this;
218         while( parent != null)
219         {
220             if ( parent.getId().equals(inId) )
221             {
222                 return true;
223             }
224             parent = parent.getParentCatalog();
225         }
226         return false;
227     }
228     
229     /**
230      * @return
231      */

232     public boolean hasChildren()
233     {
234         return fieldChildren != null && fieldChildren.size() > 0;
235     }
236     public boolean hasCatalog( String JavaDoc inId )
237     {
238         if( getId().equals( inId) )
239         {
240             return true;
241         }
242         if ( hasChildren() )
243         {
244             for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
245             {
246                 Category child = (Category) iter.next();
247                 if( child.hasCatalog(inId))
248                 {
249                     return true;
250                 }
251             }
252         }
253         return false;
254     }
255     
256
257     public boolean hasChild(String JavaDoc inId)
258     {
259         if ( hasChildren() )
260         {
261             for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
262             {
263                 Category child = (Category) iter.next();
264                 if( child.getId().equals( inId) )
265                 {
266                     return true;
267                 }
268             }
269         }
270         return false;
271     }
272     
273     public boolean isAncestorOf( Category inCatalog )
274     {
275         for (Iterator JavaDoc children = getChildren().iterator(); children.hasNext();)
276         {
277             Category child = (Category) children.next();
278             if (child == inCatalog)
279             {
280                 return true;
281             }
282             else if (child.hasChildren() && child.isAncestorOf(inCatalog))
283             {
284                 return true;
285             }
286         }
287         return false;
288     }
289     public Category getParentCatalog()
290     {
291         return fieldParentCatalog;
292     }
293     public void setParentCatalog( Category parentCatalog )
294     {
295         fieldParentCatalog = parentCatalog;
296     }
297     
298     /**
299      * Returns a list of all the ancestors of this catalog, starting at the
300      * catalog at the given level and ending at this catalog itself.
301      *
302      * @param inStartLevel The level at which to start listing ancestors (0 is
303      * the root, 1 is the first-level children, etc.)
304      *
305      * @return The list of ancestors of this catalog
306      */

307     public List JavaDoc listAncestorsAndSelf( int inStartLevel )
308     {
309         LinkedList JavaDoc result = new LinkedList JavaDoc();
310         Category catalog = this;
311         while ( catalog != null )
312         {
313             result.addFirst( catalog );
314             catalog = catalog.getParentCatalog();
315         }
316         return result.subList( inStartLevel, result.size() );
317     }
318     public List JavaDoc getChildrenInRows(int inColCount)
319     {
320         //Now break up the page into rows by dividing the count they wanted
321
List JavaDoc children = getChildren();
322         double rowscount = (double)children.size() / (double)inColCount;
323         
324         List JavaDoc rows = new ArrayList JavaDoc();
325         for (int i = 0; i < rowscount; i++)
326         {
327             int start = i*inColCount;
328             int end = i*inColCount + inColCount;
329             List JavaDoc sublist = children.subList(start,Math.min( children.size(),end ));
330             rows.add(sublist);
331         }
332         return rows;
333     }
334     public int getLevel()
335     {
336         int i = 1;
337         Category parent = this;
338         while(parent != null)
339         {
340             parent = parent.getParentCatalog();
341         }
342         return i;
343     }
344     
345     public String JavaDoc getDescription()
346     {
347         return fieldDescription;
348     }
349     public void setDescription(String JavaDoc inDescription)
350     {
351         fieldDescription = inDescription;
352     }
353     public String JavaDoc get(String JavaDoc inKey)
354     {
355         Category cat = this;
356         while(cat != null )
357         {
358             String JavaDoc val = cat.getProperty(inKey);
359             if ( val != null)
360             {
361                 return val;
362             }
363             cat = cat.getParentCatalog();
364         }
365         return null;
366     }
367     public Map JavaDoc getProperties()
368     {
369         if ( fieldProperties == null )
370         {
371             fieldProperties = new HashMap JavaDoc();
372         }
373         return fieldProperties;
374     }
375
376     public void setProperties(Map JavaDoc inProperties)
377     {
378         fieldProperties = inProperties;
379     }
380
381     public void setProperty( String JavaDoc inKey, String JavaDoc inValue )
382     {
383         if ( inValue != null )
384         {
385             getProperties().put( inKey, inValue );
386         }
387         else
388         {
389             getProperties().remove( inKey );
390         }
391     }
392
393     public void setProperty( String JavaDoc inKey, boolean inValue )
394     {
395         setProperty( inKey, String.valueOf( inValue ) );
396     }
397     public boolean isPropertyTrue(String JavaDoc inKey)
398     {
399         String JavaDoc val = getProperty(inKey);
400         if ( "true".equalsIgnoreCase(val))
401         {
402             return true;
403         }
404         return false;
405     }
406     public String JavaDoc getProperty( String JavaDoc inKey )
407     {
408         return (String JavaDoc)getProperties().get( inKey );
409     }
410
411     /**
412      * @param inKey
413      */

414     public void removeProperty(String JavaDoc inKey)
415     {
416         getProperties().remove(inKey);
417     }
418     public Image getImage( String JavaDoc inKey)
419     {
420         return (Image)getImages().get( inKey );
421     }
422     public Map JavaDoc getImages()
423     {
424         if ( fieldImages == null)
425         {
426             fieldImages = new HashMap JavaDoc(4);
427         }
428         return fieldImages;
429     }
430     public List JavaDoc loadImages(String JavaDoc inType)
431     {
432         List JavaDoc list = new ArrayList JavaDoc(getImages().keySet());
433         Collections.sort(list);
434         List JavaDoc images = new ArrayList JavaDoc();
435         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();)
436         {
437             Image image = getImage((String JavaDoc) iter.next());
438             if ( image.getType().equals(inType))
439             {
440                 images.add(image);
441             }
442         }
443         return images;
444         
445     }
446     public List JavaDoc getImageList()
447     {
448         List JavaDoc list = new ArrayList JavaDoc(getImages().keySet());
449         Collections.sort(list);
450         List JavaDoc images = new ArrayList JavaDoc();
451         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();)
452         {
453             Image image = getImage((String JavaDoc) iter.next());
454             images.add(image);
455             
456         }
457         return images;
458     }
459     
460     /**
461      * Adds the given image to the list of actual images for this catalog, as
462      * returned by {@link #getImageList()}. If there is already an image with
463      * the given image's ID, it will be replaced (in memory) with the new image.
464      *
465      * @param inImage The image to add
466      */

467     public void addImage( Image inImage )
468     {
469         getImages().put(inImage.getId(), inImage);
470     }
471     
472     public String JavaDoc getShortDescription()
473     {
474         return fieldShortDecription;
475     }
476     public void setShortDescription(String JavaDoc inShortDecription)
477     {
478         fieldShortDecription = inShortDecription;
479     }
480     
481     /**
482      * This is a list of links associated with this catalog
483      * @return
484      */

485     public List JavaDoc getLinkedFileList()
486     {
487         List JavaDoc list = new ArrayList JavaDoc(getLinkedFiles().keySet());
488         Collections.sort(list);
489         List JavaDoc files = new ArrayList JavaDoc();
490         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();)
491         {
492             LinkedFile file = getLinkedFile( (String JavaDoc) iter.next());
493             files.add(file);
494             
495         }
496         return files;
497     }
498     public LinkedFile getLinkedFile(String JavaDoc inFilename)
499     {
500         return (LinkedFile)getLinkedFiles().get(inFilename);
501     }
502     public void putLinkedFile(String JavaDoc inFilename, LinkedFile inFile)
503     {
504         getLinkedFiles().put(inFilename,inFile);
505     }
506     public Map JavaDoc getLinkedFiles()
507     {
508         if (fieldLinkedFiles == null)
509         {
510             fieldLinkedFiles = new HashMap JavaDoc(4);
511         }
512         return fieldLinkedFiles;
513     }
514
515     public void clearImages()
516     {
517         getImages().clear();
518     }
519
520     public void clearLinkedFiles()
521     {
522         getLinkedFiles().clear();
523     }
524
525     public String JavaDoc getBrochure()
526     {
527         return fieldBrochure;
528     }
529     public void setBrochure(String JavaDoc inBrochure)
530     {
531         fieldBrochure = inBrochure;
532     }
533     public String JavaDoc getSpecSheet()
534     {
535         return fieldSpecSheet;
536     }
537     public void setSpecSheet(String JavaDoc inSpecSheet)
538     {
539         fieldSpecSheet = inSpecSheet;
540     }
541     
542     public List JavaDoc getOptions()
543     {
544         if (fieldOptions == null)
545         {
546             fieldOptions = new ArrayList JavaDoc();
547         }
548         return fieldOptions;
549     }
550     
551     public List JavaDoc getAllOptions()
552     {
553         Map JavaDoc optionsMap = new HashMap JavaDoc();
554
555         Category parent = this;
556         while (parent != null)
557         {
558             if( parent.fieldOptions != null)
559             {
560                 List JavaDoc catalogOptions = parent.getOptions();
561     
562                 for (Iterator JavaDoc iter = catalogOptions.iterator(); iter.hasNext();)
563                 {
564                     Option option = (Option) iter.next();
565                     if( !optionsMap.containsKey(option.getId()))
566                     {
567                         optionsMap.put(option.getId(), option);
568                     }
569                 }
570             }
571             parent = parent.getParentCatalog();
572         }
573         
574         return new ArrayList JavaDoc(optionsMap.values());
575     }
576
577     public void setOptions(List JavaDoc inOptions)
578     {
579         fieldOptions = inOptions;
580     }
581
582     public void addOption(Option inOption)
583     {
584         getOptions().add(inOption);
585     }
586     public void removeOption(String JavaDoc id)
587     {
588         List JavaDoc options = getOptions();
589         for (int i = 0; i < options.size(); i++)
590         {
591             Option option = (Option)options.get(i);
592             if (option.getId().equals( id ) )
593             {
594                 getOptions().remove(i);
595             }
596         }
597     }
598
599     public void clearOptions()
600     {
601         getOptions().clear();
602     }
603
604     public Option getOption(String JavaDoc inOptionId)
605     {
606         for (Iterator JavaDoc it = getOptions().iterator(); it.hasNext();)
607         {
608             Option option = (Option)it.next();
609             if (inOptionId.equals(option.getId()))
610             {
611                 return option;
612             }
613         }
614         
615         if (getParentCatalog() != null)
616         {
617             return getParentCatalog().getOption(inOptionId);
618         }
619         
620         return null;
621         /*
622         if ( getParentCatalog() != null)
623         {
624             return getParentCatalog().getOption(inOptionId);
625         }
626         */

627
628     }
629
630     public void clearChildren()
631     {
632         getChildren().clear();
633     }
634
635     public Category getChildByName(String JavaDoc inCatName)
636     {
637         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
638         {
639             Category cat = (Category) iter.next();
640             if ( cat.getName().equals(inCatName))
641             {
642                 return cat;
643             }
644         }
645         return null;
646     }
647
648 }
649
Popular Tags