View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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  
35  
36  package fr.paris.lutece.plugins.formresponsxpage.business;
37  
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.util.ReferenceList;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  import java.sql.Statement;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Map;
46  
47  import java.util.Optional;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  /**
52   * This class provides Data Access methods for Formsreponseedito objects
53   */
54  public final class FormsreponseeditoDAO extends AbstractFilterDao implements IFormsreponseeditoDAO
55  {
56      // Constants
57      private static final String SQL_QUERY_INSERT = "INSERT INTO formresponsxpage_formsreponseedito ( labelrichtext_un, labelrichtext_deux ) VALUES ( ?, ? ) ";
58      private static final String SQL_QUERY_DELETE = "DELETE FROM formresponsxpage_formsreponseedito WHERE id_formsreponseedito = ? ";
59      private static final String SQL_QUERY_UPDATE = "UPDATE formresponsxpage_formsreponseedito SET labelrichtext_un = ?, labelrichtext_deux = ? WHERE id_formsreponseedito = ?";
60     
61  	private static final String SQL_QUERY_SELECTALL = "SELECT id_formsreponseedito, labelrichtext_un, labelrichtext_deux FROM formresponsxpage_formsreponseedito";
62      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_formsreponseedito FROM formresponsxpage_formsreponseedito";
63  
64      private static final String SQL_QUERY_SELECTALL_BY_IDS = SQL_QUERY_SELECTALL + " WHERE id_formsreponseedito IN (  ";
65  	private static final String SQL_QUERY_SELECT_BY_ID = SQL_QUERY_SELECTALL + " WHERE id_formsreponseedito = ?";
66  
67  
68  	/**
69       * Constructor
70       */
71  	public FormsreponseeditoDAO() {
72  
73  		initMapSql(Formsreponseedito.class); //Maps with name and type of each databases column associated to the business class attributes 
74  	}
75  
76      /**
77       * {@inheritDoc }
78       */
79      @Override
80      public void insert( Formsreponseedito formsreponseedito, Plugin plugin )
81      {
82          try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
83          {
84              int nIndex = 1;
85              daoUtil.setString( nIndex++ , formsreponseedito.getLabelrichtextUn( ) );
86              daoUtil.setString( nIndex++ , formsreponseedito.getLabelrichtextDeux( ) );
87              
88              daoUtil.executeUpdate( );
89              if ( daoUtil.nextGeneratedKey( ) ) 
90              {
91                  formsreponseedito.setId( daoUtil.getGeneratedKeyInt( 1 ) );
92              }
93          }
94          
95      }
96  
97      /**
98       * {@inheritDoc }
99       */
100     @Override
101     public Optional<Formsreponseedito> load( int nKey, Plugin plugin )
102     {
103         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID, plugin ) )
104         {
105 	        daoUtil.setInt( 1 , nKey );
106 	        daoUtil.executeQuery( );
107 	        Formsreponseedito formsreponseedito = null;
108 	
109 	        if ( daoUtil.next( ) )
110 	        {
111 	            formsreponseedito = loadFromDaoUtil( daoUtil );
112 	        }
113 	
114 	        return Optional.ofNullable( formsreponseedito );
115         }
116     }
117 
118     /**
119      * {@inheritDoc }
120      */
121     @Override
122     public void delete( int nKey, Plugin plugin )
123     {
124         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
125         {
126 	        daoUtil.setInt( 1 , nKey );
127 	        daoUtil.executeUpdate( );
128         }
129     }
130 
131     /**
132      * {@inheritDoc }
133      */
134     @Override
135     public void store( Formsreponseedito formsreponseedito, Plugin plugin )
136     {
137         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
138         {
139 	        int nIndex = 1;
140 	        
141             	daoUtil.setString( nIndex++ , formsreponseedito.getLabelrichtextUn( ) );
142             	daoUtil.setString( nIndex++ , formsreponseedito.getLabelrichtextDeux( ) );
143 	        daoUtil.setInt( nIndex , formsreponseedito.getId( ) );
144 	
145 	        daoUtil.executeUpdate( );
146         }
147     }
148 
149     /**
150      * {@inheritDoc }
151      */
152     @Override
153     public List<Formsreponseedito> selectFormsreponseeditosList( Plugin plugin )
154     {
155         List<Formsreponseedito> formsreponseeditoList = new ArrayList<>(  );
156         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
157         {
158 	        daoUtil.executeQuery(  );
159 	
160 	        while ( daoUtil.next(  ) )
161 	        {
162 				formsreponseeditoList.add( loadFromDaoUtil( daoUtil ) );
163 	        }
164 	
165 	        return formsreponseeditoList;
166         }
167     }
168     
169     /**
170      * {@inheritDoc }
171      */
172     @Override
173     public List<Integer> selectIdFormsreponseeditosList( Plugin plugin,  Map <String,String> mapFilterCriteria, String strColumnToOrder, String strSortMode )
174     {
175         List<Integer> formsreponseeditoList = new ArrayList<>( );
176         
177         String strSelectStatement =  prepareSelectStatement(SQL_QUERY_SELECTALL_ID, mapFilterCriteria, strColumnToOrder, strSortMode);  
178         
179         try( DAOUtil daoUtil = new DAOUtil( strSelectStatement, plugin ) )
180         {
181         
182         	int nIndex = 1;
183     	        
184    	        for(Map.Entry<String, String> filter : mapFilterCriteria.entrySet()) {
185    	        	
186    	        	if(StringUtils.isNotBlank(filter.getValue())  && _mapSql.containsKey(filter.getKey())) {
187    	        		daoUtil.setString( nIndex++ , filter.getValue() );
188    	        	}
189    	        }
190     	        
191 	        daoUtil.executeQuery(  );
192 	
193 	        while ( daoUtil.next(  ) )
194 	        {
195 	            formsreponseeditoList.add( daoUtil.getInt( 1 ) );
196 	        }
197 	
198 	        return formsreponseeditoList;
199         }
200     }
201     
202     /**
203      * {@inheritDoc }
204      */
205     @Override
206     public ReferenceList selectFormsreponseeditosReferenceList( Plugin plugin )
207     {
208         ReferenceList formsreponseeditoList = new ReferenceList();
209         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
210         {
211 	        daoUtil.executeQuery(  );
212 	
213 	        while ( daoUtil.next(  ) )
214 	        {
215 	            formsreponseeditoList.addItem( daoUtil.getInt( 1 ) , daoUtil.getString( 2 ) );
216 	        }
217 	
218 	        return formsreponseeditoList;
219     	}
220     }
221     
222     /**
223      * {@inheritDoc }
224      */
225 	@Override
226 	public List<Formsreponseedito> selectFormsreponseeditosListByIds( Plugin plugin, List<Integer> listIds ) {
227 		List<Formsreponseedito> formsreponseeditoList = new ArrayList<>(  );
228 		
229 		StringBuilder builder = new StringBuilder( );
230 
231 		if ( !listIds.isEmpty( ) )
232 		{
233 			for( int i = 0 ; i < listIds.size(); i++ ) {
234 			    builder.append( "?," );
235 			}
236 	
237 			String placeHolders =  builder.deleteCharAt( builder.length( ) -1 ).toString( );
238 			String stmt = SQL_QUERY_SELECTALL_BY_IDS + placeHolders + ")";
239 			
240 			
241 	        try ( DAOUtil daoUtil = new DAOUtil( stmt, plugin ) )
242 	        {
243 	        	int index = 1;
244 				for( Integer n : listIds ) {
245 					daoUtil.setInt(  index++, n ); 
246 				}
247 	        	
248 	        	daoUtil.executeQuery(  );
249 	        	while ( daoUtil.next(  ) )
250 		        {
251 		            formsreponseeditoList.add( loadFromDaoUtil( daoUtil ) );
252 		        }
253 	        }
254 	    }
255 		return formsreponseeditoList;
256 		
257 	}
258 
259 
260 	private Formsreponseedito loadFromDaoUtil (DAOUtil daoUtil) {
261 		
262 		Formsreponseedito formsreponseedito = new Formsreponseedito(  );
263 		int nIndex = 1;
264 		
265 		formsreponseedito.setId( daoUtil.getInt( nIndex++ ) );
266 		formsreponseedito.setLabelrichtextUn( daoUtil.getString( nIndex++ ) );
267 		formsreponseedito.setLabelrichtextDeux( daoUtil.getString( nIndex ) );
268 		
269 		return formsreponseedito;
270 	}
271 }