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