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