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