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