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.myportal.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 Style objects
45   *
46   */
47  public final class ColumnStyleDAO implements IWidgetStyleDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_style ) FROM myportal_column_style";
51      private static final String SQL_QUERY_SELECT = "SELECT id_style, name, css_class FROM myportal_column_style WHERE id_style = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO myportal_column_style ( id_style, name, css_class ) VALUES ( ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM myportal_column_style WHERE id_style = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE myportal_column_style SET id_style = ?, name = ?, css_class = ? WHERE id_style = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_style, name, css_class FROM myportal_column_style";
56  
57      /**
58       * Generates a new primary key
59       * 
60       * @param plugin
61       *            The Plugin
62       * @return The new primary key
63       */
64      public 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       * 
86       * @param style
87       *            instance of the Style object to insert
88       * @param plugin
89       *            The plugin
90       */
91      public void insert( Style style, Plugin plugin )
92      {
93          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
94  
95          style.setId( newPrimaryKey( plugin ) );
96  
97          daoUtil.setInt( 1, style.getId( ) );
98          daoUtil.setString( 2, style.getName( ) );
99          daoUtil.setString( 3, style.getCssClass( ) );
100 
101         daoUtil.executeUpdate( );
102         daoUtil.free( );
103     }
104 
105     /**
106      * Load the data of the style from the table
107      * 
108      * @param nId
109      *            The identifier of the style
110      * @param plugin
111      *            The plugin
112      * @return the instance of the Style
113      */
114     public Style load( int nId, Plugin plugin )
115     {
116         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
117         daoUtil.setInt( 1, nId );
118         daoUtil.executeQuery( );
119 
120         Style style = null;
121 
122         if ( daoUtil.next( ) )
123         {
124             style = new Style( );
125 
126             style.setId( daoUtil.getInt( 1 ) );
127             style.setName( daoUtil.getString( 2 ) );
128             style.setCssClass( daoUtil.getString( 3 ) );
129         }
130 
131         daoUtil.free( );
132 
133         return style;
134     }
135 
136     /**
137      * Delete a record from the table
138      * 
139      * @param nStyleId
140      *            The identifier of the style
141      * @param plugin
142      *            The plugin
143      */
144     public void delete( int nStyleId, Plugin plugin )
145     {
146         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
147         daoUtil.setInt( 1, nStyleId );
148         daoUtil.executeUpdate( );
149         daoUtil.free( );
150     }
151 
152     /**
153      * Update the record in the table
154      * 
155      * @param style
156      *            The reference of the style
157      * @param plugin
158      *            The plugin
159      */
160     public void store( Style style, Plugin plugin )
161     {
162         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
163 
164         daoUtil.setInt( 1, style.getId( ) );
165         daoUtil.setString( 2, style.getName( ) );
166         daoUtil.setString( 3, style.getCssClass( ) );
167         daoUtil.setInt( 4, style.getId( ) );
168 
169         daoUtil.executeUpdate( );
170         daoUtil.free( );
171     }
172 
173     /**
174      * Load the data of all the styles and returns them as a List
175      * 
176      * @param plugin
177      *            The plugin
178      * @return The List which contains the data of all the styles
179      */
180     public List<Style> selectStylesList( Plugin plugin )
181     {
182         List<Style> styleList = new ArrayList<Style>( );
183         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
184         daoUtil.executeQuery( );
185 
186         while ( daoUtil.next( ) )
187         {
188             Styleins/myportal/business/Style.html#Style">Style style = new Style( );
189 
190             style.setId( daoUtil.getInt( 1 ) );
191             style.setName( daoUtil.getString( 2 ) );
192             style.setCssClass( daoUtil.getString( 3 ) );
193 
194             styleList.add( style );
195         }
196 
197         daoUtil.free( );
198 
199         return styleList;
200     }
201 }