View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.shoppingcart.business;
35  
36  import java.io.Serializable;
37  import java.util.Date;
38  
39  import org.apache.commons.lang.StringUtils;
40  import org.apache.commons.lang.time.DateUtils;
41  
42  
43  /**
44   * Business class of the shopping cart items
45   */
46  public class ShoppingCartItem implements Serializable
47  {
48      /**
49       * New id lot of an anonymous user
50       */
51      public static final int NEW_ID_LOT_FOR_ANONYMOUS_USER = -10;
52      /**
53       * Last id of lot of user for an anonymous user
54       */
55      public static final int LAST_ID_LOT_FOR_ANONYMOUS_USER = -1;
56  
57      /**
58       * Serial version UID
59       */
60      private static final long serialVersionUID = 8367766101486301893L;
61  
62      private int _nIdItem;
63      private String _strIdProvider;
64      private int _nIdLot;
65      private String _strIdUser;
66      private String _strResourceType;
67      private String _strIdResource;
68      private double _dItemPrice;
69      private Date _dateCreation;
70  
71      /**
72       * Returns the IdItem
73       * @return The IdItem
74       */
75      public int getIdItem( )
76      {
77          return _nIdItem;
78      }
79  
80      /**
81       * Sets the IdItem
82       * @param nIdItem The IdItem
83       */
84      public void setIdItem( int nIdItem )
85      {
86          _nIdItem = nIdItem;
87      }
88  
89      /**
90       * Returns the id of the service that provides the item
91       * @return The id of the service that provides the item
92       */
93      public String getIdProvider( )
94      {
95          return _strIdProvider;
96      }
97  
98      /**
99       * Sets the id of the service that provides the item
100      * @param strIdProvider The id of the service that provides the item
101      */
102     public void setIdProvider( String strIdProvider )
103     {
104         _strIdProvider = strIdProvider;
105     }
106 
107     /**
108      * Returns the id of the lot
109      * @return The id of the lot
110      */
111     public int getIdLot( )
112     {
113         return _nIdLot;
114     }
115 
116     /**
117      * Sets the id of the lot
118      * @param nIdLot The id of the lot
119      */
120     public void setIdLot( int nIdLot )
121     {
122         _nIdLot = nIdLot;
123     }
124 
125     /**
126      * Returns the id of the user
127      * @return The id of the user
128      */
129     public String getIdUser( )
130     {
131         return _strIdUser;
132     }
133 
134     /**
135      * Sets the id of the user
136      * @param strIdUser The id of the user
137      */
138     public void setIdUser( String strIdUser )
139     {
140         _strIdUser = strIdUser;
141     }
142 
143     /**
144      * Returns the type of the resource added to the shopping cart
145      * @return The type of the resource added to the shopping cart
146      */
147     public String getResourceType( )
148     {
149         return _strResourceType;
150     }
151 
152     /**
153      * Sets the type of the resource added to the shopping cart
154      * @param strResourceType The type of the resource added to the shopping
155      *            cart
156      */
157     public void setResourceType( String strResourceType )
158     {
159         _strResourceType = strResourceType;
160     }
161 
162     /**
163      * Returns the id of the resource added to the shopping cart
164      * @return The id of the resource added to the shopping cart
165      */
166     public String getIdResource( )
167     {
168         return _strIdResource;
169     }
170 
171     /**
172      * Sets the id of the resource added to the shopping cart
173      * @param strIdResource The id of the resource added to the shopping cart
174      */
175     public void setIdResource( String strIdResource )
176     {
177         _strIdResource = strIdResource;
178     }
179 
180     /**
181      * Returns the price of the item
182      * @return The price of the item
183      */
184     public double getItemPrice( )
185     {
186         return _dItemPrice;
187     }
188 
189     /**
190      * Sets the price of the item
191      * @param dItemPrice The price of the item
192      */
193     public void setItemPrice( double dItemPrice )
194     {
195         _dItemPrice = dItemPrice;
196     }
197 
198     /**
199      * Get the creation date of this shopping item
200      * @return The creation date of this shopping item
201      */
202     public Date getDateCreation( )
203     {
204         return _dateCreation == null ? null : (Date) _dateCreation.clone( );
205     }
206 
207     /**
208      * Set the creation date of this shopping item
209      * @param dateCreation The creation date of this shopping item
210      */
211     public void setDateCreation( Date dateCreation )
212     {
213         this._dateCreation = dateCreation == null ? null : (Date) dateCreation.clone( );
214     }
215 
216     /**
217      * {@inheritDoc}
218      */
219     @Override
220     public boolean equals( Object o )
221     {
222         if ( o instanceof ShoppingCartItem )
223         {
224             ShoppingCartItem other = (ShoppingCartItem) o;
225             return getIdItem( ) == other.getIdItem( )
226                     && StringUtils.equals( getIdProvider( ), other.getIdProvider( ) )
227                     && getIdLot( ) == other.getIdLot( )
228                     && StringUtils.equals( getIdUser( ), other.getIdUser( ) )
229                     && StringUtils.equals( getResourceType( ), other.getResourceType( ) )
230                     && StringUtils.equals( getIdResource( ), other.getIdResource( ) )
231                     && getItemPrice( ) == other.getItemPrice( )
232                     && ( ( getDateCreation( ) == null && other.getDateCreation( ) == null ) || ( getDateCreation( ) != null
233                             && other.getDateCreation( ) != null && DateUtils.isSameDay( getDateCreation( ),
234                             other.getDateCreation( ) ) ) );
235         }
236         return false;
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     public int hashCode( )
244     {
245         int nHash = _nIdItem;
246         nHash = 30 * nHash + ( _strIdProvider == null ? 0 : _strIdProvider.hashCode( ) );
247         nHash = 30 * nHash + _nIdLot;
248         nHash = 30 * nHash + ( _strIdUser == null ? 0 : _strIdUser.hashCode( ) );
249         nHash = 30 * nHash + ( _strResourceType == null ? 0 : _strResourceType.hashCode( ) );
250         nHash = 30 * nHash + ( _strIdResource == null ? 0 : _strIdResource.hashCode( ) );
251         nHash = 30 * nHash + ( _dItemPrice > 0d ? Double.valueOf( _dItemPrice ).hashCode( ) : 0 );
252         nHash = 30 * nHash + ( _dateCreation == null ? 0 : _dateCreation.hashCode( ) );
253         return nHash;
254     }
255 }