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