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