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