View Javadoc
1   /*
2    * Copyright (c) 2002-2015, 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  
35  package fr.paris.lutece.plugins.directory.modules.exportfile.business;
36  
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 MappingEntry objects
45   */
46  
47  public final class MappingEntryDAO implements IMappingEntryDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_mappingentry ) FROM exportfile_mapping_entry";
51      private static final String SQL_QUERY_SELECT = "SELECT id_mappingentry, idEntry, idDirectory, path, isActive FROM exportfile_mapping_entry WHERE id_mappingentry = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO exportfile_mapping_entry ( id_mappingentry, idEntry, idDirectory, path, isActive ) VALUES ( ?, ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM exportfile_mapping_entry WHERE id_mappingentry = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE exportfile_mapping_entry SET idEntry = ?, idDirectory = ?, path = ?, isActive = ? WHERE id_mappingentry = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_mappingentry, idEntry, idDirectory, path, isActive FROM exportfile_mapping_entry";
56      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_mappingentry FROM exportfile_mapping_entry";
57  
58      /**
59       * Generates a new primary key
60       * 
61       * @param plugin
62       *            The Plugin
63       * @return The new primary key
64       */
65      public int newPrimaryKey( Plugin plugin )
66      {
67          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
68          daoUtil.executeQuery( );
69  
70          int nKey = 1;
71  
72          if ( daoUtil.next( ) )
73          {
74              nKey = daoUtil.getInt( 1 ) + 1;
75          }
76  
77          daoUtil.free( );
78  
79          return nKey;
80      }
81  
82      /**
83       * {@inheritDoc }
84       */
85      @Override
86      public void insert( MappingEntry mappingEntry, Plugin plugin )
87      {
88          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
89  
90          mappingEntry.setId( newPrimaryKey( plugin ) );
91  
92          daoUtil.setInt( 1, mappingEntry.getId( ) );
93          daoUtil.setInt( 2, mappingEntry.getIdEntry( ) );
94          daoUtil.setInt( 3, mappingEntry.getIdDirectory( ) );
95          daoUtil.setString( 4, mappingEntry.getPath( ) );
96          daoUtil.setBoolean( 5, mappingEntry.getIsActive( ) );
97  
98          daoUtil.executeUpdate( );
99          daoUtil.free( );
100     }
101 
102     /**
103      * {@inheritDoc }
104      */
105     @Override
106     public MappingEntry load( int nKey, Plugin plugin )
107     {
108         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
109         daoUtil.setInt( 1, nKey );
110         daoUtil.executeQuery( );
111 
112         MappingEntry mappingEntry = null;
113 
114         if ( daoUtil.next( ) )
115         {
116             mappingEntry = new MappingEntry( );
117             mappingEntry.setId( daoUtil.getInt( 1 ) );
118             mappingEntry.setIdEntry( daoUtil.getInt( 2 ) );
119             mappingEntry.setIdDirectory( daoUtil.getInt( 3 ) );
120             mappingEntry.setPath( daoUtil.getString( 4 ) );
121             mappingEntry.setIsActive( daoUtil.getBoolean( 5 ) );
122         }
123 
124         daoUtil.free( );
125         return mappingEntry;
126     }
127 
128     /**
129      * {@inheritDoc }
130      */
131     @Override
132     public void delete( int nKey, Plugin plugin )
133     {
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
135         daoUtil.setInt( 1, nKey );
136         daoUtil.executeUpdate( );
137         daoUtil.free( );
138     }
139 
140     /**
141      * {@inheritDoc }
142      */
143     @Override
144     public void store( MappingEntry mappingEntry, Plugin plugin )
145     {
146         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
147 
148         daoUtil.setInt( 1, mappingEntry.getIdEntry( ) );
149         daoUtil.setInt( 2, mappingEntry.getIdDirectory( ) );
150         daoUtil.setBoolean( 4, mappingEntry.getIsActive( ) );
151         daoUtil.setString( 3, mappingEntry.getPath( ) );
152         daoUtil.setInt( 5, mappingEntry.getId( ) );
153 
154         daoUtil.executeUpdate( );
155         daoUtil.free( );
156     }
157 
158     /**
159      * {@inheritDoc }
160      */
161     @Override
162     public Collection<MappingEntry> selectMappingEntrysList( Plugin plugin )
163     {
164         Collection<MappingEntry> mappingEntryList = new ArrayList<MappingEntry>( );
165         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
166         daoUtil.executeQuery( );
167 
168         while ( daoUtil.next( ) )
169         {
170             MappingEntry mappingEntry = new MappingEntry( );
171 
172             mappingEntry.setId( daoUtil.getInt( 1 ) );
173             mappingEntry.setIdEntry( daoUtil.getInt( 2 ) );
174             mappingEntry.setIdDirectory( daoUtil.getInt( 3 ) );
175             mappingEntry.setPath( daoUtil.getString( 4 ) );
176             mappingEntry.setIsActive( daoUtil.getBoolean( 5 ) );
177 
178             mappingEntryList.add( mappingEntry );
179         }
180 
181         daoUtil.free( );
182         return mappingEntryList;
183     }
184 
185     /**
186      * {@inheritDoc }
187      */
188     @Override
189     public Collection<Integer> selectIdMappingEntrysList( Plugin plugin )
190     {
191         Collection<Integer> mappingEntryList = new ArrayList<Integer>( );
192         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
193         daoUtil.executeQuery( );
194 
195         while ( daoUtil.next( ) )
196         {
197             mappingEntryList.add( daoUtil.getInt( 1 ) );
198         }
199 
200         daoUtil.free( );
201         return mappingEntryList;
202     }
203 }