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