001
014
015 package com.liferay.portlet.shopping.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.model.ResourceConstants;
021 import com.liferay.portal.model.User;
022 import com.liferay.portal.service.ServiceContext;
023 import com.liferay.portlet.shopping.CategoryNameException;
024 import com.liferay.portlet.shopping.model.ShoppingCategory;
025 import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
026 import com.liferay.portlet.shopping.model.ShoppingItem;
027 import com.liferay.portlet.shopping.service.base.ShoppingCategoryLocalServiceBaseImpl;
028
029 import java.util.ArrayList;
030 import java.util.Collections;
031 import java.util.Date;
032 import java.util.List;
033
034
037 public class ShoppingCategoryLocalServiceImpl
038 extends ShoppingCategoryLocalServiceBaseImpl {
039
040 @Override
041 public ShoppingCategory addCategory(
042 long userId, long parentCategoryId, String name, String description,
043 ServiceContext serviceContext)
044 throws PortalException, SystemException {
045
046
047
048 User user = userPersistence.findByPrimaryKey(userId);
049 long groupId = serviceContext.getScopeGroupId();
050 parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
051 Date now = new Date();
052
053 validate(name);
054
055 long categoryId = counterLocalService.increment();
056
057 ShoppingCategory category = shoppingCategoryPersistence.create(
058 categoryId);
059
060 category.setGroupId(groupId);
061 category.setCompanyId(user.getCompanyId());
062 category.setUserId(user.getUserId());
063 category.setUserName(user.getFullName());
064 category.setCreateDate(now);
065 category.setModifiedDate(now);
066 category.setParentCategoryId(parentCategoryId);
067 category.setName(name);
068 category.setDescription(description);
069
070 shoppingCategoryPersistence.update(category, false);
071
072
073
074 if (serviceContext.isAddGroupPermissions() ||
075 serviceContext.isAddGuestPermissions()) {
076
077 addCategoryResources(
078 category, serviceContext.isAddGroupPermissions(),
079 serviceContext.isAddGuestPermissions());
080 }
081 else {
082 addCategoryResources(
083 category, serviceContext.getGroupPermissions(),
084 serviceContext.getGuestPermissions());
085 }
086
087 return category;
088 }
089
090 @Override
091 public void addCategoryResources(
092 long categoryId, boolean addGroupPermissions,
093 boolean addGuestPermissions)
094 throws PortalException, SystemException {
095
096 ShoppingCategory category =
097 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
098
099 addCategoryResources(
100 category, addGroupPermissions, addGuestPermissions);
101 }
102
103 @Override
104 public void addCategoryResources(
105 long categoryId, String[] groupPermissions,
106 String[] guestPermissions)
107 throws PortalException, SystemException {
108
109 ShoppingCategory category =
110 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
111
112 addCategoryResources(category, groupPermissions, guestPermissions);
113 }
114
115 @Override
116 public void addCategoryResources(
117 ShoppingCategory category, boolean addGroupPermissions,
118 boolean addGuestPermissions)
119 throws PortalException, SystemException {
120
121 resourceLocalService.addResources(
122 category.getCompanyId(), category.getGroupId(),
123 category.getUserId(), ShoppingCategory.class.getName(),
124 category.getCategoryId(), false, addGroupPermissions,
125 addGuestPermissions);
126 }
127
128 @Override
129 public void addCategoryResources(
130 ShoppingCategory category, String[] groupPermissions,
131 String[] guestPermissions)
132 throws PortalException, SystemException {
133
134 resourceLocalService.addModelResources(
135 category.getCompanyId(), category.getGroupId(),
136 category.getUserId(), ShoppingCategory.class.getName(),
137 category.getCategoryId(), groupPermissions, guestPermissions);
138 }
139
140 @Override
141 public void deleteCategories(long groupId)
142 throws PortalException, SystemException {
143
144 List<ShoppingCategory> categories =
145 shoppingCategoryPersistence.findByGroupId(groupId);
146
147 for (ShoppingCategory category : categories) {
148 deleteCategory(category);
149 }
150 }
151
152 @Override
153 public void deleteCategory(long categoryId)
154 throws PortalException, SystemException {
155
156 ShoppingCategory category =
157 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
158
159 deleteCategory(category);
160 }
161
162 @Override
163 public void deleteCategory(ShoppingCategory category)
164 throws PortalException, SystemException {
165
166
167
168 List<ShoppingCategory> categories =
169 shoppingCategoryPersistence.findByG_P(
170 category.getGroupId(), category.getCategoryId());
171
172 for (ShoppingCategory curCategory : categories) {
173 deleteCategory(curCategory);
174 }
175
176
177
178 shoppingCategoryPersistence.remove(category);
179
180
181
182 resourceLocalService.deleteResource(
183 category.getCompanyId(), ShoppingCategory.class.getName(),
184 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
185
186
187
188 shoppingItemLocalService.deleteItems(
189 category.getGroupId(), category.getCategoryId());
190 }
191
192 @Override
193 public List<ShoppingCategory> getCategories(long groupId)
194 throws SystemException {
195
196 return shoppingCategoryPersistence.findByGroupId(groupId);
197 }
198
199 @Override
200 public List<ShoppingCategory> getCategories(
201 long groupId, long parentCategoryId, int start, int end)
202 throws SystemException {
203
204 return shoppingCategoryPersistence.findByG_P(
205 groupId, parentCategoryId, start, end);
206 }
207
208 @Override
209 public int getCategoriesCount(long groupId, long parentCategoryId)
210 throws SystemException {
211
212 return shoppingCategoryPersistence.countByG_P(
213 groupId, parentCategoryId);
214 }
215
216 @Override
217 public ShoppingCategory getCategory(long categoryId)
218 throws PortalException, SystemException {
219
220 return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
221 }
222
223 @Override
224 public List<ShoppingCategory> getParentCategories(long categoryId)
225 throws PortalException, SystemException {
226
227 return getParentCategories(
228 shoppingCategoryPersistence.findByPrimaryKey(categoryId));
229 }
230
231 @Override
232 public List<ShoppingCategory> getParentCategories(ShoppingCategory category)
233 throws PortalException, SystemException {
234
235 List<ShoppingCategory> parentCategories =
236 new ArrayList<ShoppingCategory>();
237
238 ShoppingCategory tempCategory = category;
239
240 for (;;) {
241 parentCategories.add(tempCategory);
242
243 if (tempCategory.getParentCategoryId() ==
244 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
245
246 break;
247 }
248
249 tempCategory = shoppingCategoryPersistence.findByPrimaryKey(
250 tempCategory.getParentCategoryId());
251 }
252
253 Collections.reverse(parentCategories);
254
255 return parentCategories;
256 }
257
258 @Override
259 public ShoppingCategory getParentCategory(ShoppingCategory category)
260 throws PortalException, SystemException {
261
262 ShoppingCategory parentCategory =
263 shoppingCategoryPersistence.findByPrimaryKey(
264 category.getParentCategoryId());
265
266 return parentCategory;
267 }
268
269 @Override
270 public void getSubcategoryIds(
271 List<Long> categoryIds, long groupId, long categoryId)
272 throws SystemException {
273
274 List<ShoppingCategory> categories =
275 shoppingCategoryPersistence.findByG_P(groupId, categoryId);
276
277 for (ShoppingCategory category : categories) {
278 categoryIds.add(category.getCategoryId());
279
280 getSubcategoryIds(
281 categoryIds, category.getGroupId(), category.getCategoryId());
282 }
283 }
284
285 @Override
286 public ShoppingCategory updateCategory(
287 long categoryId, long parentCategoryId, String name,
288 String description, boolean mergeWithParentCategory,
289 ServiceContext serviceContext)
290 throws PortalException, SystemException {
291
292
293
294 ShoppingCategory category =
295 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
296
297 parentCategoryId = getParentCategoryId(category, parentCategoryId);
298
299 if (mergeWithParentCategory &&
300 (categoryId != parentCategoryId) &&
301 (parentCategoryId !=
302 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
303
304 mergeCategories(category, parentCategoryId);
305
306 return category;
307 }
308
309
310
311 validate(name);
312
313 category.setModifiedDate(new Date());
314 category.setParentCategoryId(parentCategoryId);
315 category.setName(name);
316 category.setDescription(description);
317
318 shoppingCategoryPersistence.update(category, false);
319
320 return category;
321 }
322
323 protected long getParentCategoryId(long groupId, long parentCategoryId)
324 throws SystemException {
325
326 if (parentCategoryId !=
327 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
328
329 ShoppingCategory parentCategory =
330 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
331
332 if ((parentCategory == null) ||
333 (groupId != parentCategory.getGroupId())) {
334
335 parentCategoryId =
336 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
337 }
338 }
339
340 return parentCategoryId;
341 }
342
343 protected long getParentCategoryId(
344 ShoppingCategory category, long parentCategoryId)
345 throws SystemException {
346
347 if (parentCategoryId ==
348 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
349
350 return parentCategoryId;
351 }
352
353 if (category.getCategoryId() == parentCategoryId) {
354 return category.getParentCategoryId();
355 }
356 else {
357 ShoppingCategory parentCategory =
358 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
359
360 if ((parentCategory == null) ||
361 (category.getGroupId() != parentCategory.getGroupId())) {
362
363 return category.getParentCategoryId();
364 }
365
366 List<Long> subcategoryIds = new ArrayList<Long>();
367
368 getSubcategoryIds(
369 subcategoryIds, category.getGroupId(),
370 category.getCategoryId());
371
372 if (subcategoryIds.contains(parentCategoryId)) {
373 return category.getParentCategoryId();
374 }
375
376 return parentCategoryId;
377 }
378 }
379
380 protected void mergeCategories(
381 ShoppingCategory fromCategory, long toCategoryId)
382 throws PortalException, SystemException {
383
384 List<ShoppingCategory> categories =
385 shoppingCategoryPersistence.findByG_P(
386 fromCategory.getGroupId(), fromCategory.getCategoryId());
387
388 for (ShoppingCategory category : categories) {
389 mergeCategories(category, toCategoryId);
390 }
391
392 List<ShoppingItem> items = shoppingItemPersistence.findByG_C(
393 fromCategory.getGroupId(), fromCategory.getCategoryId());
394
395 for (ShoppingItem item : items) {
396
397
398
399 item.setCategoryId(toCategoryId);
400
401 shoppingItemPersistence.update(item, false);
402 }
403
404 deleteCategory(fromCategory);
405 }
406
407 protected void validate(String name) throws PortalException {
408 if (Validator.isNull(name) || name.contains("\\\\") ||
409 name.contains("
410
411 throw new CategoryNameException();
412 }
413 }
414
415 }