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.util.GetterUtil;
019 import com.liferay.portal.kernel.util.PropsKeys;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.model.User;
022 import com.liferay.portal.util.PropsUtil;
023 import com.liferay.portlet.shopping.CartMinQuantityException;
024 import com.liferay.portlet.shopping.CouponActiveException;
025 import com.liferay.portlet.shopping.CouponEndDateException;
026 import com.liferay.portlet.shopping.CouponStartDateException;
027 import com.liferay.portlet.shopping.NoSuchCouponException;
028 import com.liferay.portlet.shopping.model.ShoppingCart;
029 import com.liferay.portlet.shopping.model.ShoppingCartItem;
030 import com.liferay.portlet.shopping.model.ShoppingCategory;
031 import com.liferay.portlet.shopping.model.ShoppingCoupon;
032 import com.liferay.portlet.shopping.model.ShoppingItem;
033 import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
034 import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
035 import com.liferay.portlet.shopping.util.ShoppingUtil;
036
037 import java.util.ArrayList;
038 import java.util.Date;
039 import java.util.List;
040 import java.util.Map;
041 import java.util.TreeMap;
042
043
046 public class ShoppingCartLocalServiceImpl
047 extends ShoppingCartLocalServiceBaseImpl {
048
049 @Override
050 public void deleteGroupCarts(long groupId) {
051 List<ShoppingCart> carts = shoppingCartPersistence.findByGroupId(
052 groupId);
053
054 for (ShoppingCart cart : carts) {
055 deleteShoppingCart(cart);
056 }
057 }
058
059 @Override
060 public void deleteUserCarts(long userId) {
061 List<ShoppingCart> shoppingCarts = shoppingCartPersistence.findByUserId(
062 userId);
063
064 for (ShoppingCart shoppingCart : shoppingCarts) {
065 deleteShoppingCart(shoppingCart);
066 }
067 }
068
069 @Override
070 public ShoppingCart getCart(long userId, long groupId)
071 throws PortalException {
072
073 return shoppingCartPersistence.findByG_U(groupId, userId);
074 }
075
076 @Override
077 public Map<ShoppingCartItem, Integer> getItems(
078 long groupId, String itemIds) {
079
080 Map<ShoppingCartItem, Integer> items =
081 new TreeMap<ShoppingCartItem, Integer>();
082
083 String[] itemIdsArray = StringUtil.split(itemIds);
084
085 for (int i = 0; i < itemIdsArray.length; i++) {
086 long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
087 String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
088
089 ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
090 itemId);
091
092 if (item != null) {
093 ShoppingCategory category = item.getCategory();
094
095 if (category.getGroupId() == groupId) {
096 ShoppingCartItem cartItem = new ShoppingCartItemImpl(
097 item, fields);
098
099 Integer count = items.get(cartItem);
100
101 if (count == null) {
102 count = new Integer(1);
103 }
104 else {
105 count = new Integer(count.intValue() + 1);
106 }
107
108 items.put(cartItem, count);
109 }
110 }
111 }
112
113 return items;
114 }
115
116 @Override
117 public ShoppingCart updateCart(
118 long userId, long groupId, String itemIds, String couponCodes,
119 int altShipping, boolean insure)
120 throws PortalException {
121
122 List<Long> badItemIds = new ArrayList<Long>();
123
124 Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
125
126 boolean minQtyMultiple = GetterUtil.getBoolean(
127 PropsUtil.get(PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
128
129 for (Map.Entry<ShoppingCartItem, Integer> entry : items.entrySet()) {
130 ShoppingCartItem cartItem = entry.getKey();
131 Integer count = entry.getValue();
132
133 ShoppingItem item = cartItem.getItem();
134
135 int minQuantity = ShoppingUtil.getMinQuantity(item);
136
137 if (minQuantity <= 0) {
138 continue;
139 }
140
141 if (minQtyMultiple) {
142 if ((count.intValue() % minQuantity) > 0) {
143 badItemIds.add(item.getItemId());
144 }
145 }
146 else {
147 if (count.intValue() < minQuantity) {
148 badItemIds.add(item.getItemId());
149 }
150 }
151 }
152
153 if (!badItemIds.isEmpty()) {
154 throw new CartMinQuantityException(
155 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
184
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);
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.removeFromList(itemIds, itemIdsArray[i]);
253 }
254 }
255
256 return itemIds;
257 }
258
259 }