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.wrapper.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  
43  /**
44   * This class provides Data Access methods for Wrapper objects
45   * @author lenaini
46   */
47  public class WrapperDAO implements IWrapperDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEWPK = "SELECT max( id_wrapper ) FROM wrapper ";
51      private static final String SQL_QUERY_SELECT = "SELECT id_wrapper, description, wrapper_url, wrapper_styles, status, workgroup_key, role FROM wrapper WHERE id_wrapper = ? ";
52      private static final String SQL_QUERY_SELECTALL = "SELECT id_wrapper, description, wrapper_url, wrapper_styles, status, workgroup_key, role FROM wrapper ORDER BY description, id_wrapper DESC";
53      private static final String SQL_QUERY_SELECT_ENABLED = "SELECT id_wrapper, description, wrapper_url, wrapper_styles, status, workgroup_key, role FROM wrapper WHERE id_wrapper = ? AND status = 0 ";
54      private static final String SQL_QUERY_SELECT_ENABLED_WRAPPER_LIST = "SELECT id_wrapper, description, wrapper_url, wrapper_styles, status, workgroup_key, role FROM wrapper WHERE status = 0 ORDER BY description, id_wrapper DESC";
55      private static final String SQL_QUERY_INSERT = "INSERT INTO wrapper ( id_wrapper , description, wrapper_url, wrapper_styles, status, workgroup_key, role )  VALUES ( ?, ?, ?, ?, ?, ?, ? ) ";
56      private static final String SQL_QUERY_DELETE = "DELETE FROM wrapper WHERE id_wrapper = ? ";
57      private static final String SQL_QUERY_UPDATE = "UPDATE wrapper SET description = ? , wrapper_url = ?, wrapper_styles = ? ,status = ?, workgroup_key = ?, role = ?  WHERE id_wrapper = ?  ";
58  
59      ///////////////////////////////////////////////////////////////////////////////////////
60      //Access methods to data
61  
62      /**
63       * Generates a new primary key
64       * @param plugin The plugin
65       * @return The new primary key
66       */
67      private int newPrimaryKey( Plugin plugin )
68      {
69          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEWPK, plugin );
70          daoUtil.executeQuery(  );
71  
72          int nKey;
73  
74          if ( !daoUtil.next(  ) )
75          {
76              // if the table is empty
77              nKey = 1;
78          }
79  
80          nKey = daoUtil.getInt( 1 ) + 1;
81  
82          daoUtil.free(  );
83  
84          return nKey;
85      }
86  
87      ////////////////////////////////////////////////////////////////////////
88      // Methods using a dynamic pool
89  
90      /**
91       * Insert a new record in the table.
92       *
93       * @param wrapper The wrapper object
94       * @param plugin The plugin
95       */
96      public void insert( Wrapper wrapper, Plugin plugin )
97      {
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
99          wrapper.setId( newPrimaryKey( plugin ) );
100         daoUtil.setInt( 1, wrapper.getId(  ) );
101         daoUtil.setString( 2, wrapper.getDescription(  ) );
102         daoUtil.setString( 3, wrapper.getWrapperUrl(  ) );
103         daoUtil.setString( 4, wrapper.getWrapperStyles(  ) );
104         daoUtil.setInt( 5, wrapper.getStatus(  ) );
105         daoUtil.setString( 6, wrapper.getWorkgroup(  ) );
106         daoUtil.setString( 7, wrapper.getRole(  ) );
107 
108         daoUtil.executeUpdate(  );
109         daoUtil.free(  );
110     }
111 
112     /**
113      * Load the data of Wrapper from the table
114      * @param nWrapperId The identifier of Wrapper
115      * @param plugin The plugin
116      * @return the instance of the Wrapper
117      */
118     public Wrapper load( int nWrapperId, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
121         daoUtil.setInt( 1, nWrapperId );
122         daoUtil.executeQuery(  );
123 
124         Wrapper wrapper = null;
125 
126         if ( daoUtil.next(  ) )
127         {
128             wrapper = new Wrapper(  );
129             wrapper.setId( daoUtil.getInt( 1 ) );
130             wrapper.setDescription( daoUtil.getString( 2 ) );
131             wrapper.setWrapperUrl( daoUtil.getString( 3 ) );
132             wrapper.setWrapperStyles( daoUtil.getString( 4 ) );
133             wrapper.setStatus( daoUtil.getInt( 5 ) );
134             wrapper.setWorkgroup( daoUtil.getString( 6 ) );
135             wrapper.setRole( daoUtil.getString( 7 ) );
136         }
137 
138         daoUtil.free(  );
139 
140         return wrapper;
141     }
142 
143     /**
144      * Delete a record from the table
145      * @param wrapper The Wrapper object
146      * @param plugin The plugin
147      */
148     public void delete( Wrapper wrapper, Plugin plugin )
149     {
150         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
151         daoUtil.setInt( 1, wrapper.getId(  ) );
152         daoUtil.executeUpdate(  );
153         daoUtil.free(  );
154     }
155 
156     /**
157      * Update the record in the table
158      * @param wrapper The reference of wrapper
159      * @param plugin The plugin
160      */
161     public void store( Wrapper wrapper, Plugin plugin )
162     {
163         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
164         int nWrapperId = wrapper.getId(  );
165 
166         daoUtil.setString( 1, wrapper.getDescription(  ) );
167         daoUtil.setString( 2, wrapper.getWrapperUrl(  ) );
168         daoUtil.setString( 3, wrapper.getWrapperStyles(  ) );
169         daoUtil.setInt( 4, wrapper.getStatus(  ) );
170         daoUtil.setString( 5, wrapper.getWorkgroup(  ) );
171         daoUtil.setString( 6, wrapper.getRole(  ) );
172         daoUtil.setInt( 7, nWrapperId );
173 
174         daoUtil.executeUpdate(  );
175         daoUtil.free(  );
176     }
177 
178     /**
179      * Load the list of wrappers
180      *
181      * @param plugin The plugin
182      * @return The Collection of the Wrappers
183      */
184     public Collection<Wrapper> selectAll( Plugin plugin )
185     {
186         Collection<Wrapper> wrapperList = new ArrayList<Wrapper>(  );
187         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
188         daoUtil.executeQuery(  );
189 
190         while ( daoUtil.next(  ) )
191         {
192             Wrapper wrapper = new Wrapper(  );
193             wrapper.setId( daoUtil.getInt( 1 ) );
194             wrapper.setDescription( daoUtil.getString( 2 ) );
195             wrapper.setWrapperUrl( daoUtil.getString( 3 ) );
196             wrapper.setWrapperStyles( daoUtil.getString( 4 ) );
197             wrapper.setStatus( daoUtil.getInt( 5 ) );
198             wrapper.setWorkgroup( daoUtil.getString( 6 ) );
199             wrapper.setRole( daoUtil.getString( 7 ) );
200             wrapperList.add( wrapper );
201         }
202 
203         daoUtil.free(  );
204 
205         return wrapperList;
206     }
207 
208     /**
209      * Load enabled wrapper
210      * @param nWrapperId The page id
211      * @param plugin The plugin
212      * @return The Collection of the Wrappers
213      */
214     public Wrapper selectEnabledWrapper( int nWrapperId, Plugin plugin )
215     {
216         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ENABLED, plugin );
217         daoUtil.setInt( 1, nWrapperId );
218         daoUtil.executeQuery(  );
219 
220         Wrapper wrapper = null;
221 
222         if ( daoUtil.next(  ) )
223         {
224             wrapper = new Wrapper(  );
225             wrapper.setId( daoUtil.getInt( 1 ) );
226             wrapper.setDescription( daoUtil.getString( 2 ) );
227             wrapper.setWrapperUrl( daoUtil.getString( 3 ) );
228             wrapper.setWrapperStyles( daoUtil.getString( 4 ) );
229             wrapper.setStatus( daoUtil.getInt( 5 ) );
230             wrapper.setWorkgroup( daoUtil.getString( 6 ) );
231             wrapper.setRole( daoUtil.getString( 7 ) );
232         }
233 
234         daoUtil.free(  );
235 
236         return wrapper;
237     }
238 
239     /**
240      * Load the list of wrappers
241      *
242      * @param plugin The plugin
243      * @return The Collection of the Wrappers
244      */
245     public Collection<Wrapper> selectEnabledWrapperList( Plugin plugin )
246     {
247         Collection<Wrapper> wrapperList = new ArrayList<Wrapper>(  );
248         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ENABLED_WRAPPER_LIST, plugin );
249         daoUtil.executeQuery(  );
250 
251         while ( daoUtil.next(  ) )
252         {
253             Wrapper wrapper = new Wrapper(  );
254             wrapper.setId( daoUtil.getInt( 1 ) );
255             wrapper.setDescription( daoUtil.getString( 2 ) );
256             wrapper.setWrapperUrl( daoUtil.getString( 3 ) );
257             wrapper.setWrapperStyles( daoUtil.getString( 4 ) );
258             wrapper.setStatus( daoUtil.getInt( 5 ) );
259             wrapper.setWorkgroup( daoUtil.getString( 6 ) );
260             wrapper.setRole( daoUtil.getString( 7 ) );
261             wrapperList.add( wrapper );
262         }
263 
264         daoUtil.free(  );
265 
266         return wrapperList;
267     }
268 }