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.billetterie.dao.quartier;
35  
36  import fr.paris.lutece.plugins.stock.commons.ResultList;
37  import fr.paris.lutece.plugins.stock.commons.dao.AbstractStockDAO;
38  import fr.paris.lutece.plugins.stock.commons.dao.PaginationProperties;
39  import fr.paris.lutece.plugins.stock.modules.billetterie.business.district.District;
40  import fr.paris.lutece.plugins.stock.modules.billetterie.business.district.DistrictFilter;
41  import fr.paris.lutece.plugins.stock.service.StockPlugin;
42  
43  import org.springframework.stereotype.Repository;
44  
45  import java.util.LinkedList;
46  import java.util.List;
47  
48  import javax.persistence.EntityManager;
49  import javax.persistence.criteria.CriteriaBuilder;
50  import javax.persistence.criteria.CriteriaQuery;
51  import javax.persistence.criteria.Order;
52  import javax.persistence.criteria.Root;
53  
54  /**
55   *
56   * @author jchaline
57   *
58   */
59  @Repository
60  public class DistrictDAO extends AbstractStockDAO<Integer, District>
61  {
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public String getPluginName( )
67      {
68          return StockPlugin.PLUGIN_NAME;
69      }
70  
71      /**
72       * Find purchases by filter.
73       *
74       * @param filter
75       *            the filter
76       * @param paginationProperties
77       *            the pagination properties
78       * @return list of purchases
79       */
80      public ResultList<District> findByFilter( DistrictFilter filter, PaginationProperties paginationProperties )
81      {
82          EntityManager em = getEM( );
83          CriteriaBuilder cb = em.getCriteriaBuilder( );
84  
85          CriteriaQuery<District> cq = cb.createQuery( District.class );
86  
87          Root<District> root = cq.from( District.class );
88          buildCriteriaQuery( filter, root, cq, cb );
89          buildSortQuery( filter, root, cq, cb );
90          cq.distinct( true );
91  
92          return createPagedQuery( cq, paginationProperties ).getResultList( );
93      }
94  
95      /**
96       * Add the order by parameter to the query.
97       *
98       * @param filter
99       *            the filter
100      * @param root
101      *            the entity root
102      * @param query
103      *            the criteria query
104      * @param builder
105      *            the criteria builder
106      */
107     protected void buildSortQuery( DistrictFilter filter, Root<District> root, CriteriaQuery<District> query, CriteriaBuilder builder )
108     {
109         if ( ( filter.getOrders( ) != null ) && !filter.getOrders( ).isEmpty( ) )
110         {
111             List<Order> orderList = new LinkedList<Order>( );
112 
113             // get asc order
114             for ( String order : filter.getOrders( ) )
115             {
116                 if ( filter.isOrderAsc( ) )
117                 {
118                     orderList.add( builder.asc( root.get( order ) ) );
119                 }
120                 else
121                 {
122                     orderList.add( builder.desc( root.get( order ) ) );
123                 }
124             }
125 
126             query.orderBy( orderList );
127         }
128     }
129 
130     /**
131      * Build the criteria query used when entity are searched by filter.
132      *
133      * @param filter
134      *            the filter
135      * @param root
136      *            the entity root
137      * @param query
138      *            the criteria query
139      * @param builder
140      *            the criteria builder
141      */
142     protected void buildCriteriaQuery( Object filter, Root<District> root, CriteriaQuery<District> query, CriteriaBuilder builder )
143     {
144     }
145 }