View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.campagnebp.business;
35  
36  import fr.paris.lutece.portal.business.file.FileHome;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  
43  /**
44   * This class provides Data Access methods for CampagnePhase objects
45   */
46  
47  public final class CampagnePhaseDAO implements ICampagnePhaseDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_campagne_phase ) FROM ideation_campagnes_phases";
51      private static final String SQL_QUERY_SELECT = "SELECT id_campagne_phase, code_phase_type, code_campagne, start, end FROM ideation_campagnes_phases WHERE id_campagne_phase = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO ideation_campagnes_phases ( id_campagne_phase, code_phase_type, code_campagne, start, end ) VALUES ( ?, ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM ideation_campagnes_phases WHERE id_campagne_phase = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE ideation_campagnes_phases SET id_campagne_phase = ?, code_phase_type = ?, code_campagne = ?, start = ?, end = ? WHERE id_campagne_phase = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_campagne_phase, code_phase_type, code_campagne, start, end FROM ideation_campagnes_phases";
56      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_campagne_phase FROM ideation_campagnes_phases";
57      private static final String SQL_QUERY_SELECTALL_BY_CAMPAGNE = SQL_QUERY_SELECTALL + " WHERE code_campagne = ?";
58  
59      /**
60       * Generates a new primary key
61       * 
62       * @param plugin
63       *            The Plugin
64       * @return The new primary key
65       */
66      public int newPrimaryKey( Plugin plugin )
67      {
68          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
69          daoUtil.executeQuery( );
70  
71          int nKey = 1;
72  
73          if ( daoUtil.next( ) )
74          {
75              nKey = daoUtil.getInt( 1 ) + 1;
76          }
77  
78          daoUtil.free( );
79  
80          return nKey;
81      }
82  
83      /**
84       * {@inheritDoc }
85       */
86      @Override
87      public void insert( CampagnePhase campagnePhase, Plugin plugin )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
90  
91          campagnePhase.setId( newPrimaryKey( plugin ) );
92  
93          daoUtil.setInt( 1, campagnePhase.getId( ) );
94          daoUtil.setString( 2, campagnePhase.getCodePhaseType( ) );
95          daoUtil.setString( 3, campagnePhase.getCodeCampagne( ) );
96          daoUtil.setTimestamp( 4, campagnePhase.getStart( ) );
97          daoUtil.setTimestamp( 5, campagnePhase.getEnd( ) );
98  
99          daoUtil.executeUpdate( );
100         daoUtil.free( );
101     }
102 
103     /**
104      * {@inheritDoc }
105      */
106     @Override
107     public CampagnePhase load( int nKey, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
110         daoUtil.setInt( 1, nKey );
111         daoUtil.executeQuery( );
112 
113         CampagnePhase campagnePhase = null;
114 
115         if ( daoUtil.next( ) )
116         {
117             campagnePhase = new CampagnePhase( );
118             campagnePhase.setId( daoUtil.getInt( 1 ) );
119             campagnePhase.setCodePhaseType( daoUtil.getString( 2 ) );
120             campagnePhase.setCodeCampagne( daoUtil.getString( 3 ) );
121             campagnePhase.setStart( daoUtil.getTimestamp( 4 ) );
122             campagnePhase.setEnd( daoUtil.getTimestamp( 5 ) );
123         }
124 
125         daoUtil.free( );
126         return campagnePhase;
127     }
128 
129     /**
130      * {@inheritDoc }
131      */
132     @Override
133     public void delete( int nKey, Plugin plugin )
134     {
135         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
136         daoUtil.setInt( 1, nKey );
137         daoUtil.executeUpdate( );
138         daoUtil.free( );
139     }
140 
141     /**
142      * {@inheritDoc }
143      */
144     @Override
145     public void store( CampagnePhase campagnePhase, Plugin plugin )
146     {
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
148 
149         daoUtil.setInt( 1, campagnePhase.getId( ) );
150         daoUtil.setString( 2, campagnePhase.getCodePhaseType( ) );
151         daoUtil.setString( 3, campagnePhase.getCodeCampagne( ) );
152         daoUtil.setTimestamp( 4, campagnePhase.getStart( ) );
153         daoUtil.setTimestamp( 5, campagnePhase.getEnd( ) );
154         daoUtil.setInt( 6, campagnePhase.getId( ) );
155 
156         daoUtil.executeUpdate( );
157         daoUtil.free( );
158     }
159 
160     /**
161      * {@inheritDoc }
162      */
163     @Override
164     public Collection<CampagnePhase> selectCampagnePhasesList( Plugin plugin )
165     {
166         Collection<CampagnePhase> campagnePhaseList = new ArrayList<CampagnePhase>( );
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
168         daoUtil.executeQuery( );
169 
170         while ( daoUtil.next( ) )
171         {
172             CampagnePhaseusiness/CampagnePhase.html#CampagnePhase">CampagnePhase campagnePhase = new CampagnePhase( );
173 
174             campagnePhase.setId( daoUtil.getInt( 1 ) );
175             campagnePhase.setCodePhaseType( daoUtil.getString( 2 ) );
176             campagnePhase.setCodeCampagne( daoUtil.getString( 3 ) );
177             campagnePhase.setStart( daoUtil.getTimestamp( 4 ) );
178             campagnePhase.setEnd( daoUtil.getTimestamp( 5 ) );
179 
180             campagnePhaseList.add( campagnePhase );
181         }
182 
183         daoUtil.free( );
184         return campagnePhaseList;
185     }
186 
187     /**
188      * {@inheritDoc }
189      */
190     @Override
191     public Collection<Integer> selectIdCampagnePhasesList( Plugin plugin )
192     {
193         Collection<Integer> campagnePhaseList = new ArrayList<Integer>( );
194         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
195         daoUtil.executeQuery( );
196 
197         while ( daoUtil.next( ) )
198         {
199             campagnePhaseList.add( daoUtil.getInt( 1 ) );
200         }
201 
202         daoUtil.free( );
203         return campagnePhaseList;
204     }
205 
206     /**
207      * {@inheritDoc }
208      */
209     @Override
210     public Collection<CampagnePhase> selectCampagnePhasesListByCampagne( String campagneCode, Plugin plugin )
211     {
212         Collection<CampagnePhase> campagnePhaseList = new ArrayList<CampagnePhase>( );
213         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_CAMPAGNE, plugin );
214         daoUtil.setString( 1, campagneCode );
215         daoUtil.executeQuery( );
216 
217         while ( daoUtil.next( ) )
218         {
219             CampagnePhaseusiness/CampagnePhase.html#CampagnePhase">CampagnePhase campagnePhase = new CampagnePhase( );
220 
221             campagnePhase.setId( daoUtil.getInt( 1 ) );
222             campagnePhase.setCodePhaseType( daoUtil.getString( 2 ) );
223             campagnePhase.setCodeCampagne( daoUtil.getString( 3 ) );
224             campagnePhase.setStart( daoUtil.getTimestamp( 4 ) );
225             campagnePhase.setEnd( daoUtil.getTimestamp( 5 ) );
226 
227             campagnePhaseList.add( campagnePhase );
228         }
229 
230         daoUtil.free( );
231         return campagnePhaseList;
232     }
233 }