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