1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.model.impl.ResourceImpl;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.shopping.CategoryNameException;
32  import com.liferay.portlet.shopping.model.ShoppingCategory;
33  import com.liferay.portlet.shopping.model.ShoppingItem;
34  import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
35  import com.liferay.portlet.shopping.service.base.ShoppingCategoryLocalServiceBaseImpl;
36  
37  import java.util.ArrayList;
38  import java.util.Collections;
39  import java.util.Date;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="ShoppingCategoryLocalServiceImpl.java.html"><b><i>View Source</i>
45   * </b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ShoppingCategoryLocalServiceImpl
51      extends ShoppingCategoryLocalServiceBaseImpl {
52  
53      public ShoppingCategory addCategory(
54              long userId, long plid, long parentCategoryId, String name,
55              String description, boolean addCommunityPermissions,
56              boolean addGuestPermissions)
57          throws PortalException, SystemException {
58  
59          return addCategory(
60              userId, plid, parentCategoryId, name, description,
61              Boolean.valueOf(addCommunityPermissions),
62              Boolean.valueOf(addGuestPermissions), null, null);
63      }
64  
65      public ShoppingCategory addCategory(
66              long userId, long plid, long parentCategoryId, String name,
67              String description, String[] communityPermissions,
68              String[] guestPermissions)
69          throws PortalException, SystemException {
70  
71          return addCategory(
72              userId, plid, parentCategoryId, name, description, null, null,
73              communityPermissions, guestPermissions);
74      }
75  
76      public ShoppingCategory addCategory(
77              long userId, long plid, long parentCategoryId, String name,
78              String description, Boolean addCommunityPermissions,
79              Boolean addGuestPermissions, String[] communityPermissions,
80              String[] guestPermissions)
81          throws PortalException, SystemException {
82  
83          // Category
84  
85          User user = userPersistence.findByPrimaryKey(userId);
86          long groupId = PortalUtil.getPortletGroupId(plid);
87          parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
88          Date now = new Date();
89  
90          validate(name);
91  
92          long categoryId = counterLocalService.increment();
93  
94          ShoppingCategory category = shoppingCategoryPersistence.create(
95              categoryId);
96  
97          category.setGroupId(groupId);
98          category.setCompanyId(user.getCompanyId());
99          category.setUserId(user.getUserId());
100         category.setUserName(user.getFullName());
101         category.setCreateDate(now);
102         category.setModifiedDate(now);
103         category.setParentCategoryId(parentCategoryId);
104         category.setName(name);
105         category.setDescription(description);
106 
107         shoppingCategoryPersistence.update(category);
108 
109         // Resources
110 
111         if ((addCommunityPermissions != null) &&
112             (addGuestPermissions != null)) {
113 
114             addCategoryResources(
115                 category, addCommunityPermissions.booleanValue(),
116                 addGuestPermissions.booleanValue());
117         }
118         else {
119             addCategoryResources(
120                 category, communityPermissions, guestPermissions);
121         }
122 
123         return category;
124     }
125 
126     public void addCategoryResources(
127             long categoryId, boolean addCommunityPermissions,
128             boolean addGuestPermissions)
129         throws PortalException, SystemException {
130 
131         ShoppingCategory category =
132             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
133 
134         addCategoryResources(
135             category, addCommunityPermissions, addGuestPermissions);
136     }
137 
138     public void addCategoryResources(
139             ShoppingCategory category, boolean addCommunityPermissions,
140             boolean addGuestPermissions)
141         throws PortalException, SystemException {
142 
143         resourceLocalService.addResources(
144             category.getCompanyId(), category.getGroupId(),
145             category.getUserId(), ShoppingCategory.class.getName(),
146             category.getCategoryId(), false, addCommunityPermissions,
147             addGuestPermissions);
148     }
149 
150     public void addCategoryResources(
151             long categoryId, String[] communityPermissions,
152             String[] guestPermissions)
153         throws PortalException, SystemException {
154 
155         ShoppingCategory category =
156             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
157 
158         addCategoryResources(category, communityPermissions, guestPermissions);
159     }
160 
161     public void addCategoryResources(
162             ShoppingCategory category, String[] communityPermissions,
163             String[] guestPermissions)
164         throws PortalException, SystemException {
165 
166         resourceLocalService.addModelResources(
167             category.getCompanyId(), category.getGroupId(),
168             category.getUserId(), ShoppingCategory.class.getName(),
169             category.getCategoryId(), communityPermissions, guestPermissions);
170     }
171 
172     public void deleteCategories(long groupId)
173         throws PortalException, SystemException {
174 
175         Iterator itr = shoppingCategoryPersistence.findByGroupId(
176             groupId).iterator();
177 
178         while (itr.hasNext()) {
179             ShoppingCategory category = (ShoppingCategory)itr.next();
180 
181             deleteCategory(category);
182         }
183     }
184 
185     public void deleteCategory(long categoryId)
186         throws PortalException, SystemException {
187 
188         ShoppingCategory category =
189             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
190 
191         deleteCategory(category);
192     }
193 
194     public void deleteCategory(ShoppingCategory category)
195         throws PortalException, SystemException {
196 
197         // Categories
198 
199         Iterator itr = shoppingCategoryPersistence.findByG_P(
200             category.getGroupId(), category.getCategoryId()).iterator();
201 
202         while (itr.hasNext()) {
203             ShoppingCategory curCategory = (ShoppingCategory)itr.next();
204 
205             deleteCategory(curCategory);
206         }
207 
208         // Items
209 
210         shoppingItemLocalService.deleteItems(category.getCategoryId());
211 
212         // Resources
213 
214         resourceLocalService.deleteResource(
215             category.getCompanyId(), ShoppingCategory.class.getName(),
216             ResourceImpl.SCOPE_INDIVIDUAL, category.getCategoryId());
217 
218         // Category
219 
220         shoppingCategoryPersistence.remove(category.getCategoryId());
221     }
222 
223     public List getCategories(long groupId) throws SystemException {
224         return shoppingCategoryPersistence.findByGroupId(groupId);
225     }
226 
227     public List getCategories(
228             long groupId, long parentCategoryId, int begin, int end)
229         throws SystemException {
230 
231         return shoppingCategoryPersistence.findByG_P(
232             groupId, parentCategoryId, begin, end);
233     }
234 
235     public int getCategoriesCount(long groupId, long parentCategoryId)
236         throws SystemException {
237 
238         return shoppingCategoryPersistence.countByG_P(
239             groupId, parentCategoryId);
240     }
241 
242     public ShoppingCategory getCategory(long categoryId)
243         throws PortalException, SystemException {
244 
245         return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
246     }
247 
248     public ShoppingCategory getParentCategory(ShoppingCategory category)
249         throws PortalException, SystemException {
250 
251         ShoppingCategory parentCategory =
252             shoppingCategoryPersistence.findByPrimaryKey(
253                 category.getParentCategoryId());
254 
255         return parentCategory;
256     }
257 
258     public List getParentCategories(long categoryId)
259         throws PortalException, SystemException {
260 
261         return getParentCategories(
262             shoppingCategoryPersistence.findByPrimaryKey(categoryId));
263     }
264 
265     public List getParentCategories(ShoppingCategory category)
266         throws PortalException, SystemException {
267 
268         List parentCategories = new ArrayList();
269 
270         ShoppingCategory tempCategory = category;
271 
272         for (;;) {
273             parentCategories.add(tempCategory);
274 
275             if (tempCategory.getParentCategoryId() ==
276                     ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
277 
278                 break;
279             }
280 
281             tempCategory = shoppingCategoryPersistence.findByPrimaryKey(
282                 tempCategory.getParentCategoryId());
283         }
284 
285         Collections.reverse(parentCategories);
286 
287         return parentCategories;
288     }
289 
290     public void getSubcategoryIds(
291             List categoryIds, long groupId, long categoryId)
292         throws SystemException {
293 
294         Iterator itr = shoppingCategoryPersistence.findByG_P(
295             groupId, categoryId).iterator();
296 
297         while (itr.hasNext()) {
298             ShoppingCategory category = (ShoppingCategory)itr.next();
299 
300             categoryIds.add(new Long(category.getCategoryId()));
301 
302             getSubcategoryIds(
303                 categoryIds, category.getGroupId(), category.getCategoryId());
304         }
305     }
306 
307     public ShoppingCategory updateCategory(
308             long categoryId, long parentCategoryId, String name,
309             String description, boolean mergeWithParentCategory)
310         throws PortalException, SystemException {
311 
312         // Category
313 
314         ShoppingCategory category =
315             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
316 
317         parentCategoryId = getParentCategoryId(category, parentCategoryId);
318 
319         validate(name);
320 
321         category.setModifiedDate(new Date());
322         category.setParentCategoryId(parentCategoryId);
323         category.setName(name);
324         category.setDescription(description);
325 
326         shoppingCategoryPersistence.update(category);
327 
328         // Merge categories
329 
330         if (mergeWithParentCategory &&
331             (categoryId != parentCategoryId) &&
332             (parentCategoryId !=
333                 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
334 
335             mergeCategories(category, parentCategoryId);
336         }
337 
338         return category;
339     }
340 
341     protected long getParentCategoryId(long groupId, long parentCategoryId)
342         throws SystemException {
343 
344         if (parentCategoryId !=
345                 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
346 
347             ShoppingCategory parentCategory =
348                 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
349 
350             if ((parentCategory == null) ||
351                 (groupId != parentCategory.getGroupId())) {
352 
353                 parentCategoryId =
354                     ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID;
355             }
356         }
357 
358         return parentCategoryId;
359     }
360 
361     protected long getParentCategoryId(
362             ShoppingCategory category, long parentCategoryId)
363         throws SystemException {
364 
365         if (parentCategoryId ==
366                 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
367 
368             return parentCategoryId;
369         }
370 
371         if (category.getCategoryId() == parentCategoryId) {
372             return category.getParentCategoryId();
373         }
374         else {
375             ShoppingCategory parentCategory =
376                 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
377 
378             if ((parentCategory == null) ||
379                 (category.getGroupId() != parentCategory.getGroupId())) {
380 
381                 return category.getParentCategoryId();
382             }
383 
384             List subcategoryIds = new ArrayList();
385 
386             getSubcategoryIds(
387                 subcategoryIds, category.getGroupId(),
388                 category.getCategoryId());
389 
390             if (subcategoryIds.contains(new Long(parentCategoryId))) {
391                 return category.getParentCategoryId();
392             }
393 
394             return parentCategoryId;
395         }
396     }
397 
398     protected void mergeCategories(
399             ShoppingCategory fromCategory, long toCategoryId)
400         throws PortalException, SystemException {
401 
402         Iterator itr = shoppingCategoryPersistence.findByG_P(
403             fromCategory.getGroupId(), fromCategory.getCategoryId()).iterator();
404 
405         while (itr.hasNext()) {
406             ShoppingCategory category = (ShoppingCategory)itr.next();
407 
408             mergeCategories(category, toCategoryId);
409         }
410 
411         itr = shoppingItemPersistence.findByCategoryId(
412             fromCategory.getCategoryId()).iterator();
413 
414         while (itr.hasNext()) {
415 
416             // Item
417 
418             ShoppingItem item = (ShoppingItem)itr.next();
419 
420             item.setCategoryId(toCategoryId);
421 
422             shoppingItemPersistence.update(item);
423         }
424 
425         shoppingCategoryPersistence.remove(fromCategory.getCategoryId());
426     }
427 
428     protected void validate(String name) throws PortalException {
429         if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
430             (name.indexOf("//") != -1)) {
431 
432             throw new CategoryNameException();
433         }
434     }
435 
436 }