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 public ShoppingCategory addCategory(
041 long userId, long parentCategoryId, String name,
042 String description, ServiceContext serviceContext)
043 throws PortalException, SystemException {
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, false);
070
071
072
073 if (serviceContext.getAddCommunityPermissions() ||
074 serviceContext.getAddGuestPermissions()) {
075
076 addCategoryResources(
077 category, serviceContext.getAddCommunityPermissions(),
078 serviceContext.getAddGuestPermissions());
079 }
080 else {
081 addCategoryResources(
082 category, serviceContext.getCommunityPermissions(),
083 serviceContext.getGuestPermissions());
084 }
085
086 return category;
087 }
088
089 public void addCategoryResources(
090 long categoryId, boolean addCommunityPermissions,
091 boolean addGuestPermissions)
092 throws PortalException, SystemException {
093
094 ShoppingCategory category =
095 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
096
097 addCategoryResources(
098 category, addCommunityPermissions, addGuestPermissions);
099 }
100
101 public void addCategoryResources(
102 long categoryId, String[] communityPermissions,
103 String[] guestPermissions)
104 throws PortalException, SystemException {
105
106 ShoppingCategory category =
107 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
108
109 addCategoryResources(category, communityPermissions, guestPermissions);
110 }
111
112 public void addCategoryResources(
113 ShoppingCategory category, boolean addCommunityPermissions,
114 boolean addGuestPermissions)
115 throws PortalException, SystemException {
116
117 resourceLocalService.addResources(
118 category.getCompanyId(), category.getGroupId(),
119 category.getUserId(), ShoppingCategory.class.getName(),
120 category.getCategoryId(), false, addCommunityPermissions,
121 addGuestPermissions);
122 }
123
124 public void addCategoryResources(
125 ShoppingCategory category, String[] communityPermissions,
126 String[] guestPermissions)
127 throws PortalException, SystemException {
128
129 resourceLocalService.addModelResources(
130 category.getCompanyId(), category.getGroupId(),
131 category.getUserId(), ShoppingCategory.class.getName(),
132 category.getCategoryId(), communityPermissions, guestPermissions);
133 }
134
135 public void deleteCategories(long groupId)
136 throws PortalException, SystemException {
137
138 List<ShoppingCategory> categories =
139 shoppingCategoryPersistence.findByGroupId(groupId);
140
141 for (ShoppingCategory category : categories) {
142 deleteCategory(category);
143 }
144 }
145
146 public void deleteCategory(long categoryId)
147 throws PortalException, SystemException {
148
149 ShoppingCategory category =
150 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
151
152 deleteCategory(category);
153 }
154
155 public void deleteCategory(ShoppingCategory category)
156 throws PortalException, SystemException {
157
158
159
160 List<ShoppingCategory> categories =
161 shoppingCategoryPersistence.findByG_P(
162 category.getGroupId(), category.getCategoryId());
163
164 for (ShoppingCategory curCategory : categories) {
165 deleteCategory(curCategory);
166 }
167
168
169
170 shoppingCategoryPersistence.remove(category);
171
172
173
174 resourceLocalService.deleteResource(
175 category.getCompanyId(), ShoppingCategory.class.getName(),
176 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
177
178
179
180 shoppingItemLocalService.deleteItems(
181 category.getGroupId(), category.getCategoryId());
182 }
183
184 public List<ShoppingCategory> getCategories(long groupId)
185 throws SystemException {
186
187 return shoppingCategoryPersistence.findByGroupId(groupId);
188 }
189
190 public List<ShoppingCategory> getCategories(
191 long groupId, long parentCategoryId, int start, int end)
192 throws SystemException {
193
194 return shoppingCategoryPersistence.findByG_P(
195 groupId, parentCategoryId, start, end);
196 }
197
198 public int getCategoriesCount(long groupId, long parentCategoryId)
199 throws SystemException {
200
201 return shoppingCategoryPersistence.countByG_P(
202 groupId, parentCategoryId);
203 }
204
205 public ShoppingCategory getCategory(long categoryId)
206 throws PortalException, SystemException {
207
208 return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
209 }
210
211 public List<ShoppingCategory> getParentCategories(long categoryId)
212 throws PortalException, SystemException {
213
214 return getParentCategories(
215 shoppingCategoryPersistence.findByPrimaryKey(categoryId));
216 }
217
218 public List<ShoppingCategory> getParentCategories(ShoppingCategory category)
219 throws PortalException, SystemException {
220
221 List<ShoppingCategory> parentCategories =
222 new ArrayList<ShoppingCategory>();
223
224 ShoppingCategory tempCategory = category;
225
226 for (;;) {
227 parentCategories.add(tempCategory);
228
229 if (tempCategory.getParentCategoryId() ==
230 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
231
232 break;
233 }
234
235 tempCategory = shoppingCategoryPersistence.findByPrimaryKey(
236 tempCategory.getParentCategoryId());
237 }
238
239 Collections.reverse(parentCategories);
240
241 return parentCategories;
242 }
243
244 public ShoppingCategory getParentCategory(ShoppingCategory category)
245 throws PortalException, SystemException {
246
247 ShoppingCategory parentCategory =
248 shoppingCategoryPersistence.findByPrimaryKey(
249 category.getParentCategoryId());
250
251 return parentCategory;
252 }
253
254 public void getSubcategoryIds(
255 List<Long> categoryIds, long groupId, long categoryId)
256 throws SystemException {
257
258 List<ShoppingCategory> categories =
259 shoppingCategoryPersistence.findByG_P(groupId, categoryId);
260
261 for (ShoppingCategory category : categories) {
262 categoryIds.add(category.getCategoryId());
263
264 getSubcategoryIds(
265 categoryIds, category.getGroupId(), category.getCategoryId());
266 }
267 }
268
269 public ShoppingCategory updateCategory(
270 long categoryId, long parentCategoryId, String name,
271 String description, boolean mergeWithParentCategory,
272 ServiceContext serviceContext)
273 throws PortalException, SystemException {
274
275
276
277 ShoppingCategory category =
278 shoppingCategoryPersistence.findByPrimaryKey(categoryId);
279
280 parentCategoryId = getParentCategoryId(category, parentCategoryId);
281
282 if (mergeWithParentCategory &&
283 (categoryId != parentCategoryId) &&
284 (parentCategoryId !=
285 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
286
287 mergeCategories(category, parentCategoryId);
288
289 return category;
290 }
291
292
293
294 validate(name);
295
296 category.setModifiedDate(new Date());
297 category.setParentCategoryId(parentCategoryId);
298 category.setName(name);
299 category.setDescription(description);
300
301 shoppingCategoryPersistence.update(category, false);
302
303 return category;
304 }
305
306 protected long getParentCategoryId(long groupId, long parentCategoryId)
307 throws SystemException {
308
309 if (parentCategoryId !=
310 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
311
312 ShoppingCategory parentCategory =
313 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
314
315 if ((parentCategory == null) ||
316 (groupId != parentCategory.getGroupId())) {
317
318 parentCategoryId =
319 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
320 }
321 }
322
323 return parentCategoryId;
324 }
325
326 protected long getParentCategoryId(
327 ShoppingCategory category, long parentCategoryId)
328 throws SystemException {
329
330 if (parentCategoryId ==
331 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
332
333 return parentCategoryId;
334 }
335
336 if (category.getCategoryId() == parentCategoryId) {
337 return category.getParentCategoryId();
338 }
339 else {
340 ShoppingCategory parentCategory =
341 shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);
342
343 if ((parentCategory == null) ||
344 (category.getGroupId() != parentCategory.getGroupId())) {
345
346 return category.getParentCategoryId();
347 }
348
349 List<Long> subcategoryIds = new ArrayList<Long>();
350
351 getSubcategoryIds(
352 subcategoryIds, category.getGroupId(),
353 category.getCategoryId());
354
355 if (subcategoryIds.contains(parentCategoryId)) {
356 return category.getParentCategoryId();
357 }
358
359 return parentCategoryId;
360 }
361 }
362
363 protected void mergeCategories(
364 ShoppingCategory fromCategory, long toCategoryId)
365 throws PortalException, SystemException {
366
367 List<ShoppingCategory> categories =
368 shoppingCategoryPersistence.findByG_P(
369 fromCategory.getGroupId(), fromCategory.getCategoryId());
370
371 for (ShoppingCategory category : categories) {
372 mergeCategories(category, toCategoryId);
373 }
374
375 List<ShoppingItem> items = shoppingItemPersistence.findByG_C(
376 fromCategory.getGroupId(), fromCategory.getCategoryId());
377
378 for (ShoppingItem item : items) {
379
380
381
382 item.setCategoryId(toCategoryId);
383
384 shoppingItemPersistence.update(item, false);
385 }
386
387 deleteCategory(fromCategory);
388 }
389
390 protected void validate(String name) throws PortalException {
391 if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
392 (name.indexOf("
393
394 throw new CategoryNameException();
395 }
396 }
397
398 }