001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.ResourceConstants;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PrefsPropsUtil;
029    import com.liferay.portlet.shopping.DuplicateItemFieldNameException;
030    import com.liferay.portlet.shopping.DuplicateItemSKUException;
031    import com.liferay.portlet.shopping.ItemLargeImageNameException;
032    import com.liferay.portlet.shopping.ItemLargeImageSizeException;
033    import com.liferay.portlet.shopping.ItemMediumImageNameException;
034    import com.liferay.portlet.shopping.ItemMediumImageSizeException;
035    import com.liferay.portlet.shopping.ItemNameException;
036    import com.liferay.portlet.shopping.ItemSKUException;
037    import com.liferay.portlet.shopping.ItemSmallImageNameException;
038    import com.liferay.portlet.shopping.ItemSmallImageSizeException;
039    import com.liferay.portlet.shopping.model.ShoppingCategory;
040    import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
041    import com.liferay.portlet.shopping.model.ShoppingItem;
042    import com.liferay.portlet.shopping.model.ShoppingItemField;
043    import com.liferay.portlet.shopping.model.ShoppingItemPrice;
044    import com.liferay.portlet.shopping.model.ShoppingItemPriceConstants;
045    import com.liferay.portlet.shopping.service.base.ShoppingItemLocalServiceBaseImpl;
046    
047    import java.io.File;
048    import java.io.IOException;
049    
050    import java.util.ArrayList;
051    import java.util.List;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     */
056    public class ShoppingItemLocalServiceImpl
057            extends ShoppingItemLocalServiceBaseImpl {
058    
059            @Override
060            public ShoppingItem addItem(
061                            long userId, long groupId, long categoryId, String sku, String name,
062                            String description, String properties, String fieldsQuantities,
063                            boolean requiresShipping, int stockQuantity, boolean featured,
064                            Boolean sale, boolean smallImage, String smallImageURL,
065                            File smallImageFile, boolean mediumImage, String mediumImageURL,
066                            File mediumImageFile, boolean largeImage, String largeImageURL,
067                            File largeImageFile, List<ShoppingItemField> itemFields,
068                            List<ShoppingItemPrice> itemPrices, ServiceContext serviceContext)
069                    throws PortalException {
070    
071                    // Item
072    
073                    User user = userPersistence.findByPrimaryKey(userId);
074                    sku = StringUtil.toUpperCase(sku.trim());
075    
076                    byte[] smallImageBytes = null;
077                    byte[] mediumImageBytes = null;
078                    byte[] largeImageBytes = null;
079    
080                    try {
081                            smallImageBytes = FileUtil.getBytes(smallImageFile);
082                            mediumImageBytes = FileUtil.getBytes(mediumImageFile);
083                            largeImageBytes = FileUtil.getBytes(largeImageFile);
084                    }
085                    catch (IOException ioe) {
086                    }
087    
088                    validate(
089                            user.getCompanyId(), 0, sku, name, smallImage, smallImageURL,
090                            smallImageFile, smallImageBytes, mediumImage, mediumImageURL,
091                            mediumImageFile, mediumImageBytes, largeImage, largeImageURL,
092                            largeImageFile, largeImageBytes, itemFields);
093    
094                    long itemId = counterLocalService.increment();
095    
096                    ShoppingItem item = shoppingItemPersistence.create(itemId);
097    
098                    item.setGroupId(groupId);
099                    item.setCompanyId(user.getCompanyId());
100                    item.setUserId(user.getUserId());
101                    item.setUserName(user.getFullName());
102                    item.setCategoryId(categoryId);
103                    item.setSku(sku);
104                    item.setName(name);
105                    item.setDescription(description);
106                    item.setProperties(properties);
107                    item.setFields(!itemFields.isEmpty());
108                    item.setFieldsQuantities(fieldsQuantities);
109    
110                    for (ShoppingItemPrice itemPrice : itemPrices) {
111                            if (itemPrice.getStatus() ==
112                                            ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT) {
113    
114                                    item.setMinQuantity(itemPrice.getMinQuantity());
115                                    item.setMaxQuantity(itemPrice.getMaxQuantity());
116                                    item.setPrice(itemPrice.getPrice());
117                                    item.setDiscount(itemPrice.getDiscount());
118                                    item.setTaxable(itemPrice.getTaxable());
119                                    item.setShipping(itemPrice.getShipping());
120                                    item.setUseShippingFormula(itemPrice.getUseShippingFormula());
121                            }
122    
123                            if ((sale == null) && (itemPrice.getDiscount() > 0) &&
124                                    ((itemPrice.getStatus() ==
125                                            ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT) ||
126                                     (itemPrice.getStatus() ==
127                                             ShoppingItemPriceConstants.STATUS_ACTIVE))) {
128    
129                                    sale = Boolean.TRUE;
130                            }
131                    }
132    
133                    item.setRequiresShipping(requiresShipping);
134                    item.setStockQuantity(stockQuantity);
135                    item.setFeatured(featured);
136                    item.setSale((sale != null) ? sale.booleanValue() : false);
137                    item.setSmallImage(smallImage);
138                    item.setSmallImageId(counterLocalService.increment());
139                    item.setSmallImageURL(smallImageURL);
140                    item.setMediumImage(mediumImage);
141                    item.setMediumImageId(counterLocalService.increment());
142                    item.setMediumImageURL(mediumImageURL);
143                    item.setLargeImage(largeImage);
144                    item.setLargeImageId(counterLocalService.increment());
145                    item.setLargeImageURL(largeImageURL);
146    
147                    shoppingItemPersistence.update(item);
148    
149                    // Resources
150    
151                    if (serviceContext.isAddGroupPermissions() ||
152                            serviceContext.isAddGuestPermissions()) {
153    
154                            addItemResources(
155                                    item, serviceContext.isAddGroupPermissions(),
156                                    serviceContext.isAddGuestPermissions());
157                    }
158                    else {
159                            addItemResources(
160                                    item, serviceContext.getGroupPermissions(),
161                                    serviceContext.getGuestPermissions());
162                    }
163    
164                    // Images
165    
166                    saveImages(
167                            smallImage, item.getSmallImageId(), smallImageFile, smallImageBytes,
168                            mediumImage, item.getMediumImageId(), mediumImageFile,
169                            mediumImageBytes, largeImage, item.getLargeImageId(),
170                            largeImageFile, largeImageBytes);
171    
172                    // Item fields
173    
174                    for (ShoppingItemField itemField : itemFields) {
175                            long itemFieldId = counterLocalService.increment();
176    
177                            itemField.setItemFieldId(itemFieldId);
178                            itemField.setItemId(itemId);
179                            itemField.setName(checkItemField(itemField.getName()));
180                            itemField.setValues(checkItemField(itemField.getValues()));
181    
182                            shoppingItemFieldPersistence.update(itemField);
183                    }
184    
185                    // Item prices
186    
187                    if (itemPrices.size() > 1) {
188                            for (ShoppingItemPrice itemPrice : itemPrices) {
189                                    long itemPriceId = counterLocalService.increment();
190    
191                                    itemPrice.setItemPriceId(itemPriceId);
192                                    itemPrice.setItemId(itemId);
193    
194                                    shoppingItemPricePersistence.update(itemPrice);
195                            }
196                    }
197    
198                    return item;
199            }
200    
201            @Override
202            public void addItemResources(
203                            long itemId, boolean addGroupPermissions,
204                            boolean addGuestPermissions)
205                    throws PortalException {
206    
207                    ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
208    
209                    addItemResources(item, addGroupPermissions, addGuestPermissions);
210            }
211    
212            @Override
213            public void addItemResources(
214                            long itemId, String[] groupPermissions, String[] guestPermissions)
215                    throws PortalException {
216    
217                    ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
218    
219                    addItemResources(item, groupPermissions, guestPermissions);
220            }
221    
222            @Override
223            public void addItemResources(
224                            ShoppingItem item, boolean addGroupPermissions,
225                            boolean addGuestPermissions)
226                    throws PortalException {
227    
228                    resourceLocalService.addResources(
229                            item.getCompanyId(), item.getGroupId(), item.getUserId(),
230                            ShoppingItem.class.getName(), item.getItemId(), false,
231                            addGroupPermissions, addGuestPermissions);
232            }
233    
234            @Override
235            public void addItemResources(
236                            ShoppingItem item, String[] groupPermissions,
237                            String[] guestPermissions)
238                    throws PortalException {
239    
240                    resourceLocalService.addModelResources(
241                            item.getCompanyId(), item.getGroupId(), item.getUserId(),
242                            ShoppingItem.class.getName(), item.getItemId(), groupPermissions,
243                            guestPermissions);
244            }
245    
246            @Override
247            public void deleteItem(long itemId) throws PortalException {
248                    ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
249    
250                    deleteItem(item);
251            }
252    
253            @Override
254            public void deleteItem(ShoppingItem item) throws PortalException {
255    
256                    // Item
257    
258                    shoppingItemPersistence.remove(item);
259    
260                    // Resources
261    
262                    resourceLocalService.deleteResource(
263                            item.getCompanyId(), ShoppingItem.class.getName(),
264                            ResourceConstants.SCOPE_INDIVIDUAL, item.getItemId());
265    
266                    // Images
267    
268                    imageLocalService.deleteImage(item.getSmallImageId());
269                    imageLocalService.deleteImage(item.getMediumImageId());
270                    imageLocalService.deleteImage(item.getLargeImageId());
271    
272                    // Item fields
273    
274                    shoppingItemFieldPersistence.removeByItemId(item.getItemId());
275    
276                    // Item prices
277    
278                    shoppingItemPricePersistence.removeByItemId(item.getItemId());
279            }
280    
281            @Override
282            public void deleteItems(long groupId, long categoryId)
283                    throws PortalException {
284    
285                    List<ShoppingItem> items = shoppingItemPersistence.findByG_C(
286                            groupId, categoryId);
287    
288                    for (ShoppingItem item : items) {
289                            deleteItem(item);
290                    }
291            }
292    
293            @Override
294            public int getCategoriesItemsCount(long groupId, List<Long> categoryIds) {
295                    return shoppingItemFinder.countByG_C(groupId, categoryIds);
296            }
297    
298            @Override
299            public List<ShoppingItem> getFeaturedItems(
300                    long groupId, long categoryId, int numOfItems) {
301    
302                    List<ShoppingItem> featuredItems = shoppingItemFinder.findByFeatured(
303                            groupId, new long[] {categoryId}, numOfItems);
304    
305                    if (featuredItems.isEmpty()) {
306                            List<ShoppingCategory> childCategories =
307                                    shoppingCategoryPersistence.findByG_P(groupId, categoryId);
308    
309                            if (!childCategories.isEmpty()) {
310                                    long[] categoryIds = new long[childCategories.size()];
311    
312                                    for (int i = 0; i < childCategories.size(); i++) {
313                                            ShoppingCategory childCategory = childCategories.get(i);
314    
315                                            categoryIds[i] = childCategory.getCategoryId();
316                                    }
317    
318                                    featuredItems = shoppingItemFinder.findByFeatured(
319                                            groupId, categoryIds, numOfItems);
320                            }
321                    }
322    
323                    return featuredItems;
324            }
325    
326            @Override
327            public ShoppingItem getItem(long itemId) throws PortalException {
328                    return shoppingItemPersistence.findByPrimaryKey(itemId);
329            }
330    
331            @Override
332            public ShoppingItem getItem(long companyId, String sku)
333                    throws PortalException {
334    
335                    return shoppingItemPersistence.findByC_S(companyId, sku);
336            }
337    
338            @Override
339            public ShoppingItem getItemByLargeImageId(long largeImageId)
340                    throws PortalException {
341    
342                    return shoppingItemPersistence.findByLargeImageId(largeImageId);
343            }
344    
345            @Override
346            public ShoppingItem getItemByMediumImageId(long mediumImageId)
347                    throws PortalException {
348    
349                    return shoppingItemPersistence.findByMediumImageId(mediumImageId);
350            }
351    
352            @Override
353            public ShoppingItem getItemBySmallImageId(long smallImageId)
354                    throws PortalException {
355    
356                    return shoppingItemPersistence.findBySmallImageId(smallImageId);
357            }
358    
359            @Override
360            public List<ShoppingItem> getItems(long groupId, long categoryId) {
361                    return shoppingItemPersistence.findByG_C(groupId, categoryId);
362            }
363    
364            @Override
365            public List<ShoppingItem> getItems(
366                    long groupId, long categoryId, int start, int end,
367                    OrderByComparator<ShoppingItem> obc) {
368    
369                    return shoppingItemPersistence.findByG_C(
370                            groupId, categoryId, start, end, obc);
371            }
372    
373            @Override
374            public int getItemsCount(long groupId, long categoryId) {
375                    return shoppingItemPersistence.countByG_C(groupId, categoryId);
376            }
377    
378            @Override
379            public ShoppingItem[] getItemsPrevAndNext(
380                            long itemId, OrderByComparator<ShoppingItem> obc)
381                    throws PortalException {
382    
383                    ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
384    
385                    return shoppingItemPersistence.findByG_C_PrevAndNext(
386                            item.getItemId(), item.getGroupId(), item.getCategoryId(), obc);
387            }
388    
389            @Override
390            public List<ShoppingItem> getSaleItems(
391                    long groupId, long categoryId, int numOfItems) {
392    
393                    List<ShoppingItem> saleItems = shoppingItemFinder.findBySale(
394                            groupId, new long[] {categoryId}, numOfItems);
395    
396                    if (saleItems.isEmpty()) {
397                            List<ShoppingCategory> childCategories =
398                                    shoppingCategoryPersistence.findByG_P(groupId, categoryId);
399    
400                            if (!childCategories.isEmpty()) {
401                                    long[] categoryIds = new long[childCategories.size()];
402    
403                                    for (int i = 0; i < childCategories.size(); i++) {
404                                            ShoppingCategory childCategory = childCategories.get(i);
405    
406                                            categoryIds[i] = childCategory.getCategoryId();
407                                    }
408    
409                                    saleItems = shoppingItemFinder.findBySale(
410                                            groupId, categoryIds, numOfItems);
411                            }
412                    }
413    
414                    return saleItems;
415            }
416    
417            @Override
418            public List<ShoppingItem> search(
419                    long groupId, long[] categoryIds, String keywords, int start, int end) {
420    
421                    return shoppingItemFinder.findByKeywords(
422                            groupId, categoryIds, keywords, start, end);
423            }
424    
425            @Override
426            public int searchCount(long groupId, long[] categoryIds, String keywords) {
427                    return shoppingItemFinder.countByKeywords(
428                            groupId, categoryIds, keywords);
429            }
430    
431            @Override
432            public ShoppingItem updateItem(
433                            long userId, long itemId, long groupId, long categoryId, String sku,
434                            String name, String description, String properties,
435                            String fieldsQuantities, boolean requiresShipping,
436                            int stockQuantity, boolean featured, Boolean sale,
437                            boolean smallImage, String smallImageURL, File smallImageFile,
438                            boolean mediumImage, String mediumImageURL, File mediumImageFile,
439                            boolean largeImage, String largeImageURL, File largeImageFile,
440                            List<ShoppingItemField> itemFields,
441                            List<ShoppingItemPrice> itemPrices, ServiceContext serviceContext)
442                    throws PortalException {
443    
444                    // Item
445    
446                    ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(itemId);
447    
448                    User user = userPersistence.findByPrimaryKey(userId);
449                    categoryId = getCategory(item, categoryId);
450                    sku = StringUtil.toUpperCase(sku.trim());
451    
452                    byte[] smallImageBytes = null;
453                    byte[] mediumImageBytes = null;
454                    byte[] largeImageBytes = null;
455    
456                    try {
457                            smallImageBytes = FileUtil.getBytes(smallImageFile);
458                            mediumImageBytes = FileUtil.getBytes(mediumImageFile);
459                            largeImageBytes = FileUtil.getBytes(largeImageFile);
460                    }
461                    catch (IOException ioe) {
462                    }
463    
464                    validate(
465                            user.getCompanyId(), itemId, sku, name, smallImage, smallImageURL,
466                            smallImageFile, smallImageBytes, mediumImage, mediumImageURL,
467                            mediumImageFile, mediumImageBytes, largeImage, largeImageURL,
468                            largeImageFile, largeImageBytes, itemFields);
469    
470                    item.setCategoryId(categoryId);
471                    item.setSku(sku);
472                    item.setName(name);
473                    item.setDescription(description);
474                    item.setProperties(properties);
475                    item.setFields(!itemFields.isEmpty());
476                    item.setFieldsQuantities(fieldsQuantities);
477    
478                    for (ShoppingItemPrice itemPrice : itemPrices) {
479                            if (itemPrice.getStatus() ==
480                                            ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT) {
481    
482                                    item.setMinQuantity(itemPrice.getMinQuantity());
483                                    item.setMaxQuantity(itemPrice.getMaxQuantity());
484                                    item.setPrice(itemPrice.getPrice());
485                                    item.setDiscount(itemPrice.getDiscount());
486                                    item.setTaxable(itemPrice.getTaxable());
487                                    item.setShipping(itemPrice.getShipping());
488                                    item.setUseShippingFormula(itemPrice.getUseShippingFormula());
489                            }
490    
491                            if ((sale == null) && (itemPrice.getDiscount() > 0) &&
492                                    ((itemPrice.getStatus() ==
493                                            ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT) ||
494                                     (itemPrice.getStatus() ==
495                                             ShoppingItemPriceConstants.STATUS_ACTIVE))) {
496    
497                                    sale = Boolean.TRUE;
498                            }
499                    }
500    
501                    item.setRequiresShipping(requiresShipping);
502                    item.setStockQuantity(stockQuantity);
503                    item.setFeatured(featured);
504                    item.setSale((sale != null) ? sale.booleanValue() : false);
505                    item.setSmallImage(smallImage);
506                    item.setSmallImageURL(smallImageURL);
507                    item.setMediumImage(mediumImage);
508                    item.setMediumImageURL(mediumImageURL);
509                    item.setLargeImage(largeImage);
510                    item.setLargeImageURL(largeImageURL);
511    
512                    shoppingItemPersistence.update(item);
513    
514                    // Images
515    
516                    saveImages(
517                            smallImage, item.getSmallImageId(), smallImageFile, smallImageBytes,
518                            mediumImage, item.getMediumImageId(), mediumImageFile,
519                            mediumImageBytes, largeImage, item.getLargeImageId(),
520                            largeImageFile, largeImageBytes);
521    
522                    // Item fields
523    
524                    shoppingItemFieldPersistence.removeByItemId(itemId);
525    
526                    for (ShoppingItemField itemField : itemFields) {
527                            long itemFieldId = counterLocalService.increment();
528    
529                            itemField.setItemFieldId(itemFieldId);
530                            itemField.setItemId(itemId);
531                            itemField.setName(checkItemField(itemField.getName()));
532                            itemField.setValues(checkItemField(itemField.getValues()));
533    
534                            shoppingItemFieldPersistence.update(itemField);
535                    }
536    
537                    // Item prices
538    
539                    shoppingItemPricePersistence.removeByItemId(itemId);
540    
541                    if (itemPrices.size() > 1) {
542                            for (ShoppingItemPrice itemPrice : itemPrices) {
543                                    long itemPriceId = counterLocalService.increment();
544    
545                                    itemPrice.setItemPriceId(itemPriceId);
546                                    itemPrice.setItemId(itemId);
547    
548                                    shoppingItemPricePersistence.update(itemPrice);
549                            }
550                    }
551    
552                    return item;
553            }
554    
555            protected String checkItemField(String value) {
556                    return StringUtil.replace(
557                            value,
558                            new String[] {
559                                    StringPool.AMPERSAND, StringPool.APOSTROPHE, StringPool.EQUAL,
560                                    StringPool.PIPE, StringPool.QUOTE
561                            },
562                            new String[] {
563                                    StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
564                                    StringPool.BLANK, StringPool.BLANK
565                            }
566                    );
567            }
568    
569            protected long getCategory(ShoppingItem item, long categoryId) {
570                    if ((item.getCategoryId() != categoryId) &&
571                            (categoryId !=
572                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
573    
574                            ShoppingCategory newCategory =
575                                    shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
576    
577                            if ((newCategory == null) ||
578                                    (item.getGroupId() != newCategory.getGroupId())) {
579    
580                                    categoryId = item.getCategoryId();
581                            }
582                    }
583    
584                    return categoryId;
585            }
586    
587            protected void saveImages(
588                            boolean smallImage, long smallImageId, File smallImageFile,
589                            byte[] smallImageBytes, boolean mediumImage, long mediumImageId,
590                            File mediumImageFile, byte[] mediumImageBytes, boolean largeImage,
591                            long largeImageId, File largeImageFile, byte[] largeImageBytes)
592                    throws PortalException {
593    
594                    // Small image
595    
596                    if (smallImage) {
597                            if ((smallImageFile != null) && (smallImageBytes != null)) {
598                                    imageLocalService.updateImage(smallImageId, smallImageBytes);
599                            }
600                    }
601                    else {
602                            imageLocalService.deleteImage(smallImageId);
603                    }
604    
605                    // Medium image
606    
607                    if (mediumImage) {
608                            if ((mediumImageFile != null) && (mediumImageBytes != null)) {
609                                    imageLocalService.updateImage(mediumImageId, mediumImageBytes);
610                            }
611                    }
612                    else {
613                            imageLocalService.deleteImage(mediumImageId);
614                    }
615    
616                    // Large image
617    
618                    if (largeImage) {
619                            if ((largeImageFile != null) && (largeImageBytes != null)) {
620                                    imageLocalService.updateImage(largeImageId, largeImageBytes);
621                            }
622                    }
623                    else {
624                            imageLocalService.deleteImage(largeImageId);
625                    }
626            }
627    
628            protected void validate(
629                            long companyId, long itemId, String sku, String name,
630                            boolean smallImage, String smallImageURL, File smallImageFile,
631                            byte[] smallImageBytes, boolean mediumImage, String mediumImageURL,
632                            File mediumImageFile, byte[] mediumImageBytes, boolean largeImage,
633                            String largeImageURL, File largeImageFile, byte[] largeImageBytes,
634                            List<ShoppingItemField> itemFields)
635                    throws PortalException {
636    
637                    if (Validator.isNull(sku)) {
638                            throw new ItemSKUException();
639                    }
640    
641                    ShoppingItem item = shoppingItemPersistence.fetchByC_S(companyId, sku);
642    
643                    if ((item != null) && (item.getItemId() != itemId)) {
644                            StringBundler sb = new StringBundler(5);
645    
646                            sb.append("{companyId=");
647                            sb.append(companyId);
648                            sb.append(", sku=");
649                            sb.append(sku);
650                            sb.append("}");
651    
652                            throw new DuplicateItemSKUException(sb.toString());
653                    }
654    
655                    if (Validator.isNull(name)) {
656                            throw new ItemNameException();
657                    }
658    
659                    if (!itemFields.isEmpty()) {
660                            List<String> itemFieldNames = new ArrayList<>();
661                            List<String> duplicateItemFieldNames = new ArrayList<>();
662    
663                            StringBundler sb = new StringBundler(itemFields.size());
664    
665                            for (ShoppingItemField itemField : itemFields) {
666                                    if (itemFieldNames.contains(itemField.getName())) {
667                                            if (!duplicateItemFieldNames.contains(
668                                                            itemField.getName())) {
669    
670                                                    duplicateItemFieldNames.add(itemField.getName());
671    
672                                                    sb.append(itemField.getName());
673                                                    sb.append(StringPool.COMMA_AND_SPACE);
674                                            }
675                                    }
676                                    else {
677                                            itemFieldNames.add(itemField.getName());
678                                    }
679                            }
680    
681                            if (!duplicateItemFieldNames.isEmpty()) {
682                                    sb.setIndex(sb.index() - 1);
683    
684                                    throw new DuplicateItemFieldNameException(sb.toString());
685                            }
686                    }
687    
688                    String[] imageExtensions = PrefsPropsUtil.getStringArray(
689                            PropsKeys.SHOPPING_IMAGE_EXTENSIONS, StringPool.COMMA);
690    
691                    // Small image
692    
693                    if (smallImage && Validator.isNull(smallImageURL) &&
694                            (smallImageFile != null) && (smallImageBytes != null)) {
695    
696                            String smallImageName = smallImageFile.getName();
697    
698                            if (smallImageName != null) {
699                                    boolean validSmallImageExtension = false;
700    
701                                    for (String imageExtension : imageExtensions) {
702                                            if (StringPool.STAR.equals(imageExtension) ||
703                                                    StringUtil.endsWith(
704                                                            smallImageName, imageExtension)) {
705    
706                                                    validSmallImageExtension = true;
707    
708                                                    break;
709                                            }
710                                    }
711    
712                                    if (!validSmallImageExtension) {
713                                            throw new ItemSmallImageNameException(smallImageName);
714                                    }
715                            }
716    
717                            long smallImageMaxSize = PrefsPropsUtil.getLong(
718                                    PropsKeys.SHOPPING_IMAGE_SMALL_MAX_SIZE);
719    
720                            if ((smallImageMaxSize > 0) &&
721                                    ((smallImageBytes == null) ||
722                                     (smallImageBytes.length > smallImageMaxSize))) {
723    
724                                    throw new ItemSmallImageSizeException();
725                            }
726                    }
727    
728                    // Medium image
729    
730                    if (mediumImage && Validator.isNull(mediumImageURL) &&
731                            (mediumImageFile != null) && (mediumImageBytes != null)) {
732    
733                            String mediumImageName = mediumImageFile.getName();
734    
735                            if (mediumImageName != null) {
736                                    boolean validMediumImageExtension = false;
737    
738                                    for (String imageExtension : imageExtensions) {
739                                            if (StringPool.STAR.equals(imageExtension) ||
740                                                    StringUtil.endsWith(
741                                                            mediumImageName, imageExtension)) {
742    
743                                                    validMediumImageExtension = true;
744    
745                                                    break;
746                                            }
747                                    }
748    
749                                    if (!validMediumImageExtension) {
750                                            throw new ItemMediumImageNameException(mediumImageName);
751                                    }
752                            }
753    
754                            long mediumImageMaxSize = PrefsPropsUtil.getLong(
755                                    PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);
756    
757                            if ((mediumImageMaxSize > 0) &&
758                                    ((mediumImageBytes == null) ||
759                                     (mediumImageBytes.length > mediumImageMaxSize))) {
760    
761                                    throw new ItemMediumImageSizeException();
762                            }
763                    }
764    
765                    // Large image
766    
767                    if (!largeImage || Validator.isNotNull(largeImageURL) ||
768                            (largeImageFile == null) || (largeImageBytes == null)) {
769    
770                            return;
771                    }
772    
773                    String largeImageName = largeImageFile.getName();
774    
775                    if (largeImageName != null) {
776                            boolean validLargeImageExtension = false;
777    
778                            for (String imageExtension : imageExtensions) {
779                                    if (StringPool.STAR.equals(imageExtension) ||
780                                            StringUtil.endsWith(
781                                                    largeImageName, imageExtension)) {
782    
783                                            validLargeImageExtension = true;
784    
785                                            break;
786                                    }
787                            }
788    
789                            if (!validLargeImageExtension) {
790                                    throw new ItemLargeImageNameException(largeImageName);
791                            }
792                    }
793    
794                    long largeImageMaxSize = PrefsPropsUtil.getLong(
795                            PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE);
796    
797                    if ((largeImageMaxSize > 0) &&
798                            ((largeImageBytes == null) ||
799                             (largeImageBytes.length > largeImageMaxSize))) {
800    
801                            throw new ItemLargeImageSizeException();
802                    }
803            }
804    
805    }