View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.commons.dao;
35  
36  import fr.paris.lutece.plugins.stock.commons.ResultList;
37  import fr.paris.lutece.portal.service.jpa.JPALuteceDAO;
38  
39  import javax.persistence.EntityManager;
40  import javax.persistence.Query;
41  import javax.persistence.criteria.CriteriaBuilder;
42  import javax.persistence.criteria.CriteriaQuery;
43  import javax.persistence.criteria.Expression;
44  import javax.persistence.criteria.Predicate;
45  import javax.persistence.criteria.Root;
46  
47  /**
48   * Abstract class for stock DAO.
49   * 
50   * @param <K>
51   *            the key type
52   * @param <E>
53   *            the entity type
54   * @author abataille
55   */
56  public abstract class AbstractStockDAO<K, E> extends JPALuteceDAO<K, E>
57  {
58  
59      /**
60       * Generate count query from criteria query and return a paged query.
61       * 
62       * @param <T>
63       *            the generic type of criteria query
64       * @param criteriaQuery
65       *            criteria query
66       * @param paginationProperties
67       *            pagination data
68       * @return query paged
69       */
70      protected <T> PagedQuery createPagedQuery( CriteriaQuery<T> criteriaQuery, PaginationProperties paginationProperties )
71      {
72  
73          // Generate count query
74          EntityManager em = getEM( );
75          CriteriaBuilder cb = em.getCriteriaBuilder( );
76          CriteriaQuery<Long> countQuery = cb.createQuery( Long.class );
77          countQuery.select( cb.count( countQuery.from( criteriaQuery.getResultType( ) ) ) );
78          // Rebuild the roots if
79          // if ( cq.getRoots( ).size( ) > 1 )
80          // {
81          countQuery.getRoots( ).clear( );
82          for ( Root<?> root : criteriaQuery.getRoots( ) )
83          {
84              countQuery.getRoots( ).add( root );
85          }
86          // }
87          if ( criteriaQuery.getRestriction( ) != null )
88          {
89              countQuery.where( criteriaQuery.getRestriction( ) ).distinct( true );
90          }
91  
92          // Create the paged query
93          PagedQuerygins/stock/commons/dao/PagedQuery.html#PagedQuery">PagedQuery pq = new PagedQuery( em.createQuery( criteriaQuery ), em.createQuery( countQuery ), paginationProperties );
94  
95          return pq;
96      }
97  
98      /**
99       * Return all entities paged.
100      * 
101      * @param paginationProperties
102      *            properties for pagination
103      * @return the result list
104      */
105     public ResultList<E> findAll( PaginationProperties paginationProperties )
106     {
107 
108         Query query = getEM( ).createQuery( "SELECT e FROM " + getEntityClassName( ) + " e " );
109 
110         Query countQuery = getEM( ).createQuery( "SELECT count(e) FROM " + getEntityClassName( ) + " e " );
111 
112         PagedQuerygins/stock/commons/dao/PagedQuery.html#PagedQuery">PagedQuery pq = new PagedQuery( query, countQuery, paginationProperties );
113         return pq.getResultList( );
114     }
115 
116     /**
117      * Add a predicate to an existing query
118      * 
119      * @param query
120      *            existing query
121      * @param exp
122      *            restriction
123      */
124     protected void addRestriction( CriteriaQuery<?> query, Expression<Boolean> exp )
125     {
126         CriteriaBuilder builder = getEM( ).getCriteriaBuilder( );
127         Predicate restriction = query.getRestriction( );
128         if ( restriction == null )
129         {
130             query.where( exp );
131         }
132         else
133         {
134             query.where( builder.and( restriction, exp ) );
135         }
136     }
137 }