View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.dbpage.business;
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  /**
45   * This class provides Data Access methods for DbPageDatabase objects
46   */
47  public final class DbPageDatabaseDAO implements IDbPageDatabaseDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_page ) FROM dbpage_page ";
51      private static final String SQL_QUERY_SELECT = " SELECT id_page, param_name, title, workgroup_key FROM dbpage_page WHERE id_page = ?  ";
52      private static final String SQL_QUERY_INSERT = " INSERT INTO dbpage_page ( id_page, param_name, title, workgroup_key ) VALUES ( ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = " DELETE FROM dbpage_page WHERE id_page = ?  ";
54      private static final String SQL_QUERY_UPDATE = " UPDATE dbpage_page SET id_page = ?, param_name = ?, title = ?, workgroup_key = ? WHERE id_page = ?  ";
55      private static final String SQL_QUERY_SELECTALL = " SELECT id_page, param_name, title, workgroup_key FROM dbpage_page ";
56      private static final String SQL_QUERY_SELECT_COMBO = " SELECT id_page , param_name FROM dbpage_page ";
57      private static final String SQL_QUERY_SELECT_BY_NAME = "SELECT id_page, param_name, title, workgroup_key FROM dbpage_page where param_name = ?  ";
58  
59      /**
60       * Generates a new primary key
61       * @return The new primary key
62       * @param plugin The plugin object
63       */
64      private int newPrimaryKey( Plugin plugin )
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
67          daoUtil.executeQuery(  );
68  
69          int nKey;
70  
71          if ( !daoUtil.next(  ) )
72          {
73              // if the table is empty
74              nKey = 1;
75          }
76  
77          nKey = daoUtil.getInt( 1 ) + 1;
78          daoUtil.free(  );
79  
80          return nKey;
81      }
82  
83      /**
84       * Insert a new record in the table.
85       * @param plugin The plugin
86       * @param dbPageDatabase The dbPageDatabase object
87       */
88      public void insert( DbPageDatabase dbPageDatabase, Plugin plugin )
89      {
90          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
91          dbPageDatabase.setId( newPrimaryKey( plugin ) );
92          daoUtil.setInt( 1, dbPageDatabase.getId(  ) );
93          daoUtil.setString( 2, dbPageDatabase.getParamName(  ) );
94          daoUtil.setString( 3, dbPageDatabase.getTitle(  ) );
95          daoUtil.setString( 4, dbPageDatabase.getWorkgroup(  ) );
96  
97          daoUtil.executeUpdate(  );
98          daoUtil.free(  );
99      }
100 
101     /**
102      * Load the data of DbPageDatabase from the table
103      * @param plugin The plugin
104      * @return the instance of the DbPageDatabase
105      * @param nDbPageDatabaseId The identifier of DbPageDatabase
106      */
107     public DbPageDatabase load( int nDbPageDatabaseId, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
110         daoUtil.setInt( 1, nDbPageDatabaseId );
111         daoUtil.executeQuery(  );
112 
113         DbPageDatabase dbPageDatabase = null;
114 
115         if ( daoUtil.next(  ) )
116         {
117             dbPageDatabase = new DbPageDatabase(  );
118             dbPageDatabase.setId( daoUtil.getInt( 1 ) );
119             dbPageDatabase.setParamName( daoUtil.getString( 2 ) );
120             dbPageDatabase.setTitle( daoUtil.getString( 3 ) );
121             dbPageDatabase.setWorkgroup( daoUtil.getString( 4 ) );
122         }
123 
124         daoUtil.free(  );
125 
126         return dbPageDatabase;
127     }
128 
129     /**
130     * Load the data of DbPageDatabase from the table
131     * @param plugin The plugin
132     * @return the instance of the DbPageDatabase
133     * @param strDbPageName The identifier of DbPageDatabase
134     */
135     public DbPageDatabase loadByName( String strDbPageName, Plugin plugin )
136     {
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_NAME, plugin );
138         daoUtil.setString( 1, strDbPageName );
139         daoUtil.executeQuery(  );
140 
141         DbPageDatabase dbPageDatabase = null;
142 
143         if ( daoUtil.next(  ) )
144         {
145             dbPageDatabase = new DbPageDatabase(  );
146             dbPageDatabase.setId( daoUtil.getInt( 1 ) );
147             dbPageDatabase.setParamName( daoUtil.getString( 2 ) );
148             dbPageDatabase.setTitle( daoUtil.getString( 3 ) );
149             dbPageDatabase.setWorkgroup( daoUtil.getString( 4 ) );
150         }
151 
152         daoUtil.free(  );
153 
154         return dbPageDatabase;
155     }
156 
157     /**
158      * Delete a record from the table
159      * @param nDbPageDatabaseId The DbPageDatabase Id
160      * @param plugin The plugin
161      */
162     public void delete( int nDbPageDatabaseId, Plugin plugin )
163     {
164         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
165         daoUtil.setInt( 1, nDbPageDatabaseId );
166         daoUtil.executeUpdate(  );
167         daoUtil.free(  );
168     }
169 
170     /**
171      * Update the record in the table
172      * @param dbPageDatabase The reference of dbPageDatabase
173      * @param plugin The plugin
174      */
175     public void store( DbPageDatabase dbPageDatabase, Plugin plugin )
176     {
177         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
178         daoUtil.setInt( 1, dbPageDatabase.getId(  ) );
179         daoUtil.setString( 2, dbPageDatabase.getParamName(  ) );
180         daoUtil.setString( 3, dbPageDatabase.getTitle(  ) );
181         daoUtil.setString( 4, dbPageDatabase.getWorkgroup(  ) );
182         daoUtil.setInt( 5, dbPageDatabase.getId(  ) );
183         daoUtil.executeUpdate(  );
184         daoUtil.free(  );
185     }
186 
187     /**
188     * Load the list of dbPageDatabases
189     * @param plugin The plugin
190     * @return The List of the DbPages
191     */
192     public List<DbPageDatabase> selectDbPageDatabaseList( Plugin plugin )
193     {
194         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
195         daoUtil.executeQuery(  );
196 
197         List<DbPageDatabase> list = new ArrayList<DbPageDatabase>(  );
198 
199         while ( daoUtil.next(  ) )
200         {
201             DbPageDatabase dbPageDatabase = new DbPageDatabase(  );
202             dbPageDatabase.setId( daoUtil.getInt( 1 ) );
203             dbPageDatabase.setParamName( daoUtil.getString( 2 ) );
204             dbPageDatabase.setTitle( daoUtil.getString( 3 ) );
205             dbPageDatabase.setWorkgroup( daoUtil.getString( 4 ) );
206             list.add( dbPageDatabase );
207         }
208 
209         daoUtil.free(  );
210 
211         return list;
212     }
213 
214     /**
215      * Load the list of dbpages
216      * @param plugin The plugin
217      * @return The ReferenceList of the DbPages
218      */
219     public ReferenceList selectDbPagesList( Plugin plugin )
220     {
221         ReferenceList dbPageList = new ReferenceList(  );
222         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_COMBO, plugin );
223         daoUtil.executeQuery(  );
224 
225         while ( daoUtil.next(  ) )
226         {
227             dbPageList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
228         }
229 
230         daoUtil.free(  );
231 
232         return dbPageList;
233     }
234 }