View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.stock.modules.tickets.business;
35  
36  import fr.paris.lutece.plugins.stock.commons.dao.AbstractStockDAO;
37  import fr.paris.lutece.plugins.stock.modules.tickets.service.TicketsPlugin;
38  
39  import java.math.BigInteger;
40  
41  import java.sql.Timestamp;
42  
43  import java.util.ArrayList;
44  import java.util.Calendar;
45  import java.util.GregorianCalendar;
46  import java.util.List;
47  
48  import javax.persistence.EntityManager;
49  import javax.persistence.Query;
50  import javax.persistence.TypedQuery;
51  import javax.persistence.criteria.CriteriaBuilder;
52  import javax.persistence.criteria.CriteriaQuery;
53  import javax.persistence.criteria.Predicate;
54  import javax.persistence.criteria.Root;
55  
56  /**
57   * This class provides Data Access methods for Person objects
58   */
59  public final class PurchaseStatisticDAO extends AbstractStockDAO<Integer, PurchaseStatistic> implements IPurchaseStatisticDAO
60  {
61      /**
62       *
63       * {@inheritDoc}
64       */
65      @Override
66      public String getPluginName( )
67      {
68          return TicketsPlugin.PLUGIN_NAME;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public List<PurchaseStatistic> getAllByIdPurchase( Integer idPurchase )
75      {
76          EntityManager em = getEM( );
77          CriteriaBuilder cb = em.getCriteriaBuilder( );
78  
79          CriteriaQuery<PurchaseStatistic> cq = cb.createQuery( PurchaseStatistic.class );
80  
81          Root<PurchaseStatistic> root = cq.from( PurchaseStatistic.class );
82  
83          // predicates list
84          List<Predicate> listPredicates = new ArrayList<Predicate>( );
85  
86          if ( idPurchase != null )
87          {
88              listPredicates.add( cb.equal( root.get( PurchaseStatistic_.purchase ), idPurchase ) );
89          }
90  
91          if ( !listPredicates.isEmpty( ) )
92          {
93              // add existing predicates to Where clause
94              cq.where( listPredicates.toArray( new Predicate [ 0] ) );
95          }
96  
97          // buildSortQuery( filter, root, cq, cb );
98          cq.distinct( true );
99  
100         TypedQuery<PurchaseStatistic> query = em.createQuery( cq );
101 
102         return query.getResultList( );
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     public List<ResultStatistic> getAllResultStatisticByParameters( String strTimesUnit, String strDateDebut, String strDateFin )
109     {
110         StringBuffer requeteSQL = new StringBuffer( );
111 
112         requeteSQL.append( "SELECT count(distinct purchase_statistic.purchase_id_purchase) AS compteur, purchase_statistic." );
113 
114         if ( strTimesUnit.equals( "0" ) )
115         {
116             requeteSQL.append( "dayOfYear" );
117         }
118         else
119             if ( strTimesUnit.equals( "1" ) )
120             {
121                 requeteSQL.append( "week" );
122             }
123             else
124             {
125                 requeteSQL.append( "month" );
126             }
127 
128         requeteSQL.append( ",purchase_statistic.year FROM stock_ticket_purchase_statistic AS purchase_statistic" );
129 
130         Boolean isFirstCondition = Boolean.TRUE;
131 
132         if ( ( strDateDebut != null ) && !strDateDebut.equals( "" ) )
133         {
134             requeteSQL.append( " WHERE purchase_statistic.date >= CAST('" + strDateDebut + " 00:00:00' AS DATETIME)" );
135             isFirstCondition = Boolean.FALSE;
136         }
137 
138         if ( ( strDateFin != null ) && !strDateFin.equals( "" ) )
139         {
140             if ( isFirstCondition )
141             {
142                 requeteSQL.append( " WHERE" );
143             }
144             else
145             {
146                 requeteSQL.append( " AND" );
147             }
148 
149             requeteSQL.append( " purchase_statistic.date <= CAST('" + strDateFin + " 23:59:59' AS DATETIME)" );
150         }
151 
152         requeteSQL.append( " GROUP BY purchase_statistic." );
153 
154         if ( strTimesUnit.equals( "0" ) )
155         {
156             requeteSQL.append( "dayOfYear" );
157         }
158         else
159             if ( strTimesUnit.equals( "1" ) )
160             {
161                 requeteSQL.append( "week" );
162             }
163             else
164             {
165                 requeteSQL.append( "month" );
166             }
167 
168         requeteSQL.append( ", purchase_statistic.year" );
169 
170         Query query = getEM( ).createNativeQuery( requeteSQL.toString( ) );
171 
172         List<Object> listeResultat = query.getResultList( );
173 
174         List<ResultStatistic> listeResultStatistic = new ArrayList<ResultStatistic>( );
175 
176         if ( listeResultat.size( ) > 0 )
177         {
178             for ( Object ligneResultat : listeResultat )
179             {
180                 Object [ ] listeAttributs = (Object [ ]) ligneResultat;
181 
182                 if ( ( listeAttributs [0] != null ) && ( listeAttributs [1] != null ) && ( listeAttributs [2] != null ) )
183                 {
184                     ResultStatistic resultStatistic = new ResultStatistic( );
185                     resultStatistic.setNumberResponse( Integer.decode( listeAttributs [0].toString( ) ) );
186 
187                     Calendar calendar = new GregorianCalendar( );
188 
189                     int nTimesUnit;
190 
191                     if ( strTimesUnit.equals( "0" ) )
192                     {
193                         nTimesUnit = Calendar.DAY_OF_YEAR;
194                     }
195                     else
196                         if ( strTimesUnit.equals( "1" ) )
197                         {
198                             nTimesUnit = Calendar.WEEK_OF_YEAR;
199                         }
200                         else
201                         {
202                             nTimesUnit = Calendar.MONTH;
203                         }
204 
205                     calendar.set( nTimesUnit, Integer.decode( listeAttributs [1].toString( ) ) );
206                     calendar.set( Calendar.YEAR, Integer.decode( listeAttributs [2].toString( ) ) );
207                     resultStatistic.setStatisticDate( new Timestamp( calendar.getTimeInMillis( ) ) );
208 
209                     listeResultStatistic.add( resultStatistic );
210                 }
211             }
212         }
213 
214         return listeResultStatistic;
215     }
216 
217     /**
218      * {@inheritDoc}
219      */
220     public Integer getCountPurchasesByDates( String strDateDebut, String strDateFin )
221     {
222         Integer result = 0;
223         StringBuffer requeteSQL = new StringBuffer( );
224 
225         requeteSQL.append( "SELECT count( distinct purchase_statistic.purchase_id_purchase)  " );
226         requeteSQL.append( " FROM stock_ticket_purchase_statistic AS purchase_statistic" );
227 
228         Boolean isFirstCondition = Boolean.TRUE;
229 
230         if ( ( strDateDebut != null ) && !strDateDebut.equals( "" ) )
231         {
232             requeteSQL.append( " WHERE purchase_statistic.date >= CAST('" + strDateDebut + " 00:00:00' AS DATETIME)" );
233             isFirstCondition = Boolean.FALSE;
234         }
235 
236         if ( ( strDateFin != null ) && !strDateFin.equals( "" ) )
237         {
238             if ( isFirstCondition )
239             {
240                 requeteSQL.append( " WHERE" );
241             }
242             else
243             {
244                 requeteSQL.append( " AND" );
245             }
246 
247             requeteSQL.append( " purchase_statistic.date <= CAST('" + strDateFin + " 23:59:59' AS DATETIME)" );
248         }
249 
250         Query query = getEM( ).createNativeQuery( requeteSQL.toString( ) );
251         List<Object> listeCount = query.getResultList( );
252 
253         if ( listeCount.size( ) == 1 )
254         {
255             Object obj = listeCount.get( 0 );
256 
257             if ( obj != null )
258             {
259                 BigInteger bigInt = (BigInteger) obj;
260                 result = bigInt.intValue( );
261             }
262         }
263 
264         return result;
265     }
266 }