1 4 package com.openedit.store.edit; 5 6 import java.awt.Dimension ; 7 import java.io.File ; 8 import java.io.Reader ; 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.dom4j.DocumentHelper; 17 import org.dom4j.Element; 18 import org.dom4j.Node; 19 import org.dom4j.io.SAXReader; 20 import org.openedit.money.Money; 21 import org.openedit.repository.RepositoryException; 22 import org.openedit.repository.filesystem.StringItem; 23 24 import com.openedit.OpenEditException; 25 import com.openedit.modules.image.ImageResizer; 26 import com.openedit.page.Page; 27 import com.openedit.page.manage.PageManager; 28 import com.openedit.store.Category; 29 import com.openedit.store.Image; 30 import com.openedit.store.InventoryItem; 31 import com.openedit.store.Option; 32 import com.openedit.store.Product; 33 import com.openedit.store.Store; 34 import com.openedit.store.StoreException; 35 import com.openedit.store.products.Detail; 36 import com.openedit.store.products.PropertyDetails; 37 import com.openedit.util.XmlUtil; 38 39 43 public class StoreEditor 44 { 45 protected Category fieldCurrentCatalog; 46 protected Category fieldPickedCatalog; 47 protected Store fieldStore; 48 protected Product fieldCurrentProduct; 49 protected InventoryItem fieldCurrentItem; 50 protected List fieldAllOptions; 51 protected PageManager fieldPageManager; 52 protected List fieldImageTypes; 53 54 private static final Log log = LogFactory.getLog(StoreEditor.class); 55 public StoreEditor() 56 { 57 } 58 59 public Category getCurrentCatalog() throws StoreException 60 { 61 return fieldCurrentCatalog; 62 } 63 64 public void setCurrentCatalog(Category inCurrentCatalog) 65 { 66 if ( inCurrentCatalog == null) 67 { 68 throw new IllegalArgumentException ("Category cannot be null"); 69 } 70 fieldCurrentCatalog = inCurrentCatalog; 71 setCurrentProduct(null); 72 setCurrentItem(null); 73 74 } 75 76 public Store getStore() 77 { 78 return fieldStore; 79 } 80 81 public void setStore(Store inStore) 82 { 83 fieldStore = inStore; 84 } 85 86 90 public Category getCatalog(String inString) throws StoreException 91 { 92 Category catalog = getStore().getCatalogArchive().getCatalog(inString); 93 94 return catalog; 95 } 96 97 public void moveCatalogUp(Category inCatalog) throws StoreException 98 { 99 Category parent = inCatalog.getParentCatalog(); 100 List children = (List )parent.getChildren(); 101 Category prev = null; 102 for (Iterator iter = children.iterator(); iter.hasNext();) 103 { 104 Category child = (Category) iter.next(); 105 if (child == inCatalog) 106 { 107 if (prev != null) 108 { 109 int childIndex = children.indexOf(child); 110 children.set(childIndex - 1, child); 111 children.set(childIndex, prev); 112 } 113 break; 114 } 115 prev = child; 116 } 117 parent.setChildren(children); 118 saveCatalog(parent); 119 } 120 public void moveCatalogDown(Category inCatalog) throws StoreException 121 { 122 Category parent = inCatalog.getParentCatalog(); 123 List children = (List )parent.getChildren(); 124 for (Iterator iter = children.iterator(); iter.hasNext();) 125 { 126 Category child = (Category) iter.next(); 127 if (child == inCatalog) 128 { 129 if (iter.hasNext()) 130 { 131 int childIndex = children.indexOf(child); 132 children.set(childIndex, (Category) iter.next()); 133 children.set(childIndex + 1, child); 134 } 135 break; 136 } 137 } 138 parent.setChildren(children); 139 saveCatalog(parent); 140 } 141 142 147 public Category addNewCatalog(String inId, String inName) throws StoreException 148 { 149 Category newCat = new Category(); 150 newCat.setId(inId); 151 newCat.setName(inName); 152 if (getCurrentCatalog() != null) 153 { 154 getCurrentCatalog().addChild(newCat); 155 } 156 else if (getRootCatalog() != null) 157 { 158 getRootCatalog().addChild(newCat); 159 } 160 else 161 { 162 getStore().getCatalogArchive().setRootCatalog(newCat); 163 } 164 getStore().getCatalogArchive().addCatalog(newCat); 165 return newCat; 166 } 168 169 172 public void saveCatalog(Category inBlank) throws StoreException 173 { 174 if ( inBlank.getParentCatalog() == null && getStore().getCatalogArchive().getRootCatalog() != inBlank) 175 { 176 getStore().getCatalogArchive().getRootCatalog().addChild(inBlank); 177 getStore().getCatalogArchive().addCatalog(inBlank); 178 } 179 try 180 { 181 Page desc = getPageManager().getPage(getStore().getStoreHome() + "/categories/" + inBlank.getId() + ".html"); 182 if ( !desc.exists() ) 183 { 184 StringItem item = new StringItem(desc.getPath(), " ",desc.getCharacterEncoding() ); 185 desc.setContentItem(item); 186 getPageManager().putPage(desc); 187 } 188 } 189 catch ( Exception ex ) 190 { 191 throw new StoreException(ex); 192 } 193 getStore().getCatalogArchive().saveCatalog(inBlank); 194 } 195 196 199 public void deleteCatalog(Category inCatalog) throws StoreException 200 { 201 List products = getStore().getProductsInCatalog(inCatalog); 202 for (Iterator iter = products.iterator(); iter.hasNext();) 203 { 204 Product element = (Product) iter.next(); 205 element.removeCatalog(inCatalog); 206 } 207 getStore().getCatalogArchive().deleteCatalog(inCatalog); 208 getStore().saveProducts(products); 210 } 211 212 215 public Category getRootCatalog() throws StoreException 216 { 217 return getStore().getCatalogArchive().getRootCatalog(); 218 } 219 220 223 public void clearCatalogs() throws StoreException 224 { 225 getStore().getCatalogArchive().clearCatalogs(); 226 } 227 228 public void reloadCatalogs() throws StoreException 229 { 230 if (getCurrentCatalog() != null) 231 { 232 String id = getCurrentCatalog().getId(); 233 getStore().getCatalogArchive().reloadCatalogs(); 234 Category catalog = getCatalog(id); 235 if ( catalog == null) 236 { 237 catalog = getRootCatalog(); 238 } 239 setCurrentCatalog(catalog); 240 } 241 else 242 { 243 getStore().getCatalogArchive().reloadCatalogs(); 244 } 245 } 246 247 public Product createProduct() 248 { 249 return new Product(); 250 } 251 252 public void addToCatalog(Product inProduct, Category inCatalog) throws StoreException 253 { 254 inProduct.addCatalog(inCatalog); 255 if ( inProduct.getCatalogs().size() == 1) 256 { 257 inProduct.setDefaultCatalog(inCatalog); 258 } 259 } 260 261 public void deleteProduct(Product inProduct) throws StoreException 262 { 263 264 265 getStore().getProductArchive().deleteProduct(inProduct); 266 getStore().getStoreSearcher().deleteFromIndex(inProduct); 267 268 if (getCurrentProduct() != null && inProduct.getId().equals(getCurrentProduct().getId())) 269 { 270 setCurrentProduct(null); 271 } 272 273 } 274 275 public Product getProduct(String inProductId) throws StoreException 276 { 277 Product prod = getStore().getProductArchive().getProduct(inProductId); 278 if ( prod == null) 279 { 280 return null; 281 } 282 List list = getStore().getProductArchive().getPropertyDetails().getDetails(); 284 for (Iterator iter = list.iterator(); iter.hasNext();) 285 { 286 Detail detail = (Detail) iter.next(); 287 if( prod.get(detail.getId()) == null ) 288 { 289 prod.getProperties().put(detail.getId(),""); } 291 } 292 return prod; 293 294 } 295 296 public void deleteItem(InventoryItem inItem) throws StoreException 297 { 298 Product product = inItem.getProduct(); 299 if (product != null) 300 { 301 inItem.setProduct(null); 302 product.getInventoryItems().remove(inItem); 303 saveProduct(product); 304 } 306 } 307 308 public void saveProduct(Product inProduct) throws StoreException 309 { 310 getStore().saveProduct(inProduct); 311 } 312 public void saveProducts(List inProducts) throws StoreException 313 { 314 getStore().saveProducts(inProducts); 315 } 316 317 public Product getCurrentProduct() 318 { 319 return fieldCurrentProduct; 320 } 321 322 public void setCurrentProduct(Product inCurrentProduct) 323 { 324 fieldCurrentProduct = inCurrentProduct; 325 setCurrentItem(null); 326 } 327 328 331 public Product createProductWithDefaults() throws StoreException 332 { 333 Product product = createProduct(); 334 String id = getStore().getProductArchive().nextProductNumber(getStore()); 335 product.setId(id); product.setName("No Name"); 337 product.setDescription("No description available"); 338 return product; 339 340 } 341 342 public InventoryItem createItem() 343 { 344 InventoryItem item = new InventoryItem(); 345 item.setSku("newsku"); 346 return item; 347 } 348 349 public InventoryItem getCurrentItem() 350 { 351 return fieldCurrentItem; 352 } 353 354 public void setCurrentItem(InventoryItem inCurrentItem) 355 { 356 fieldCurrentItem = inCurrentItem; 357 } 358 359 public Money getItemPrice(int inQuantity, InventoryItem inItem) 360 { 361 return inItem.getPriceSupport().getYourPriceByQuantity(inQuantity); 362 } 363 364 public void deleteImage(Image inImage) 365 { 366 File out = new File (getStore().getRootDirectory(), inImage.getPath()); 367 out.delete(); 368 369 } 370 374 public Image getImage(String inId) throws StoreException 375 { 376 for (Iterator iter = getImageList().iterator(); iter.hasNext();) 377 { 378 Image image = (Image) iter.next(); 379 if (inId.equals(image.getId()) ) 380 { 381 return image; 382 } 383 } 384 return null; 385 } 386 387 390 public void resizeImage(Image inOriginal, String inId) throws StoreException 391 { 392 ImageResizer resizer = new ImageResizer(); 393 File root = getStore().getRootDirectory(); 394 File in = new File (root, inOriginal.getPath()); 395 try 396 { 397 for (int i = 1; i < getImageList().size(); i++) 398 { 399 Image image = (Image) getImageList().get(i); 400 401 resizer.setMaxScaledSize(new Dimension (image.getWidth(), Integer.MAX_VALUE)); 402 403 String newpath = image.buildLink( inId ); 404 File out = new File (root, newpath); 405 out.getParentFile().mkdirs(); 406 log.info("Output " + out.getPath()); 407 resizer.resizeImage(in, out); 408 } 409 } 410 catch (Exception ex) 411 { 412 throw new StoreException(ex); 413 } 414 } 415 416 421 public List getImageList() throws StoreException 422 { 423 return getStore().getCatalogArchive().getImageList(); 424 } 425 426 public List getImageList(String type) throws StoreException 427 { 428 return getStore().getCatalogArchive().getImageList(type); 429 } 430 431 435 503 504 505 public List getAllOptions() throws StoreException 506 { 507 List all = new ArrayList (); 509 File file = new File ( getStore().getRootDirectory(),getStore().getStoreHome() + "/configuration/alloptions.xml"); 510 if ( file.exists() ) 511 { 512 Element root = null; 513 try 514 { 515 root = new XmlUtil().getXml(file,"UTF-8"); 516 } 517 catch ( Exception ex) 518 { 519 throw new StoreException(ex); 520 } 521 for (Iterator iter = root.elementIterator("option"); iter.hasNext();) 522 { 523 Element element = (Element) iter.next(); 524 Option op = new Option(); 525 op.setId(element.attributeValue("id")); 526 op.setName(element.attributeValue("name")); 527 all.add( op); 528 } 529 } 530 return all; 531 532 } 533 534 538 public void addOptionToAll(Option inOption) throws OpenEditException 539 { 540 541 List all = getAllOptions(); 542 for (Iterator iter = all.iterator(); iter.hasNext();) 543 { 544 Option option = (Option) iter.next(); 545 if ( inOption.getId().equals(option.getId())) 546 { 547 return; 548 } 549 } 550 551 Page settings = getPageManager().getPage(getStore().getStoreHome() +"/configuration/alloptions.xml"); 552 553 Element root = null; 554 if (settings.exists()) 555 { 556 try 557 { 558 Reader read = settings.getReader(); 559 root = new SAXReader().read(read).getRootElement(); 560 } 562 catch ( Exception ex) 563 { 564 throw new StoreException(ex); 565 } 566 } 567 else 568 { 569 root = DocumentHelper.createElement("optionlist"); 570 } 571 Element opt = root.addElement("option"); 572 opt.addAttribute("id",inOption.getId()); 573 opt.addAttribute("name",inOption.getName()); 574 StringItem item = new StringItem(settings.getPath(),root.asXML(),"UTF-8"); 576 settings.setContentItem(item); 577 getPageManager().putPage(settings); 578 } 579 580 public void removeOptionFromAll(String id) throws StoreException 581 { 582 583 File file = new File ( getStore().getStoreDirectory(), "/configuration/alloptions.xml"); 584 Element root = null; 585 try 586 { 587 root = new XmlUtil().getXml(file,"UTF-8"); 588 } 589 catch ( Exception ex) 590 { 591 throw new StoreException(ex); 592 } 593 Node node = root.selectSingleNode("//option[@id='" + id +"']"); 594 if (node != null) 595 { 596 root.remove(node); 597 new XmlUtil().saveXml(root.getDocument(),file); 598 } 599 } 600 601 public void addProductAffixes( Category newCatalog, String newPrefix, String newSuffix ) throws StoreException 603 { 604 Category oldCatalog = getCurrentCatalog(); 605 List products = getStore().getProductsInCatalog(oldCatalog); String newId; 607 List productsToSave = new ArrayList (); if (products != null) 609 { 610 for (Iterator iter = products.iterator(); iter.hasNext();) 611 { 612 Product element = (Product) iter.next(); if (newPrefix.length() > 0 || newSuffix.length() > 0) 614 { 615 newId = element.getId(); 616 newId = newPrefix + newId + newSuffix; 617 Product product = copyProduct(element, newId ); product.addCatalog(newCatalog); productsToSave.add(product); } 621 else { 623 element.addCatalog(newCatalog); productsToSave.add(element); } 626 } 627 getStore().saveProducts(productsToSave); } 629 } 630 631 632 635 public PageManager getPageManager() 636 { 637 return fieldPageManager; 638 } 639 642 public void setPageManager(PageManager inPageManager) 643 { 644 fieldPageManager = inPageManager; 645 } 646 647 648 public Product copyProduct(Product inProduct, String inId) 649 { 650 Product product = null; 651 if (inProduct != null) 652 { 653 product = new Product(); 654 product.setId(inId); 655 product.setName(inProduct.getName()); 656 product.setDescription(inProduct.getDescription()); 657 product.setHandlingChargeLevel(inProduct.getHandlingChargeLevel()); 658 product.setOptions(inProduct.getOptions()); 659 product.setOrdering(inProduct.getOrdering()); 660 product.setDepartment(inProduct.getDepartment()); 661 product.setPriceSupport(inProduct.getPriceSupport()); 662 product.setKeywords(inProduct.getKeywords()); 663 product.setProperties(new HashMap (inProduct.getProperties())); 664 product.setInventoryItems(inProduct.getInventoryItems()); 665 } 666 return product; 667 } 668 669 674 public void changeCatalogId(Category inCatalog, String inId) throws OpenEditException 675 { 676 inId = inId.replace('(', '-'); 677 inId = inId.replace(')', '-'); 678 inId = inId.replace(' ', '-'); 679 680 List products = getStore().getProductsInCatalog(inCatalog); 681 682 PageManager pageManager = getPageManager(); 683 Page oldPage = pageManager.getPage(getStore().getStoreHome() +"/catalogs/" + inCatalog.getId() + ".html"); 685 Page newPage = pageManager.getPage(getStore().getStoreHome() +"/catalogs/" + inId + ".html"); 686 if (oldPage.exists() && !newPage.exists()) 687 { 688 try 689 { 690 pageManager.movePage(oldPage, newPage); 691 } 692 catch ( RepositoryException re ) 693 { 694 throw new OpenEditException( re ); 695 } 696 } 697 if (products != null) 698 { 699 for (Iterator iter = products.iterator(); iter.hasNext();) 700 { 701 Product element = (Product) iter.next(); element.removeCatalog(inCatalog); } 704 } 705 706 inCatalog.setId( inId ); 707 saveCatalog( inCatalog ); 708 709 if (products != null) 710 { 711 for (Iterator iter = products.iterator(); iter.hasNext();) 712 { 713 Product element = (Product) iter.next(); element.addCatalog(inCatalog); } 716 getStore().saveProducts(products); } 718 719 } 720 public List getItemProperties() throws OpenEditException 721 { 722 723 Page config = getPageManager().getPage(getStore().getStoreHome() +"/configuration/itemproperties.xml"); 724 PropertyDetails d = new PropertyDetails(); 725 d.addAllDetails(config.getReader()); 726 727 return d.getDetails(); 728 } 729 730 public Category getPickedCatalog() 731 { 732 return fieldPickedCatalog; 733 } 734 735 public void setPickedCatalog(Category inCatalog) 736 { 737 fieldPickedCatalog = inCatalog; 738 } 739 740 public void removeProductFromCatalog(Category catalog, String [] productIds) throws OpenEditException 741 { 742 if ( catalog == null ) 743 { 744 throw new OpenEditException("No catalog found "); 745 } 746 List productsToSave = new ArrayList (); 747 748 for (int i = 0; i < productIds.length; i++) 749 { 750 Product product = getProduct( productIds[i] ); 751 if ( product != null ) 752 { 753 product.removeCatalog(catalog); 754 productsToSave.add(product); 755 } 756 } 757 saveProducts(productsToSave); 758 759 } 760 761 public void addProductsToCatalog(String [] productid, String prefix, String suffix, Category catalog) throws StoreException 762 { 763 List productsToSave = new ArrayList (); 764 765 if (catalog != null) 766 { 767 List products = null; 768 if ( productid == null) 769 { 770 products = getStore().getProductsInCatalog(getCurrentCatalog()); 772 } 773 else 774 { 775 products = new ArrayList (); 776 for (int i = 0; i < productid.length; i++) 777 { 778 Product element = getProduct(productid[i]); 779 products.add(element); 780 } 781 } 782 for (Iterator iter = products.iterator(); iter.hasNext();) 783 { 784 Product element = (Product) iter.next(); 785 Product product = element; 786 if (prefix != null || suffix != null) 787 { 788 if ( prefix == null) prefix = ""; 789 if ( suffix == null) suffix = ""; 790 791 product = copyProduct(element, prefix + element.getId() + suffix); 792 } 793 if (product != null) 794 { 795 product.addCatalog(catalog); 796 productsToSave.add(product); 797 } 798 } 799 saveProducts(productsToSave); 800 } 801 } 802 public void moveProductsToCatalog(String [] inProductid, Category inCatalog1, Category inCatalog2) throws StoreException 803 { 804 List productsToSave = new ArrayList (); 805 806 if (inCatalog1 != null && inCatalog2 != null && inCatalog1 != inCatalog2) 807 { 808 for (int i = 0; i < inProductid.length; i++) 809 { 810 Product product = getProduct(inProductid[i]); 811 if (product != null) 812 { 813 product.removeCatalog(inCatalog1); 814 product.addCatalog(inCatalog2); 815 productsToSave.add(product); 816 } 817 } 818 saveProducts(productsToSave); 819 } 820 821 822 } 823 824 } | Popular Tags |