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