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