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