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