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