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  package fr.paris.lutece.plugins.gru.business.demandtype;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for DemandTypeAction objects
45   */
46  public final class DemandTypeActionDAO implements IDemandTypeActionDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_demand_type_action ) FROM gru_demand_type_action";
50      private static final String SQL_QUERY_SELECT = "SELECT a.id_demand_type_action, a.action_link, a.action_label, a.id_demand_type, b.title "
51              + " FROM gru_demand_type_action a, gru_demand_type b WHERE a.id_demand_type = b.id_demand_type AND id_demand_type_action = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO gru_demand_type_action ( id_demand_type_action, action_link, action_label, id_demand_type ) VALUES ( ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM gru_demand_type_action WHERE id_demand_type_action = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE gru_demand_type_action SET id_demand_type_action = ?, action_link = ?, action_label = ?, id_demand_type = ? WHERE id_demand_type_action = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT a.id_demand_type_action, a.action_link, a.action_label, a.id_demand_type, b.title "
56              + " FROM gru_demand_type_action a, gru_demand_type b WHERE a.id_demand_type = b.id_demand_type ";
57      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_demand_type_action FROM gru_demand_type_action";
58      private static final String SQL_QUERY_SELECT_BY_TYPE = "SELECT a.id_demand_type_action, a.action_link, a.action_label, a.id_demand_type, b.title "
59              + " FROM gru_demand_type_action a, gru_demand_type b WHERE a.id_demand_type = b.id_demand_type AND a.id_demand_type = ?";
60  
61      /**
62       * Generates a new primary key
63       * 
64       * @param plugin
65       *            The Plugin
66       * @return The new primary key
67       */
68      public int newPrimaryKey( Plugin plugin )
69      {
70          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
71          daoUtil.executeQuery( );
72  
73          int nKey = 1;
74  
75          if ( daoUtil.next( ) )
76          {
77              nKey = daoUtil.getInt( 1 ) + 1;
78          }
79  
80          daoUtil.free( );
81  
82          return nKey;
83      }
84  
85      /**
86       * {@inheritDoc }
87       */
88      @Override
89      public void insert( DemandTypeAction demandTypeAction, Plugin plugin )
90      {
91          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
92  
93          demandTypeAction.setId( newPrimaryKey( plugin ) );
94  
95          daoUtil.setInt( 1, demandTypeAction.getId( ) );
96          daoUtil.setString( 2, demandTypeAction.getLink( ) );
97          daoUtil.setString( 3, demandTypeAction.getLabel( ) );
98          daoUtil.setInt( 4, demandTypeAction.getIdDemandType( ) );
99  
100         daoUtil.executeUpdate( );
101         daoUtil.free( );
102     }
103 
104     /**
105      * {@inheritDoc }
106      */
107     @Override
108     public DemandTypeAction load( int nKey, Plugin plugin )
109     {
110         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
111         daoUtil.setInt( 1, nKey );
112         daoUtil.executeQuery( );
113 
114         DemandTypeAction demandTypeAction = null;
115 
116         if ( daoUtil.next( ) )
117         {
118             demandTypeAction = new DemandTypeAction( );
119             demandTypeAction.setId( daoUtil.getInt( 1 ) );
120             demandTypeAction.setLink( daoUtil.getString( 2 ) );
121             demandTypeAction.setLabel( daoUtil.getString( 3 ) );
122             demandTypeAction.setIdDemandType( daoUtil.getInt( 4 ) );
123             demandTypeAction.setDemandType( daoUtil.getString( 5 ) );
124         }
125 
126         daoUtil.free( );
127 
128         return demandTypeAction;
129     }
130 
131     /**
132      * {@inheritDoc }
133      */
134     @Override
135     public void delete( int nKey, Plugin plugin )
136     {
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
138         daoUtil.setInt( 1, nKey );
139         daoUtil.executeUpdate( );
140         daoUtil.free( );
141     }
142 
143     /**
144      * {@inheritDoc }
145      */
146     @Override
147     public void store( DemandTypeAction demandTypeAction, Plugin plugin )
148     {
149         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
150 
151         daoUtil.setInt( 1, demandTypeAction.getId( ) );
152         daoUtil.setString( 2, demandTypeAction.getLink( ) );
153         daoUtil.setString( 3, demandTypeAction.getLabel( ) );
154         daoUtil.setInt( 4, demandTypeAction.getIdDemandType( ) );
155         daoUtil.setInt( 5, demandTypeAction.getId( ) );
156 
157         daoUtil.executeUpdate( );
158         daoUtil.free( );
159     }
160 
161     /**
162      * {@inheritDoc }
163      */
164     @Override
165     public List<DemandTypeAction> selectDemandTypeActionsList( Plugin plugin )
166     {
167         List<DemandTypeAction> demandTypeActionList = new ArrayList<DemandTypeAction>( );
168         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
169         daoUtil.executeQuery( );
170 
171         while ( daoUtil.next( ) )
172         {
173             DemandTypeActionmandtype/DemandTypeAction.html#DemandTypeAction">DemandTypeAction demandTypeAction = new DemandTypeAction( );
174 
175             demandTypeAction.setId( daoUtil.getInt( 1 ) );
176             demandTypeAction.setLink( daoUtil.getString( 2 ) );
177             demandTypeAction.setLabel( daoUtil.getString( 3 ) );
178             demandTypeAction.setIdDemandType( daoUtil.getInt( 4 ) );
179             demandTypeAction.setDemandType( daoUtil.getString( 5 ) );
180 
181             demandTypeActionList.add( demandTypeAction );
182         }
183 
184         daoUtil.free( );
185 
186         return demandTypeActionList;
187     }
188 
189     /**
190      * {@inheritDoc }
191      */
192     @Override
193     public List<Integer> selectIdDemandTypeActionsList( Plugin plugin )
194     {
195         List<Integer> demandTypeActionList = new ArrayList<Integer>( );
196         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
197         daoUtil.executeQuery( );
198 
199         while ( daoUtil.next( ) )
200         {
201             demandTypeActionList.add( daoUtil.getInt( 1 ) );
202         }
203 
204         daoUtil.free( );
205 
206         return demandTypeActionList;
207     }
208 
209     /**
210      * {@inheritDoc }
211      */
212     @Override
213     public List<DemandTypeAction> selectActionsByType( int nDemandTypeId, Plugin plugin )
214     {
215         List<DemandTypeAction> demandTypeActionList = new ArrayList<DemandTypeAction>( );
216         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_TYPE, plugin );
217         daoUtil.setInt( 1, nDemandTypeId );
218         daoUtil.executeQuery( );
219 
220         while ( daoUtil.next( ) )
221         {
222             DemandTypeActionmandtype/DemandTypeAction.html#DemandTypeAction">DemandTypeAction demandTypeAction = new DemandTypeAction( );
223 
224             demandTypeAction.setId( daoUtil.getInt( 1 ) );
225             demandTypeAction.setLink( daoUtil.getString( 2 ) );
226             demandTypeAction.setLabel( daoUtil.getString( 3 ) );
227             demandTypeAction.setIdDemandType( daoUtil.getInt( 4 ) );
228             demandTypeAction.setDemandType( daoUtil.getString( 5 ) );
229 
230             demandTypeActionList.add( demandTypeAction );
231         }
232 
233         daoUtil.free( );
234 
235         return demandTypeActionList;
236     }
237 
238     /**
239      * {@inheritDoc }
240      */
241     @Override
242     public ReferenceList selectActionsReferenceList( Plugin plugin )
243     {
244         ReferenceList list = new ReferenceList( );
245         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
246         daoUtil.executeQuery( );
247 
248         while ( daoUtil.next( ) )
249         {
250             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 5 ) + " - " + daoUtil.getString( 3 ) );
251         }
252 
253         daoUtil.free( );
254 
255         return list;
256     }
257 }