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