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