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