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