KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > cart > editor > CatalogEditModule


1 /*
2  * Created on Nov 16, 2004
3  */

4 package com.openedit.modules.cart.editor;
5
6 import java.awt.Dimension JavaDoc;
7 import java.io.File JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Date 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
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.openedit.money.Money;
19 import org.openedit.repository.RepositoryException;
20
21 import com.openedit.OpenEditException;
22 import com.openedit.WebPageRequest;
23 import com.openedit.modules.admin.filemanager.FileUpload;
24 import com.openedit.modules.image.ImageResizer;
25 import com.openedit.modules.store.BaseStoreModule;
26 import com.openedit.page.Page;
27 import com.openedit.store.Category;
28 import com.openedit.store.Image;
29 import com.openedit.store.InventoryItem;
30 import com.openedit.store.Option;
31 import com.openedit.store.Price;
32 import com.openedit.store.PriceSupport;
33 import com.openedit.store.Product;
34 import com.openedit.store.StoreException;
35 import com.openedit.store.edit.AutoImageCreator;
36 import com.openedit.store.edit.StoreEditor;
37 import com.openedit.store.products.Detail;
38 import com.openedit.util.PathUtilities;
39
40 /**
41  * @author cburkey
42  *
43  */

44 public class CatalogEditModule extends BaseStoreModule
45 {
46     private static final String JavaDoc LINKTREE = "linktree";
47     private static final String JavaDoc ERROR = "editerror";
48     private static final Log log = LogFactory.getLog(CatalogEditModule.class);
49     
50     public void addCatalog( WebPageRequest inContext ) throws OpenEditException
51     {
52         StoreEditor storeEditor = getStoreEditor( inContext );
53         Category newCatalog = storeEditor.addNewCatalog(new Date JavaDoc().getTime() + "", "New Category");
54         storeEditor.setCurrentCatalog( newCatalog );
55         storeEditor.saveCatalog( newCatalog );
56
57     }
58     
59     public void moveCatalog ( WebPageRequest inContext ) throws OpenEditException
60     {
61         StoreEditor storeEditor = getStoreEditor( inContext );
62         String JavaDoc catalog2Id = inContext.getRequestParameter("catalogs2");
63         Category catalog1 = storeEditor.getCurrentCatalog();
64         Category catalog2 = storeEditor.getCatalog( catalog2Id );
65         
66         if (catalog1 != null && catalog2 != null)
67         {
68             //don't move if same catalog or catalog2 is already the parent
69
if (catalog1 != catalog2
70                     && catalog1.getParentCatalog() != catalog2
71                     && !catalog1.isAncestorOf(catalog2))
72             {
73                 catalog1.getParentCatalog().removeChild(catalog1);
74                 
75                 catalog1.setParentCatalog(catalog2);
76                 catalog2.addChild(catalog1);
77                 
78                 storeEditor.saveCatalog(catalog1);
79                 storeEditor.saveCatalog(catalog2);
80             }
81         }
82     }
83     public void moveCatalogUp ( WebPageRequest inContext ) throws OpenEditException
84     {
85         StoreEditor storeEditor = getStoreEditor( inContext );
86         String JavaDoc catalogId = inContext.getRequestParameter(CATEGORYID);
87         Category catalog = storeEditor.getCatalog( catalogId );
88         
89         //don't move if doesn't have a parent
90
if (catalog.getParentCatalog() != null)
91         {
92             storeEditor.moveCatalogUp(catalog);
93         }
94     }
95     public void moveCatalogDown ( WebPageRequest inContext ) throws OpenEditException
96     {
97         StoreEditor storeEditor = getStoreEditor( inContext );
98         String JavaDoc catalogId = inContext.getRequestParameter(CATEGORYID);
99         Category catalog = storeEditor.getCatalog( catalogId );
100         
101         //don't move if doesn't have a parent
102
if (catalog.getParentCatalog() != null)
103         {
104             storeEditor.moveCatalogDown(catalog);
105         }
106     }
107
108     public void deleteCatalog( WebPageRequest inContext ) throws OpenEditException
109     {
110         String JavaDoc catalogId = inContext.getRequestParameter(CATEGORYID);
111         StoreEditor storeEditor = getStoreEditor( inContext );
112         Category catalog = storeEditor.getCatalog( catalogId );
113         if ( catalog != null )
114         {
115             Category parent = catalog.getParentCatalog();
116             storeEditor.deleteCatalog( catalog );
117             if( parent != null)
118             {
119                 storeEditor.setCurrentCatalog( parent );
120             }
121             else
122             {
123                 reloadCatalogs(inContext);
124             }
125         }
126     }
127     public void uploadItemImages( WebPageRequest inContext) throws OpenEditException
128     {
129         FileUpload command = new FileUpload();
130         command.setPageManager(getPageManager());
131         Map JavaDoc properties = command.parseArguments(inContext);
132         if ( properties == null)
133         {
134             return;
135         }
136         String JavaDoc id = (String JavaDoc)properties.get("imageid");
137         //handle the upload
138
//figure out path
139
if ( id != null && id.length() > 0)
140         {
141             StoreEditor editor = getStoreEditor(inContext);
142             Image image = editor.getImage(id );
143             
144             InventoryItem item = editor.getCurrentItem();
145             String JavaDoc pid =item.getSku();
146             String JavaDoc path = editor.getStore().getStoreHome() + "/products/images/items/" + image.getType() + "/"
147                 + pid
148                 + image.getPostfix() + ".jpg";
149         
150             command.saveFile(properties,path,inContext);
151         }
152     }
153     public void uploadProductImages( WebPageRequest inContext) throws OpenEditException
154     {
155         FileUpload command = new FileUpload();
156         command.setPageManager(getPageManager());
157         Map JavaDoc properties = command.parseArguments(inContext);
158         if ( properties == null)
159         {
160             return;
161         }
162         String JavaDoc id = (String JavaDoc)properties.get("imageid");
163         //handle the upload
164
//figure out path
165
if ( id != null && id.length() > 0)
166         {
167             StoreEditor editor = getStoreEditor(inContext);
168             Image image = editor.getImage(id );
169             
170             Product prod = editor.getCurrentProduct();
171             String JavaDoc pid =prod.getId();
172             String JavaDoc path = editor.getStore().getStoreHome() + "/products/images/" + image.getType() + "/"
173                 + editor.getStore().getProductPathFinder().idToPath(pid)
174                 + image.getPostfix() + ".jpg";
175         
176             command.saveFile(properties,path,inContext);
177         }
178     }
179
180     public void uploadCatalogImages( WebPageRequest inContext) throws OpenEditException
181     {
182         FileUpload command = new FileUpload();
183         command.setPageManager(getPageManager());
184         Map JavaDoc properties = command.parseArguments(inContext);
185         if ( properties == null)
186         {
187             return;
188         }
189         String JavaDoc id = (String JavaDoc)properties.get("imageid");
190         //handle the upload
191
//figure out path
192
if ( id != null && id.length() > 0)
193         {
194             StoreEditor editor = getStoreEditor(inContext);
195             Image image = editor.getImage( id );
196             
197             Category catalog = editor.getCurrentCatalog();
198             String JavaDoc cid =catalog.getId();
199             String JavaDoc path = editor.getStore().getStoreHome() + "/categories/images/" + image.getType() + "/"
200                 + cid
201                 + image.getPostfix() + ".jpg";
202         
203             command.saveFile(properties,path,inContext);
204         }
205     }
206     public void uploadCatalogFiles( WebPageRequest inContext) throws OpenEditException
207     {
208         FileUpload command = new FileUpload();
209         command.setPageManager(getPageManager());
210         Map JavaDoc properties = command.parseArguments(inContext);
211         if ( properties == null)
212         {
213             return;
214         }
215         String JavaDoc name = (String JavaDoc)properties.get("uploadedfilename");
216         //prefix the name with the catalog id
217
name = PathUtilities.extractFileName(name);
218         
219         //handle the upload
220
//figure out path
221
StoreEditor editor = getStoreEditor(inContext);
222         String JavaDoc saveas = editor.getCurrentCatalog().getId() + "-" + name;
223         
224         command.saveFile(properties,saveas,inContext);
225             
226     }
227
228     public void resizeImage( WebPageRequest inContext ) throws OpenEditException
229     {
230         String JavaDoc path = inContext.getRequestParameter("imagepath");
231         ImageResizer resizer = new ImageResizer();
232         File JavaDoc in = new File JavaDoc(getRoot(), path);
233         String JavaDoc width = inContext.getRequestParameter("width");
234         resizer.setMaxScaledSize(new Dimension JavaDoc(Integer.parseInt(width), Integer.MAX_VALUE));
235
236         try
237         {
238             resizer.resizeImage(in,in);
239         }
240         catch (Exception JavaDoc ex)
241         {
242             log.error( ex );
243             throw new StoreException(ex);
244         }
245     }
246     public void resizeAllImages( WebPageRequest inContext ) throws Exception JavaDoc
247     {
248         AutoImageCreator creator = new AutoImageCreator();
249         creator.setStore(getStoreEditor(inContext).getStore());
250         creator.setUse100s(true);
251         creator.run();
252     }
253     public void deleteImage( WebPageRequest inContext ) throws OpenEditException
254     {
255         StoreEditor editor = getStoreEditor(inContext);
256         Category catalog = editor.getCurrentCatalog();
257         String JavaDoc type = inContext.getRequestParameter("imageid");
258         
259         Image image = catalog.getImage(type);
260         if ( image != null)
261         {
262             editor.deleteImage(image);
263         }
264         
265     }
266     
267     public void generateAllThumbnails( WebPageRequest inContext ) throws OpenEditException
268     {
269         ImageResizer resizer = new ImageResizer();
270         File JavaDoc storeDir = getStoreEditor( inContext ).getStore().getStoreDirectory();
271         try
272         {
273             resizer.resizeImagesInDirectory( new File JavaDoc ( storeDir, "images/medium" ),
274                 new File JavaDoc( storeDir, "images/thumb" ), true );
275         }
276         catch ( Exception JavaDoc ioe )
277         {
278             throw new OpenEditException( ioe );
279         }
280     }
281     
282     public void reloadCatalogs( WebPageRequest inRequest ) throws OpenEditException
283     {
284 // StoreEditor editor = getStoreEditor(inRequest);
285
// editor.reloadCatalogs();
286
//
287
inRequest.removeSessionValue("storeAdminCatalogTree" + inRequest.getUser() );
288         
289     }
290     public void saveCatalog( WebPageRequest inContext ) throws OpenEditException
291     {
292         String JavaDoc id = inContext.getRequestParameter( "id" );
293         String JavaDoc name = inContext.getRequestParameter( "name" );
294         
295         StoreEditor editor = getStoreEditor(inContext);
296         Category currentCatalog = editor.getCurrentCatalog();
297
298         String JavaDoc copy = inContext.getRequestParameter("saveasnew");
299         if( currentCatalog != null && Boolean.parseBoolean(copy))
300         {
301             currentCatalog = new Category(currentCatalog.getId() +"copy" , currentCatalog.getName());
302             editor.getCurrentCatalog().getParentCatalog().addChild(currentCatalog);
303         }
304         else if (!id.equals(currentCatalog.getId()))
305         {
306             editor.changeCatalogId(currentCatalog, id);
307         }
308         
309         currentCatalog.setShortDescription( inContext.getRequestParameter( "shortdescription" ) );
310         currentCatalog.setName( name );
311         
312         String JavaDoc sortfield = inContext.getRequestParameter( "sortfield" );
313         if (sortfield == null || sortfield.length() < 1)
314         {
315             currentCatalog.removeProperty("sortfield");
316         }
317         else
318         {
319             currentCatalog.setProperty("sortfield", sortfield);
320         }
321         //currentCatalog.setProperty( "quickship", inContext.getRequestParameter( "quickship" ) != null );
322
/*
323         final String[] CATALOG_PROPERTIES =
324         {
325             "otherdownload"
326         };
327         for (int n = 0; n < CATALOG_PROPERTIES.length; n++)
328         {
329             String propValue = inContext.getRequestParameter( CATALOG_PROPERTIES[n] );
330             if ( propValue != null && propValue.length() == 0 )
331             {
332                 propValue = null;
333             }
334             currentCatalog.setProperty( CATALOG_PROPERTIES[n], propValue );
335         }
336         */

337         editor.saveCatalog(currentCatalog);
338     }
339
340     
341     public void selectProduct( WebPageRequest inContext ) throws OpenEditException
342     {
343         String JavaDoc id = inContext.getRequestParameter("productid");
344         if ( id != null )
345         {
346             StoreEditor editor = getStoreEditor(inContext);
347             Product product = editor.getProduct( id );
348             if ( product != editor.getCurrentProduct() )
349             {
350                 editor.setCurrentItem( null );
351             }
352             editor.setCurrentProduct( product );
353             inContext.putPageValue("product", product);
354
355             String JavaDoc cid = inContext.getRequestParameter(CATEGORYID);
356             if( cid != null)
357             {
358                 Category cat = editor.getCatalog(cid);
359                 editor.setCurrentCatalog(cat);
360             }
361         
362         }
363         
364     }
365     public void createProduct( WebPageRequest inContext ) throws OpenEditException
366     {
367         StoreEditor editor = getStoreEditor(inContext);
368         String JavaDoc existingproductid = inContext.getRequestParameter("existingproductid");
369         Product product = null;
370         if ( existingproductid != null && existingproductid.length() > 0)
371         {
372             product = editor.getProduct(existingproductid);
373         }
374         if ( product == null)
375         {
376             product = editor.createProductWithDefaults();
377             if ( existingproductid != null && existingproductid.length() > 0)
378             {
379                 product.setId(existingproductid);
380             }
381         }
382         product.addCatalog(editor.getCurrentCatalog());
383
384         editor.saveProduct(product);
385         editor.setCurrentProduct(product);
386         inContext.putPageValue("product",product);
387     }
388
389     public void deleteProducts( WebPageRequest inContext ) throws OpenEditException
390     {
391         StoreEditor editor = getStoreEditor(inContext);
392         String JavaDoc[] productIds = inContext.getRequestParameters( "productid" );
393         
394         Product product;
395         
396         for (int i = 0; i < productIds.length; i++)
397         {
398             product = editor.getProduct( productIds[i] );
399             if ( product != null )
400             {
401                 editor.deleteProduct( product );
402             }
403         }
404         
405     }
406     public void removeProductsFromCatalog( WebPageRequest inContext ) throws OpenEditException
407     {
408         StoreEditor editor = getStoreEditor(inContext);
409         String JavaDoc catalogId = inContext.getRequestParameter("selectedcatalog");
410         if ( catalogId == null)
411         {
412             catalogId = editor.getCurrentCatalog().getId();
413         }
414         String JavaDoc[] productIds = inContext.getRequestParameters( "productid" );
415         Category cat = editor.getCatalog(catalogId);
416         editor.removeProductFromCatalog(cat,productIds);
417     }
418
419     public void saveProduct( WebPageRequest inContext ) throws OpenEditException
420     {
421         String JavaDoc saveAsNew = inContext.getRequestParameter("saveasnew");
422         StoreEditor editor = getStoreEditor(inContext);
423         Product product = editor.getCurrentProduct();
424             
425         String JavaDoc newId = inContext.getRequestParameter("newproductid");
426         //was id changed?
427
if ( newId != null && !newId.equals(product.getId()))
428         {
429             Product newproduct = editor.copyProduct(product, newId);
430             Set JavaDoc catalogs = product.getCatalogs();
431             for (Iterator JavaDoc iter = catalogs.iterator(); iter.hasNext();)
432             {
433                 Category element = (Category) iter.next();
434                 newproduct.addCatalog(element);
435             }
436             if (saveAsNew == null || saveAsNew.equalsIgnoreCase("false"))
437             {
438                 Page oldPage = getPageManager().getPage( editor.getStore().getStoreHome() + "/products/" + product.getId() + ".html");
439                 if (oldPage.exists())
440                 {
441                     Page newPage = getPageManager().getPage(editor.getStore().getStoreHome() + "/products/" + newproduct.getId() + ".html");
442                     try
443                     {
444                         getPageManager().movePage(oldPage, newPage);
445                     }
446                     catch ( RepositoryException re )
447                     {
448                         throw new OpenEditException( re );
449                     }
450                 }
451                 
452                 editor.deleteProduct(product); //changing product id, and erase the old id
453
//editor.getStore().reindexAll();
454
}
455             else
456             {
457                 Page oldPage = getPageManager().getPage( editor.getStore().getStoreHome() + "/products/" + product.getId() + ".html");
458                 if (oldPage.exists())
459                 {
460                     Page newPage = getPageManager().getPage(editor.getStore().getStoreHome() + "/products/" + newproduct.getId() + ".html");
461                     try
462                     {
463                         getPageManager().copyPage(oldPage, newPage);
464                     }
465                     catch ( RepositoryException re )
466                     {
467                         throw new OpenEditException( re );
468                     }
469                 }
470             }
471             product = newproduct;
472         }
473
474         product.setName( inContext.getRequestParameter( "name" ) );
475         //product.setDescription( inContext.getRequestParameter( "description" ) );
476
product.setHandlingChargeLevel( inContext.getRequestParameter( "handlingchargelevel" ) );
477         
478         String JavaDoc shipping = inContext.getRequestParameter("shippingmethod");
479         product.setShippingMethodId(shipping);
480
481         String JavaDoc sortnum = inContext.getRequestParameter("sortnum");
482         if ( sortnum != null)
483         {
484             product.setOrdering(Integer.parseInt(sortnum) );
485         }
486
487         editor.saveProduct( product );
488         editor.setCurrentProduct(product);
489         inContext.putPageValue("product",product);
490     }
491     
492     public void loadProperties( WebPageRequest inContext ) throws OpenEditException
493     {
494         String JavaDoc type = inContext.getRequestParameter("type");
495         StoreEditor editor = getStoreEditor(inContext);
496         Map JavaDoc properties = null;
497         if (type.equals("product"))
498         {
499             Product product = editor.getCurrentProduct();
500             properties = product.getProperties();
501             //properties = ListOrderedMap.decorate(new HashMap(properties));
502
List JavaDoc details = editor.getStore().getProductArchive().getPropertyDetails().getDetails();
503             for (Iterator JavaDoc iterator = details.iterator(); iterator.hasNext();)
504             {
505                 Detail detail = (Detail) iterator.next();
506                 if( !properties.containsKey(detail.getId()))
507                 {
508                     properties.put(detail.getId(), "");
509                 }
510             }
511         }
512         else if (type.equals("catalog"))
513         {
514             Category catalog = editor.getCurrentCatalog();
515             properties = catalog.getProperties();
516         }
517         else if (type.equals("item"))
518         {
519             InventoryItem item = editor.getCurrentItem();
520             properties = item.getProperties();
521         }
522         inContext.putPageValue("properties", properties);
523         inContext.putPageValue("type", type);
524     }
525     
526     public void loadOptions( WebPageRequest inContext ) throws OpenEditException
527     {
528         String JavaDoc type = inContext.getRequestParameter("type");
529         StoreEditor editor = getStoreEditor(inContext);
530         List JavaDoc options = null;
531         if (type.equals("product"))
532         {
533             Product product = editor.getCurrentProduct();
534             options = product.getOptions();
535         }
536         else if (type.equals("catalog"))
537         {
538             Category catalog = editor.getCurrentCatalog();
539             options = catalog.getOptions();
540         }
541         else if (type.equals("item"))
542         {
543             InventoryItem item = editor.getCurrentItem();
544             //TODO:Fix item options.
545
//options = item.getOptions();
546
}
547         inContext.putPageValue("options", options);
548         inContext.putPageValue("type", type);
549     }
550     
551     public void saveObjectProperties( WebPageRequest inContext ) throws OpenEditException
552     {
553         String JavaDoc type = inContext.getRequestParameter("type");
554         StoreEditor editor = getStoreEditor(inContext);
555         loadProperties(inContext);
556         Map JavaDoc properties = (Map JavaDoc)inContext.getPageValue("properties");
557         saveProperties(inContext, properties);
558         
559         if (type.equals("product"))
560         {
561             editor.saveProduct(editor.getCurrentProduct());
562         }
563         else if (type.equals("catalog"))
564         {
565             editor.saveCatalog(editor.getCurrentCatalog());
566         }
567         else if (type.equals("item"))
568         {
569             //editor.saveItem(editor.getCurrentItem());
570
//TODO: Finish Item properties.
571
}
572     }
573         
574     public void saveProperties( WebPageRequest inContext, Map JavaDoc inProperties ) throws OpenEditException
575     {
576         String JavaDoc name[] = inContext.getRequestParameters("propertyname");
577         String JavaDoc value[] = inContext.getRequestParameters("propertyvalue");
578         inProperties.clear();
579         for (int i = 0; i < name.length; i++)
580         {
581             if (name[i].length() > 0 && value[i].length() > 0)
582             {
583                 inProperties.put(name[i], value[i]);
584             }
585         }
586         
587     }
588     
589     public void addProductsToCatalog( WebPageRequest inContext ) throws OpenEditException
590     {
591         String JavaDoc prefix = inContext.getRequestParameter("prefix");
592         String JavaDoc suffix = inContext.getRequestParameter("suffix");
593         String JavaDoc catalogid = inContext.getRequestParameter("catalogs2");
594         String JavaDoc[] productid = inContext.getRequestParameters("productid");
595         
596         StoreEditor editor = getStoreEditor(inContext);
597         Category catalog = editor.getCatalog(catalogid);
598         
599         editor.addProductsToCatalog(productid, prefix, suffix, catalog);
600     }
601
602     
603     public void moveProductsToCatalog( WebPageRequest inContext ) throws OpenEditException
604     {
605         String JavaDoc catalog1id = inContext.getRequestParameter("catalogs1");
606         String JavaDoc catalog2id = inContext.getRequestParameter("catalogs2");
607         String JavaDoc[] productid = inContext.getRequestParameters("productid");
608         StoreEditor editor = getStoreEditor(inContext);
609         Category catalog1 = editor.getCatalog(catalog1id);
610         Category catalog2 = editor.getCatalog(catalog2id);
611
612         editor.moveProductsToCatalog(productid, catalog1, catalog2);
613     }
614     
615     public void loadCatalog( WebPageRequest inContext ) throws OpenEditException
616     {
617         String JavaDoc catalogid = inContext.getRequestParameter(CATEGORYID);
618         StoreEditor editor = getStoreEditor(inContext);
619         if ( catalogid != null)
620         {
621             //load up catalog and products
622
Category catalog = editor.getCatalog(catalogid);
623             if (catalog != null)
624             {
625                 editor.setCurrentCatalog(catalog);
626             }
627         }
628     }
629     
630     /**
631      * Set the "picked catalog" in the store editor based on the
632      * <code>catalogid</code> request parameter that is passed in.
633      *
634      * @param inContext The web request
635      *
636      * @throws OpenEditException
637      */

638     public void pickCatalog( WebPageRequest inContext ) throws OpenEditException
639     {
640         String JavaDoc catalogid = inContext.getRequestParameter(CATEGORYID);
641         StoreEditor editor = getStoreEditor(inContext);
642         if ( catalogid != null)
643         {
644             Category catalog = editor.getCatalog(catalogid);
645             if (catalog != null)
646             {
647                 editor.setPickedCatalog(catalog);
648             }
649         }
650     }
651     
652     public StoreEditor getStoreEditor( WebPageRequest inContext ) throws OpenEditException
653     {
654         StoreEditor editor = (StoreEditor)inContext.getSessionValue("storeeditor");
655         if ( editor == null )
656         {
657             editor = new StoreEditor(); //TODO: Use Spring to create one
658
editor.setPageManager(getPageManager());
659             editor.setStore(getStore(inContext) );
660             inContext.putSessionValue("storeeditor",editor);
661         
662         }
663 // CatalogToHtml cataloghtml = new CatalogToHtml();
664
// cataloghtml.setRootCatalog(editor.getRootCatalog());
665
// inContext.putPageValue("cataloghtml",cataloghtml);
666

667         return editor;
668     }
669
670     public void selectItem( WebPageRequest inContext ) throws OpenEditException
671     {
672         StoreEditor storeEditor = getStoreEditor( inContext );
673         Product product = storeEditor.getCurrentProduct();
674
675         String JavaDoc sku = inContext.getRequestParameter("sku");
676         if ( sku != null && sku.length() > 0 )
677         {
678             StoreEditor editor = getStoreEditor(inContext);
679             InventoryItem item = product.getInventoryItemBySku( sku );
680             if ( item == null)
681             {
682                 storeEditor.getStore().getProductArchive().clearProducts();
683                 inContext.putPageValue(ERROR,"Could not find sku in product. Reloaded products " + sku);
684                 return;
685             }
686             editor.setCurrentItem( item );
687             inContext.putPageValue("item",editor.getCurrentItem());
688         }
689         else
690         {
691             inContext.putPageValue(ERROR,"Could not find sku in product. " + sku);
692         }
693     }
694
695     public void createItem( WebPageRequest inContext ) throws OpenEditException
696     {
697         StoreEditor editor = getStoreEditor(inContext);
698         Product product = editor.getCurrentProduct();
699         String JavaDoc sku = inContext.getRequestParameter("newsku");
700         if ( sku == null || sku.length() == 0)
701         {
702             return;
703         }
704         InventoryItem old = product.getInventoryItemBySku(sku);
705         if ( old != null)
706         {
707             editor.setCurrentItem( old );
708             inContext.putPageValue("item",old);
709             return;
710         }
711         InventoryItem item = editor.createItem();
712         item.setSku(sku);
713         item.setQuantityInStock(1);
714         log.info("Added item to product " + item + " to " + product);
715         product.addInventoryItem(item);
716         editor.saveProduct(product);
717         editor.setCurrentItem(product.getInventoryItemBySku(sku));
718     }
719
720     public void deleteItem( WebPageRequest inContext ) throws OpenEditException
721     {
722         StoreEditor editor = getStoreEditor(inContext);
723         Product product = editor.getCurrentProduct();
724         if ( product == null )
725         {
726             return;
727         }
728         String JavaDoc sku = inContext.getRequestParameter( "sku" );
729         InventoryItem item = product.getInventoryItemBySku( sku );
730         if ( item != null )
731         {
732             if ( editor.getCurrentItem() != null &&
733                 item.getSku().equals( editor.getCurrentItem().getSku() ) )
734             {
735                 editor.setCurrentItem( null );
736             }
737             editor.deleteItem( item );
738         }
739     }
740
741     public void saveItem( WebPageRequest inContext ) throws OpenEditException
742     {
743         StoreEditor editor = getStoreEditor(inContext);
744
745         InventoryItem item = editor.getCurrentItem();
746         if ( item != null )
747         {
748             item.setDescription( inContext.getRequestParameter( "description" ) );
749             item.addProperty( "quickship", String.valueOf(inContext.getRequestParameter( "quickship" ) != null) );
750             
751             /*final String[] ITEM_PROPERTIES =
752             {
753                 "width", "depth", "height",
754                 "seatcap", "basesreq",
755                 "list", "com", "col",
756                 "a", "b", "c", "d", "e", "f", "g", "l"
757             };*/

758             List JavaDoc itemProperties = editor.getItemProperties();
759             for (Iterator JavaDoc iter = itemProperties.iterator(); iter.hasNext();)
760             {
761                 Detail config = (Detail) iter.next();
762                 String JavaDoc propString = config.getId();
763                 String JavaDoc propValue = inContext.getRequestParameter( propString );
764                 if ( propValue != null && propValue.length() == 0 )
765                 {
766                     propValue = null;
767                 }
768                 item.addProperty( propString, propValue );
769                 
770             }
771             
772             String JavaDoc qinstock = inContext.getRequestParameter("qinstock");
773             if ( qinstock != null)
774             {
775                 int q = Integer.parseInt(qinstock);
776                 item.setQuantityInStock(q);
777             }
778             
779             String JavaDoc price = inContext.getRequestParameter("price");
780             if ( price != null)
781             {
782                 PriceSupport ps = new PriceSupport();
783                 ps.addTierPrice(1,new Price(new Money(price)));
784                 item.setPriceSupport(ps);
785             }
786
787             //Legacy
788
String JavaDoc color = inContext.getRequestParameter("color");
789             if ( color != null)
790             {
791                 item.setColor(color);
792             }
793             String JavaDoc size = inContext.getRequestParameter("size");
794             if ( size != null)
795             {
796                 item.setSize(size);
797             }
798             String JavaDoc weight = inContext.getRequestParameter("weight");
799             if ( weight != null)
800             {
801                 item.setWeight(Double.parseDouble( weight) );
802             }
803             item.getOptions().clear();
804             for (Iterator JavaDoc iter = inContext.getParameterMap().keySet().iterator(); iter.hasNext();)
805             {
806                 String JavaDoc id = (String JavaDoc ) iter.next();
807                 if( id.startsWith("optionid"))
808                 {
809                     String JavaDoc oid = id.substring("optionid".length());
810                     Option newOption = new Option();
811                     newOption.setId(oid);
812                     
813                     Option old = item.getOption(oid);
814                     if( old != null)
815                     {
816                         newOption.setPriceSupport(old.getPriceSupport());
817                         newOption.setRequired(old.isRequired());
818                         newOption.setName(old.getName());
819                     }
820                     newOption.setValue(inContext.getRequestParameter("optionvalue" + oid));
821                     item.addOption(newOption);
822                 }
823             }
824             
825             editor.saveProduct( item.getProduct() );
826         }
827         //editor.getStore().reindexAll();
828
}
829
830     public void importImages( WebPageRequest inContext ) throws OpenEditException
831     {
832         StoreEditor editor = getStoreEditor( inContext );
833         String JavaDoc catalogId = inContext.getRequestParameter( CATEGORYID );
834         Category catalog = editor.getCatalog( catalogId );
835         if ( catalog == null )
836         {
837             return;
838         }
839         String JavaDoc directoryName = inContext.getRequestParameter( "directory" );
840         int maxWidth = Integer.parseInt( inContext.getRequestParameter( "maxWidth" ) );
841
842         ImageResizer resizer = new ImageResizer();
843         resizer.setMaxScaledSize( new Dimension JavaDoc( maxWidth, Integer.MAX_VALUE ) );
844         File JavaDoc inDirectory = new File JavaDoc( directoryName );
845         // TODO: set the output directory to whatever...
846
File JavaDoc outDirectory = new File JavaDoc( editor.getStore().getStoreDirectory(),
847             "images" + File.separatorChar + "thumb" );
848         try
849         {
850             resizer.resizeImagesInDirectory( inDirectory, outDirectory, true );
851         }
852         catch ( Exception JavaDoc ioe )
853         {
854             throw new OpenEditException( ioe );
855         }
856     }
857     
858     /*
859     public void createCatalogLinks( WebPageRequest inContext ) throws OpenEditException
860     {
861         StoreEditor editor = getStoreEditor( inContext );
862         LinkTree links = (LinkTree)inContext.getPageValue(LINKTREE);
863         String id = inContext.getRequestParameter("linkid");
864         
865         if ( id == null && links.getRootLink() != null)
866         {
867             id = links.getRootLink().getId();
868         }
869         Link parentLink = links.getLink(id);
870         editor.addCatalogAsLink(editor.getCurrentCatalog(),links, parentLink, false, null);
871         getModuleManager().execute("LinkTree.saveAllLinks",inContext);
872     }
873     public void createCatalogLinksWithProducts( WebPageRequest inContext ) throws OpenEditException
874     {
875         StoreEditor editor = getStoreEditor( inContext );
876         LinkTree links = (LinkTree)inContext.getPageValue(LINKTREE);
877         String id = inContext.getRequestParameter("linkid");
878         
879         if ( id == null && links.getRootLink() != null)
880         {
881             id = links.getRootLink().getId();
882         }
883         Link parentLink = links.getLink(id);
884         String[] args = inContext.getRequestParameters("productid");
885         editor.addCatalogAsLink(editor.getCurrentCatalog(),links, parentLink, true, args);
886         
887         getModuleManager().execute("LinkTree.saveAllLinks",inContext);
888     }
889     */

890     
891     /*
892     public void loadOptions(WebPageRequest inReq) throws OpenEditException
893     {
894         StoreEditor editor = getStoreEditor( inReq );
895         inReq.putPageValue("options",editor.getAllOptions());
896     }
897     */

898     /*
899     public void saveOptionsToCatalog(WebPageRequest inReq) throws OpenEditException
900     {
901         StoreEditor editor = getStoreEditor( inReq );
902         Category cat = editor.getCurrentCatalog();
903         cat.clearAvailableOptions(); //this includes notes?
904         
905         //loop over all known options
906         List all = editor.getAllOptions();
907         for (Iterator iter = all.iterator(); iter.hasNext();)
908         {
909             Option element = (Option) iter.next();
910             String value = inReq.getRequestParameter(element.getId() );
911             if ( value != null && value.length() > 0)
912             {
913                 cat.addAvailableOption(element);
914             }
915         }
916         //grab the notes
917         
918         editor.saveCatalog(cat);
919         
920     }
921     */

922     //If you want to add a property, use saveCatalogProperties
923
public void addOption(WebPageRequest inReq) throws OpenEditException
924     {
925         StoreEditor editor = getStoreEditor( inReq );
926         String JavaDoc name = inReq.getRequestParameter("optionname");
927         String JavaDoc id = inReq.getRequestParameter("optionid");
928         String JavaDoc type = inReq.getRequestParameter("type");
929         
930         Option option = new Option();
931         option.setId(id);
932         option.setName(name);
933         
934         if ( name != null && name.length() > 0
935                 && id != null && id.length() > 0)
936         {
937
938             if (type.equals("product"))
939             {
940                 editor.getCurrentProduct().addOption(option);
941                 editor.saveProduct(editor.getCurrentProduct());
942             }
943             else if (type.equals("catalog"))
944             {
945                 editor.getCurrentCatalog().addOption(option);
946                 editor.saveCatalog(editor.getCurrentCatalog());
947             }
948             else if (type.equals("item"))
949             {
950                 //editor.saveItem(editor.getCurrentItem());
951
//TODO: Finish Item properties.
952
}
953
954         }
955     }
956     
957     //Deprecated. Use saveObjectOptions
958
/*public void removeOptions(WebPageRequest inReq ) throws OpenEditException
959     {
960         StoreEditor editor = getStoreEditor( inReq );
961         String type = inReq.getRequestParameter("type");
962         String id[] = inReq.getRequestParameters("removeid");
963
964         for (int i = 0; i < id.length; i++)
965         {
966             if ( id[i] != null && id[i].length() > 0 )
967             {
968                 if (type.equals("product"))
969                 {
970                     editor.getCurrentProduct().removeOption(id[i]);
971                 }
972                 else if (type.equals("catalog"))
973                 {
974                     editor.getCurrentCatalog().removeOption(id[i]);
975                 }
976                 else if (type.equals("item"))
977                 {
978                     //editor.saveItem(editor.getCurrentItem());
979                     //TODO: Finish Item properties.
980                 }
981                 
982             }
983         }
984     }*/

985     
986     public void saveObjectOptions( WebPageRequest inContext ) throws OpenEditException
987     {
988         String JavaDoc type = inContext.getRequestParameter("type");
989         StoreEditor editor = getStoreEditor(inContext);
990         loadOptions(inContext);
991         List JavaDoc options = (List JavaDoc)inContext.getPageValue("options");
992         saveOptions(inContext, options);
993         
994         if (type.equals("product"))
995         {
996             editor.saveProduct(editor.getCurrentProduct());
997         }
998         else if (type.equals("catalog"))
999         {
1000            editor.saveCatalog(editor.getCurrentCatalog());
1001        }
1002        else if (type.equals("item"))
1003        {
1004            //editor.saveItem(editor.getCurrentItem());
1005
//TODO: Finish Item properties.
1006
}
1007    }
1008    
1009    public void saveOptions( WebPageRequest inContext, List JavaDoc inOptions ) throws OpenEditException
1010    {
1011        String JavaDoc id[] = inContext.getRequestParameters("optionid");
1012        String JavaDoc name[] = inContext.getRequestParameters("optionname");
1013        String JavaDoc defaultvalue[] = inContext.getRequestParameters("defaultvalue");
1014        String JavaDoc price[] = inContext.getRequestParameters("price");
1015        String JavaDoc required[] = inContext.getRequestParameters("required");
1016        String JavaDoc delete[] = inContext.getRequestParameters("delete");
1017        inOptions.clear();
1018        List JavaDoc deletelist = new ArrayList JavaDoc();
1019        if (delete != null)
1020        {
1021            deletelist = Arrays.asList(delete);
1022        }
1023        List JavaDoc requiredlist = new ArrayList JavaDoc();
1024        if (required != null)
1025        {
1026            requiredlist = Arrays.asList(required);
1027        }
1028        
1029        for (int i = 0; i < id.length; i++)
1030        {
1031            if (!deletelist.contains(id[i]))
1032            {
1033                if (id[i].length() > 0)
1034                {
1035                    Option option = new Option();
1036                    option.setId(id[i]);
1037                    option.setName(name[i]);
1038                    option.setValue(defaultvalue[i]);
1039                    if (price[i] != null && price[i].length() > 0)
1040                    {
1041                        PriceSupport pricesupport = new PriceSupport();
1042                        pricesupport.addTierPrice(1, new Price(new Money(price[i])));
1043                        option.setPriceSupport(pricesupport);
1044                    }
1045        
1046                    if (requiredlist.contains(id[i]))
1047                    {
1048                        option.setRequired(true);
1049                    }
1050                    
1051                    inOptions.add(option);
1052                }
1053            }
1054        }
1055        
1056    }
1057    
1058    /**
1059     * @param inString
1060     * @return
1061     */

1062    private String JavaDoc makeId(String JavaDoc inString)
1063    {
1064        String JavaDoc id = inString.toLowerCase();
1065        id = id.replaceAll(" ","");
1066        
1067        return id;
1068    }
1069    
1070    public void addRelatedProductId( WebPageRequest inRequest ) throws OpenEditException
1071    {
1072        StoreEditor editor = getStoreEditor( inRequest );
1073        Product product = editor.getCurrentProduct();
1074        String JavaDoc relatedProductId = inRequest.getRequestParameter( "productid" );
1075        product.addRelatedProductId( relatedProductId );
1076        editor.saveProduct( product );
1077    }
1078    
1079    public void removeRelatedProductIds( WebPageRequest inRequest ) throws OpenEditException
1080    {
1081        StoreEditor editor = getStoreEditor( inRequest );
1082        Product product = editor.getCurrentProduct();
1083        String JavaDoc[] productIds = inRequest.getRequestParameters( "productid" );
1084        for ( int i = 0; i < productIds.length; i++ )
1085        {
1086            product.removeRelatedProductId( productIds[i] );
1087        }
1088        editor.saveProduct( product );
1089    }
1090}
1091
Popular Tags