001
014
015 package com.liferay.portlet.shopping.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.PropsKeys;
021 import com.liferay.portal.kernel.util.StringUtil;
022 import com.liferay.portal.model.User;
023 import com.liferay.portal.util.PropsUtil;
024 import com.liferay.portlet.shopping.CartMinQuantityException;
025 import com.liferay.portlet.shopping.CouponActiveException;
026 import com.liferay.portlet.shopping.CouponEndDateException;
027 import com.liferay.portlet.shopping.CouponStartDateException;
028 import com.liferay.portlet.shopping.NoSuchCouponException;
029 import com.liferay.portlet.shopping.model.ShoppingCart;
030 import com.liferay.portlet.shopping.model.ShoppingCartItem;
031 import com.liferay.portlet.shopping.model.ShoppingCategory;
032 import com.liferay.portlet.shopping.model.ShoppingCoupon;
033 import com.liferay.portlet.shopping.model.ShoppingItem;
034 import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
035 import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
036 import com.liferay.portlet.shopping.util.ShoppingUtil;
037
038 import java.util.ArrayList;
039 import java.util.Date;
040 import java.util.Iterator;
041 import java.util.List;
042 import java.util.Map;
043 import java.util.TreeMap;
044
045
048 public class ShoppingCartLocalServiceImpl
049 extends ShoppingCartLocalServiceBaseImpl {
050
051 public void deleteGroupCarts(long groupId) throws SystemException {
052 shoppingCartPersistence.removeByGroupId(groupId);
053 }
054
055 public void deleteUserCarts(long userId) throws SystemException {
056 shoppingCartPersistence.removeByUserId(userId);
057 }
058
059 public ShoppingCart getCart(long userId, long groupId)
060 throws PortalException, SystemException {
061
062 return shoppingCartPersistence.findByG_U(groupId, userId);
063 }
064
065 public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
066 throws SystemException {
067
068 Map<ShoppingCartItem, Integer> items =
069 new TreeMap<ShoppingCartItem, Integer>();
070
071 String[] itemIdsArray = StringUtil.split(itemIds);
072
073 for (int i = 0; i < itemIdsArray.length; i++) {
074 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
075 String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
076
077 ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
078 itemId);
079
080 if (item != null) {
081 ShoppingCategory category = item.getCategory();
082
083 if (category.getGroupId() == groupId) {
084 ShoppingCartItem cartItem = new ShoppingCartItemImpl(
085 item, fields);
086
087 Integer count = items.get(cartItem);
088
089 if (count == null) {
090 count = new Integer(1);
091 }
092 else {
093 count = new Integer(count.intValue() + 1);
094 }
095
096 items.put(cartItem, count);
097 }
098 }
099 }
100
101 return items;
102 }
103
104 public ShoppingCart updateCart(
105 long userId, long groupId, String itemIds, String couponCodes,
106 int altShipping, boolean insure)
107 throws PortalException, SystemException {
108
109 List<Long> badItemIds = new ArrayList<Long>();
110
111 Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
112
113 boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
114 PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
115
116 Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
117 items.entrySet().iterator();
118
119 while (itr.hasNext()) {
120 Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
121
122 ShoppingCartItem cartItem = entry.getKey();
123 Integer count = entry.getValue();
124
125 ShoppingItem item = cartItem.getItem();
126
127 int minQuantity = ShoppingUtil.getMinQuantity(item);
128
129 if (minQuantity <= 0) {
130 continue;
131 }
132
133 if (minQtyMultiple) {
134 if ((count.intValue() % minQuantity) > 0) {
135 badItemIds.add(item.getItemId());
136 }
137 }
138 else {
139 if (count.intValue() < minQuantity) {
140 badItemIds.add(item.getItemId());
141 }
142 }
143 }
144
145 if (badItemIds.size() > 0) {
146 throw new CartMinQuantityException(StringUtil.merge(
147 badItemIds.toArray(new Long[badItemIds.size()])));
148 }
149
150 String[] couponCodesArray = StringUtil.split(couponCodes);
151
152 for (int i = 0; i < couponCodesArray.length; i++) {
153 try {
154 ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
155 couponCodesArray[i]);
156
157 if (coupon.getGroupId() != groupId) {
158 throw new NoSuchCouponException(couponCodesArray[i]);
159 }
160 else if (!coupon.isActive()) {
161 throw new CouponActiveException(couponCodesArray[i]);
162 }
163 else if (!coupon.hasValidStartDate()) {
164 throw new CouponStartDateException(couponCodesArray[i]);
165 }
166 else if (!coupon.hasValidEndDate()) {
167 throw new CouponEndDateException(couponCodesArray[i]);
168 }
169 }
170 catch (NoSuchCouponException nsce) {
171 throw new NoSuchCouponException(couponCodesArray[i]);
172 }
173
174
175
176 break;
177 }
178
179 User user = userPersistence.findByPrimaryKey(userId);
180 Date now = new Date();
181
182 ShoppingCart cart = null;
183
184 if (user.isDefaultUser()) {
185 cart = shoppingCartPersistence.create(0);
186
187 cart.setGroupId(groupId);
188 cart.setCompanyId(user.getCompanyId());
189 cart.setUserId(userId);
190 cart.setUserName(user.getFullName());
191 cart.setCreateDate(now);
192 }
193 else {
194 cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
195
196 if (cart == null) {
197 long cartId = counterLocalService.increment();
198
199 cart = shoppingCartPersistence.create(cartId);
200
201 cart.setGroupId(groupId);
202 cart.setCompanyId(user.getCompanyId());
203 cart.setUserId(userId);
204 cart.setUserName(user.getFullName());
205 cart.setCreateDate(now);
206 }
207 }
208
209 cart.setModifiedDate(now);
210 cart.setItemIds(checkItemIds(groupId, itemIds));
211 cart.setCouponCodes(couponCodes);
212 cart.setAltShipping(altShipping);
213 cart.setInsure(insure);
214
215 if (!user.isDefaultUser()) {
216 shoppingCartPersistence.update(cart, false);
217 }
218
219 return cart;
220 }
221
222 protected String checkItemIds(long groupId, String itemIds) {
223 String[] itemIdsArray = StringUtil.split(itemIds);
224
225 for (int i = 0; i < itemIdsArray.length; i++) {
226 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
227
228 ShoppingItem item = null;
229
230 try {
231 item = shoppingItemPersistence.findByPrimaryKey(itemId);
232
233 ShoppingCategory category = item.getCategory();
234
235 if (category.getGroupId() != groupId) {
236 item = null;
237 }
238 }
239 catch (Exception e) {
240 }
241
242 if (item == null) {
243 itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
244 }
245 }
246
247 return itemIds;
248 }
249
250 }