001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.shopping.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.model.impl.ResourceImpl;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portlet.shopping.CategoryNameException;
030: import com.liferay.portlet.shopping.model.ShoppingCategory;
031: import com.liferay.portlet.shopping.model.ShoppingItem;
032: import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
033: import com.liferay.portlet.shopping.service.base.ShoppingCategoryLocalServiceBaseImpl;
034:
035: import java.util.ArrayList;
036: import java.util.Collections;
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: /**
042: * <a href="ShoppingCategoryLocalServiceImpl.java.html"><b><i>View Source</i>
043: * </b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class ShoppingCategoryLocalServiceImpl extends
049: ShoppingCategoryLocalServiceBaseImpl {
050:
051: public ShoppingCategory addCategory(long userId, long plid,
052: long parentCategoryId, String name, String description,
053: boolean addCommunityPermissions, boolean addGuestPermissions)
054: throws PortalException, SystemException {
055:
056: return addCategory(userId, plid, parentCategoryId, name,
057: description, Boolean.valueOf(addCommunityPermissions),
058: Boolean.valueOf(addGuestPermissions), null, null);
059: }
060:
061: public ShoppingCategory addCategory(long userId, long plid,
062: long parentCategoryId, String name, String description,
063: String[] communityPermissions, String[] guestPermissions)
064: throws PortalException, SystemException {
065:
066: return addCategory(userId, plid, parentCategoryId, name,
067: description, null, null, communityPermissions,
068: guestPermissions);
069: }
070:
071: public ShoppingCategory addCategory(long userId, long plid,
072: long parentCategoryId, String name, String description,
073: Boolean addCommunityPermissions,
074: Boolean addGuestPermissions, String[] communityPermissions,
075: String[] guestPermissions) throws PortalException,
076: SystemException {
077:
078: // Category
079:
080: User user = userPersistence.findByPrimaryKey(userId);
081: long groupId = PortalUtil.getPortletGroupId(plid);
082: parentCategoryId = getParentCategoryId(groupId,
083: parentCategoryId);
084: Date now = new Date();
085:
086: validate(name);
087:
088: long categoryId = counterLocalService.increment();
089:
090: ShoppingCategory category = shoppingCategoryPersistence
091: .create(categoryId);
092:
093: category.setGroupId(groupId);
094: category.setCompanyId(user.getCompanyId());
095: category.setUserId(user.getUserId());
096: category.setUserName(user.getFullName());
097: category.setCreateDate(now);
098: category.setModifiedDate(now);
099: category.setParentCategoryId(parentCategoryId);
100: category.setName(name);
101: category.setDescription(description);
102:
103: shoppingCategoryPersistence.update(category);
104:
105: // Resources
106:
107: if ((addCommunityPermissions != null)
108: && (addGuestPermissions != null)) {
109:
110: addCategoryResources(category, addCommunityPermissions
111: .booleanValue(), addGuestPermissions.booleanValue());
112: } else {
113: addCategoryResources(category, communityPermissions,
114: guestPermissions);
115: }
116:
117: return category;
118: }
119:
120: public void addCategoryResources(long categoryId,
121: boolean addCommunityPermissions, boolean addGuestPermissions)
122: throws PortalException, SystemException {
123:
124: ShoppingCategory category = shoppingCategoryPersistence
125: .findByPrimaryKey(categoryId);
126:
127: addCategoryResources(category, addCommunityPermissions,
128: addGuestPermissions);
129: }
130:
131: public void addCategoryResources(ShoppingCategory category,
132: boolean addCommunityPermissions, boolean addGuestPermissions)
133: throws PortalException, SystemException {
134:
135: resourceLocalService.addResources(category.getCompanyId(),
136: category.getGroupId(), category.getUserId(),
137: ShoppingCategory.class.getName(), category
138: .getCategoryId(), false,
139: addCommunityPermissions, addGuestPermissions);
140: }
141:
142: public void addCategoryResources(long categoryId,
143: String[] communityPermissions, String[] guestPermissions)
144: throws PortalException, SystemException {
145:
146: ShoppingCategory category = shoppingCategoryPersistence
147: .findByPrimaryKey(categoryId);
148:
149: addCategoryResources(category, communityPermissions,
150: guestPermissions);
151: }
152:
153: public void addCategoryResources(ShoppingCategory category,
154: String[] communityPermissions, String[] guestPermissions)
155: throws PortalException, SystemException {
156:
157: resourceLocalService.addModelResources(category.getCompanyId(),
158: category.getGroupId(), category.getUserId(),
159: ShoppingCategory.class.getName(), category
160: .getCategoryId(), communityPermissions,
161: guestPermissions);
162: }
163:
164: public void deleteCategories(long groupId) throws PortalException,
165: SystemException {
166:
167: Iterator itr = shoppingCategoryPersistence.findByGroupId(
168: groupId).iterator();
169:
170: while (itr.hasNext()) {
171: ShoppingCategory category = (ShoppingCategory) itr.next();
172:
173: deleteCategory(category);
174: }
175: }
176:
177: public void deleteCategory(long categoryId) throws PortalException,
178: SystemException {
179:
180: ShoppingCategory category = shoppingCategoryPersistence
181: .findByPrimaryKey(categoryId);
182:
183: deleteCategory(category);
184: }
185:
186: public void deleteCategory(ShoppingCategory category)
187: throws PortalException, SystemException {
188:
189: // Categories
190:
191: Iterator itr = shoppingCategoryPersistence.findByG_P(
192: category.getGroupId(), category.getCategoryId())
193: .iterator();
194:
195: while (itr.hasNext()) {
196: ShoppingCategory curCategory = (ShoppingCategory) itr
197: .next();
198:
199: deleteCategory(curCategory);
200: }
201:
202: // Items
203:
204: shoppingItemLocalService.deleteItems(category.getCategoryId());
205:
206: // Resources
207:
208: resourceLocalService
209: .deleteResource(category.getCompanyId(),
210: ShoppingCategory.class.getName(),
211: ResourceImpl.SCOPE_INDIVIDUAL, category
212: .getCategoryId());
213:
214: // Category
215:
216: shoppingCategoryPersistence.remove(category.getCategoryId());
217: }
218:
219: public List getCategories(long groupId) throws SystemException {
220: return shoppingCategoryPersistence.findByGroupId(groupId);
221: }
222:
223: public List getCategories(long groupId, long parentCategoryId,
224: int begin, int end) throws SystemException {
225:
226: return shoppingCategoryPersistence.findByG_P(groupId,
227: parentCategoryId, begin, end);
228: }
229:
230: public int getCategoriesCount(long groupId, long parentCategoryId)
231: throws SystemException {
232:
233: return shoppingCategoryPersistence.countByG_P(groupId,
234: parentCategoryId);
235: }
236:
237: public ShoppingCategory getCategory(long categoryId)
238: throws PortalException, SystemException {
239:
240: return shoppingCategoryPersistence.findByPrimaryKey(categoryId);
241: }
242:
243: public ShoppingCategory getParentCategory(ShoppingCategory category)
244: throws PortalException, SystemException {
245:
246: ShoppingCategory parentCategory = shoppingCategoryPersistence
247: .findByPrimaryKey(category.getParentCategoryId());
248:
249: return parentCategory;
250: }
251:
252: public List getParentCategories(long categoryId)
253: throws PortalException, SystemException {
254:
255: return getParentCategories(shoppingCategoryPersistence
256: .findByPrimaryKey(categoryId));
257: }
258:
259: public List getParentCategories(ShoppingCategory category)
260: throws PortalException, SystemException {
261:
262: List parentCategories = new ArrayList();
263:
264: ShoppingCategory tempCategory = category;
265:
266: for (;;) {
267: parentCategories.add(tempCategory);
268:
269: if (tempCategory.getParentCategoryId() == ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
270:
271: break;
272: }
273:
274: tempCategory = shoppingCategoryPersistence
275: .findByPrimaryKey(tempCategory
276: .getParentCategoryId());
277: }
278:
279: Collections.reverse(parentCategories);
280:
281: return parentCategories;
282: }
283:
284: public void getSubcategoryIds(List categoryIds, long groupId,
285: long categoryId) throws SystemException {
286:
287: Iterator itr = shoppingCategoryPersistence.findByG_P(groupId,
288: categoryId).iterator();
289:
290: while (itr.hasNext()) {
291: ShoppingCategory category = (ShoppingCategory) itr.next();
292:
293: categoryIds.add(new Long(category.getCategoryId()));
294:
295: getSubcategoryIds(categoryIds, category.getGroupId(),
296: category.getCategoryId());
297: }
298: }
299:
300: public ShoppingCategory updateCategory(long categoryId,
301: long parentCategoryId, String name, String description,
302: boolean mergeWithParentCategory) throws PortalException,
303: SystemException {
304:
305: // Category
306:
307: ShoppingCategory category = shoppingCategoryPersistence
308: .findByPrimaryKey(categoryId);
309:
310: parentCategoryId = getParentCategoryId(category,
311: parentCategoryId);
312:
313: validate(name);
314:
315: category.setModifiedDate(new Date());
316: category.setParentCategoryId(parentCategoryId);
317: category.setName(name);
318: category.setDescription(description);
319:
320: shoppingCategoryPersistence.update(category);
321:
322: // Merge categories
323:
324: if (mergeWithParentCategory
325: && (categoryId != parentCategoryId)
326: && (parentCategoryId != ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
327:
328: mergeCategories(category, parentCategoryId);
329: }
330:
331: return category;
332: }
333:
334: protected long getParentCategoryId(long groupId,
335: long parentCategoryId) throws SystemException {
336:
337: if (parentCategoryId != ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
338:
339: ShoppingCategory parentCategory = shoppingCategoryPersistence
340: .fetchByPrimaryKey(parentCategoryId);
341:
342: if ((parentCategory == null)
343: || (groupId != parentCategory.getGroupId())) {
344:
345: parentCategoryId = ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID;
346: }
347: }
348:
349: return parentCategoryId;
350: }
351:
352: protected long getParentCategoryId(ShoppingCategory category,
353: long parentCategoryId) throws SystemException {
354:
355: if (parentCategoryId == ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
356:
357: return parentCategoryId;
358: }
359:
360: if (category.getCategoryId() == parentCategoryId) {
361: return category.getParentCategoryId();
362: } else {
363: ShoppingCategory parentCategory = shoppingCategoryPersistence
364: .fetchByPrimaryKey(parentCategoryId);
365:
366: if ((parentCategory == null)
367: || (category.getGroupId() != parentCategory
368: .getGroupId())) {
369:
370: return category.getParentCategoryId();
371: }
372:
373: List subcategoryIds = new ArrayList();
374:
375: getSubcategoryIds(subcategoryIds, category.getGroupId(),
376: category.getCategoryId());
377:
378: if (subcategoryIds.contains(new Long(parentCategoryId))) {
379: return category.getParentCategoryId();
380: }
381:
382: return parentCategoryId;
383: }
384: }
385:
386: protected void mergeCategories(ShoppingCategory fromCategory,
387: long toCategoryId) throws PortalException, SystemException {
388:
389: Iterator itr = shoppingCategoryPersistence
390: .findByG_P(fromCategory.getGroupId(),
391: fromCategory.getCategoryId()).iterator();
392:
393: while (itr.hasNext()) {
394: ShoppingCategory category = (ShoppingCategory) itr.next();
395:
396: mergeCategories(category, toCategoryId);
397: }
398:
399: itr = shoppingItemPersistence.findByCategoryId(
400: fromCategory.getCategoryId()).iterator();
401:
402: while (itr.hasNext()) {
403:
404: // Item
405:
406: ShoppingItem item = (ShoppingItem) itr.next();
407:
408: item.setCategoryId(toCategoryId);
409:
410: shoppingItemPersistence.update(item);
411: }
412:
413: shoppingCategoryPersistence
414: .remove(fromCategory.getCategoryId());
415: }
416:
417: protected void validate(String name) throws PortalException {
418: if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1)
419: || (name.indexOf("//") != -1)) {
420:
421: throw new CategoryNameException();
422: }
423: }
424:
425: }
|