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.folderlisting.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.List;
41  
42  
43  /**
44   * This class provides Data Access methods for FolderListingDatabase objects
45   */
46  public final class FolderListingDatabaseDAO implements IFolderListingDatabaseDAO
47  {
48      ///////////////////////////////////////////////////////////////////////
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_folder ) FROM folderlisting_folders ";
51      private static final String SQL_QUERY_SELECT = " SELECT id_folder, folder_name, folder_path, workgroup_key FROM folderlisting_folders WHERE id_folder = ?  ";
52      private static final String SQL_QUERY_INSERT = " INSERT INTO folderlisting_folders ( id_folder, folder_name, folder_path, workgroup_key ) VALUES ( ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = " DELETE FROM folderlisting_folders WHERE id_folder = ?  ";
54      private static final String SQL_QUERY_UPDATE = " UPDATE folderlisting_folders SET id_folder = ?, folder_name = ?, folder_path = ?, workgroup_key = ? WHERE id_folder = ?  ";
55      private static final String SQL_QUERY_SELECTALL = " SELECT id_folder, folder_name, folder_path, workgroup_key FROM folderlisting_folders ";
56  
57      /**
58       * Generates a new primary key
59       * @param plugin The Plugin using this data access service
60       * @return The new primary key
61       */
62      private int newPrimaryKey( Plugin plugin )
63      {
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
65          daoUtil.executeQuery(  );
66  
67          int nKey;
68  
69          if ( !daoUtil.next(  ) )
70          {
71              // if the table is empty
72              nKey = 1;
73          }
74  
75          nKey = daoUtil.getInt( 1 ) + 1;
76  
77          daoUtil.free(  );
78  
79          return nKey;
80      }
81  
82      /**
83       * Insert a new record in the table.
84       * @param folderListingDatabase The folderListingDatabase object
85       * @param plugin The Plugin using this data access service
86       */
87      public void insert( FolderListingDatabase folderListingDatabase, Plugin plugin )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
90          folderListingDatabase.setId( newPrimaryKey( plugin ) );
91          daoUtil.setInt( 1, folderListingDatabase.getId(  ) );
92          daoUtil.setString( 2, folderListingDatabase.getFolderName(  ) );
93          daoUtil.setString( 3, folderListingDatabase.getFolderPath(  ) );
94          daoUtil.setString( 4, folderListingDatabase.getWorkgroup(  ) );
95  
96          daoUtil.executeUpdate(  );
97          daoUtil.free(  );
98      }
99  
100     /**
101      * Load the data of FolderListingDatabase from the table
102      * @param nFolderListingDatabaseId The identifier of FolderListingDatabase
103      * @param plugin The Plugin using this data access service
104      * @return the instance of the FolderListingDatabase
105      */
106     public FolderListingDatabase load( int nFolderListingDatabaseId, Plugin plugin )
107     {
108         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
109         daoUtil.setInt( 1, nFolderListingDatabaseId );
110         daoUtil.executeQuery(  );
111 
112         FolderListingDatabase folderListingDatabase = null;
113 
114         if ( daoUtil.next(  ) )
115         {
116             folderListingDatabase = new FolderListingDatabase(  );
117             folderListingDatabase.setId( daoUtil.getInt( 1 ) );
118             folderListingDatabase.setFolderName( daoUtil.getString( 2 ) );
119             folderListingDatabase.setFolderPath( daoUtil.getString( 3 ) );
120             folderListingDatabase.setWorkgroup( daoUtil.getString( 4 ) );
121         }
122 
123         daoUtil.free(  );
124 
125         return folderListingDatabase;
126     }
127 
128     /**
129      * Delete a record from the table
130      * @param nFolderListingDatabaseId The FolderListingDatabase Id
131      * @param plugin The Plugin using this data access service
132      */
133     public void delete( int nFolderListingDatabaseId, Plugin plugin )
134     {
135         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
136         daoUtil.setInt( 1, nFolderListingDatabaseId );
137         daoUtil.executeUpdate(  );
138         daoUtil.free(  );
139     }
140 
141     /**
142      * Update the record in the table
143      * @param folderListingDatabase The reference of folderListingDatabase
144      * @param plugin The Plugin using this data access service
145      */
146     public void store( FolderListingDatabase folderListingDatabase, Plugin plugin )
147     {
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
149         daoUtil.setInt( 1, folderListingDatabase.getId(  ) );
150         daoUtil.setString( 2, folderListingDatabase.getFolderName(  ) );
151         daoUtil.setString( 3, folderListingDatabase.getFolderPath(  ) );
152         daoUtil.setString( 4, folderListingDatabase.getWorkgroup(  ) );
153         daoUtil.setInt( 5, folderListingDatabase.getId(  ) );
154 
155         daoUtil.executeUpdate(  );
156         daoUtil.free(  );
157     }
158 
159     /**
160      * Load the list of folderListingDatabases
161      * @param plugin The Plugin using this data access service
162      * @return The Collection of the FolderListingDatabases
163      */
164     public List selectFolderListingDatabaseList( Plugin plugin )
165     {
166         List listFolderListingDatabases = new ArrayList(  );
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
168         daoUtil.executeQuery(  );
169 
170         while ( daoUtil.next(  ) )
171         {
172             FolderListingDatabase folderListingDatabase = new FolderListingDatabase(  );
173             folderListingDatabase.setId( daoUtil.getInt( 1 ) );
174             folderListingDatabase.setFolderName( daoUtil.getString( 2 ) );
175             folderListingDatabase.setFolderPath( daoUtil.getString( 3 ) );
176             folderListingDatabase.setWorkgroup( daoUtil.getString( 4 ) );
177 
178             listFolderListingDatabases.add( folderListingDatabase );
179         }
180 
181         daoUtil.free(  );
182 
183         return listFolderListingDatabases;
184     }
185 }