KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.openedit.store;
5
6 import java.io.File JavaDoc;
7 import java.io.FileFilter JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.TreeSet JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.lucene.document.Document;
20 import org.openedit.money.Fraction;
21
22 import com.openedit.OpenEditException;
23 import com.openedit.WebPageRequest;
24 import com.openedit.config.Configuration;
25 import com.openedit.modules.search.LuceneHitTracker;
26 import com.openedit.modules.xml.AttributeDesc;
27 import com.openedit.modules.xml.XmlArchive;
28 import com.openedit.modules.xml.XmlFile;
29 import com.openedit.store.customer.Customer;
30 import com.openedit.util.PathUtilities;
31 import com.openedit.web.Crumb;
32
33 /**
34  * @author cburkey
35  *
36  */

37 public class Store
38 {
39     protected File JavaDoc fieldRootDirectory;
40     protected File JavaDoc fieldStoreDirectory;
41     protected long fieldLastModified;
42     
43     protected Set JavaDoc fieldCreditCardTypes;
44     protected String JavaDoc fieldSmtpServer;
45     protected List JavaDoc fieldToAddresses;
46     protected List JavaDoc fieldNotifyAddresses;
47     protected String JavaDoc fieldFromAddress;
48     protected String JavaDoc fieldEmailLayout;
49     protected String JavaDoc fieldOrderLayout;
50     protected String JavaDoc fieldHostName;
51     protected String JavaDoc fieldName; //this is the name of the store
52
protected String JavaDoc fieldCatalogId;
53     
54     protected Map JavaDoc fieldItems;
55     protected ProductArchive fieldProductArchive;
56     protected ProductArchive fieldMirrorProductArchive;
57     protected Map JavaDoc fieldTaxRates;
58     protected StoreSearcher fieldStoreSearcher;
59     protected List JavaDoc fieldAllShippingMethods;
60     private static final Log log = LogFactory.getLog( Store.class );
61     protected CatalogConverter fieldImportConverter;
62     protected CatalogArchive fieldCatalogArchive;
63     protected CustomerArchive fieldCustomerArchive;
64     protected OrderArchive fieldOrderArchive;
65     protected OrderGenerator fieldOrderGenerator;
66     protected ProductExport fieldProductExport;
67     protected boolean fieldAssignShippingMethod;
68     protected boolean fieldAllowSpecialRequest;
69     protected boolean fieldCouponsAccepted;
70     protected boolean fieldUsesPoNumbers;
71     protected boolean fieldDisplayTermsConditions;
72     protected boolean fieldUsesBillMeLater;
73     protected boolean fieldAllowDuplicateAccounts;
74     protected boolean fieldAutoCapture;
75     
76     protected ProductPathFinder fieldProductPathFinder;
77     protected Configuration fieldConfiguration;
78     protected XmlArchive fieldXmlArchive;
79     
80     public Store()
81     {
82         log.info( "Created new store" );
83     }
84
85     public Set JavaDoc getCreditCardTypes()
86     {
87         if ( fieldCreditCardTypes == null )
88         {
89             fieldCreditCardTypes = new TreeSet JavaDoc();
90         }
91         return fieldCreditCardTypes;
92     }
93
94     public List JavaDoc listUserSelectedCatalogs() throws StoreException
95     {
96         return getCatalogArchive().listUserSelectedCatalogs();
97     }
98
99     public List JavaDoc listNonUserSelectedCatalogs() throws StoreException
100     {
101         return getCatalogArchive().listNonUserSelectedCatalogs();
102     }
103
104     public Category getCatalog( String JavaDoc inCatalogId ) throws StoreException
105     {
106         return getCategory(inCatalogId);
107     }
108     public Category getCategory( String JavaDoc inCatalogId ) throws StoreException
109         {
110         CatalogArchive archive = getCatalogArchive();
111         if ( archive != null)
112         {
113             return archive.getCatalog( inCatalogId );
114         }
115         else
116         {
117             log.error("Archive is not set");
118             return null;
119         }
120     }
121
122     public void addCreditCardType( CreditCardType inCreditCardType )
123     {
124         getCreditCardTypes().add( inCreditCardType );
125     }
126
127     public void removeCreditCardType( CreditCardType inCreditCardType )
128     {
129         getCreditCardTypes().remove( inCreditCardType );
130     }
131
132     public CreditCardType getCreditCardType( String JavaDoc inString )
133     {
134         for ( Iterator JavaDoc it = getCreditCardTypes().iterator(); it.hasNext(); )
135         {
136             CreditCardType creditCardType = (CreditCardType) it.next();
137             if ( inString.equals( creditCardType.getId() ) )
138             {
139                 return creditCardType;
140             }
141         }
142         return null;
143     }
144
145     public String JavaDoc getStoreHome()
146     {
147         return "/" + getCatalogId();
148     }
149     public String JavaDoc getCatalogId()
150     {
151         return fieldCatalogId;
152     }
153
154     public void setCatalogId( String JavaDoc inString )
155     {
156         fieldCatalogId = inString;
157     }
158
159     //We have a chicken and egg problem here.
160
//We need the storeloader to actually do the clear
161

162     public void clear() throws Exception JavaDoc
163     {
164         getTaxRates().clear();
165         clearProducts();
166     }
167
168     public void clearProducts()
169     {
170         if ( fieldProductArchive != null)
171         {
172             getProductArchive().clearProducts();
173         }
174     }
175
176     public String JavaDoc getSmtpServer()
177     {
178         return fieldSmtpServer;
179     }
180
181     public List JavaDoc getToAddresses()
182     {
183         if (fieldToAddresses == null)
184         {
185             fieldToAddresses = new ArrayList JavaDoc();
186         }
187         return fieldToAddresses;
188     }
189
190     public void setSmtpServer( String JavaDoc inString )
191     {
192         fieldSmtpServer = inString;
193     }
194
195     public void setToAddresses( List JavaDoc inList )
196     {
197         fieldToAddresses = inList;
198     }
199
200     public void addToAddress( String JavaDoc inToAddress )
201     {
202         getToAddresses().add( inToAddress );
203     }
204     public void addNotifyAddress( String JavaDoc inToAddress )
205     {
206         getNotifyAddresses().add( inToAddress );
207     }
208
209     public String JavaDoc getEmailLayout()
210     {
211         return fieldEmailLayout;
212     }
213
214     public void setEmailLayout( String JavaDoc inEmailLayout )
215     {
216         fieldEmailLayout = inEmailLayout;
217     }
218
219     public String JavaDoc getFromAddress()
220     {
221         return fieldFromAddress;
222     }
223
224     public void setFromAddress( String JavaDoc inFromAddress )
225     {
226         fieldFromAddress = inFromAddress;
227     }
228
229     /**
230      * @param inString
231      * @return
232      */

233     public Fraction getTaxRateFor( String JavaDoc inString )
234     {
235         Fraction frac = (Fraction) getTaxRates().get( inString );
236         return frac;
237     }
238
239     public void putTaxRate( String JavaDoc inState, Fraction inRate )
240     {
241         getTaxRates().put( inState, inRate );
242     }
243
244     protected Map JavaDoc getTaxRates()
245     {
246         if ( fieldTaxRates == null )
247         {
248             fieldTaxRates = new HashMap JavaDoc();
249         }
250
251         return fieldTaxRates;
252     }
253
254     public String JavaDoc getOrderLayout()
255     {
256         return fieldOrderLayout;
257     }
258
259     public void setOrderLayout( String JavaDoc inThanksLayout )
260     {
261         fieldOrderLayout = inThanksLayout;
262     }
263
264     public Product getProduct( String JavaDoc inItem ) throws StoreException
265     {
266         return getProductArchive().getProduct( inItem );
267     }
268     public ProductArchive getProductArchive()
269     {
270         return fieldProductArchive;
271     }
272
273     public void setProductArchive(ProductArchive inArchive)
274     {
275         fieldProductArchive = inArchive;
276         if( inArchive != null)
277         {
278             inArchive.setStore(this);
279         }
280         
281 // inStore.getCatalogArchive().setStore( inStore);
282
// inStore.getProductArchive().setStore(inStore);
283
// inStore.getStoreSearcher().setStore(inStore);
284

285     }
286     /**
287      * @deprecated
288      * @param inArchive
289      */

290     public void setProductLoader(ProductArchive inArchive)
291     {
292         setProductArchive(inArchive);
293     }
294
295     /**
296      * @param inItem
297      */

298     public void saveProduct( Product inProduct ) throws StoreException
299     {
300         getProductArchive().saveProduct( inProduct );
301         if( getMirrorProductArchive() != null)
302         {
303             getMirrorProductArchive().saveProduct(inProduct);
304         }
305         getProductArchive().saveBlankProductDescription(inProduct);
306         getStoreSearcher().updateIndex(inProduct);
307     }
308     public void saveProducts( List JavaDoc inProducts ) throws StoreException
309     {
310         for (Iterator JavaDoc iter = inProducts.iterator(); iter.hasNext();)
311         {
312             Product product = (Product) iter.next();
313             getProductArchive().saveProduct( product );
314             if( getMirrorProductArchive() != null)
315             {
316                 getMirrorProductArchive().saveProduct(product);
317             }
318             String JavaDoc desc = product.getDescription();
319             getProductArchive().saveProductDescription( product,desc );
320         }
321         getStoreSearcher().updateIndex(inProducts);
322     }
323     
324
325     public void save() throws StoreException
326     {
327         getCatalogArchive().saveCatalogs();
328     }
329
330     protected FileFilter JavaDoc xconfFilter()
331     {
332         return new FileFilter JavaDoc()
333             {
334                 public boolean accept( File JavaDoc inFile )
335                 {
336                     if ( inFile.isDirectory()
337                         || inFile.getName().endsWith( "_default.xconf" ) )
338                     {
339                         return false;
340                     }
341                     if ( inFile.getName().endsWith( "xconf" ) )
342                     {
343                         return true;
344                     }
345                     return false;
346                 }
347             };
348     }
349
350     public List JavaDoc listAllKnownProductIds()
351     {
352         File JavaDoc[] allProductFiles = new File JavaDoc( getStoreDirectory(), "products" ).listFiles( xconfFilter() );
353         List JavaDoc ids = new ArrayList JavaDoc( allProductFiles.length );
354         for ( int i = 0; i < allProductFiles.length; i++ )
355         {
356             String JavaDoc id = PathUtilities.extractPageName( allProductFiles[i].getName() );
357             if( id != null)
358             {
359                 ids.add( id );
360             }
361         }
362         Collections.sort( ids );
363         return ids;
364     }
365
366     /**
367      *
368      */

369     public void reindexAll() throws StoreException
370     {
371         getProductArchive().clearProducts(); //lets hope they are all saved before we delete em
372
getStoreSearcher().reIndexAll();
373         
374     }
375
376     public LuceneHitTracker search( String JavaDoc inSearch ) throws StoreException
377     {
378         return getStoreSearcher().search( inSearch, null );
379     }
380
381     public StoreSearcher getStoreSearcher()
382     {
383         return fieldStoreSearcher;
384     }
385
386     public void setStoreSearcher( StoreSearcher inStoreSearcher )
387     {
388         fieldStoreSearcher = inStoreSearcher;
389         if( fieldStoreSearcher != null)
390         {
391             fieldStoreSearcher.setStore(this);
392         }
393     }
394
395     public ShippingMethod findShippingMethod( String JavaDoc inId )
396     {
397         for ( Iterator JavaDoc iter = getAllShippingMethods().iterator(); iter.hasNext(); )
398         {
399             ShippingMethod method = (ShippingMethod) iter.next();
400             if ( method.getId().equals( inId ) )
401             {
402                 return method;
403             }
404         }
405         return null;
406     }
407
408     public List JavaDoc getAllShippingMethods()
409     {
410         if ( fieldAllShippingMethods == null )
411         {
412             fieldAllShippingMethods = new ArrayList JavaDoc();
413
414         }
415
416         return fieldAllShippingMethods;
417     }
418
419     public void setAllShippingMethods( List JavaDoc inAllShippingMethods )
420     {
421         fieldAllShippingMethods = inAllShippingMethods;
422     }
423     public File JavaDoc getStoreDirectory()
424     {
425         if(fieldStoreDirectory == null )
426         {
427             fieldStoreDirectory = new File JavaDoc(fieldRootDirectory, getCatalogId());
428         }
429         return fieldStoreDirectory;
430     }
431
432     public void setStoreDirectory( File JavaDoc inHome )
433     {
434         fieldStoreDirectory = inHome;
435     }
436
437
438     public CatalogConverter getCatalogImportConverter()
439     {
440         return fieldImportConverter;
441     }
442
443     public void setCatalogImportConverter( CatalogConverter inCatalogImportConverter )
444     {
445         fieldImportConverter = inCatalogImportConverter;
446     }
447
448     /**
449      *
450      */

451     public synchronized List JavaDoc convertCatalog() throws Exception JavaDoc
452     {
453         boolean converted = false;
454         List JavaDoc errors = new ArrayList JavaDoc();
455         errors.add("conversion started on " + getCatalogId());
456         if ( getCatalogImportConverter() != null)
457         {
458             converted = getCatalogImportConverter().convert(this, errors);
459         }
460         if ( converted )
461         {
462             log.info("Convertion completed");
463             getProductArchive().clearProducts();
464             //should we also touch web.xml?
465
getCatalogArchive().reloadCatalogs();
466             errors.add("reindexing " + getCatalogId());
467             reindexAll();
468             getProductArchive().clearProducts();
469             return errors;
470         }
471         else
472         {
473             errors.add("No inventory changes found");
474             return errors;
475         }
476     }
477
478     public CatalogArchive getCatalogArchive()
479     {
480         return fieldCatalogArchive;
481     }
482
483     public void setCatalogArchive( CatalogArchive inCatalogArchive )
484     {
485         fieldCatalogArchive = inCatalogArchive;
486         if( inCatalogArchive != null)
487         {
488             inCatalogArchive.setStore(this);
489         }
490     }
491
492     public CustomerArchive getCustomerArchive()
493     {
494         return fieldCustomerArchive;
495     }
496
497     public void setCustomerArchive( CustomerArchive inCustomerArchive )
498     {
499         fieldCustomerArchive = inCustomerArchive;
500     }
501
502     /**
503      * Save the order to the order archive system
504      * @param inCart
505      */

506     public void exportNewOrder( WebPageRequest inContext, Order inOrder )
507         throws StoreException
508     {
509             getCustomerArchive().saveAndExportCustomer(inOrder.getCustomer());
510
511             OrderArchive orderArchive = getOrderArchive();
512             orderArchive.exportNewOrder( inContext, this, inOrder );
513             
514             //save the state again
515
orderArchive.changeOrderStatus(inOrder.getOrderState(), this, inOrder);
516     }
517
518     public OrderArchive getOrderArchive()
519     {
520         return fieldOrderArchive;
521     }
522
523     public void setOrderArchive( OrderArchive inOrderArchive )
524     {
525         fieldOrderArchive = inOrderArchive;
526     }
527
528     public void saveCustomer( Customer customer ) throws StoreException
529     {
530         getCustomerArchive().saveCustomer( customer );
531     }
532
533     /**
534      * @return
535      */

536     public File JavaDoc getRootDirectory()
537     {
538         return fieldRootDirectory;
539     }
540     public void setRootDirectory(File JavaDoc inRoot)
541     {
542         fieldRootDirectory = inRoot;
543     }
544
545     public OrderGenerator getOrderGenerator()
546     {
547         return fieldOrderGenerator;
548     }
549
550     public void setOrderGenerator(OrderGenerator inOrderGenerator)
551     {
552         fieldOrderGenerator = inOrderGenerator;
553     }
554
555     public boolean isAssignShippingMethod()
556     {
557         return fieldAssignShippingMethod;
558     }
559     public void setAssignShippingMethod(boolean inAssignShippingMethod)
560     {
561         fieldAssignShippingMethod = inAssignShippingMethod;
562     }
563     /**
564      * These are used to notify people of an email
565      * @return
566      */

567     
568     public List JavaDoc getNotifyAddresses()
569     {
570         if ( fieldNotifyAddresses == null)
571         {
572             fieldNotifyAddresses = new ArrayList JavaDoc();
573         }
574         return fieldNotifyAddresses;
575     }
576     public void setNotifyAddresses(List JavaDoc inBccAddresses)
577     {
578         fieldNotifyAddresses = inBccAddresses;
579     }
580     public String JavaDoc getHostName()
581     {
582         return fieldHostName;
583     }
584     public void setHostName(String JavaDoc inHostName)
585     {
586         fieldHostName = inHostName;
587     }
588     public boolean areCouponsAccepted()
589     {
590         return fieldCouponsAccepted;
591     }
592     public void setCouponsAccepted(boolean inCouponsAccepted)
593     {
594         fieldCouponsAccepted = inCouponsAccepted;
595     }
596     public boolean usesPoNumbers()
597     {
598         return fieldUsesPoNumbers;
599     }
600     public void setUsesPoNumbers(boolean inUsesPoNumbers)
601     {
602         fieldUsesPoNumbers = inUsesPoNumbers;
603     }
604     public boolean displayTermsConditions()
605     {
606         return fieldDisplayTermsConditions;
607     }
608     public void setDisplayTermsConditions(boolean inDisplayTermsConditions)
609     {
610         fieldDisplayTermsConditions = inDisplayTermsConditions;
611     }
612     public boolean usesBillMeLater()
613     {
614         return fieldUsesBillMeLater;
615     }
616     public void setUsesBillMeLater(boolean inUsesBillMeLater)
617     {
618         fieldUsesBillMeLater = inUsesBillMeLater;
619     }
620     
621     public boolean getAllowDuplicateAccounts()
622     {
623         return fieldAllowDuplicateAccounts;
624     }
625     
626     public void setAllowDuplicateAccounts(boolean inAllowDuplicateAccounts)
627     {
628         fieldAllowDuplicateAccounts = inAllowDuplicateAccounts;
629     }
630
631     public ProductPathFinder getProductPathFinder()
632     {
633         return fieldProductPathFinder;
634     }
635
636     public void setProductPathFinder(ProductPathFinder inProductPathFinder)
637     {
638         fieldProductPathFinder = inProductPathFinder;
639     }
640     public String JavaDoc showThumb(String JavaDoc inId)
641     {
642         StringBuffer JavaDoc path = new StringBuffer JavaDoc(getStoreHome());
643         path.append("/products/images/thumb/");
644         path.append(getProductPathFinder().idToPath(inId));
645         path.append(".jpg");
646         return path.toString();
647     }
648     public String JavaDoc showMedium(String JavaDoc inId)
649     {
650         StringBuffer JavaDoc path = new StringBuffer JavaDoc(getStoreHome());
651         path.append("/products/images/medium/");
652         path.append(getProductPathFinder().idToPath(inId));
653         path.append(".jpg");
654         return path.toString();
655     }
656
657     public LuceneHitTracker search(String JavaDoc inFinalq, String JavaDoc inOrdering) throws StoreException
658     {
659         LuceneHitTracker hits = getStoreSearcher().search( inFinalq, inOrdering);
660         
661         return hits;
662     }
663
664     public boolean isAllowSpecialRequest()
665     {
666         return fieldAllowSpecialRequest;
667     }
668
669     public void setAllowSpecialRequest(boolean inAllowSpecialRequest)
670     {
671         fieldAllowSpecialRequest = inAllowSpecialRequest;
672     }
673     
674     public List JavaDoc getProductsInCatalog(Category inCatalog) throws StoreException
675     {
676         if (inCatalog == null)
677         {
678             return null;
679         }
680         List JavaDoc products = new ArrayList JavaDoc();
681         String JavaDoc query = "category:(" + inCatalog.getId() + ")";
682         LuceneHitTracker hits = getStoreSearcher().search( query , null);
683         if ( hits == null)
684         {
685             return products;
686         }
687         for (Iterator JavaDoc it = hits.getAllHits(); it.hasNext();)
688         {
689             Document doc = (Document) it.next();
690             String JavaDoc id = doc.get("id");
691             Product product = getProduct(id);
692             if (product != null)
693             {
694                 products.add(product);
695             }
696             else
697             {
698                 log.info("Cannot find product with id " + id);
699             }
700         }
701         return products;
702     }
703
704     public Crumb buildCrumb(Category inCatalog)
705     {
706             Crumb crumb = new Crumb();
707             if (inCatalog == null)
708             {
709                 crumb.setText("");
710                 return crumb;
711             }
712             crumb.setPath(getStoreHome() + "/categories/" + inCatalog.getId() + ".html");
713             crumb.setText(inCatalog.getName());
714             if (inCatalog.getParentCatalog() != null
715                 && !inCatalog.getParentCatalog().getId().equals("index"))
716             {
717                 crumb.setParent(buildCrumb(inCatalog.getParentCatalog()));
718             }
719             return crumb;
720     }
721
722     public String JavaDoc getName()
723     {
724         return fieldName;
725     }
726
727     public void setName(String JavaDoc inName)
728     {
729         fieldName = inName;
730     }
731
732     public boolean isAutoCapture()
733     {
734         return fieldAutoCapture;
735     }
736
737     public void setAutoCapture(boolean inAutoCapture)
738     {
739         fieldAutoCapture = inAutoCapture;
740     }
741
742     public OrderState getOrderState(String JavaDoc inState)
743     {
744         OrderState state = (OrderState)getOrderArchive().getOrderStates().get(inState);
745         if( state == null)
746         {
747             return null;
748         }
749         return state.copy();
750     }
751     
752     public ProductExport getProductExport()
753     {
754         return fieldProductExport;
755     }
756
757     public void setProductExport(ProductExport inProductExport)
758     {
759         fieldProductExport = inProductExport;
760     }
761
762     public ProductArchive getMirrorProductArchive()
763     {
764         return fieldMirrorProductArchive;
765     }
766
767     public void setMirrorProductArchive(ProductArchive inMirrorProductArchive)
768     {
769         fieldMirrorProductArchive = inMirrorProductArchive;
770         if( fieldMirrorProductArchive != null)
771         {
772             fieldMirrorProductArchive.setStore(this);
773         }
774     }
775
776     public long getLastModified()
777     {
778         return fieldLastModified;
779     }
780
781     public void setLastModified(long inLastModified)
782     {
783         fieldLastModified = inLastModified;
784     }
785
786     protected Configuration getConfiguration()
787     {
788         return fieldConfiguration;
789     }
790
791     public void setConfiguration(Configuration inConfiguration)
792     {
793         fieldConfiguration = inConfiguration;
794     }
795     public String JavaDoc getConfigValue(String JavaDoc inKey)
796     {
797         String JavaDoc val = getConfiguration().getChildValue(inKey);
798         return val;
799     }
800     public Types getProperties(String JavaDoc inName) throws OpenEditException
801     {
802         XmlFile settings = getXmlArchive().loadXmlFile(inName);
803         if( settings == null )
804         {
805             String JavaDoc path = "/" + getCatalogId() + "/configuration/properties" + inName + ".xml";
806             settings = getXmlArchive().createXmlFile(inName, path, "property",null);
807         }
808         if( settings.isExist())
809         {
810             return new Types(settings);
811         }
812         else
813         {
814             return null;
815         }
816     }
817
818     public XmlArchive getXmlArchive()
819     {
820         return fieldXmlArchive;
821     }
822
823     public void setXmlArchive(XmlArchive inXmlArchive)
824     {
825         fieldXmlArchive = inXmlArchive;
826     }
827
828     
829 }
Popular Tags