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.portlet;
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.List;
41  import java.util.Locale;
42  
43  /**
44   * This class provides Data Access methods for PortletType objects
45   */
46  public final class PortletTypeDAO implements IPortletTypeDAO
47  {
48      // sql queries
49      private static final String SQL_QUERY_INSERT = " INSERT INTO core_portlet_type ( id_portlet_type , name , url_creation , "
50              + " url_update , home_class , plugin_name , url_docreate , create_script , create_specific , create_specific_form , "
51              + " url_domodify , modify_script , modify_specific , modify_specific_form, icon_name )  VALUES ( ?, ? ,? ,?, ?, ?, ?, ? ,? ,?, ?, ?, ? , ?, ? )";
52      private static final String SQL_QUERY_SELECT = " SELECT id_portlet_type , name , url_creation , url_update , home_class, plugin_name, "
53              + " url_docreate , create_script , create_specific , create_specific_form , "
54              + " url_domodify , modify_script , modify_specific , modify_specific_form, icon_name FROM core_portlet_type WHERE id_portlet_type = ? ORDER BY name ";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM core_portlet_type WHERE id_portlet_type = ?";
56      private static final String SQL_QUERY_SELECT_PORTLET_TYPE_ID = "SELECT id_portlet_type FROM core_portlet_type WHERE home_class = ?";
57      private static final String SQL_QUERY_SELECT_NB_PORTLET_TYPE_BY_PORTLET = " SELECT count(*) FROM core_portlet WHERE id_portlet_type = ? ";
58      private static final String SQL_QUERY_SELECT_PORTLETS_TYPE_LIST = " SELECT id_portlet_type , name FROM core_portlet_type ORDER BY name ";
59      private static final String SQL_QUERY_SELECT_PORTLET_TYPE_LIST = "SELECT id_portlet_type, name, url_creation, url_update, icon_name FROM core_portlet_type ORDER BY name";
60  
61      // /////////////////////////////////////////////////////////////////////////////////////
62      // Access methods to data
63  
64      /**
65       * Insert a new record in the table PortletType
66       * 
67       * @param portletType
68       *            The portlet Type object
69       */
70      public void insert( PortletType portletType )
71      {
72          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
73          {
74              daoUtil.setString( 1, portletType.getId( ) );
75              daoUtil.setString( 2, portletType.getNameKey( ) );
76              daoUtil.setString( 3, portletType.getUrlCreation( ) );
77              daoUtil.setString( 4, portletType.getUrlUpdate( ) );
78              daoUtil.setString( 5, portletType.getHomeClass( ) );
79              daoUtil.setString( 6, portletType.getPluginName( ) );
80              daoUtil.setString( 7, portletType.getDoCreateUrl( ) );
81              daoUtil.setString( 8, portletType.getCreateScriptTemplate( ) );
82              daoUtil.setString( 9, portletType.getCreateSpecificTemplate( ) );
83              daoUtil.setString( 10, portletType.getCreateSpecificFormTemplate( ) );
84              daoUtil.setString( 11, portletType.getDoModifyUrl( ) );
85              daoUtil.setString( 12, portletType.getModifyScriptTemplate( ) );
86              daoUtil.setString( 13, portletType.getModifySpecificTemplate( ) );
87              daoUtil.setString( 14, portletType.getModifySpecificFormTemplate( ) );
88              daoUtil.setString( 15, portletType.getIconName( ) );
89  
90              daoUtil.executeUpdate( );
91          }
92      }
93  
94      /**
95       * Load the data of PortletType from the table
96       * 
97       * @param strPortletTypeId
98       *            The identifier of PortletType
99       * @return The instance of the PortletType
100      */
101     public PortletType load( String strPortletTypeId )
102     {
103         PortletTypeportlet/PortletType.html#PortletType">PortletType portletType = new PortletType( );
104         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
105         {
106             daoUtil.setString( 1, strPortletTypeId );
107             daoUtil.executeQuery( );
108 
109             if ( daoUtil.next( ) )
110             {
111                 portletType.setId( daoUtil.getString( 1 ) );
112                 portletType.setNameKey( daoUtil.getString( 2 ) );
113                 portletType.setUrlCreation( daoUtil.getString( 3 ) );
114                 portletType.setUrlUpdate( daoUtil.getString( 4 ) );
115                 portletType.setHomeClass( daoUtil.getString( 5 ) );
116                 portletType.setPluginName( daoUtil.getString( 6 ) );
117                 portletType.setDoCreateUrl( daoUtil.getString( 7 ) );
118                 portletType.setCreateScriptTemplate( daoUtil.getString( 8 ) );
119                 portletType.setCreateSpecificTemplate( daoUtil.getString( 9 ) );
120                 portletType.setCreateSpecificFormTemplate( daoUtil.getString( 10 ) );
121                 portletType.setDoModifyUrl( daoUtil.getString( 11 ) );
122                 portletType.setModifyScriptTemplate( daoUtil.getString( 12 ) );
123                 portletType.setModifySpecificTemplate( daoUtil.getString( 13 ) );
124                 portletType.setModifySpecificFormTemplate( daoUtil.getString( 14 ) );
125                 portletType.setIconName( daoUtil.getString( 15 ) );
126             }
127 
128         }
129 
130         return portletType;
131     }
132 
133     /**
134      * Delete a record from the table
135      * 
136      * @param strPortletTypeId
137      *            The POrtletTYpe identifier
138      */
139     public void delete( String strPortletTypeId )
140     {
141         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
142         {
143             daoUtil.setString( 1, strPortletTypeId );
144 
145             daoUtil.executeUpdate( );
146         }
147     }
148 
149     /**
150      * Returns the portlet type identifier
151      * 
152      * @param strPluginHomeClass
153      *            the name of the portlet type
154      * @return the identifier of the portlet type
155      */
156     public String selectPortletTypeId( String strPluginHomeClass )
157     {
158         String strPortletTypeId = "";
159         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_TYPE_ID ) )
160         {
161             daoUtil.setString( 1, strPluginHomeClass );
162             daoUtil.executeQuery( );
163 
164             if ( daoUtil.next( ) )
165             {
166                 strPortletTypeId = daoUtil.getString( 1 );
167             }
168 
169         }
170 
171         return strPortletTypeId;
172     }
173 
174     /**
175      * Returns the portlet count for a given provider
176      * 
177      * @param strPortletTypeId
178      *            The provider's identifier
179      * @return nCount
180      */
181     public int selectNbPortletTypeByPortlet( String strPortletTypeId )
182     {
183         int nCount = 0;
184         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NB_PORTLET_TYPE_BY_PORTLET ) )
185         {
186             daoUtil.setString( 1, strPortletTypeId );
187             daoUtil.executeQuery( );
188 
189             if ( daoUtil.next( ) )
190             {
191                 nCount = daoUtil.getInt( 1 );
192             }
193 
194         }
195 
196         return nCount;
197     }
198 
199     /**
200      * Return a Reference List of portletType
201      * 
202      * @param locale
203      *            The current locale
204      * @return list The reference List
205      */
206     public ReferenceList selectPortletsTypesList( Locale locale )
207     {
208         ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
209         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLETS_TYPE_LIST ) )
210         {
211             daoUtil.executeQuery( );
212 
213             while ( daoUtil.next( ) )
214             {
215                 PortletTypeportlet/PortletType.html#PortletType">PortletType portletType = new PortletType( );
216                 portletType.setId( daoUtil.getString( 1 ) );
217                 portletType.setNameKey( daoUtil.getString( 2 ) );
218                 // Localize the portlet type
219                 portletType.setLocale( locale );
220                 list.addItem( portletType.getId( ), portletType.getName( ) );
221             }
222 
223         }
224 
225         return list;
226     }
227 
228     /**
229      * Returns the list of the portlet types
230      * 
231      * @return the list of the portlet types
232      */
233     public List<PortletType> selectPortletTypesList( )
234     {
235         List<PortletType> list = new ArrayList<>( );
236         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_TYPE_LIST ) )
237         {
238             daoUtil.executeQuery( );
239 
240             while ( daoUtil.next( ) )
241             {
242                 PortletTypeportlet/PortletType.html#PortletType">PortletType portletType = new PortletType( );
243                 portletType.setId( daoUtil.getString( 1 ) );
244                 portletType.setNameKey( daoUtil.getString( 2 ) );
245                 portletType.setUrlCreation( daoUtil.getString( 3 ) );
246                 portletType.setUrlUpdate( daoUtil.getString( 4 ) );
247                 portletType.setIconName( daoUtil.getString( 5 ) );
248                 list.add( portletType );
249             }
250 
251         }
252 
253         return list;
254     }
255 }