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