View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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  
35  package fr.paris.lutece.plugins.transparency.business;
36  
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for ElectedOfficial objects
46   */
47  public final class ElectedOfficialDAO implements IElectedOfficialDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = "SELECT core_role.role, role_description FROM core_role ";
51      private static final String SQL_QUERY_SELECTALL_ID = "SELECT core_role.role FROM core_role";
52  
53      private static final String SQL_WHERECLAUSE_BY_APPOINTMENT = " LEFT JOIN transparency_elected_official_appointment ON transparency_elected_official_appointment.role_key = core_role.role WHERE transparency_elected_official_appointment.id_appointment  = ? ";
54      private static final String SQL_WHERECLAUSE_BY_DELEGATION = " LEFT JOIN mylutece_database_user_role ON mylutece_database_user_role.role_key = core_role.role LEFT JOIN mylutece_database_user ON mylutece_database_user_role.mylutece_database_user_id = mylutece_database_user.mylutece_database_user_id WHERE login = ? ";
55      private static final String SQL_WHERECLAUSE_BY_ID = " WHERE role = ? ";
56  
57      private static final String SQL_ORDER_BY = " ORDER BY role_description ";
58  
59      /**
60       * {@inheritDoc }
61       */
62      @Override
63      public ElectedOfficial load( String strKey, Plugin plugin )
64      {
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_WHERECLAUSE_BY_ID, plugin );
66          daoUtil.setString( 1, strKey );
67          daoUtil.executeQuery( );
68          ElectedOfficial electedOfficial = null;
69  
70          if ( daoUtil.next( ) )
71          {
72              electedOfficial = new ElectedOfficial( );
73              int nIndex = 1;
74  
75              electedOfficial.setId( daoUtil.getString( nIndex++ ) );
76              electedOfficial.setLastName( daoUtil.getString( nIndex++ ) );
77          }
78  
79          daoUtil.free( );
80          return electedOfficial;
81      }
82  
83      /**
84       * {@inheritDoc }
85       */
86      @Override
87      public List<ElectedOfficial> selectElectedOfficialsList( Plugin plugin )
88      {
89          List<ElectedOfficial> electedOfficialList = new ArrayList<ElectedOfficial>( );
90          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_ORDER_BY, plugin );
91          daoUtil.executeQuery( );
92  
93          while ( daoUtil.next( ) )
94          {
95              ElectedOfficial electedOfficial = new ElectedOfficial( );
96              int nIndex = 1;
97  
98              electedOfficial.setId( daoUtil.getString( nIndex++ ) );
99              electedOfficial.setLastName( daoUtil.getString( nIndex++ ) );
100 
101             electedOfficialList.add( electedOfficial );
102         }
103 
104         daoUtil.free( );
105         return electedOfficialList;
106     }
107 
108     /**
109      * {@inheritDoc }
110      */
111     @Override
112     public List<ElectedOfficial> selectElectedOfficialsListByAppointment( int idAppointment, Plugin plugin )
113     {
114         List<ElectedOfficial> electedOfficialList = new ArrayList<>( );
115         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_WHERECLAUSE_BY_APPOINTMENT + SQL_ORDER_BY, plugin );
116         daoUtil.setInt( 1, idAppointment );
117         daoUtil.executeQuery( );
118 
119         while ( daoUtil.next( ) )
120         {
121             ElectedOfficial electedOfficial = new ElectedOfficial( );
122             int nIndex = 1;
123 
124             electedOfficial.setId( daoUtil.getString( nIndex++ ) );
125             electedOfficial.setLastName( daoUtil.getString( nIndex++ ) );
126 
127             electedOfficialList.add( electedOfficial );
128         }
129 
130         daoUtil.free( );
131         return electedOfficialList;
132     }
133 
134     /**
135      * {@inheritDoc }
136      */
137     @Override
138     public List<ElectedOfficial> selectElectedOfficialsListByDelegation( int idAdminUser, Plugin plugin )
139     {
140         List<ElectedOfficial> electedOfficialList = new ArrayList<>( );
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_WHERECLAUSE_BY_DELEGATION + SQL_ORDER_BY, plugin );
142         daoUtil.setInt( 1, idAdminUser );
143         daoUtil.executeQuery( );
144 
145         while ( daoUtil.next( ) )
146         {
147             ElectedOfficial electedOfficial = new ElectedOfficial( );
148             int nIndex = 1;
149 
150             electedOfficial.setId( daoUtil.getString( nIndex++ ) );
151             electedOfficial.setLastName( daoUtil.getString( nIndex++ ) );
152 
153             electedOfficialList.add( electedOfficial );
154         }
155 
156         daoUtil.free( );
157         return electedOfficialList;
158     }
159 
160     /**
161      * {@inheritDoc }
162      */
163     @Override
164     public List<String> selectIdElectedOfficialsList( Plugin plugin )
165     {
166         List<String> electedOfficialList = new ArrayList<>( );
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID + SQL_ORDER_BY, plugin );
168         daoUtil.executeQuery( );
169 
170         while ( daoUtil.next( ) )
171         {
172             electedOfficialList.add( daoUtil.getString( 1 ) );
173         }
174 
175         daoUtil.free( );
176         return electedOfficialList;
177     }
178 
179     /**
180      * {@inheritDoc }
181      */
182     @Override
183     public ReferenceList selectElectedOfficialsReferenceList( Plugin plugin )
184     {
185         ReferenceList electedOfficialList = new ReferenceList( );
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_ORDER_BY, plugin );
187         daoUtil.executeQuery( );
188 
189         while ( daoUtil.next( ) )
190         {
191             electedOfficialList.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
192         }
193 
194         daoUtil.free( );
195         return electedOfficialList;
196     }
197 
198     /**
199      * {@inheritDoc }
200      */
201     @Override
202     public ReferenceList selectElectedOfficialsReferenceListByDelegation( String idUser, Plugin plugin )
203     {
204         ReferenceList electedOfficialList = new ReferenceList( );
205         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_WHERECLAUSE_BY_DELEGATION + SQL_ORDER_BY, plugin );
206         daoUtil.setString( 1, idUser );
207         daoUtil.executeQuery( );
208 
209         while ( daoUtil.next( ) )
210         {
211             electedOfficialList.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
212         }
213 
214         daoUtil.free( );
215         return electedOfficialList;
216     }
217 
218 }