001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.ResourceConstants;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.shopping.CategoryNameException;
024    import com.liferay.portlet.shopping.model.ShoppingCategory;
025    import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
026    import com.liferay.portlet.shopping.model.ShoppingItem;
027    import com.liferay.portlet.shopping.service.base.ShoppingCategoryLocalServiceBaseImpl;
028    
029    import java.util.ArrayList;
030    import java.util.Collections;
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ShoppingCategoryLocalServiceImpl
038            extends ShoppingCategoryLocalServiceBaseImpl {
039    
040            @Override
041            public ShoppingCategory addCategory(
042                            long userId, long parentCategoryId, String name, String description,
043                            ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    // Category
047    
048                    User user = userPersistence.findByPrimaryKey(userId);
049                    long groupId = serviceContext.getScopeGroupId();
050                    parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
051                    Date now = new Date();
052    
053                    validate(name);
054    
055                    long categoryId = counterLocalService.increment();
056    
057                    ShoppingCategory category = shoppingCategoryPersistence.create(
058                            categoryId);
059    
060                    category.setGroupId(groupId);
061                    category.setCompanyId(user.getCompanyId());
062                    category.setUserId(user.getUserId());
063                    category.setUserName(user.getFullName());
064                    category.setCreateDate(now);
065                    category.setModifiedDate(now);
066                    category.setParentCategoryId(parentCategoryId);
067                    category.setName(name);
068                    category.setDescription(description);
069    
070                    shoppingCategoryPersistence.update(category);
071    
072                    // Resources
073    
074                    if (serviceContext.isAddGroupPermissions() ||
075                            serviceContext.isAddGuestPermissions()) {
076    
077                            addCategoryResources(
078                                    category, serviceContext.isAddGroupPermissions(),
079                                    serviceContext.isAddGuestPermissions());
080                    }
081                    else {
082                            addCategoryResources(
083                                    category, serviceContext.getGroupPermissions(),
084                                    serviceContext.getGuestPermissions());
085                    }
086    
087                    return category;
088            }
089    
090            @Override
091            public void addCategoryResources(
092                            long categoryId, boolean addGroupPermissions,
093                            boolean addGuestPermissions)
094                    throws PortalException, SystemException {
095    
096                    ShoppingCategory category =
097                            shoppingCategoryPersistence.findByPrimaryKey(categoryId);
098    
099                    addCategoryResources(
100                            category, addGroupPermissions, addGuestPermissions);
101            }
102    
103            @Override
104            public void addCategoryResources(
105                            long categoryId, String[] groupPermissions,
106                            String[] guestPermissions)
107                    throws PortalException, SystemException {
108    
109                    ShoppingCategory category =
110                            shoppingCategoryPersistence.findByPrimaryKey(categoryId);
111    
112                    addCategoryResources(category, groupPermissions, guestPermissions);
113            }
114    
115            @Override
116            public void addCategoryResources(
117                            ShoppingCategory category, boolean addGroupPermissions,
118                            boolean addGuestPermissions)
119                    throws PortalException, SystemException {
120    
121                    resourceLocalService.addResources(
122                            category.getCompanyId(), category.getGroupId(),
123                            category.getUserId(), ShoppingCategory.class.getName(),
124                            category.getCategoryId(), false, addGroupPermissions,
125                            addGuestPermissions);
126            }
127    
128            @Override
129            public void addCategoryResources(
130                            ShoppingCategory category, String[] groupPermissions,
131                            String[] guestPermissions)
132                    throws PortalException, SystemException {
133    
134                    resourceLocalService.addModelResources(
135                            category.getCompanyId(), category.getGroupId(),
136                            category.getUserId(), ShoppingCategory.class.getName(),
137                            category.getCategoryId(), groupPermissions, guestPermissions);
138            }
139    
140            @Override
141            public void deleteCategories(long groupId)
142                    throws PortalException, SystemException {
143    
144                    List<ShoppingCategory> categories =
145                            shoppingCategoryPersistence.findByGroupId(groupId);
146    
147                    for (ShoppingCategory category : categories) {
148                            deleteCategory(category);
149                    }
150            }
151    
152            @Override
153            public void deleteCategory(long categoryId)
154                    throws PortalException, SystemException {
155    
156                    ShoppingCategory category =
157                            shoppingCategoryPersistence.findByPrimaryKey(categoryId);
158    
159                    deleteCategory(category);
160            }
161    
162            @Override
163            public void deleteCategory(ShoppingCategory category)
164                    throws PortalException, SystemException {
165    
166                    // Categories
167    
168                    List<ShoppingCategory> categories =
169                            shoppingCategoryPersistence.findByG_P(
170                                    category.getGroupId(), category.getCategoryId());
171    
172                    for (ShoppingCategory curCategory : categories) {
173                            deleteCategory(curCategory);
174                    }
175    
176                    // Category
177    
178                    shoppingCategoryPersistence.remove(category);
179    
180                    // Resources
181    
182                    resourceLocalService.deleteResource(
183                            category.getCompanyId(), ShoppingCategory.class.getName(),
184                            ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
185    
186                    // Items
187    
188                    shoppingItemLocalService.deleteItems(
189                            category.getGroupId(), category.getCategoryId());
190            }
191    
192            @Override
193            public List<ShoppingCategory> getCategories(long groupId)
194                    throws SystemException {
195    
196                    return shoppingCategoryPersistence.findByGroupId(groupId);
197            }
198    
199            @Override
200            public List<ShoppingCategory> getCategories(
201                            long groupId, long parentCategoryId, int start, int end)
202                    throws SystemException {
203    
204                    return shoppingCategoryPersistence.findByG_P(
205                            groupId, parentCategoryId, start, end);
206            }
207    
208            @Override
209            public int getCategoriesCount(long groupId, long parentCategoryId)
210                    throws SystemException {
211    
212                    return shoppingCategoryPersistence.countByG_P(
213                            groupId, parentCategoryId);
214            }
215    
216            @Override
217            public ShoppingCategory getCategory(long categoryId)
218                    throws PortalException, SystemException {
219    
220                    return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
221            }
222    
223            @Override
224            public ShoppingCategory getCategory(long groupId, String categoryName)
225                    throws SystemException {
226    
227                    return shoppingCategoryPersistence.fetchByG_N(groupId, categoryName);
228            }
229    
230            @Override
231            public List<ShoppingCategory> getParentCategories(long categoryId)
232                    throws PortalException, SystemException {
233    
234                    return getParentCategories(
235                            shoppingCategoryPersistence.findByPrimaryKey(categoryId));
236            }
237    
238            @Override
239            public List<ShoppingCategory> getParentCategories(ShoppingCategory category)
240                    throws PortalException, SystemException {
241    
242                    List<ShoppingCategory> parentCategories =
243                            new ArrayList<ShoppingCategory>();
244    
245                    ShoppingCategory tempCategory = category;
246    
247                    while (true) {
248                            parentCategories.add(tempCategory);
249    
250                            if (tempCategory.getParentCategoryId() ==
251                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
252    
253                                    break;
254                            }
255    
256                            tempCategory = shoppingCategoryPersistence.findByPrimaryKey(
257                                    tempCategory.getParentCategoryId());
258                    }
259    
260                    Collections.reverse(parentCategories);
261    
262                    return parentCategories;
263            }
264    
265            @Override
266            public ShoppingCategory getParentCategory(ShoppingCategory category)
267                    throws PortalException, SystemException {
268    
269                    ShoppingCategory parentCategory =
270                            shoppingCategoryPersistence.findByPrimaryKey(
271                                    category.getParentCategoryId());
272    
273                    return parentCategory;
274            }
275    
276            @Override
277            public void getSubcategoryIds(
278                            List<Long> categoryIds, long groupId, long categoryId)
279                    throws SystemException {
280    
281                    List<ShoppingCategory> categories =
282                            shoppingCategoryPersistence.findByG_P(groupId, categoryId);
283    
284                    for (ShoppingCategory category : categories) {
285                            categoryIds.add(category.getCategoryId());
286    
287                            getSubcategoryIds(
288                                    categoryIds, category.getGroupId(), category.getCategoryId());
289                    }
290            }
291    
292            @Override
293            public ShoppingCategory updateCategory(
294                            long categoryId, long parentCategoryId, String name,
295                            String description, boolean mergeWithParentCategory,
296                            ServiceContext serviceContext)
297                    throws PortalException, SystemException {
298    
299                    // Merge categories
300    
301                    ShoppingCategory category =
302                            shoppingCategoryPersistence.findByPrimaryKey(categoryId);
303    
304                    parentCategoryId = getParentCategoryId(category, parentCategoryId);
305    
306                    if (mergeWithParentCategory &&
307                            (categoryId != parentCategoryId) &&
308                            (parentCategoryId !=
309                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
310    
311                            mergeCategories(category, parentCategoryId);
312    
313                            return category;
314                    }
315    
316                    // Category
317    
318                    validate(name);
319    
320                    category.setModifiedDate(new Date());
321                    category.setParentCategoryId(parentCategoryId);
322                    category.setName(name);
323                    category.setDescription(description);
324    
325                    shoppingCategoryPersistence.update(category);
326    
327                    return category;
328            }
329    
330            protected long getParentCategoryId(long groupId, long parentCategoryId)
331                    throws SystemException {
332    
333                    if (parentCategoryId !=
334                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
335    
336                            ShoppingCategory parentCategory =
337                                    shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
338    
339                            if ((parentCategory == null) ||
340                                    (groupId != parentCategory.getGroupId())) {
341    
342                                    parentCategoryId =
343                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
344                            }
345                    }
346    
347                    return parentCategoryId;
348            }
349    
350            protected long getParentCategoryId(
351                            ShoppingCategory category, long parentCategoryId)
352                    throws SystemException {
353    
354                    if (parentCategoryId ==
355                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
356    
357                            return parentCategoryId;
358                    }
359    
360                    if (category.getCategoryId() == parentCategoryId) {
361                            return category.getParentCategoryId();
362                    }
363    
364                    ShoppingCategory parentCategory =
365                            shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
366    
367                    if ((parentCategory == null) ||
368                            (category.getGroupId() != parentCategory.getGroupId())) {
369    
370                            return category.getParentCategoryId();
371                    }
372    
373                    List<Long> subcategoryIds = new ArrayList<Long>();
374    
375                    getSubcategoryIds(
376                            subcategoryIds, category.getGroupId(), category.getCategoryId());
377    
378                    if (subcategoryIds.contains(parentCategoryId)) {
379                            return category.getParentCategoryId();
380                    }
381    
382                    return parentCategoryId;
383            }
384    
385            protected void mergeCategories(
386                            ShoppingCategory fromCategory, long toCategoryId)
387                    throws PortalException, SystemException {
388    
389                    List<ShoppingCategory> categories =
390                            shoppingCategoryPersistence.findByG_P(
391                                    fromCategory.getGroupId(), fromCategory.getCategoryId());
392    
393                    for (ShoppingCategory category : categories) {
394                            mergeCategories(category, toCategoryId);
395                    }
396    
397                    List<ShoppingItem> items = shoppingItemPersistence.findByG_C(
398                            fromCategory.getGroupId(), fromCategory.getCategoryId());
399    
400                    for (ShoppingItem item : items) {
401    
402                            // Item
403    
404                            item.setCategoryId(toCategoryId);
405    
406                            shoppingItemPersistence.update(item);
407                    }
408    
409                    deleteCategory(fromCategory);
410            }
411    
412            protected void validate(String name) throws PortalException {
413                    if (Validator.isNull(name) || name.contains("\\\\") ||
414                            name.contains("//")) {
415    
416                            throw new CategoryNameException();
417                    }
418            }
419    
420    }