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.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
58
59 public final class PurchaseStatisticDAO extends AbstractStockDAO<Integer, PurchaseStatistic> implements IPurchaseStatisticDAO
60 {
61
62
63
64
65 @Override
66 public String getPluginName( )
67 {
68 return TicketsPlugin.PLUGIN_NAME;
69 }
70
71
72
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
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
94 cq.where( listPredicates.toArray( new Predicate [ 0] ) );
95 }
96
97
98 cq.distinct( true );
99
100 TypedQuery<PurchaseStatistic> query = em.createQuery( cq );
101
102 return query.getResultList( );
103 }
104
105
106
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
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 }