1
22
23 package com.liferay.portlet.shopping.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.model.User;
30 import com.liferay.portal.util.PropsKeys;
31 import com.liferay.portal.util.PropsUtil;
32 import com.liferay.portlet.shopping.CartMinQuantityException;
33 import com.liferay.portlet.shopping.CouponActiveException;
34 import com.liferay.portlet.shopping.CouponEndDateException;
35 import com.liferay.portlet.shopping.CouponStartDateException;
36 import com.liferay.portlet.shopping.NoSuchCouponException;
37 import com.liferay.portlet.shopping.model.ShoppingCart;
38 import com.liferay.portlet.shopping.model.ShoppingCartItem;
39 import com.liferay.portlet.shopping.model.ShoppingCategory;
40 import com.liferay.portlet.shopping.model.ShoppingCoupon;
41 import com.liferay.portlet.shopping.model.ShoppingItem;
42 import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
43 import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
44 import com.liferay.portlet.shopping.util.ShoppingUtil;
45
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.TreeMap;
52
53
59 public class ShoppingCartLocalServiceImpl
60 extends ShoppingCartLocalServiceBaseImpl {
61
62 public void deleteGroupCarts(long groupId) throws SystemException {
63 shoppingCartPersistence.removeByGroupId(groupId);
64 }
65
66 public void deleteUserCarts(long userId) throws SystemException {
67 shoppingCartPersistence.removeByUserId(userId);
68 }
69
70 public ShoppingCart getCart(long userId, long groupId)
71 throws PortalException, SystemException {
72
73 return shoppingCartPersistence.findByG_U(groupId, userId);
74 }
75
76 public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
77 throws SystemException {
78
79 Map<ShoppingCartItem, Integer> items =
80 new TreeMap<ShoppingCartItem, Integer>();
81
82 String[] itemIdsArray = StringUtil.split(itemIds);
83
84 for (int i = 0; i < itemIdsArray.length; i++) {
85 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
86 String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
87
88 ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
89 itemId);
90
91 if (item != null) {
92 ShoppingCategory category = item.getCategory();
93
94 if (category.getGroupId() == groupId) {
95 ShoppingCartItem cartItem = new ShoppingCartItemImpl(
96 item, fields);
97
98 Integer count = items.get(cartItem);
99
100 if (count == null) {
101 count = new Integer(1);
102 }
103 else {
104 count = new Integer(count.intValue() + 1);
105 }
106
107 items.put(cartItem, count);
108 }
109 }
110 }
111
112 return items;
113 }
114
115 public ShoppingCart updateCart(
116 long userId, long groupId, String itemIds, String couponCodes,
117 int altShipping, boolean insure)
118 throws PortalException, SystemException {
119
120 List<Long> badItemIds = new ArrayList<Long>();
121
122 Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
123
124 boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
125 PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
126
127 Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
128 items.entrySet().iterator();
129
130 while (itr.hasNext()) {
131 Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
132
133 ShoppingCartItem cartItem = entry.getKey();
134 Integer count = entry.getValue();
135
136 ShoppingItem item = cartItem.getItem();
137
138 int minQuantity = ShoppingUtil.getMinQuantity(item);
139
140 if (minQuantity <= 0) {
141 continue;
142 }
143
144 if (minQtyMultiple) {
145 if ((count.intValue() % minQuantity) > 0) {
146 badItemIds.add(item.getItemId());
147 }
148 }
149 else {
150 if (count.intValue() < minQuantity) {
151 badItemIds.add(item.getItemId());
152 }
153 }
154 }
155
156 if (badItemIds.size() > 0) {
157 throw new CartMinQuantityException(StringUtil.merge(
158 badItemIds.toArray(new Long[badItemIds.size()])));
159 }
160
161 String[] couponCodesArray = StringUtil.split(couponCodes);
162
163 for (int i = 0; i < couponCodesArray.length; i++) {
164 try {
165 ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
166 couponCodesArray[i]);
167
168 if (coupon.getGroupId() != groupId) {
169 throw new NoSuchCouponException(couponCodesArray[i]);
170 }
171 else if (!coupon.isActive()) {
172 throw new CouponActiveException(couponCodesArray[i]);
173 }
174 else if (!coupon.hasValidStartDate()) {
175 throw new CouponStartDateException(couponCodesArray[i]);
176 }
177 else if (!coupon.hasValidEndDate()) {
178 throw new CouponEndDateException(couponCodesArray[i]);
179 }
180 }
181 catch (NoSuchCouponException nsce) {
182 throw new NoSuchCouponException(couponCodesArray[i]);
183 }
184
185
187 break;
188 }
189
190 User user = userPersistence.findByPrimaryKey(userId);
191 Date now = new Date();
192
193 ShoppingCart cart = null;
194
195 if (user.isDefaultUser()) {
196 cart = shoppingCartPersistence.create(0);
197
198 cart.setGroupId(groupId);
199 cart.setCompanyId(user.getCompanyId());
200 cart.setUserId(userId);
201 cart.setUserName(user.getFullName());
202 cart.setCreateDate(now);
203 }
204 else {
205 cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
206
207 if (cart == null) {
208 long cartId = counterLocalService.increment();
209
210 cart = shoppingCartPersistence.create(cartId);
211
212 cart.setGroupId(groupId);
213 cart.setCompanyId(user.getCompanyId());
214 cart.setUserId(userId);
215 cart.setUserName(user.getFullName());
216 cart.setCreateDate(now);
217 }
218 }
219
220 cart.setModifiedDate(now);
221 cart.setItemIds(checkItemIds(groupId, itemIds));
222 cart.setCouponCodes(couponCodes);
223 cart.setAltShipping(altShipping);
224 cart.setInsure(insure);
225
226 if (!user.isDefaultUser()) {
227 shoppingCartPersistence.update(cart, false);
228 }
229
230 return cart;
231 }
232
233 protected String checkItemIds(long groupId, String itemIds) {
234 String[] itemIdsArray = StringUtil.split(itemIds);
235
236 for (int i = 0; i < itemIdsArray.length; i++) {
237 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
238
239 ShoppingItem item = null;
240
241 try {
242 item = shoppingItemPersistence.findByPrimaryKey(itemId);
243
244 ShoppingCategory category = item.getCategory();
245
246 if (category.getGroupId() != groupId) {
247 item = null;
248 }
249 }
250 catch (Exception e) {
251 }
252
253 if (item == null) {
254 itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
255 }
256 }
257
258 return itemIds;
259 }
260
261 }