1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.shoppingcart.service.persistence;
35
36 import fr.paris.lutece.plugins.shoppingcart.business.ShoppingCartItem;
37 import fr.paris.lutece.plugins.shoppingcart.business.ShoppingCartItemFilter;
38 import fr.paris.lutece.plugins.shoppingcart.service.provider.ShoppingCartItemProviderManagementService;
39 import fr.paris.lutece.portal.service.util.AppException;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.portal.web.LocalVariables;
42
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.List;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpSession;
49
50 import org.apache.commons.lang.StringUtils;
51 import org.apache.commons.lang.mutable.MutableInt;
52
53
54
55
56
57 public class SessionPersistenceService implements IShoppingCartPersistenceService
58 {
59
60
61
62
63 public static final String SESSION_ATTRIBUTE_HAS_SESSION_ITEMS = "shoppingcart.hasSessionItems";
64
65 private static final String SESSION_ATTRIBUTE_SHOPPING_CART = "shoppingcart.sessionShoppingCartItems";
66 private static final String SESSION_ATTRIBUTE_ID_SHOPPING_CART_ITEM = "shoppingcart.sessionIdShoppingCart";
67
68 private static final String SERVICE_NAME = "shoppingcart.seccionPersistenceService";
69
70
71
72
73 @Override
74 public void saveItem( ShoppingCartItem item )
75 {
76 if ( item.getDateCreation( ) == null )
77 {
78 item.setDateCreation( new Date( ) );
79 }
80
81 SessionedShoppingCartItem sessionedItem = getSessionedShoppingCartItem( );
82 List<ShoppingCartItem> listItems;
83 if ( sessionedItem == null )
84 {
85 listItems = new ArrayList<ShoppingCartItem>( );
86 sessionedItem = new SessionedShoppingCartItem( listItems, true );
87 saveSessionedShoppingCartItem( sessionedItem );
88 }
89 else
90 {
91 listItems = sessionedItem.getItemList( );
92 }
93 item.setIdItem( getNewShoppingCartItemId( ) );
94 listItems.add( item );
95 HttpSession session = getSession( );
96 if ( session != null )
97 {
98 session.setAttribute( SESSION_ATTRIBUTE_HAS_SESSION_ITEMS, true );
99 }
100 }
101
102
103
104
105
106 @Override
107 public boolean supportAnonymousUsers( )
108 {
109 return true;
110 }
111
112
113
114
115 @Override
116 public List<ShoppingCartItem> findItemsByFilter( ShoppingCartItemFilter filter )
117 {
118 List<ShoppingCartItem> listResult = null;
119 listResult = new ArrayList<ShoppingCartItem>( );
120 List<ShoppingCartItem> listItems;
121 SessionedShoppingCartItem sessionedItem = getSessionedShoppingCartItem( );
122 if ( sessionedItem != null )
123 {
124 listItems = sessionedItem.getItemList( );
125 for ( ShoppingCartItem item : listItems )
126 {
127 if ( doesItemMatchFilter( item, filter ) )
128 {
129 listResult.add( item );
130 }
131 }
132 }
133
134 return listResult;
135 }
136
137
138
139
140 @Override
141 public List<ShoppingCartItem> getItemsOfUser( String strUserName )
142 {
143 return getItemsOfUser( );
144 }
145
146
147
148
149 @Override
150 public String getServiceName( )
151 {
152 return SERVICE_NAME;
153 }
154
155
156
157
158 @Override
159 public void removeItemFromUserShoppingCart( String strUserName, int nIdShoppingCartItem,
160 boolean bNotifyProviderService )
161 {
162 SessionedShoppingCartItem sessionedItem = getSessionedShoppingCartItem( );
163 if ( sessionedItem != null && nIdShoppingCartItem > 0 )
164 {
165 List<ShoppingCartItem> listItems = sessionedItem.getItemList( );
166 ShoppingCartItem itemFound = null;
167 for ( ShoppingCartItem item : listItems )
168 {
169 if ( item.getIdItem( ) == nIdShoppingCartItem )
170 {
171 itemFound = item;
172 break;
173 }
174 }
175 if ( itemFound != null )
176 {
177 if ( bNotifyProviderService )
178 {
179 ShoppingCartItemProviderManagementService.notifyItemRemoval( itemFound );
180 }
181 listItems.remove( itemFound );
182 }
183 }
184 }
185
186
187
188
189 @Override
190 public void emptyShoppingCartOfUser( String strUserName, boolean bNotifyProviderService )
191 {
192 SessionedShoppingCartItem sessionedItem = getSessionedShoppingCartItem( );
193 if ( sessionedItem != null )
194 {
195 if ( bNotifyProviderService )
196 {
197 List<ShoppingCartItem> listItems = sessionedItem.getItemList( );
198 for ( ShoppingCartItem item : listItems )
199 {
200 ShoppingCartItemProviderManagementService.notifyItemRemoval( item );
201 }
202 }
203 sessionedItem.setNotifyProvider( false );
204 saveSessionedShoppingCartItem( null );
205 }
206 }
207
208
209
210
211 @Override
212 public ShoppingCartItem findItemById( int nIdShoppingCartItem )
213 {
214 List<ShoppingCartItem> listItems = getItemsOfUser( );
215 for ( ShoppingCartItem item : listItems )
216 {
217 if ( item.getIdItem( ) == nIdShoppingCartItem )
218 {
219 return item;
220 }
221 }
222 return null;
223 }
224
225
226
227
228
229
230
231 private boolean doesItemMatchFilter( ShoppingCartItem item, ShoppingCartItemFilter filter )
232 {
233 if ( ( filter.getIdProvider( ) != null && !StringUtils.equals( filter.getIdProvider( ), item.getIdProvider( ) ) )
234 || ( filter.getIdLot( ) > 0 && filter.getIdLot( ) != item.getIdLot( ) )
235 || ( filter.getIdResource( ) != null && !StringUtils.equals( filter.getIdResource( ),
236 item.getIdResource( ) ) )
237 || ( filter.getIdUser( ) != null && !StringUtils.equals( filter.getIdUser( ), item.getIdUser( ) ) )
238 || ( filter.getItemPrice( ) > 0d && filter.getItemPrice( ) == item.getItemPrice( ) )
239 || ( filter.getResourceType( ) != null && !StringUtils.equals( filter.getResourceType( ),
240 item.getResourceType( ) ) )
241 || ( filter.getDateCreationMin( ) != null && filter.getDateCreationMin( ).getTime( ) > item
242 .getDateCreation( ).getTime( ) )
243 || ( filter.getDateCreationMax( ) != null && filter.getDateCreationMax( ).getTime( ) < item
244 .getDateCreation( ).getTime( ) ) )
245 {
246 return false;
247 }
248 return true;
249 }
250
251
252
253
254
255
256 private SessionedShoppingCartItem getSessionedShoppingCartItem( )
257 {
258 HttpSession session = getSession( );
259 if ( session != null )
260 {
261 return (SessionedShoppingCartItem) session.getAttribute( SESSION_ATTRIBUTE_SHOPPING_CART );
262 }
263 AppLogService.error(
264 "No request or session attached to this context : could not return the sessioned shopping card item",
265 new AppException( ) );
266 return null;
267 }
268
269
270
271
272
273 private void saveSessionedShoppingCartItem( SessionedShoppingCartItem sessionedItem )
274 {
275 HttpSession session = getSession( );
276 if ( session != null )
277 {
278 session.setAttribute( SESSION_ATTRIBUTE_SHOPPING_CART, sessionedItem );
279 session.setAttribute( SESSION_ATTRIBUTE_HAS_SESSION_ITEMS, sessionedItem != null
280 && sessionedItem.getItemList( ).size( ) > 0 );
281 }
282 }
283
284
285
286
287
288
289 private HttpSession getSession( )
290 {
291 HttpServletRequest request = LocalVariables.getRequest( );
292 if ( request != null )
293 {
294 return request.getSession( true );
295 }
296 AppLogService.error( "No request attached to this context : could not save the sessioned shopping card item",
297 new AppException( ) );
298 return null;
299 }
300
301
302
303
304
305 private List<ShoppingCartItem> getItemsOfUser( )
306 {
307 SessionedShoppingCartItem sessionedItem = getSessionedShoppingCartItem( );
308 if ( sessionedItem == null )
309 {
310 sessionedItem = new SessionedShoppingCartItem( null, true );
311 saveSessionedShoppingCartItem( sessionedItem );
312 }
313 return new ArrayList<ShoppingCartItem>( sessionedItem.getItemList( ) );
314 }
315
316
317
318
319
320 private int getNewShoppingCartItemId( )
321 {
322 HttpSession session = getSession( );
323 if ( session != null )
324 {
325 MutableInt nCurrentId = (MutableInt) session.getAttribute( SESSION_ATTRIBUTE_ID_SHOPPING_CART_ITEM );
326 if ( nCurrentId == null )
327 {
328 nCurrentId = new MutableInt( 1 );
329 session.setAttribute( SESSION_ATTRIBUTE_ID_SHOPPING_CART_ITEM, nCurrentId );
330 }
331 else
332 {
333 nCurrentId.increment( );
334 }
335 return nCurrentId.intValue( );
336 }
337 return 0;
338 }
339 }