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