1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.ResourceConstants;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.ServiceContext;
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.List;
41  
42  /**
43   * <a href="ShoppingCategoryLocalServiceImpl.java.html"><b><i>View Source</i>
44   * </b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class ShoppingCategoryLocalServiceImpl
49      extends ShoppingCategoryLocalServiceBaseImpl {
50  
51      public ShoppingCategory addCategory(
52              long userId, long parentCategoryId, String name,
53              String description, ServiceContext serviceContext)
54          throws PortalException, SystemException {
55  
56          // Category
57  
58          User user = userPersistence.findByPrimaryKey(userId);
59          long groupId = serviceContext.getScopeGroupId();
60          parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
61          Date now = new Date();
62  
63          validate(name);
64  
65          long categoryId = counterLocalService.increment();
66  
67          ShoppingCategory category = shoppingCategoryPersistence.create(
68              categoryId);
69  
70          category.setGroupId(groupId);
71          category.setCompanyId(user.getCompanyId());
72          category.setUserId(user.getUserId());
73          category.setUserName(user.getFullName());
74          category.setCreateDate(now);
75          category.setModifiedDate(now);
76          category.setParentCategoryId(parentCategoryId);
77          category.setName(name);
78          category.setDescription(description);
79  
80          shoppingCategoryPersistence.update(category, false);
81  
82          // Resources
83  
84          if (serviceContext.getAddCommunityPermissions() ||
85              serviceContext.getAddGuestPermissions()) {
86  
87              addCategoryResources(
88                  category, serviceContext.getAddCommunityPermissions(),
89                  serviceContext.getAddGuestPermissions());
90          }
91          else {
92              addCategoryResources(
93                  category, serviceContext.getCommunityPermissions(),
94                  serviceContext.getGuestPermissions());
95          }
96  
97          return category;
98      }
99  
100     public void addCategoryResources(
101             long categoryId, boolean addCommunityPermissions,
102             boolean addGuestPermissions)
103         throws PortalException, SystemException {
104 
105         ShoppingCategory category =
106             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
107 
108         addCategoryResources(
109             category, addCommunityPermissions, addGuestPermissions);
110     }
111 
112     public void addCategoryResources(
113             long categoryId, String[] communityPermissions,
114             String[] guestPermissions)
115         throws PortalException, SystemException {
116 
117         ShoppingCategory category =
118             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
119 
120         addCategoryResources(category, communityPermissions, guestPermissions);
121     }
122 
123     public void addCategoryResources(
124             ShoppingCategory category, boolean addCommunityPermissions,
125             boolean addGuestPermissions)
126         throws PortalException, SystemException {
127 
128         resourceLocalService.addResources(
129             category.getCompanyId(), category.getGroupId(),
130             category.getUserId(), ShoppingCategory.class.getName(),
131             category.getCategoryId(), false, addCommunityPermissions,
132             addGuestPermissions);
133     }
134 
135     public void addCategoryResources(
136             ShoppingCategory category, String[] communityPermissions,
137             String[] guestPermissions)
138         throws PortalException, SystemException {
139 
140         resourceLocalService.addModelResources(
141             category.getCompanyId(), category.getGroupId(),
142             category.getUserId(), ShoppingCategory.class.getName(),
143             category.getCategoryId(), communityPermissions, guestPermissions);
144     }
145 
146     public void deleteCategories(long groupId)
147         throws PortalException, SystemException {
148 
149         List<ShoppingCategory> categories =
150             shoppingCategoryPersistence.findByGroupId(groupId);
151 
152         for (ShoppingCategory category : categories) {
153             deleteCategory(category);
154         }
155     }
156 
157     public void deleteCategory(long categoryId)
158         throws PortalException, SystemException {
159 
160         ShoppingCategory category =
161             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
162 
163         deleteCategory(category);
164     }
165 
166     public void deleteCategory(ShoppingCategory category)
167         throws PortalException, SystemException {
168 
169         // Category
170 
171         shoppingCategoryPersistence.remove(category);
172 
173         // Resources
174 
175         resourceLocalService.deleteResource(
176             category.getCompanyId(), ShoppingCategory.class.getName(),
177             ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
178 
179         // Categories
180 
181         List<ShoppingCategory> categories =
182             shoppingCategoryPersistence.findByG_P(
183                 category.getGroupId(), category.getCategoryId());
184 
185         for (ShoppingCategory curCategory : categories) {
186             deleteCategory(curCategory);
187         }
188 
189         // Items
190 
191         shoppingItemLocalService.deleteItems(category.getCategoryId());
192     }
193 
194     public List<ShoppingCategory> getCategories(long groupId)
195         throws SystemException {
196 
197         return shoppingCategoryPersistence.findByGroupId(groupId);
198     }
199 
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     public int getCategoriesCount(long groupId, long parentCategoryId)
209         throws SystemException {
210 
211         return shoppingCategoryPersistence.countByG_P(
212             groupId, parentCategoryId);
213     }
214 
215     public ShoppingCategory getCategory(long categoryId)
216         throws PortalException, SystemException {
217 
218         return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
219     }
220 
221     public List<ShoppingCategory> getParentCategories(long categoryId)
222         throws PortalException, SystemException {
223 
224         return getParentCategories(
225             shoppingCategoryPersistence.findByPrimaryKey(categoryId));
226     }
227 
228     public List<ShoppingCategory> getParentCategories(ShoppingCategory category)
229         throws PortalException, SystemException {
230 
231         List<ShoppingCategory> parentCategories =
232             new ArrayList<ShoppingCategory>();
233 
234         ShoppingCategory tempCategory = category;
235 
236         for (;;) {
237             parentCategories.add(tempCategory);
238 
239             if (tempCategory.getParentCategoryId() ==
240                     ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
241 
242                 break;
243             }
244 
245             tempCategory = shoppingCategoryPersistence.findByPrimaryKey(
246                 tempCategory.getParentCategoryId());
247         }
248 
249         Collections.reverse(parentCategories);
250 
251         return parentCategories;
252     }
253 
254     public ShoppingCategory getParentCategory(ShoppingCategory category)
255         throws PortalException, SystemException {
256 
257         ShoppingCategory parentCategory =
258             shoppingCategoryPersistence.findByPrimaryKey(
259                 category.getParentCategoryId());
260 
261         return parentCategory;
262     }
263 
264     public void getSubcategoryIds(
265             List<Long> categoryIds, long groupId, long categoryId)
266         throws SystemException {
267 
268         List<ShoppingCategory> categories =
269             shoppingCategoryPersistence.findByG_P(groupId, categoryId);
270 
271         for (ShoppingCategory category : categories) {
272             categoryIds.add(category.getCategoryId());
273 
274             getSubcategoryIds(
275                 categoryIds, category.getGroupId(), category.getCategoryId());
276         }
277     }
278 
279     public ShoppingCategory updateCategory(
280             long categoryId, long parentCategoryId, String name,
281             String description, boolean mergeWithParentCategory,
282             ServiceContext serviceContext)
283         throws PortalException, SystemException {
284 
285         // Category
286 
287         ShoppingCategory category =
288             shoppingCategoryPersistence.findByPrimaryKey(categoryId);
289 
290         parentCategoryId = getParentCategoryId(category, parentCategoryId);
291 
292         validate(name);
293 
294         category.setModifiedDate(new Date());
295         category.setParentCategoryId(parentCategoryId);
296         category.setName(name);
297         category.setDescription(description);
298 
299         shoppingCategoryPersistence.update(category, false);
300 
301         // Merge categories
302 
303         if (mergeWithParentCategory &&
304             (categoryId != parentCategoryId) &&
305             (parentCategoryId !=
306                 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
307 
308             mergeCategories(category, parentCategoryId);
309         }
310 
311         return category;
312     }
313 
314     protected long getParentCategoryId(long groupId, long parentCategoryId)
315         throws SystemException {
316 
317         if (parentCategoryId !=
318                 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
319 
320             ShoppingCategory parentCategory =
321                 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
322 
323             if ((parentCategory == null) ||
324                 (groupId != parentCategory.getGroupId())) {
325 
326                 parentCategoryId =
327                     ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID;
328             }
329         }
330 
331         return parentCategoryId;
332     }
333 
334     protected long getParentCategoryId(
335             ShoppingCategory category, long parentCategoryId)
336         throws SystemException {
337 
338         if (parentCategoryId ==
339                 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
340 
341             return parentCategoryId;
342         }
343 
344         if (category.getCategoryId() == parentCategoryId) {
345             return category.getParentCategoryId();
346         }
347         else {
348             ShoppingCategory parentCategory =
349                 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
350 
351             if ((parentCategory == null) ||
352                 (category.getGroupId() != parentCategory.getGroupId())) {
353 
354                 return category.getParentCategoryId();
355             }
356 
357             List<Long> subcategoryIds = new ArrayList<Long>();
358 
359             getSubcategoryIds(
360                 subcategoryIds, category.getGroupId(),
361                 category.getCategoryId());
362 
363             if (subcategoryIds.contains(parentCategoryId)) {
364                 return category.getParentCategoryId();
365             }
366 
367             return parentCategoryId;
368         }
369     }
370 
371     protected void mergeCategories(
372             ShoppingCategory fromCategory, long toCategoryId)
373         throws PortalException, SystemException {
374 
375         List<ShoppingCategory> categories =
376             shoppingCategoryPersistence.findByG_P(
377                 fromCategory.getGroupId(), fromCategory.getCategoryId());
378 
379         for (ShoppingCategory category : categories) {
380             mergeCategories(category, toCategoryId);
381         }
382 
383         List<ShoppingItem> items = shoppingItemPersistence.findByCategoryId(
384             fromCategory.getCategoryId());
385 
386         for (ShoppingItem item : items) {
387 
388             // Item
389 
390             item.setCategoryId(toCategoryId);
391 
392             shoppingItemPersistence.update(item, false);
393         }
394 
395         deleteCategory(fromCategory);
396     }
397 
398     protected void validate(String name) throws PortalException {
399         if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
400             (name.indexOf("//") != -1)) {
401 
402             throw new CategoryNameException();
403         }
404     }
405 
406 }