1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.HttpUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.ResourceConstants;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.util.PrefsPropsUtil;
36  import com.liferay.portal.util.PropsKeys;
37  import com.liferay.portlet.amazonrankings.model.AmazonRankings;
38  import com.liferay.portlet.amazonrankings.util.AmazonRankingsUtil;
39  import com.liferay.portlet.shopping.DuplicateItemSKUException;
40  import com.liferay.portlet.shopping.ItemLargeImageNameException;
41  import com.liferay.portlet.shopping.ItemLargeImageSizeException;
42  import com.liferay.portlet.shopping.ItemMediumImageNameException;
43  import com.liferay.portlet.shopping.ItemMediumImageSizeException;
44  import com.liferay.portlet.shopping.ItemNameException;
45  import com.liferay.portlet.shopping.ItemSKUException;
46  import com.liferay.portlet.shopping.ItemSmallImageNameException;
47  import com.liferay.portlet.shopping.ItemSmallImageSizeException;
48  import com.liferay.portlet.shopping.model.ShoppingCategory;
49  import com.liferay.portlet.shopping.model.ShoppingItem;
50  import com.liferay.portlet.shopping.model.ShoppingItemField;
51  import com.liferay.portlet.shopping.model.ShoppingItemPrice;
52  import com.liferay.portlet.shopping.model.impl.ShoppingItemPriceImpl;
53  import com.liferay.portlet.shopping.service.base.ShoppingItemLocalServiceBaseImpl;
54  import com.liferay.util.PwdGenerator;
55  import com.liferay.util.SystemProperties;
56  
57  import java.io.File;
58  import java.io.FileOutputStream;
59  import java.io.IOException;
60  import java.io.OutputStream;
61  
62  import java.util.ArrayList;
63  import java.util.Date;
64  import java.util.List;
65  
66  /**
67   * <a href="ShoppingItemLocalServiceImpl.java.html"><b><i>View Source</i></b>
68   * </a>
69   *
70   * @author Brian Wing Shun Chan
71   */
72  public class ShoppingItemLocalServiceImpl
73      extends ShoppingItemLocalServiceBaseImpl {
74  
75      public void addBookItems(long userId, long categoryId, String[] isbns)
76          throws PortalException, SystemException {
77  
78          try {
79              doAddBookItems(userId, categoryId, isbns);
80          }
81          catch (IOException ioe) {
82              throw new SystemException(ioe);
83          }
84      }
85  
86      public ShoppingItem addItem(
87              long userId, long categoryId, String sku, String name,
88              String description, String properties, String fieldsQuantities,
89              boolean requiresShipping, int stockQuantity, boolean featured,
90              Boolean sale, boolean smallImage, String smallImageURL,
91              File smallFile, boolean mediumImage, String mediumImageURL,
92              File mediumFile, boolean largeImage, String largeImageURL,
93              File largeFile, List<ShoppingItemField> itemFields,
94              List<ShoppingItemPrice> itemPrices, boolean addCommunityPermissions,
95              boolean addGuestPermissions)
96          throws PortalException, SystemException {
97  
98          return addItem(
99              userId, categoryId, sku, name, description, properties,
100             fieldsQuantities, requiresShipping, stockQuantity, featured, sale,
101             smallImage, smallImageURL, smallFile, mediumImage, mediumImageURL,
102             mediumFile, largeImage, largeImageURL, largeFile, itemFields,
103             itemPrices, Boolean.valueOf(addCommunityPermissions),
104             Boolean.valueOf(addGuestPermissions), null, null);
105     }
106 
107     public ShoppingItem addItem(
108             long userId, long categoryId, String sku, String name,
109             String description, String properties, String fieldsQuantities,
110             boolean requiresShipping, int stockQuantity, boolean featured,
111             Boolean sale, boolean smallImage, String smallImageURL,
112             File smallFile, boolean mediumImage, String mediumImageURL,
113             File mediumFile, boolean largeImage, String largeImageURL,
114             File largeFile, List<ShoppingItemField> itemFields,
115             List<ShoppingItemPrice> itemPrices, String[] communityPermissions,
116             String[] guestPermissions)
117         throws PortalException, SystemException {
118 
119         return addItem(
120             userId, categoryId, sku, name, description, properties,
121             fieldsQuantities, requiresShipping, stockQuantity, featured, sale,
122             smallImage, smallImageURL, smallFile, mediumImage, mediumImageURL,
123             mediumFile, largeImage, largeImageURL, largeFile, itemFields,
124             itemPrices, null, null, communityPermissions, guestPermissions);
125     }
126 
127     public ShoppingItem addItem(
128             long userId, long categoryId, String sku, String name,
129             String description, String properties, String fieldsQuantities,
130             boolean requiresShipping, int stockQuantity, boolean featured,
131             Boolean sale, boolean smallImage, String smallImageURL,
132             File smallFile, boolean mediumImage, String mediumImageURL,
133             File mediumFile, boolean largeImage, String largeImageURL,
134             File largeFile, List<ShoppingItemField> itemFields,
135             List<ShoppingItemPrice> itemPrices, Boolean addCommunityPermissions,
136             Boolean addGuestPermissions, String[] communityPermissions,
137             String[] guestPermissions)
138         throws PortalException, SystemException {
139 
140         // Item
141 
142         User user = userPersistence.findByPrimaryKey(userId);
143         ShoppingCategory category =
144             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
145         sku = sku.trim().toUpperCase();
146 
147         byte[] smallBytes = null;
148         byte[] mediumBytes = null;
149         byte[] largeBytes = null;
150 
151         try {
152             smallBytes = FileUtil.getBytes(smallFile);
153             mediumBytes = FileUtil.getBytes(mediumFile);
154             largeBytes = FileUtil.getBytes(largeFile);
155         }
156         catch (IOException ioe) {
157         }
158 
159         Date now = new Date();
160 
161         validate(
162             user.getCompanyId(), 0, sku, name, smallImage, smallImageURL,
163             smallFile, smallBytes, mediumImage, mediumImageURL, mediumFile,
164             mediumBytes, largeImage, largeImageURL, largeFile, largeBytes);
165 
166         long itemId = counterLocalService.increment();
167 
168         ShoppingItem item = shoppingItemPersistence.create(itemId);
169 
170         item.setCompanyId(user.getCompanyId());
171         item.setUserId(user.getUserId());
172         item.setUserName(user.getFullName());
173         item.setCreateDate(now);
174         item.setModifiedDate(now);
175         item.setCategoryId(categoryId);
176         item.setSku(sku);
177         item.setName(name);
178         item.setDescription(description);
179         item.setProperties(properties);
180         item.setFields(itemFields.size() > 0);
181         item.setFieldsQuantities(fieldsQuantities);
182 
183         for (ShoppingItemPrice itemPrice : itemPrices) {
184             if (itemPrice.getStatus() ==
185                     ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT) {
186 
187                 item.setMinQuantity(itemPrice.getMinQuantity());
188                 item.setMaxQuantity(itemPrice.getMaxQuantity());
189                 item.setPrice(itemPrice.getPrice());
190                 item.setDiscount(itemPrice.getDiscount());
191                 item.setTaxable(itemPrice.getTaxable());
192                 item.setShipping(itemPrice.getShipping());
193                 item.setUseShippingFormula(
194                     itemPrice.getUseShippingFormula());
195             }
196 
197             if ((sale == null) && (itemPrice.getDiscount() > 0) &&
198                 ((itemPrice.getStatus() ==
199                     ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT) ||
200                 (itemPrice.getStatus() ==
201                     ShoppingItemPriceImpl.STATUS_ACTIVE))) {
202 
203                 sale = Boolean.TRUE;
204             }
205         }
206 
207         item.setRequiresShipping(requiresShipping);
208         item.setStockQuantity(stockQuantity);
209         item.setFeatured(featured);
210         item.setSale((sale != null) ? sale.booleanValue() : false);
211         item.setSmallImage(smallImage);
212         item.setSmallImageId(counterLocalService.increment());
213         item.setSmallImageURL(smallImageURL);
214         item.setMediumImage(mediumImage);
215         item.setMediumImageId(counterLocalService.increment());
216         item.setMediumImageURL(mediumImageURL);
217         item.setLargeImage(largeImage);
218         item.setLargeImageId(counterLocalService.increment());
219         item.setLargeImageURL(largeImageURL);
220 
221         shoppingItemPersistence.update(item, false);
222 
223         // Fields
224 
225         for (ShoppingItemField itemField : itemFields) {
226             long itemFieldId = counterLocalService.increment();
227 
228             itemField.setItemFieldId(itemFieldId);
229             itemField.setItemId(itemId);
230             itemField.setName(checkItemField(itemField.getName()));
231             itemField.setValues(checkItemField(itemField.getValues()));
232 
233             shoppingItemFieldPersistence.update(itemField, false);
234         }
235 
236         // Prices
237 
238         if (itemPrices.size() > 1) {
239             for (ShoppingItemPrice itemPrice : itemPrices) {
240                 long itemPriceId = counterLocalService.increment();
241 
242                 itemPrice.setItemPriceId(itemPriceId);
243                 itemPrice.setItemId(itemId);
244 
245                 shoppingItemPricePersistence.update(itemPrice, false);
246             }
247         }
248 
249         // Images
250 
251         saveImages(
252             smallImage, item.getSmallImageId(), smallFile, smallBytes,
253             mediumImage, item.getMediumImageId(), mediumFile, mediumBytes,
254             largeImage, item.getLargeImageId(), largeFile, largeBytes);
255 
256         // Resources
257 
258         if ((addCommunityPermissions != null) &&
259             (addGuestPermissions != null)) {
260 
261             addItemResources(
262                 category, item, addCommunityPermissions.booleanValue(),
263                 addGuestPermissions.booleanValue());
264         }
265         else {
266             addItemResources(
267                 category, item, communityPermissions, guestPermissions);
268         }
269 
270         return item;
271     }
272 
273     public void addItemResources(
274             long itemId, boolean addCommunityPermissions,
275             boolean addGuestPermissions)
276         throws PortalException, SystemException {
277 
278         ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
279         ShoppingCategory category = item.getCategory();
280 
281         addItemResources(
282             category, item, addCommunityPermissions, addGuestPermissions);
283     }
284 
285     public void addItemResources(
286             ShoppingCategory category, ShoppingItem item,
287             boolean addCommunityPermissions, boolean addGuestPermissions)
288         throws PortalException, SystemException {
289 
290         resourceLocalService.addResources(
291             item.getCompanyId(), category.getGroupId(), item.getUserId(),
292             ShoppingItem.class.getName(), item.getItemId(), false,
293             addCommunityPermissions, addGuestPermissions);
294     }
295 
296     public void addItemResources(
297             long itemId, String[] communityPermissions,
298             String[] guestPermissions)
299         throws PortalException, SystemException {
300 
301         ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
302         ShoppingCategory category = item.getCategory();
303 
304         addItemResources(
305             category, item, communityPermissions, guestPermissions);
306     }
307 
308     public void addItemResources(
309             ShoppingCategory category, ShoppingItem item,
310             String[] communityPermissions, String[] guestPermissions)
311         throws PortalException, SystemException {
312 
313         resourceLocalService.addModelResources(
314             item.getCompanyId(), category.getGroupId(), item.getUserId(),
315             ShoppingItem.class.getName(), item.getItemId(),
316             communityPermissions, guestPermissions);
317     }
318 
319     public void deleteItem(long itemId)
320         throws PortalException, SystemException {
321 
322         ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
323 
324         deleteItem(item);
325     }
326 
327     public void deleteItem(ShoppingItem item)
328         throws PortalException, SystemException {
329 
330         // Fields
331 
332         shoppingItemFieldPersistence.removeByItemId(item.getItemId());
333 
334         // Prices
335 
336         shoppingItemPricePersistence.removeByItemId(item.getItemId());
337 
338         // Images
339 
340         imageLocalService.deleteImage(item.getSmallImageId());
341         imageLocalService.deleteImage(item.getMediumImageId());
342         imageLocalService.deleteImage(item.getLargeImageId());
343 
344         // Resources
345 
346         resourceLocalService.deleteResource(
347             item.getCompanyId(), ShoppingItem.class.getName(),
348             ResourceConstants.SCOPE_INDIVIDUAL, item.getItemId());
349 
350         // Item
351 
352         shoppingItemPersistence.remove(item);
353     }
354 
355     public void deleteItems(long categoryId)
356         throws PortalException, SystemException {
357 
358         List<ShoppingItem> items = shoppingItemPersistence.findByCategoryId(
359             categoryId);
360 
361         for (ShoppingItem item : items) {
362             deleteItem(item);
363         }
364     }
365 
366     public int getCategoriesItemsCount(List<Long> categoryIds)
367         throws SystemException {
368 
369         return shoppingItemFinder.countByCategoryIds(categoryIds);
370     }
371 
372     public List<ShoppingItem> getFeaturedItems(
373             long groupId, long categoryId, int numOfItems)
374         throws SystemException {
375 
376         List<ShoppingItem> featuredItems = shoppingItemFinder.findByFeatured(
377             groupId, new long[] {categoryId}, numOfItems);
378 
379         if (featuredItems.size() == 0) {
380             List<ShoppingCategory> childCategories =
381                 shoppingCategoryPersistence.findByG_P(groupId, categoryId);
382 
383             if (childCategories.size() > 0) {
384                 long[] categoryIds = new long[childCategories.size()];
385 
386                 for (int i = 0; i < childCategories.size(); i++) {
387                     ShoppingCategory childCategory = childCategories.get(i);
388 
389                     categoryIds[i] = childCategory.getCategoryId();
390                 }
391 
392                 featuredItems = shoppingItemFinder.findByFeatured(
393                     groupId, categoryIds, numOfItems);
394             }
395         }
396 
397         return featuredItems;
398     }
399 
400     public ShoppingItem getItem(long itemId)
401         throws PortalException, SystemException {
402 
403         return shoppingItemPersistence.findByPrimaryKey(itemId);
404     }
405 
406     public ShoppingItem getItem(long companyId, String sku)
407         throws PortalException, SystemException {
408 
409         return shoppingItemPersistence.findByC_S(companyId, sku);
410     }
411 
412     public ShoppingItem getItemByLargeImageId(long largeImageId)
413         throws PortalException, SystemException {
414 
415         return shoppingItemPersistence.findByLargeImageId(largeImageId);
416     }
417 
418     public ShoppingItem getItemByMediumImageId(long mediumImageId)
419         throws PortalException, SystemException {
420 
421         return shoppingItemPersistence.findByMediumImageId(mediumImageId);
422     }
423 
424     public ShoppingItem getItemBySmallImageId(long smallImageId)
425         throws PortalException, SystemException {
426 
427         return shoppingItemPersistence.findBySmallImageId(smallImageId);
428     }
429 
430     public List<ShoppingItem> getItems(long categoryId) throws SystemException {
431         return shoppingItemPersistence.findByCategoryId(categoryId);
432     }
433 
434     public List<ShoppingItem> getItems(
435             long categoryId, int start, int end, OrderByComparator obc)
436         throws SystemException {
437 
438         return shoppingItemPersistence.findByCategoryId(
439             categoryId, start, end, obc);
440     }
441 
442     public ShoppingItem[] getItemsPrevAndNext(
443             long itemId, OrderByComparator obc)
444         throws PortalException, SystemException {
445 
446         ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
447 
448         return shoppingItemPersistence.findByCategoryId_PrevAndNext(
449             item.getItemId(), item.getCategoryId(), obc);
450     }
451 
452     public int getItemsCount(long categoryId) throws SystemException {
453         return shoppingItemPersistence.countByCategoryId(categoryId);
454     }
455 
456     public List<ShoppingItem> getSaleItems(
457             long groupId, long categoryId, int numOfItems)
458         throws SystemException {
459 
460         List<ShoppingItem> saleItems = shoppingItemFinder.findBySale(
461             groupId, new long[] {categoryId}, numOfItems);
462 
463         if (saleItems.size() == 0) {
464             List<ShoppingCategory> childCategories =
465                 shoppingCategoryPersistence.findByG_P(groupId, categoryId);
466 
467             if (childCategories.size() > 0) {
468                 long[] categoryIds = new long[childCategories.size()];
469 
470                 for (int i = 0; i < childCategories.size(); i++) {
471                     ShoppingCategory childCategory = childCategories.get(i);
472 
473                     categoryIds[i] = childCategory.getCategoryId();
474                 }
475 
476                 saleItems = shoppingItemFinder.findBySale(
477                     groupId, categoryIds, numOfItems);
478             }
479         }
480 
481         return saleItems;
482     }
483 
484     public List<ShoppingItem> search(
485             long groupId, long[] categoryIds, String keywords, int start,
486             int end)
487         throws SystemException {
488 
489         return shoppingItemFinder.findByKeywords(
490             groupId, categoryIds, keywords, start, end);
491     }
492 
493     public int searchCount(long groupId, long[] categoryIds, String keywords)
494         throws SystemException {
495 
496         return shoppingItemFinder.countByKeywords(
497             groupId, categoryIds, keywords);
498     }
499 
500     public ShoppingItem updateItem(
501             long userId, long itemId, long categoryId, String sku, String name,
502             String description, String properties, String fieldsQuantities,
503             boolean requiresShipping, int stockQuantity, boolean featured,
504             Boolean sale, boolean smallImage, String smallImageURL,
505             File smallFile, boolean mediumImage, String mediumImageURL,
506             File mediumFile, boolean largeImage, String largeImageURL,
507             File largeFile, List<ShoppingItemField> itemFields,
508             List<ShoppingItemPrice> itemPrices)
509         throws PortalException, SystemException {
510 
511         // Item
512 
513         ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
514 
515         User user = userPersistence.findByPrimaryKey(userId);
516         ShoppingCategory category = getCategory(item, categoryId);
517         sku = sku.trim().toUpperCase();
518 
519         byte[] smallBytes = null;
520         byte[] mediumBytes = null;
521         byte[] largeBytes = null;
522 
523         try {
524             smallBytes = FileUtil.getBytes(smallFile);
525             mediumBytes = FileUtil.getBytes(mediumFile);
526             largeBytes = FileUtil.getBytes(largeFile);
527         }
528         catch (IOException ioe) {
529         }
530 
531         validate(
532             user.getCompanyId(), itemId, sku, name, smallImage, smallImageURL,
533             smallFile, smallBytes, mediumImage, mediumImageURL, mediumFile,
534             mediumBytes, largeImage, largeImageURL, largeFile, largeBytes);
535 
536         item.setModifiedDate(new Date());
537         item.setCategoryId(category.getCategoryId());
538         item.setSku(sku);
539         item.setName(name);
540         item.setDescription(description);
541         item.setProperties(properties);
542         item.setFields(itemFields.size() > 0);
543         item.setFieldsQuantities(fieldsQuantities);
544 
545         for (ShoppingItemPrice itemPrice : itemPrices) {
546             if (itemPrice.getStatus() ==
547                     ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT) {
548 
549                 item.setMinQuantity(itemPrice.getMinQuantity());
550                 item.setMaxQuantity(itemPrice.getMaxQuantity());
551                 item.setPrice(itemPrice.getPrice());
552                 item.setDiscount(itemPrice.getDiscount());
553                 item.setTaxable(itemPrice.getTaxable());
554                 item.setShipping(itemPrice.getShipping());
555                 item.setUseShippingFormula(
556                     itemPrice.getUseShippingFormula());
557             }
558 
559             if ((sale == null) && (itemPrice.getDiscount() > 0) &&
560                 ((itemPrice.getStatus() ==
561                     ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT) ||
562                 (itemPrice.getStatus() ==
563                     ShoppingItemPriceImpl.STATUS_ACTIVE))) {
564 
565                 sale = Boolean.TRUE;
566             }
567         }
568 
569         item.setRequiresShipping(requiresShipping);
570         item.setStockQuantity(stockQuantity);
571         item.setFeatured(featured);
572         item.setSale((sale != null) ? sale.booleanValue() : false);
573         item.setSmallImage(smallImage);
574         item.setSmallImageURL(smallImageURL);
575         item.setMediumImage(mediumImage);
576         item.setMediumImageURL(mediumImageURL);
577         item.setLargeImage(largeImage);
578         item.setLargeImageURL(largeImageURL);
579 
580         shoppingItemPersistence.update(item, false);
581 
582         // Fields
583 
584         shoppingItemFieldPersistence.removeByItemId(itemId);
585 
586         for (ShoppingItemField itemField : itemFields) {
587             long itemFieldId = counterLocalService.increment();
588 
589             itemField.setItemFieldId(itemFieldId);
590             itemField.setItemId(itemId);
591             itemField.setName(checkItemField(itemField.getName()));
592             itemField.setValues(checkItemField(itemField.getValues()));
593 
594             shoppingItemFieldPersistence.update(itemField, false);
595         }
596 
597         // Prices
598 
599         shoppingItemPricePersistence.removeByItemId(itemId);
600 
601         if (itemPrices.size() > 1) {
602             for (ShoppingItemPrice itemPrice : itemPrices) {
603                 long itemPriceId = counterLocalService.increment();
604 
605                 itemPrice.setItemPriceId(itemPriceId);
606                 itemPrice.setItemId(itemId);
607 
608                 shoppingItemPricePersistence.update(itemPrice, false);
609             }
610         }
611 
612         // Images
613 
614         saveImages(
615             smallImage, item.getSmallImageId(), smallFile, smallBytes,
616             mediumImage, item.getMediumImageId(), mediumFile, mediumBytes,
617             largeImage, item.getLargeImageId(), largeFile, largeBytes);
618 
619         return item;
620     }
621 
622     protected void doAddBookItems(long userId, long categoryId, String[] isbns)
623         throws IOException, PortalException, SystemException {
624 
625         String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
626 
627         for (int i = 0; (i < isbns.length) && (i < 50); i++) {
628             String isbn = isbns[i];
629 
630             AmazonRankings amazonRankings =
631                 AmazonRankingsUtil.getAmazonRankings(isbn);
632 
633             if (amazonRankings == null) {
634                 continue;
635             }
636 
637             String name = amazonRankings.getProductName();
638             String description = StringPool.BLANK;
639             String properties = getBookProperties(amazonRankings);
640 
641             int minQuantity = 0;
642             int maxQuantity = 0;
643             double price = amazonRankings.getListPrice();
644             double discount = 1 - amazonRankings.getOurPrice() / price;
645             boolean taxable = true;
646             double shipping = 0.0;
647             boolean useShippingFormula = true;
648 
649             ShoppingItemPrice itemPrice =
650                 shoppingItemPricePersistence.create(0);
651 
652             itemPrice.setMinQuantity(minQuantity);
653             itemPrice.setMaxQuantity(maxQuantity);
654             itemPrice.setPrice(price);
655             itemPrice.setDiscount(discount);
656             itemPrice.setTaxable(taxable);
657             itemPrice.setShipping(shipping);
658             itemPrice.setUseShippingFormula(useShippingFormula);
659             itemPrice.setStatus(ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT);
660 
661             boolean requiresShipping = true;
662             int stockQuantity = 0;
663             boolean featured = false;
664             Boolean sale = null;
665 
666             // Small image
667 
668             boolean smallImage = true;
669             String smallImageURL = StringPool.BLANK;
670             File smallFile = new File(
671                 tmpDir + File.separatorChar +
672                 PwdGenerator.getPassword(
673                     PwdGenerator.KEY1 + PwdGenerator.KEY2, 12) + ".jpg");
674 
675             byte[] smallBytes = HttpUtil.URLtoByteArray(
676                 amazonRankings.getSmallImageURL());
677 
678             if (smallBytes.length < 1024) {
679                 smallImage = false;
680             }
681             else {
682                 OutputStream os = new FileOutputStream(smallFile);
683 
684                 os.write(smallBytes);
685 
686                 os.close();
687             }
688 
689             // Medium image
690 
691             boolean mediumImage = true;
692             String mediumImageURL = StringPool.BLANK;
693             File mediumFile = new File(
694                 tmpDir + File.separatorChar +
695                 PwdGenerator.getPassword(
696                     PwdGenerator.KEY1 + PwdGenerator.KEY2, 12) + ".jpg");
697 
698             byte[] mediumBytes = HttpUtil.URLtoByteArray(
699                 amazonRankings.getMediumImageURL());
700 
701             if (mediumBytes.length < 1024) {
702                 mediumImage = false;
703             }
704             else {
705                 OutputStream os = new FileOutputStream(mediumFile);
706 
707                 os.write(mediumBytes);
708 
709                 os.close();
710             }
711 
712             // Large image
713 
714             boolean largeImage = true;
715             String largeImageURL = StringPool.BLANK;
716             File largeFile = new File(
717                 tmpDir + File.separatorChar +
718                 PwdGenerator.getPassword(
719                     PwdGenerator.KEY1 + PwdGenerator.KEY2, 12) + ".jpg");
720 
721             byte[] largeBytes = HttpUtil.URLtoByteArray(
722                 amazonRankings.getLargeImageURL());
723 
724             if (largeBytes.length < 1024) {
725                 largeImage = false;
726             }
727             else {
728                 OutputStream os = new FileOutputStream(largeFile);
729 
730                 os.write(largeBytes);
731 
732                 os.close();
733             }
734 
735             List<ShoppingItemField> itemFields =
736                 new ArrayList<ShoppingItemField>();
737 
738             List<ShoppingItemPrice> itemPrices =
739                 new ArrayList<ShoppingItemPrice>();
740 
741             itemPrices.add(itemPrice);
742 
743             boolean addCommunityPermissions = true;
744             boolean addGuestPermissions = true;
745 
746             addItem(
747                 userId, categoryId, isbn, name, description, properties,
748                 StringPool.BLANK, requiresShipping, stockQuantity, featured,
749                 sale, smallImage, smallImageURL, smallFile, mediumImage,
750                 mediumImageURL, mediumFile, largeImage, largeImageURL,
751                 largeFile, itemFields, itemPrices, addCommunityPermissions,
752                 addGuestPermissions);
753 
754             smallFile.delete();
755             mediumFile.delete();
756             largeFile.delete();
757         }
758     }
759 
760     protected String checkItemField(String value) {
761         return StringUtil.replace(
762             value,
763             new String[] {
764                 "\"", "&", "'", ".", "=", "|"
765             },
766             new String[] {
767                 StringPool.BLANK,
768                 StringPool.BLANK,
769                 StringPool.BLANK,
770                 StringPool.BLANK,
771                 StringPool.BLANK,
772                 StringPool.BLANK
773             }
774         );
775     }
776 
777     protected String getBookProperties(AmazonRankings amazonRankings) {
778         String isbn = amazonRankings.getISBN();
779 
780         String authors = StringUtil.merge(amazonRankings.getAuthors(), ", ");
781 
782         String publisher =
783             amazonRankings.getManufacturer() + "; (" +
784             amazonRankings.getReleaseDateAsString() + ")";
785 
786         String properties =
787             "ISBN=" + isbn + "\nAuthor=" + authors + "\nPublisher=" + publisher;
788 
789         return properties;
790     }
791 
792     protected ShoppingCategory getCategory(ShoppingItem item, long categoryId)
793         throws PortalException, SystemException {
794 
795         if (item.getCategoryId() != categoryId) {
796             ShoppingCategory oldCategory =
797                 shoppingCategoryPersistence.findByPrimaryKey(
798                     item.getCategoryId());
799 
800             ShoppingCategory newCategory =
801                 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
802 
803             if ((newCategory == null) ||
804                 (oldCategory.getGroupId() != newCategory.getGroupId())) {
805 
806                 categoryId = item.getCategoryId();
807             }
808         }
809 
810         return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
811     }
812 
813     protected void saveImages(
814             boolean smallImage, long smallImageId, File smallFile,
815             byte[] smallBytes, boolean mediumImage, long mediumImageId,
816             File mediumFile, byte[] mediumBytes, boolean largeImage,
817             long largeImageId, File largeFile, byte[] largeBytes)
818         throws PortalException, SystemException {
819 
820         // Small image
821 
822         if (smallImage) {
823             if ((smallFile != null) && (smallBytes != null)) {
824                 imageLocalService.updateImage(smallImageId, smallBytes);
825             }
826         }
827         else {
828             imageLocalService.deleteImage(smallImageId);
829         }
830 
831         // Medium image
832 
833         if (mediumImage) {
834             if ((mediumFile != null) && (mediumBytes != null)) {
835                 imageLocalService.updateImage(mediumImageId, mediumBytes);
836             }
837         }
838         else {
839             imageLocalService.deleteImage(mediumImageId);
840         }
841 
842         // Large image
843 
844         if (largeImage) {
845             if ((largeFile != null) && (largeBytes != null)) {
846                 imageLocalService.updateImage(largeImageId, largeBytes);
847             }
848         }
849         else {
850             imageLocalService.deleteImage(largeImageId);
851         }
852     }
853 
854     protected void validate(
855             long companyId, long itemId, String sku, String name,
856             boolean smallImage, String smallImageURL, File smallFile,
857             byte[] smallBytes, boolean mediumImage, String mediumImageURL,
858             File mediumFile, byte[] mediumBytes, boolean largeImage,
859             String largeImageURL, File largeFile, byte[] largeBytes)
860         throws PortalException, SystemException {
861 
862         if (Validator.isNull(sku)) {
863             throw new ItemSKUException();
864         }
865 
866         ShoppingItem item = shoppingItemPersistence.fetchByC_S(
867             companyId, sku);
868 
869         if (item != null) {
870             if (itemId > 0) {
871                 if (item.getItemId() != itemId) {
872                     throw new DuplicateItemSKUException();
873                 }
874             }
875             else {
876                 throw new DuplicateItemSKUException();
877             }
878         }
879 
880         if (Validator.isNull(name)) {
881             throw new ItemNameException();
882         }
883 
884         String[] imageExtensions = PrefsPropsUtil.getStringArray(
885             PropsKeys.SHOPPING_IMAGE_EXTENSIONS, StringPool.COMMA);
886 
887         // Small image
888 
889         if (smallImage && Validator.isNull(smallImageURL) &&
890             smallFile != null && smallBytes != null) {
891 
892             String smallImageName = smallFile.getName();
893 
894             if (smallImageName != null) {
895                 boolean validSmallImageExtension = false;
896 
897                 for (int i = 0; i < imageExtensions.length; i++) {
898                     if (StringPool.STAR.equals(imageExtensions[i]) ||
899                         StringUtil.endsWith(
900                             smallImageName, imageExtensions[i])) {
901 
902                         validSmallImageExtension = true;
903 
904                         break;
905                     }
906                 }
907 
908                 if (!validSmallImageExtension) {
909                     throw new ItemSmallImageNameException(smallImageName);
910                 }
911             }
912 
913             long smallImageMaxSize = PrefsPropsUtil.getLong(
914                 PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);
915 
916             if ((smallImageMaxSize > 0) &&
917                 ((smallBytes == null) ||
918                     (smallBytes.length > smallImageMaxSize))) {
919 
920                 throw new ItemSmallImageSizeException();
921             }
922         }
923 
924         // Medium image
925 
926         if (mediumImage && Validator.isNull(mediumImageURL) &&
927             mediumFile != null && mediumBytes != null) {
928 
929             String mediumImageName = mediumFile.getName();
930 
931             if (mediumImageName != null) {
932                 boolean validMediumImageExtension = false;
933 
934                 for (int i = 0; i < imageExtensions.length; i++) {
935                     if (StringPool.STAR.equals(imageExtensions[i]) ||
936                         StringUtil.endsWith(
937                             mediumImageName, imageExtensions[i])) {
938 
939                         validMediumImageExtension = true;
940 
941                         break;
942                     }
943                 }
944 
945                 if (!validMediumImageExtension) {
946                     throw new ItemMediumImageNameException(mediumImageName);
947                 }
948             }
949 
950             long mediumImageMaxSize = PrefsPropsUtil.getLong(
951                 PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);
952 
953             if ((mediumImageMaxSize > 0) &&
954                 ((mediumBytes == null) ||
955                     (mediumBytes.length > mediumImageMaxSize))) {
956 
957                 throw new ItemMediumImageSizeException();
958             }
959         }
960 
961         // Large image
962 
963         if (largeImage && Validator.isNull(largeImageURL) &&
964             largeFile != null && largeBytes != null) {
965 
966             String largeImageName = largeFile.getName();
967 
968             if (largeImageName != null) {
969                 boolean validLargeImageExtension = false;
970 
971                 for (int i = 0; i < imageExtensions.length; i++) {
972                     if (StringPool.STAR.equals(imageExtensions[i]) ||
973                         StringUtil.endsWith(
974                             largeImageName, imageExtensions[i])) {
975 
976                         validLargeImageExtension = true;
977 
978                         break;
979                     }
980                 }
981 
982                 if (!validLargeImageExtension) {
983                     throw new ItemLargeImageNameException(largeImageName);
984                 }
985             }
986 
987             long largeImageMaxSize = PrefsPropsUtil.getLong(
988                 PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE);
989 
990             if ((largeImageMaxSize > 0) &&
991                 ((largeBytes == null) ||
992                     (largeBytes.length > largeImageMaxSize))) {
993 
994                 throw new ItemLargeImageSizeException();
995             }
996         }
997     }
998 
999 }