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 List<ShoppingCart> carts = shoppingCartPersistence.findByGroupId(
053 groupId);
054
055 for (ShoppingCart cart : carts) {
056 deleteShoppingCart(cart);
057 }
058 }
059
060 @Override
061 public void deleteShoppingCart(long cartId)
062 throws PortalException, SystemException {
063
064 ShoppingCart cart = shoppingCartPersistence.findByPrimaryKey(cartId);
065
066 deleteShoppingCart(cart);
067 }
068
069 @Override
070 public void deleteShoppingCart(ShoppingCart cart) throws SystemException {
071 shoppingCartPersistence.remove(cart);
072 }
073
074 public void deleteUserCarts(long userId) throws SystemException {
075 List<ShoppingCart> shoppingCarts = shoppingCartPersistence.findByUserId(
076 userId);
077
078 for (ShoppingCart shoppingCart : shoppingCarts) {
079 deleteShoppingCart(shoppingCart);
080 }
081 }
082
083 public ShoppingCart getCart(long userId, long groupId)
084 throws PortalException, SystemException {
085
086 return shoppingCartPersistence.findByG_U(groupId, userId);
087 }
088
089 public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
090 throws SystemException {
091
092 Map<ShoppingCartItem, Integer> items =
093 new TreeMap<ShoppingCartItem, Integer>();
094
095 String[] itemIdsArray = StringUtil.split(itemIds);
096
097 for (int i = 0; i < itemIdsArray.length; i++) {
098 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
099 String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
100
101 ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
102 itemId);
103
104 if (item != null) {
105 ShoppingCategory category = item.getCategory();
106
107 if (category.getGroupId() == groupId) {
108 ShoppingCartItem cartItem = new ShoppingCartItemImpl(
109 item, fields);
110
111 Integer count = items.get(cartItem);
112
113 if (count == null) {
114 count = new Integer(1);
115 }
116 else {
117 count = new Integer(count.intValue() + 1);
118 }
119
120 items.put(cartItem, count);
121 }
122 }
123 }
124
125 return items;
126 }
127
128 public ShoppingCart updateCart(
129 long userId, long groupId, String itemIds, String couponCodes,
130 int altShipping, boolean insure)
131 throws PortalException, SystemException {
132
133 List<Long> badItemIds = new ArrayList<Long>();
134
135 Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
136
137 boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
138 PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
139
140 Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
141 items.entrySet().iterator();
142
143 while (itr.hasNext()) {
144 Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
145
146 ShoppingCartItem cartItem = entry.getKey();
147 Integer count = entry.getValue();
148
149 ShoppingItem item = cartItem.getItem();
150
151 int minQuantity = ShoppingUtil.getMinQuantity(item);
152
153 if (minQuantity <= 0) {
154 continue;
155 }
156
157 if (minQtyMultiple) {
158 if ((count.intValue() % minQuantity) > 0) {
159 badItemIds.add(item.getItemId());
160 }
161 }
162 else {
163 if (count.intValue() < minQuantity) {
164 badItemIds.add(item.getItemId());
165 }
166 }
167 }
168
169 if (badItemIds.size() > 0) {
170 throw new CartMinQuantityException(StringUtil.merge(
171 badItemIds.toArray(new Long[badItemIds.size()])));
172 }
173
174 String[] couponCodesArray = StringUtil.split(couponCodes);
175
176 for (int i = 0; i < couponCodesArray.length; i++) {
177 try {
178 ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
179 couponCodesArray[i]);
180
181 if (coupon.getGroupId() != groupId) {
182 throw new NoSuchCouponException(couponCodesArray[i]);
183 }
184 else if (!coupon.isActive()) {
185 throw new CouponActiveException(couponCodesArray[i]);
186 }
187 else if (!coupon.hasValidStartDate()) {
188 throw new CouponStartDateException(couponCodesArray[i]);
189 }
190 else if (!coupon.hasValidEndDate()) {
191 throw new CouponEndDateException(couponCodesArray[i]);
192 }
193 }
194 catch (NoSuchCouponException nsce) {
195 throw new NoSuchCouponException(couponCodesArray[i]);
196 }
197
198
199
200 break;
201 }
202
203 User user = userPersistence.findByPrimaryKey(userId);
204 Date now = new Date();
205
206 ShoppingCart cart = null;
207
208 if (user.isDefaultUser()) {
209 cart = shoppingCartPersistence.create(0);
210
211 cart.setGroupId(groupId);
212 cart.setCompanyId(user.getCompanyId());
213 cart.setUserId(userId);
214 cart.setUserName(user.getFullName());
215 cart.setCreateDate(now);
216 }
217 else {
218 cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
219
220 if (cart == null) {
221 long cartId = counterLocalService.increment();
222
223 cart = shoppingCartPersistence.create(cartId);
224
225 cart.setGroupId(groupId);
226 cart.setCompanyId(user.getCompanyId());
227 cart.setUserId(userId);
228 cart.setUserName(user.getFullName());
229 cart.setCreateDate(now);
230 }
231 }
232
233 cart.setModifiedDate(now);
234 cart.setItemIds(checkItemIds(groupId, itemIds));
235 cart.setCouponCodes(couponCodes);
236 cart.setAltShipping(altShipping);
237 cart.setInsure(insure);
238
239 if (!user.isDefaultUser()) {
240 shoppingCartPersistence.update(cart, false);
241 }
242
243 return cart;
244 }
245
246 protected String checkItemIds(long groupId, String itemIds) {
247 String[] itemIdsArray = StringUtil.split(itemIds);
248
249 for (int i = 0; i < itemIdsArray.length; i++) {
250 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
251
252 ShoppingItem item = null;
253
254 try {
255 item = shoppingItemPersistence.findByPrimaryKey(itemId);
256
257 ShoppingCategory category = item.getCategory();
258
259 if (category.getGroupId() != groupId) {
260 item = null;
261 }
262 }
263 catch (Exception e) {
264 }
265
266 if (item == null) {
267 itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
268 }
269 }
270
271 return itemIds;
272 }
273
274 }