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.portal.business.stylesheet.StyleSheet;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  
43  
44  /**
45   * This class provides Data Access methods for style objects
46   */
47  public final class StyleDAO implements IStyleDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = " SELECT DISTINCT a.id_portlet_type , a.id_portal_component, a.description_style , " +
51          " b.name, c.name , id_style FROM core_style a " +
52          " INNER JOIN core_portal_component c ON a.id_portal_component = c.id_portal_component " +
53          " LEFT JOIN core_portlet_type b ON a.id_portlet_type = b.id_portlet_type WHERE a.id_style = ? ";
54      private static final String SQL_QUERY_INSERT = " INSERT INTO core_style ( id_style , id_portlet_type , id_portal_component, description_style ) " +
55          " VALUES ( ?, ?, ?, ? )";
56      private static final String SQL_QUERY_DELETE = " DELETE FROM core_style WHERE id_style = ? ";
57      private static final String SQL_QUERY_UPDATE = " UPDATE core_style SET  id_portlet_type = ?, id_portal_component = ?, description_style = ? " +
58          " WHERE id_style = ?";
59      private static final String SQL_QUERY_SELECTALL = " SELECT a.id_style , a.id_portlet_type , a.id_portal_component, a.description_style , " +
60          " b.name, c.name FROM core_style a " +
61          " INNER JOIN core_portal_component c ON a.id_portal_component = c.id_portal_component " +
62          " LEFT JOIN core_portlet_type b ON a.id_portlet_type = b.id_portlet_type " +
63          " ORDER BY a.id_portal_component, a.id_style ";
64      private static final String SQL_QUERY_SELECT_STYLESHEET = " SELECT a.id_stylesheet, a.description, a.file_name " +
65          " FROM  core_stylesheet a , core_style_mode_stylesheet b" + " WHERE b.id_stylesheet = a.id_stylesheet " +
66          " AND b.id_style = ? ";
67      private static final String SQL_CHECK_STYLE_PORTLETCOMPONENT = " SELECT id_style FROM core_style WHERE id_portal_component = ? ";
68      private static final String SQL_QUERY_SELECT_PORTLETCOMPONENT = " SELECT id_portal_component , name FROM core_portal_component ORDER BY name ";
69  
70      ///////////////////////////////////////////////////////////////////////////////////////
71      //Access methods to data
72  
73      /**
74       * Insert a new record in the table.
75       * @param style The Instance of the object Style
76       */
77      public void insert( Style style )
78      {
79          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
80  
81          daoUtil.setInt( 1, style.getId(  ) );
82          daoUtil.setString( 2, style.getPortletTypeId(  ) );
83          daoUtil.setInt( 3, style.getPortalComponentId(  ) );
84          daoUtil.setString( 4, style.getDescription(  ) );
85  
86          daoUtil.executeUpdate(  );
87          daoUtil.free(  );
88      }
89  
90      /**
91       * load the data of the Style whose identifier is specified in parameter from the table
92       * @param nStyleId The identifier of the Style
93       * @return an instance of the Style which has been created
94       */
95      public Style load( int nStyleId )
96      {
97          Style style = null;
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
99          daoUtil.setInt( 1, nStyleId );
100 
101         daoUtil.executeQuery(  );
102 
103         if ( daoUtil.next(  ) )
104         {
105             style = new Style(  );
106             style.setId( nStyleId );
107             style.setPortletTypeId( daoUtil.getString( 1 ) );
108             style.setPortalComponentId( daoUtil.getInt( 2 ) );
109             style.setDescription( daoUtil.getString( 3 ) );
110             style.setPortletTypeName( daoUtil.getString( 4 ) );
111             style.setPortalComponentName( daoUtil.getString( 5 ) );
112         }
113 
114         daoUtil.free(  );
115 
116         return style;
117     }
118 
119     /**
120      * Delete a record from the table
121      * @param nStyleId the identifier of the style to delete
122      */
123     public void delete( int nStyleId )
124     {
125         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
126         daoUtil.setInt( 1, nStyleId );
127         daoUtil.executeUpdate(  );
128         daoUtil.free(  );
129     }
130 
131     /**
132      * Update the record in the table
133      * @param style The instance of the Style to update
134      */
135     public void store( Style style )
136     {
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
138 
139         daoUtil.setString( 1, style.getPortletTypeId(  ) );
140         daoUtil.setInt( 2, style.getPortalComponentId(  ) );
141         daoUtil.setString( 3, style.getDescription(  ) );
142         daoUtil.setInt( 4, style.getId(  ) );
143 
144         daoUtil.executeUpdate(  );
145         daoUtil.free(  );
146     }
147 
148     /**
149      * Load the list of styles stored in the database
150      * @return The styles list in form of a Collection object
151      */
152     public Collection<Style> selectStylesList(  )
153     {
154         Collection<Style> styleList = new ArrayList<Style>(  );
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
156         daoUtil.executeQuery(  );
157 
158         while ( daoUtil.next(  ) )
159         {
160             Style style = new Style(  );
161 
162             style.setId( daoUtil.getInt( 1 ) );
163             style.setPortletTypeId( daoUtil.getString( 2 ) );
164             style.setPortalComponentId( daoUtil.getInt( 3 ) );
165             style.setDescription( daoUtil.getString( 4 ) );
166             style.setPortletTypeName( daoUtil.getString( 5 ) );
167             style.setPortalComponentName( daoUtil.getString( 6 ) );
168 
169             styleList.add( style );
170         }
171 
172         daoUtil.free(  );
173 
174         return styleList;
175     }
176 
177     /**
178      * Returns the list of the portal component in form of a ReferenceList
179      * @return the list of the portal component
180      */
181     public ReferenceList selectPortalComponentList(  )
182     {
183         ReferenceList portletComponentList = new ReferenceList(  );
184         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLETCOMPONENT );
185         daoUtil.executeQuery(  );
186 
187         while ( daoUtil.next(  ) )
188         {
189             portletComponentList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
190         }
191 
192         daoUtil.free(  );
193 
194         return portletComponentList;
195     }
196 
197     /**
198      * load the data of the StyleSheet which re associated to the given style
199      *
200      * @param nStyleId The identifier of the Style
201      * @return an instance of the Style which has been created
202      */
203     public Collection<StyleSheet> selectStyleSheetList( int nStyleId )
204     {
205         Collection<StyleSheet> stylesheetList = new ArrayList<StyleSheet>(  );
206         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_STYLESHEET );
207 
208         daoUtil.setInt( 1, nStyleId );
209         daoUtil.executeQuery(  );
210 
211         while ( daoUtil.next(  ) )
212         {
213             StyleSheet stylesheet = new StyleSheet(  );
214 
215             stylesheet.setId( daoUtil.getInt( 1 ) );
216             stylesheet.setDescription( daoUtil.getString( 2 ) );
217             stylesheet.setFile( daoUtil.getString( 3 ) );
218 
219             stylesheetList.add( stylesheet );
220         }
221 
222         daoUtil.free(  );
223 
224         return stylesheetList;
225     }
226 
227     /**
228      * Checks if a style has been created in the database with the given portal componenet
229      * @param nPortalComponentId The identifier of the portal component
230      * @return true if a style has been created for this portal component, false otherwise
231      */
232     public boolean checkStylePortalComponent( int nPortalComponentId )
233     {
234         DAOUtil daoUtil = new DAOUtil( SQL_CHECK_STYLE_PORTLETCOMPONENT );
235 
236         daoUtil.setInt( 1, nPortalComponentId );
237         daoUtil.executeQuery(  );
238 
239         if ( daoUtil.next(  ) )
240         {
241             daoUtil.free(  );
242 
243             return true;
244         }
245 
246         daoUtil.free(  );
247 
248         return false;
249     }
250 }