1
14
15 package com.liferay.portlet.shopping.service.impl;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.util.Validator;
20 import com.liferay.portal.model.ResourceConstants;
21 import com.liferay.portal.model.User;
22 import com.liferay.portal.service.ServiceContext;
23 import com.liferay.portlet.shopping.CategoryNameException;
24 import com.liferay.portlet.shopping.model.ShoppingCategory;
25 import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
26 import com.liferay.portlet.shopping.model.ShoppingItem;
27 import com.liferay.portlet.shopping.service.base.ShoppingCategoryLocalServiceBaseImpl;
28
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Date;
32 import java.util.List;
33
34
40 public class ShoppingCategoryLocalServiceImpl
41 extends ShoppingCategoryLocalServiceBaseImpl {
42
43 public ShoppingCategory addCategory(
44 long userId, long parentCategoryId, String name,
45 String description, ServiceContext serviceContext)
46 throws PortalException, SystemException {
47
48
50 User user = userPersistence.findByPrimaryKey(userId);
51 long groupId = serviceContext.getScopeGroupId();
52 parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
53 Date now = new Date();
54
55 validate(name);
56
57 long categoryId = counterLocalService.increment();
58
59 ShoppingCategory category = shoppingCategoryPersistence.create(
60 categoryId);
61
62 category.setGroupId(groupId);
63 category.setCompanyId(user.getCompanyId());
64 category.setUserId(user.getUserId());
65 category.setUserName(user.getFullName());
66 category.setCreateDate(now);
67 category.setModifiedDate(now);
68 category.setParentCategoryId(parentCategoryId);
69 category.setName(name);
70 category.setDescription(description);
71
72 shoppingCategoryPersistence.update(category, false);
73
74
76 if (serviceContext.getAddCommunityPermissions() ||
77 serviceContext.getAddGuestPermissions()) {
78
79 addCategoryResources(
80 category, serviceContext.getAddCommunityPermissions(),
81 serviceContext.getAddGuestPermissions());
82 }
83 else {
84 addCategoryResources(
85 category, serviceContext.getCommunityPermissions(),
86 serviceContext.getGuestPermissions());
87 }
88
89 return category;
90 }
91
92 public void addCategoryResources(
93 long categoryId, boolean addCommunityPermissions,
94 boolean addGuestPermissions)
95 throws PortalException, SystemException {
96
97 ShoppingCategory category =
98 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
99
100 addCategoryResources(
101 category, addCommunityPermissions, addGuestPermissions);
102 }
103
104 public void addCategoryResources(
105 long categoryId, String[] communityPermissions,
106 String[] guestPermissions)
107 throws PortalException, SystemException {
108
109 ShoppingCategory category =
110 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
111
112 addCategoryResources(category, communityPermissions, guestPermissions);
113 }
114
115 public void addCategoryResources(
116 ShoppingCategory category, boolean addCommunityPermissions,
117 boolean addGuestPermissions)
118 throws PortalException, SystemException {
119
120 resourceLocalService.addResources(
121 category.getCompanyId(), category.getGroupId(),
122 category.getUserId(), ShoppingCategory.class.getName(),
123 category.getCategoryId(), false, addCommunityPermissions,
124 addGuestPermissions);
125 }
126
127 public void addCategoryResources(
128 ShoppingCategory category, String[] communityPermissions,
129 String[] guestPermissions)
130 throws PortalException, SystemException {
131
132 resourceLocalService.addModelResources(
133 category.getCompanyId(), category.getGroupId(),
134 category.getUserId(), ShoppingCategory.class.getName(),
135 category.getCategoryId(), communityPermissions, guestPermissions);
136 }
137
138 public void deleteCategories(long groupId)
139 throws PortalException, SystemException {
140
141 List<ShoppingCategory> categories =
142 shoppingCategoryPersistence.findByGroupId(groupId);
143
144 for (ShoppingCategory category : categories) {
145 deleteCategory(category);
146 }
147 }
148
149 public void deleteCategory(long categoryId)
150 throws PortalException, SystemException {
151
152 ShoppingCategory category =
153 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
154
155 deleteCategory(category);
156 }
157
158 public void deleteCategory(ShoppingCategory category)
159 throws PortalException, SystemException {
160
161
163 shoppingCategoryPersistence.remove(category);
164
165
167 resourceLocalService.deleteResource(
168 category.getCompanyId(), ShoppingCategory.class.getName(),
169 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
170
171
173 List<ShoppingCategory> categories =
174 shoppingCategoryPersistence.findByG_P(
175 category.getGroupId(), category.getCategoryId());
176
177 for (ShoppingCategory curCategory : categories) {
178 deleteCategory(curCategory);
179 }
180
181
183 shoppingItemLocalService.deleteItems(
184 category.getGroupId(), category.getCategoryId());
185 }
186
187 public List<ShoppingCategory> getCategories(long groupId)
188 throws SystemException {
189
190 return shoppingCategoryPersistence.findByGroupId(groupId);
191 }
192
193 public List<ShoppingCategory> getCategories(
194 long groupId, long parentCategoryId, int start, int end)
195 throws SystemException {
196
197 return shoppingCategoryPersistence.findByG_P(
198 groupId, parentCategoryId, start, end);
199 }
200
201 public int getCategoriesCount(long groupId, long parentCategoryId)
202 throws SystemException {
203
204 return shoppingCategoryPersistence.countByG_P(
205 groupId, parentCategoryId);
206 }
207
208 public ShoppingCategory getCategory(long categoryId)
209 throws PortalException, SystemException {
210
211 return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
212 }
213
214 public List<ShoppingCategory> getParentCategories(long categoryId)
215 throws PortalException, SystemException {
216
217 return getParentCategories(
218 shoppingCategoryPersistence.findByPrimaryKey(categoryId));
219 }
220
221 public List<ShoppingCategory> getParentCategories(ShoppingCategory category)
222 throws PortalException, SystemException {
223
224 List<ShoppingCategory> parentCategories =
225 new ArrayList<ShoppingCategory>();
226
227 ShoppingCategory tempCategory = category;
228
229 for (;;) {
230 parentCategories.add(tempCategory);
231
232 if (tempCategory.getParentCategoryId() ==
233 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
234
235 break;
236 }
237
238 tempCategory = shoppingCategoryPersistence.findByPrimaryKey(
239 tempCategory.getParentCategoryId());
240 }
241
242 Collections.reverse(parentCategories);
243
244 return parentCategories;
245 }
246
247 public ShoppingCategory getParentCategory(ShoppingCategory category)
248 throws PortalException, SystemException {
249
250 ShoppingCategory parentCategory =
251 shoppingCategoryPersistence.findByPrimaryKey(
252 category.getParentCategoryId());
253
254 return parentCategory;
255 }
256
257 public void getSubcategoryIds(
258 List<Long> categoryIds, long groupId, long categoryId)
259 throws SystemException {
260
261 List<ShoppingCategory> categories =
262 shoppingCategoryPersistence.findByG_P(groupId, categoryId);
263
264 for (ShoppingCategory category : categories) {
265 categoryIds.add(category.getCategoryId());
266
267 getSubcategoryIds(
268 categoryIds, category.getGroupId(), category.getCategoryId());
269 }
270 }
271
272 public ShoppingCategory updateCategory(
273 long categoryId, long parentCategoryId, String name,
274 String description, boolean mergeWithParentCategory,
275 ServiceContext serviceContext)
276 throws PortalException, SystemException {
277
278
280 ShoppingCategory category =
281 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
282
283 parentCategoryId = getParentCategoryId(category, parentCategoryId);
284
285 if (mergeWithParentCategory &&
286 (categoryId != parentCategoryId) &&
287 (parentCategoryId !=
288 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
289
290 mergeCategories(category, parentCategoryId);
291
292 return category;
293 }
294
295
297 validate(name);
298
299 category.setModifiedDate(new Date());
300 category.setParentCategoryId(parentCategoryId);
301 category.setName(name);
302 category.setDescription(description);
303
304 shoppingCategoryPersistence.update(category, false);
305
306 return category;
307 }
308
309 protected long getParentCategoryId(long groupId, long parentCategoryId)
310 throws SystemException {
311
312 if (parentCategoryId !=
313 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
314
315 ShoppingCategory parentCategory =
316 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
317
318 if ((parentCategory == null) ||
319 (groupId != parentCategory.getGroupId())) {
320
321 parentCategoryId =
322 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
323 }
324 }
325
326 return parentCategoryId;
327 }
328
329 protected long getParentCategoryId(
330 ShoppingCategory category, long parentCategoryId)
331 throws SystemException {
332
333 if (parentCategoryId ==
334 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
335
336 return parentCategoryId;
337 }
338
339 if (category.getCategoryId() == parentCategoryId) {
340 return category.getParentCategoryId();
341 }
342 else {
343 ShoppingCategory parentCategory =
344 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
345
346 if ((parentCategory == null) ||
347 (category.getGroupId() != parentCategory.getGroupId())) {
348
349 return category.getParentCategoryId();
350 }
351
352 List<Long> subcategoryIds = new ArrayList<Long>();
353
354 getSubcategoryIds(
355 subcategoryIds, category.getGroupId(),
356 category.getCategoryId());
357
358 if (subcategoryIds.contains(parentCategoryId)) {
359 return category.getParentCategoryId();
360 }
361
362 return parentCategoryId;
363 }
364 }
365
366 protected void mergeCategories(
367 ShoppingCategory fromCategory, long toCategoryId)
368 throws PortalException, SystemException {
369
370 List<ShoppingCategory> categories =
371 shoppingCategoryPersistence.findByG_P(
372 fromCategory.getGroupId(), fromCategory.getCategoryId());
373
374 for (ShoppingCategory category : categories) {
375 mergeCategories(category, toCategoryId);
376 }
377
378 List<ShoppingItem> items = shoppingItemPersistence.findByG_C(
379 fromCategory.getGroupId(), fromCategory.getCategoryId());
380
381 for (ShoppingItem item : items) {
382
383
385 item.setCategoryId(toCategoryId);
386
387 shoppingItemPersistence.update(item, false);
388 }
389
390 deleteCategory(fromCategory);
391 }
392
393 protected void validate(String name) throws PortalException {
394 if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
395 (name.indexOf("//") != -1)) {
396
397 throw new CategoryNameException();
398 }
399 }
400
401 }