1
22
23 package com.liferay.portlet.shopping.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.User;
30 import com.liferay.portal.model.impl.ResourceImpl;
31 import com.liferay.portal.service.ResourceLocalServiceUtil;
32 import com.liferay.portal.service.persistence.UserUtil;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.portlet.shopping.CategoryNameException;
35 import com.liferay.portlet.shopping.model.ShoppingCategory;
36 import com.liferay.portlet.shopping.model.ShoppingItem;
37 import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
38 import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
39 import com.liferay.portlet.shopping.service.base.ShoppingCategoryLocalServiceBaseImpl;
40 import com.liferay.portlet.shopping.service.persistence.ShoppingCategoryUtil;
41 import com.liferay.portlet.shopping.service.persistence.ShoppingItemUtil;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.Iterator;
47 import java.util.List;
48
49
56 public class ShoppingCategoryLocalServiceImpl
57 extends ShoppingCategoryLocalServiceBaseImpl {
58
59 public ShoppingCategory addCategory(
60 long userId, long plid, long parentCategoryId, String name,
61 String description, boolean addCommunityPermissions,
62 boolean addGuestPermissions)
63 throws PortalException, SystemException {
64
65 return addCategory(
66 userId, plid, parentCategoryId, name, description,
67 Boolean.valueOf(addCommunityPermissions),
68 Boolean.valueOf(addGuestPermissions), null, null);
69 }
70
71 public ShoppingCategory addCategory(
72 long userId, long plid, long parentCategoryId, String name,
73 String description, String[] communityPermissions,
74 String[] guestPermissions)
75 throws PortalException, SystemException {
76
77 return addCategory(
78 userId, plid, parentCategoryId, name, description, null, null,
79 communityPermissions, guestPermissions);
80 }
81
82 public ShoppingCategory addCategory(
83 long userId, long plid, long parentCategoryId, String name,
84 String description, Boolean addCommunityPermissions,
85 Boolean addGuestPermissions, String[] communityPermissions,
86 String[] guestPermissions)
87 throws PortalException, SystemException {
88
89
91 User user = UserUtil.findByPrimaryKey(userId);
92 long groupId = PortalUtil.getPortletGroupId(plid);
93 parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
94 Date now = new Date();
95
96 validate(name);
97
98 long categoryId = CounterLocalServiceUtil.increment();
99
100 ShoppingCategory category = ShoppingCategoryUtil.create(categoryId);
101
102 category.setGroupId(groupId);
103 category.setCompanyId(user.getCompanyId());
104 category.setUserId(user.getUserId());
105 category.setUserName(user.getFullName());
106 category.setCreateDate(now);
107 category.setModifiedDate(now);
108 category.setParentCategoryId(parentCategoryId);
109 category.setName(name);
110 category.setDescription(description);
111
112 ShoppingCategoryUtil.update(category);
113
114
116 if ((addCommunityPermissions != null) &&
117 (addGuestPermissions != null)) {
118
119 addCategoryResources(
120 category, addCommunityPermissions.booleanValue(),
121 addGuestPermissions.booleanValue());
122 }
123 else {
124 addCategoryResources(
125 category, communityPermissions, guestPermissions);
126 }
127
128 return category;
129 }
130
131 public void addCategoryResources(
132 long categoryId, boolean addCommunityPermissions,
133 boolean addGuestPermissions)
134 throws PortalException, SystemException {
135
136 ShoppingCategory category =
137 ShoppingCategoryUtil.findByPrimaryKey(categoryId);
138
139 addCategoryResources(
140 category, addCommunityPermissions, addGuestPermissions);
141 }
142
143 public void addCategoryResources(
144 ShoppingCategory category, boolean addCommunityPermissions,
145 boolean addGuestPermissions)
146 throws PortalException, SystemException {
147
148 ResourceLocalServiceUtil.addResources(
149 category.getCompanyId(), category.getGroupId(),
150 category.getUserId(), ShoppingCategory.class.getName(),
151 category.getCategoryId(), false, addCommunityPermissions,
152 addGuestPermissions);
153 }
154
155 public void addCategoryResources(
156 long categoryId, String[] communityPermissions,
157 String[] guestPermissions)
158 throws PortalException, SystemException {
159
160 ShoppingCategory category =
161 ShoppingCategoryUtil.findByPrimaryKey(categoryId);
162
163 addCategoryResources(category, communityPermissions, guestPermissions);
164 }
165
166 public void addCategoryResources(
167 ShoppingCategory category, String[] communityPermissions,
168 String[] guestPermissions)
169 throws PortalException, SystemException {
170
171 ResourceLocalServiceUtil.addModelResources(
172 category.getCompanyId(), category.getGroupId(),
173 category.getUserId(), ShoppingCategory.class.getName(),
174 category.getCategoryId(), communityPermissions, guestPermissions);
175 }
176
177 public void deleteCategory(long categoryId)
178 throws PortalException, SystemException {
179
180 ShoppingCategory category =
181 ShoppingCategoryUtil.findByPrimaryKey(categoryId);
182
183 deleteCategory(category);
184 }
185
186 public void deleteCategory(ShoppingCategory category)
187 throws PortalException, SystemException {
188
189
191 Iterator itr = ShoppingCategoryUtil.findByG_P(
192 category.getGroupId(), category.getCategoryId()).iterator();
193
194 while (itr.hasNext()) {
195 ShoppingCategory curCategory = (ShoppingCategory)itr.next();
196
197 deleteCategory(curCategory);
198 }
199
200
202 ShoppingItemLocalServiceUtil.deleteItems(category.getCategoryId());
203
204
206 ResourceLocalServiceUtil.deleteResource(
207 category.getCompanyId(), ShoppingCategory.class.getName(),
208 ResourceImpl.SCOPE_INDIVIDUAL, category.getCategoryId());
209
210
212 ShoppingCategoryUtil.remove(category.getCategoryId());
213 }
214
215 public List getCategories(long groupId) throws SystemException {
216 return ShoppingCategoryUtil.findByGroupId(groupId);
217 }
218
219 public List getCategories(
220 long groupId, long parentCategoryId, int begin, int end)
221 throws SystemException {
222
223 return ShoppingCategoryUtil.findByG_P(
224 groupId, parentCategoryId, begin, end);
225 }
226
227 public int getCategoriesCount(long groupId, long parentCategoryId)
228 throws SystemException {
229
230 return ShoppingCategoryUtil.countByG_P(groupId, parentCategoryId);
231 }
232
233 public ShoppingCategory getCategory(long categoryId)
234 throws PortalException, SystemException {
235
236 return ShoppingCategoryUtil.findByPrimaryKey(categoryId);
237 }
238
239 public ShoppingCategory getParentCategory(ShoppingCategory category)
240 throws PortalException, SystemException {
241
242 ShoppingCategory parentCategory = ShoppingCategoryUtil.findByPrimaryKey(
243 category.getParentCategoryId());
244
245 return parentCategory;
246 }
247
248 public List getParentCategories(long categoryId)
249 throws PortalException, SystemException {
250
251 return getParentCategories(
252 ShoppingCategoryUtil.findByPrimaryKey(categoryId));
253 }
254
255 public List getParentCategories(ShoppingCategory category)
256 throws PortalException, SystemException {
257
258 List parentCategories = new ArrayList();
259
260 ShoppingCategory tempCategory = category;
261
262 for (;;) {
263 parentCategories.add(tempCategory);
264
265 if (tempCategory.getParentCategoryId() ==
266 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
267
268 break;
269 }
270
271 tempCategory = ShoppingCategoryUtil.findByPrimaryKey(
272 tempCategory.getParentCategoryId());
273 }
274
275 Collections.reverse(parentCategories);
276
277 return parentCategories;
278 }
279
280 public void getSubcategoryIds(
281 List categoryIds, long groupId, long categoryId)
282 throws SystemException {
283
284 Iterator itr = ShoppingCategoryUtil.findByG_P(
285 groupId, categoryId).iterator();
286
287 while (itr.hasNext()) {
288 ShoppingCategory category = (ShoppingCategory)itr.next();
289
290 categoryIds.add(new Long(category.getCategoryId()));
291
292 getSubcategoryIds(
293 categoryIds, category.getGroupId(), category.getCategoryId());
294 }
295 }
296
297 public ShoppingCategory updateCategory(
298 long categoryId, long parentCategoryId, String name,
299 String description, boolean mergeWithParentCategory)
300 throws PortalException, SystemException {
301
302
304 ShoppingCategory category =
305 ShoppingCategoryUtil.findByPrimaryKey(categoryId);
306
307 parentCategoryId = getParentCategoryId(category, parentCategoryId);
308
309 validate(name);
310
311 category.setModifiedDate(new Date());
312 category.setParentCategoryId(parentCategoryId);
313 category.setName(name);
314 category.setDescription(description);
315
316 ShoppingCategoryUtil.update(category);
317
318
320 if (mergeWithParentCategory &&
321 (categoryId != parentCategoryId) &&
322 (parentCategoryId !=
323 ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
324
325 mergeCategories(category, parentCategoryId);
326 }
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 ShoppingCategoryUtil.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 ShoppingCategoryUtil.fetchByPrimaryKey(parentCategoryId);
367
368 if ((parentCategory == null) ||
369 (category.getGroupId() != parentCategory.getGroupId())) {
370
371 return category.getParentCategoryId();
372 }
373
374 List subcategoryIds = new ArrayList();
375
376 getSubcategoryIds(
377 subcategoryIds, category.getGroupId(),
378 category.getCategoryId());
379
380 if (subcategoryIds.contains(new Long(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 Iterator itr = ShoppingCategoryUtil.findByG_P(
393 fromCategory.getGroupId(), fromCategory.getCategoryId()).iterator();
394
395 while (itr.hasNext()) {
396 ShoppingCategory category = (ShoppingCategory)itr.next();
397
398 mergeCategories(category, toCategoryId);
399 }
400
401 itr = ShoppingItemUtil.findByCategoryId(
402 fromCategory.getCategoryId()).iterator();
403
404 while (itr.hasNext()) {
405
406
408 ShoppingItem item = (ShoppingItem)itr.next();
409
410 item.setCategoryId(toCategoryId);
411
412 ShoppingItemUtil.update(item);
413 }
414
415 ShoppingCategoryUtil.remove(fromCategory.getCategoryId());
416 }
417
418 protected void validate(String name) throws PortalException {
419 if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
420 (name.indexOf("//") != -1)) {
421
422 throw new CategoryNameException();
423 }
424 }
425
426 }