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.portal.business.style;
35  
36  import fr.paris.lutece.util.ReferenceList;
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 Mode objects
45   */
46  public final class ModeDAO implements IModeDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_mode ) FROM core_mode";
50      private static final String SQL_QUERY_SELECT = " SELECT id_mode, description_mode, path, output_xsl_method, output_xsl_version, " +
51          " output_xsl_media_type, output_xsl_encoding, output_xsl_indent, output_xsl_omit_xml_dec, " +
52          " output_xsl_standalone FROM core_mode WHERE id_mode = ?";
53      private static final String SQL_QUERY_INSERT = " INSERT INTO core_mode ( id_mode, description_mode, path, output_xsl_method, output_xsl_version, output_xsl_media_type, output_xsl_encoding, output_xsl_indent, output_xsl_omit_xml_dec, output_xsl_standalone ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
54      private static final String SQL_QUERY_DELETE = " DELETE FROM core_mode WHERE id_mode = ?";
55      private static final String SQL_QUERY_UPDATE = " UPDATE core_mode SET description_mode = ?, path = ?, " +
56          " output_xsl_method = ? , output_xsl_version = ?, output_xsl_media_type = ?, output_xsl_encoding = ?, " +
57          " output_xsl_indent = ?, output_xsl_omit_xml_dec = ?, output_xsl_standalone = ?" + " WHERE id_mode = ?";
58      private static final String SQL_QUERY_SELECTALL = " SELECT id_mode, description_mode, path, output_xsl_method, output_xsl_version, output_xsl_media_type, " +
59          " output_xsl_encoding, output_xsl_indent, output_xsl_omit_xml_dec, output_xsl_standalone " +
60          " FROM core_mode ORDER BY id_mode";
61      private static final String SQL_QUERY_SELECT_MODES = " SELECT id_mode , description_mode FROM core_mode";
62  
63      ///////////////////////////////////////////////////////////////////////////////////////
64      //Access methods to data
65  
66      /**
67       * Generates a new primary key
68       * @return The new primary key
69       */
70      int newPrimaryKey(  )
71      {
72          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
73          daoUtil.executeQuery(  );
74  
75          int nKey;
76  
77          if ( !daoUtil.next(  ) )
78          {
79              // if the table is empty
80              nKey = 1;
81          }
82  
83          nKey = daoUtil.getInt( 1 ) + 1;
84  
85          daoUtil.free(  );
86  
87          return nKey;
88      }
89  
90      /**
91       * Insert a new record in the table.
92       * @param mode The mode object
93       */
94      public synchronized void insert( Mode mode )
95      {
96          mode.setId( newPrimaryKey(  ) );
97  
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
99  
100         daoUtil.setInt( 1, mode.getId(  ) );
101         daoUtil.setString( 2, mode.getDescription(  ) );
102         daoUtil.setString( 3, mode.getPath(  ) );
103         daoUtil.setString( 4, mode.getOutputXslPropertyMethod(  ) );
104         daoUtil.setString( 5, mode.getOutputXslPropertyVersion(  ) );
105         daoUtil.setString( 6, mode.getOutputXslPropertyMediaType(  ) );
106         daoUtil.setString( 7, mode.getOutputXslPropertyEncoding(  ) );
107         daoUtil.setString( 8, mode.getOutputXslPropertyIndent(  ) );
108         daoUtil.setString( 9, mode.getOutputXslPropertyOmitXmlDeclaration(  ) );
109         daoUtil.setString( 10, mode.getOutputXslPropertyStandalone(  ) );
110 
111         daoUtil.executeUpdate(  );
112         daoUtil.free(  );
113     }
114 
115     /**
116      * load the data of Level from the table
117      *
118      * @param nIdMode The indentifier of the object Mode
119      * @return The Instance of the object Mode
120      */
121     public Mode load( int nIdMode )
122     {
123         Mode mode = null;
124         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
125         daoUtil.setInt( 1, nIdMode );
126 
127         daoUtil.executeQuery(  );
128 
129         if ( daoUtil.next(  ) )
130         {
131             mode = new Mode(  );
132             mode.setId( daoUtil.getInt( 1 ) );
133             mode.setDescription( daoUtil.getString( 2 ) );
134             mode.setPath( daoUtil.getString( 3 ) );
135             mode.setOutputXslPropertyMethod( daoUtil.getString( 4 ) );
136             mode.setOutputXslPropertyVersion( daoUtil.getString( 5 ) );
137             mode.setOutputXslPropertyMediaType( daoUtil.getString( 6 ) );
138             mode.setOutputXslPropertyEncoding( daoUtil.getString( 7 ) );
139             mode.setOutputXslPropertyIndent( daoUtil.getString( 8 ) );
140             mode.setOutputXslPropertyOmitXmlDeclaration( daoUtil.getString( 9 ) );
141             mode.setOutputXslPropertyStandalone( daoUtil.getString( 10 ) );
142         }
143 
144         daoUtil.free(  );
145 
146         return mode;
147     }
148 
149     /**
150      * Delete a record from the table
151      * @param nModeId The indentifier of the object Mode
152      */
153     public void delete( int nModeId )
154     {
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
156         daoUtil.setInt( 1, nModeId );
157         daoUtil.executeUpdate(  );
158         daoUtil.free(  );
159     }
160 
161     /**
162      * Update the record in the table
163      * @param mode The instance of the Mode to update
164      */
165     public void store( Mode mode )
166     {
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
168 
169         daoUtil.setString( 1, mode.getDescription(  ) );
170         daoUtil.setString( 2, mode.getPath(  ) );
171         daoUtil.setString( 3, mode.getOutputXslPropertyMethod(  ) );
172         daoUtil.setString( 4, mode.getOutputXslPropertyVersion(  ) );
173         daoUtil.setString( 5, mode.getOutputXslPropertyMediaType(  ) );
174         daoUtil.setString( 6, mode.getOutputXslPropertyEncoding(  ) );
175         daoUtil.setString( 7, mode.getOutputXslPropertyIndent(  ) );
176         daoUtil.setString( 8, mode.getOutputXslPropertyOmitXmlDeclaration(  ) );
177         daoUtil.setString( 9, mode.getOutputXslPropertyStandalone(  ) );
178         daoUtil.setInt( 10, mode.getId(  ) );
179 
180         daoUtil.executeUpdate(  );
181         daoUtil.free(  );
182     }
183 
184     /**
185      * Returns a list of all the modes
186      * @return A collection of modes objects
187      */
188     public Collection<Mode> selectModesList(  )
189     {
190         Collection<Mode> modeList = new ArrayList<Mode>(  );
191         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
192         daoUtil.executeQuery(  );
193 
194         while ( daoUtil.next(  ) )
195         {
196             Mode mode = new Mode(  );
197 
198             mode.setId( daoUtil.getInt( 1 ) );
199             mode.setDescription( daoUtil.getString( 2 ) );
200             mode.setPath( daoUtil.getString( 3 ) );
201             mode.setOutputXslPropertyMethod( daoUtil.getString( 4 ) );
202             mode.setOutputXslPropertyVersion( daoUtil.getString( 5 ) );
203             mode.setOutputXslPropertyMediaType( daoUtil.getString( 6 ) );
204             mode.setOutputXslPropertyEncoding( daoUtil.getString( 7 ) );
205             mode.setOutputXslPropertyIndent( daoUtil.getString( 8 ) );
206             mode.setOutputXslPropertyOmitXmlDeclaration( daoUtil.getString( 9 ) );
207             mode.setOutputXslPropertyStandalone( daoUtil.getString( 10 ) );
208 
209             modeList.add( mode );
210         }
211 
212         daoUtil.free(  );
213 
214         return modeList;
215     }
216 
217     /**
218      * Returns the list of the modes in form of a reference list
219      *
220      * @return the modes list in form of a ReferenceList object
221      */
222     public ReferenceList getModesList(  )
223     {
224         ReferenceList modesList = new ReferenceList(  );
225         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MODES );
226 
227         daoUtil.executeQuery(  );
228 
229         while ( daoUtil.next(  ) )
230         {
231             modesList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
232         }
233 
234         daoUtil.free(  );
235 
236         return modesList;
237     }
238 }