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, false);
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, false);
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, false);
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, false);
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, false);
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, false);
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(
669 PwdGenerator.KEY1 + PwdGenerator.KEY2, 12) + ".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(
692 PwdGenerator.KEY1 + PwdGenerator.KEY2, 12) + ".jpg");
693
694 byte[] mediumImageBytes = HttpUtil.URLtoByteArray(
695 amazonRankings.getMediumImageURL());
696
697 if (mediumImageBytes.length < 1024) {
698 mediumImage = false;
699 }
700 else {
701 OutputStream os = new FileOutputStream(mediumImageFile);
702
703 os.write(mediumImageBytes);
704
705 os.close();
706 }
707
708
709
710 boolean largeImage = true;
711 String largeImageURL = StringPool.BLANK;
712 File largeImageFile = new File(
713 tmpDir + File.separatorChar +
714 PwdGenerator.getPassword(
715 PwdGenerator.KEY1 + PwdGenerator.KEY2, 12) + ".jpg");
716
717 byte[] largeImageBytes = HttpUtil.URLtoByteArray(
718 amazonRankings.getLargeImageURL());
719
720 if (largeImageBytes.length < 1024) {
721 largeImage = false;
722 }
723 else {
724 OutputStream os = new FileOutputStream(largeImageFile);
725
726 os.write(largeImageBytes);
727
728 os.close();
729 }
730
731 List<ShoppingItemField> itemFields =
732 new ArrayList<ShoppingItemField>();
733
734 List<ShoppingItemPrice> itemPrices =
735 new ArrayList<ShoppingItemPrice>();
736
737 itemPrices.add(itemPrice);
738
739 ServiceContext serviceContext = new ServiceContext();
740
741 serviceContext.setAddGroupPermissions(true);
742 serviceContext.setAddGuestPermissions(true);
743
744 addItem(
745 userId, groupId, categoryId, isbn, name, description,
746 properties, StringPool.BLANK, requiresShipping, stockQuantity,
747 featured, sale, smallImage, smallImageURL, smallImageFile,
748 mediumImage, mediumImageURL, mediumImageFile, largeImage,
749 largeImageURL, largeImageFile, itemFields, itemPrices,
750 serviceContext);
751
752 smallImageFile.delete();
753 mediumImageFile.delete();
754 largeImageFile.delete();
755 }
756 }
757
758 protected String getBookProperties(AmazonRankings amazonRankings) {
759 String isbn = amazonRankings.getISBN();
760
761 String authors = StringUtil.merge(amazonRankings.getAuthors(), ", ");
762
763 String publisher =
764 amazonRankings.getManufacturer() + "; (" +
765 amazonRankings.getReleaseDateAsString() + ")";
766
767 String properties =
768 "ISBN=" + isbn + "\nAuthor=" + authors + "\nPublisher=" + publisher;
769
770 return properties;
771 }
772
773 protected long getCategory(ShoppingItem item, long categoryId)
774 throws SystemException {
775
776 if ((item.getCategoryId() != categoryId) &&
777 (categoryId !=
778 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
779
780 ShoppingCategory newCategory =
781 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
782
783 if ((newCategory == null) ||
784 (item.getGroupId() != newCategory.getGroupId())) {
785
786 categoryId = item.getCategoryId();
787 }
788 }
789
790 return categoryId;
791 }
792
793 protected void saveImages(
794 boolean smallImage, long smallImageId, File smallImageFile,
795 byte[] smallImageBytes, boolean mediumImage, long mediumImageId,
796 File mediumImageFile, byte[] mediumImageBytes, boolean largeImage,
797 long largeImageId, File largeImageFile, byte[] largeImageBytes)
798 throws PortalException, SystemException {
799
800
801
802 if (smallImage) {
803 if ((smallImageFile != null) && (smallImageBytes != null)) {
804 imageLocalService.updateImage(smallImageId, smallImageBytes);
805 }
806 }
807 else {
808 imageLocalService.deleteImage(smallImageId);
809 }
810
811
812
813 if (mediumImage) {
814 if ((mediumImageFile != null) && (mediumImageBytes != null)) {
815 imageLocalService.updateImage(mediumImageId, mediumImageBytes);
816 }
817 }
818 else {
819 imageLocalService.deleteImage(mediumImageId);
820 }
821
822
823
824 if (largeImage) {
825 if ((largeImageFile != null) && (largeImageBytes != null)) {
826 imageLocalService.updateImage(largeImageId, largeImageBytes);
827 }
828 }
829 else {
830 imageLocalService.deleteImage(largeImageId);
831 }
832 }
833
834 protected void validate(
835 long companyId, long itemId, String sku, String name,
836 boolean smallImage, String smallImageURL, File smallImageFile,
837 byte[] smallImageBytes, boolean mediumImage, String mediumImageURL,
838 File mediumImageFile, byte[] mediumImageBytes, boolean largeImage,
839 String largeImageURL, File largeImageFile, byte[] largeImageBytes)
840 throws PortalException, SystemException {
841
842 if (Validator.isNull(sku)) {
843 throw new ItemSKUException();
844 }
845
846 ShoppingItem item = shoppingItemPersistence.fetchByC_S(companyId, sku);
847
848 if (item != null) {
849 if (itemId > 0) {
850 if (item.getItemId() != itemId) {
851 throw new DuplicateItemSKUException();
852 }
853 }
854 else {
855 throw new DuplicateItemSKUException();
856 }
857 }
858
859 if (Validator.isNull(name)) {
860 throw new ItemNameException();
861 }
862
863 String[] imageExtensions = PrefsPropsUtil.getStringArray(
864 PropsKeys.SHOPPING_IMAGE_EXTENSIONS, StringPool.COMMA);
865
866
867
868 if (smallImage && Validator.isNull(smallImageURL) &&
869 (smallImageFile != null) && (smallImageBytes != null)) {
870
871 String smallImageName = smallImageFile.getName();
872
873 if (smallImageName != null) {
874 boolean validSmallImageExtension = false;
875
876 for (int i = 0; i < imageExtensions.length; i++) {
877 if (StringPool.STAR.equals(imageExtensions[i]) ||
878 StringUtil.endsWith(
879 smallImageName, imageExtensions[i])) {
880
881 validSmallImageExtension = true;
882
883 break;
884 }
885 }
886
887 if (!validSmallImageExtension) {
888 throw new ItemSmallImageNameException(smallImageName);
889 }
890 }
891
892 long smallImageMaxSize = PrefsPropsUtil.getLong(
893 PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);
894
895 if ((smallImageMaxSize > 0) &&
896 ((smallImageBytes == null) ||
897 (smallImageBytes.length > smallImageMaxSize))) {
898
899 throw new ItemSmallImageSizeException();
900 }
901 }
902
903
904
905 if (mediumImage && Validator.isNull(mediumImageURL) &&
906 (mediumImageFile != null) && (mediumImageBytes != null)) {
907
908 String mediumImageName = mediumImageFile.getName();
909
910 if (mediumImageName != null) {
911 boolean validMediumImageExtension = false;
912
913 for (int i = 0; i < imageExtensions.length; i++) {
914 if (StringPool.STAR.equals(imageExtensions[i]) ||
915 StringUtil.endsWith(
916 mediumImageName, imageExtensions[i])) {
917
918 validMediumImageExtension = true;
919
920 break;
921 }
922 }
923
924 if (!validMediumImageExtension) {
925 throw new ItemMediumImageNameException(mediumImageName);
926 }
927 }
928
929 long mediumImageMaxSize = PrefsPropsUtil.getLong(
930 PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);
931
932 if ((mediumImageMaxSize > 0) &&
933 ((mediumImageBytes == null) ||
934 (mediumImageBytes.length > mediumImageMaxSize))) {
935
936 throw new ItemMediumImageSizeException();
937 }
938 }
939
940
941
942 if (!largeImage || Validator.isNotNull(largeImageURL) ||
943 (largeImageFile == null) || (largeImageBytes == null)) {
944
945 return;
946 }
947
948 String largeImageName = largeImageFile.getName();
949
950 if (largeImageName != null) {
951 boolean validLargeImageExtension = false;
952
953 for (int i = 0; i < imageExtensions.length; i++) {
954 if (StringPool.STAR.equals(imageExtensions[i]) ||
955 StringUtil.endsWith(
956 largeImageName, imageExtensions[i])) {
957
958 validLargeImageExtension = true;
959
960 break;
961 }
962 }
963
964 if (!validLargeImageExtension) {
965 throw new ItemLargeImageNameException(largeImageName);
966 }
967 }
968
969 long largeImageMaxSize = PrefsPropsUtil.getLong(
970 PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE);
971
972 if ((largeImageMaxSize > 0) &&
973 ((largeImageBytes == null) ||
974 (largeImageBytes.length > largeImageMaxSize))) {
975
976 throw new ItemLargeImageSizeException();
977 }
978 }
979
980 }