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