1 4 package com.openedit.modules.cart.editor; 5 6 import java.awt.Dimension ; 7 import java.io.File ; 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Date ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Set ; 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 44 public class CatalogEditModule extends BaseStoreModule 45 { 46 private static final String LINKTREE = "linktree"; 47 private static final String 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 ().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 catalog2Id = inContext.getRequestParameter("catalogs2"); 63 Category catalog1 = storeEditor.getCurrentCatalog(); 64 Category catalog2 = storeEditor.getCatalog( catalog2Id ); 65 66 if (catalog1 != null && catalog2 != null) 67 { 68 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 catalogId = inContext.getRequestParameter(CATEGORYID); 87 Category catalog = storeEditor.getCatalog( catalogId ); 88 89 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 catalogId = inContext.getRequestParameter(CATEGORYID); 99 Category catalog = storeEditor.getCatalog( catalogId ); 100 101 if (catalog.getParentCatalog() != null) 103 { 104 storeEditor.moveCatalogDown(catalog); 105 } 106 } 107 108 public void deleteCatalog( WebPageRequest inContext ) throws OpenEditException 109 { 110 String 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 properties = command.parseArguments(inContext); 132 if ( properties == null) 133 { 134 return; 135 } 136 String id = (String )properties.get("imageid"); 137 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 pid =item.getSku(); 146 String 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 properties = command.parseArguments(inContext); 158 if ( properties == null) 159 { 160 return; 161 } 162 String id = (String )properties.get("imageid"); 163 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 pid =prod.getId(); 172 String 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 properties = command.parseArguments(inContext); 185 if ( properties == null) 186 { 187 return; 188 } 189 String id = (String )properties.get("imageid"); 190 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 cid =catalog.getId(); 199 String 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 properties = command.parseArguments(inContext); 211 if ( properties == null) 212 { 213 return; 214 } 215 String name = (String )properties.get("uploadedfilename"); 216 name = PathUtilities.extractFileName(name); 218 219 StoreEditor editor = getStoreEditor(inContext); 222 String 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 path = inContext.getRequestParameter("imagepath"); 231 ImageResizer resizer = new ImageResizer(); 232 File in = new File (getRoot(), path); 233 String width = inContext.getRequestParameter("width"); 234 resizer.setMaxScaledSize(new Dimension (Integer.parseInt(width), Integer.MAX_VALUE)); 235 236 try 237 { 238 resizer.resizeImage(in,in); 239 } 240 catch (Exception ex) 241 { 242 log.error( ex ); 243 throw new StoreException(ex); 244 } 245 } 246 public void resizeAllImages( WebPageRequest inContext ) throws Exception 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 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 storeDir = getStoreEditor( inContext ).getStore().getStoreDirectory(); 271 try 272 { 273 resizer.resizeImagesInDirectory( new File ( storeDir, "images/medium" ), 274 new File ( storeDir, "images/thumb" ), true ); 275 } 276 catch ( Exception ioe ) 277 { 278 throw new OpenEditException( ioe ); 279 } 280 } 281 282 public void reloadCatalogs( WebPageRequest inRequest ) throws OpenEditException 283 { 284 inRequest.removeSessionValue("storeAdminCatalogTree" + inRequest.getUser() ); 288 289 } 290 public void saveCatalog( WebPageRequest inContext ) throws OpenEditException 291 { 292 String id = inContext.getRequestParameter( "id" ); 293 String name = inContext.getRequestParameter( "name" ); 294 295 StoreEditor editor = getStoreEditor(inContext); 296 Category currentCatalog = editor.getCurrentCatalog(); 297 298 String 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 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 337 editor.saveCatalog(currentCatalog); 338 } 339 340 341 public void selectProduct( WebPageRequest inContext ) throws OpenEditException 342 { 343 String 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 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 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 [] 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 catalogId = inContext.getRequestParameter("selectedcatalog"); 410 if ( catalogId == null) 411 { 412 catalogId = editor.getCurrentCatalog().getId(); 413 } 414 String [] 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 saveAsNew = inContext.getRequestParameter("saveasnew"); 422 StoreEditor editor = getStoreEditor(inContext); 423 Product product = editor.getCurrentProduct(); 424 425 String newId = inContext.getRequestParameter("newproductid"); 426 if ( newId != null && !newId.equals(product.getId())) 428 { 429 Product newproduct = editor.copyProduct(product, newId); 430 Set catalogs = product.getCatalogs(); 431 for (Iterator 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); } 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.setHandlingChargeLevel( inContext.getRequestParameter( "handlingchargelevel" ) ); 477 478 String shipping = inContext.getRequestParameter("shippingmethod"); 479 product.setShippingMethodId(shipping); 480 481 String 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 type = inContext.getRequestParameter("type"); 495 StoreEditor editor = getStoreEditor(inContext); 496 Map properties = null; 497 if (type.equals("product")) 498 { 499 Product product = editor.getCurrentProduct(); 500 properties = product.getProperties(); 501 List details = editor.getStore().getProductArchive().getPropertyDetails().getDetails(); 503 for (Iterator 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 type = inContext.getRequestParameter("type"); 529 StoreEditor editor = getStoreEditor(inContext); 530 List 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 } 547 inContext.putPageValue("options", options); 548 inContext.putPageValue("type", type); 549 } 550 551 public void saveObjectProperties( WebPageRequest inContext ) throws OpenEditException 552 { 553 String type = inContext.getRequestParameter("type"); 554 StoreEditor editor = getStoreEditor(inContext); 555 loadProperties(inContext); 556 Map properties = (Map )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 } 572 } 573 574 public void saveProperties( WebPageRequest inContext, Map inProperties ) throws OpenEditException 575 { 576 String name[] = inContext.getRequestParameters("propertyname"); 577 String 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 prefix = inContext.getRequestParameter("prefix"); 592 String suffix = inContext.getRequestParameter("suffix"); 593 String catalogid = inContext.getRequestParameter("catalogs2"); 594 String [] 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 catalog1id = inContext.getRequestParameter("catalogs1"); 606 String catalog2id = inContext.getRequestParameter("catalogs2"); 607 String [] 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 catalogid = inContext.getRequestParameter(CATEGORYID); 618 StoreEditor editor = getStoreEditor(inContext); 619 if ( catalogid != null) 620 { 621 Category catalog = editor.getCatalog(catalogid); 623 if (catalog != null) 624 { 625 editor.setCurrentCatalog(catalog); 626 } 627 } 628 } 629 630 638 public void pickCatalog( WebPageRequest inContext ) throws OpenEditException 639 { 640 String 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(); editor.setPageManager(getPageManager()); 659 editor.setStore(getStore(inContext) ); 660 inContext.putSessionValue("storeeditor",editor); 661 662 } 663 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 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 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 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 758 List itemProperties = editor.getItemProperties(); 759 for (Iterator iter = itemProperties.iterator(); iter.hasNext();) 760 { 761 Detail config = (Detail) iter.next(); 762 String propString = config.getId(); 763 String 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 qinstock = inContext.getRequestParameter("qinstock"); 773 if ( qinstock != null) 774 { 775 int q = Integer.parseInt(qinstock); 776 item.setQuantityInStock(q); 777 } 778 779 String 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 String color = inContext.getRequestParameter("color"); 789 if ( color != null) 790 { 791 item.setColor(color); 792 } 793 String size = inContext.getRequestParameter("size"); 794 if ( size != null) 795 { 796 item.setSize(size); 797 } 798 String weight = inContext.getRequestParameter("weight"); 799 if ( weight != null) 800 { 801 item.setWeight(Double.parseDouble( weight) ); 802 } 803 item.getOptions().clear(); 804 for (Iterator iter = inContext.getParameterMap().keySet().iterator(); iter.hasNext();) 805 { 806 String id = (String ) iter.next(); 807 if( id.startsWith("optionid")) 808 { 809 String 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 } 829 830 public void importImages( WebPageRequest inContext ) throws OpenEditException 831 { 832 StoreEditor editor = getStoreEditor( inContext ); 833 String catalogId = inContext.getRequestParameter( CATEGORYID ); 834 Category catalog = editor.getCatalog( catalogId ); 835 if ( catalog == null ) 836 { 837 return; 838 } 839 String directoryName = inContext.getRequestParameter( "directory" ); 840 int maxWidth = Integer.parseInt( inContext.getRequestParameter( "maxWidth" ) ); 841 842 ImageResizer resizer = new ImageResizer(); 843 resizer.setMaxScaledSize( new Dimension ( maxWidth, Integer.MAX_VALUE ) ); 844 File inDirectory = new File ( directoryName ); 845 File outDirectory = new File ( editor.getStore().getStoreDirectory(), 847 "images" + File.separatorChar + "thumb" ); 848 try 849 { 850 resizer.resizeImagesInDirectory( inDirectory, outDirectory, true ); 851 } 852 catch ( Exception ioe ) 853 { 854 throw new OpenEditException( ioe ); 855 } 856 } 857 858 890 891 898 922 public void addOption(WebPageRequest inReq) throws OpenEditException 924 { 925 StoreEditor editor = getStoreEditor( inReq ); 926 String name = inReq.getRequestParameter("optionname"); 927 String id = inReq.getRequestParameter("optionid"); 928 String 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 } 953 954 } 955 } 956 957 985 986 public void saveObjectOptions( WebPageRequest inContext ) throws OpenEditException 987 { 988 String type = inContext.getRequestParameter("type"); 989 StoreEditor editor = getStoreEditor(inContext); 990 loadOptions(inContext); 991 List options = (List )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 } 1007 } 1008 1009 public void saveOptions( WebPageRequest inContext, List inOptions ) throws OpenEditException 1010 { 1011 String id[] = inContext.getRequestParameters("optionid"); 1012 String name[] = inContext.getRequestParameters("optionname"); 1013 String defaultvalue[] = inContext.getRequestParameters("defaultvalue"); 1014 String price[] = inContext.getRequestParameters("price"); 1015 String required[] = inContext.getRequestParameters("required"); 1016 String delete[] = inContext.getRequestParameters("delete"); 1017 inOptions.clear(); 1018 List deletelist = new ArrayList (); 1019 if (delete != null) 1020 { 1021 deletelist = Arrays.asList(delete); 1022 } 1023 List requiredlist = new ArrayList (); 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 1062 private String makeId(String inString) 1063 { 1064 String 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 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 [] 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 |