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