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