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