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