View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.linkpages.business.portlet;
35  
36  import fr.paris.lutece.portal.business.page.Page;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
38  import fr.paris.lutece.portal.service.util.AppException;
39  import fr.paris.lutece.util.ReferenceList;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  
46  /**
47   * This class provides Data Access methods for LinkPagesPortlet objects
48   */
49  public class LinkPagesPortletDAO implements ILinkPagesPortletDAO
50  {
51      private static final String SQL_QUERY_SELECT_LINKPAGES_LIST = "SELECT id_page, name FROM core_page ORDER BY name";
52      private static final String SQL_QUERY_SELECT_MAX_ORDER = "SELECT max( linkpage_order ) FROM linkpages_portlet WHERE id_portlet=?";
53      private static final String SQL_QUERY_SELECT_LINKPAGE_IN_PORTLET = " SELECT a.id_page, a.name, a.description, a.page_order, a.status, a.role, a.code_theme, a.image_content " +
54          " FROM core_page a, linkpages_portlet b  WHERE a.id_page = b.id_linkpage AND b.id_portlet = ? " +
55          " ORDER BY b.linkpage_order";
56      private static final String SQL_QUERY_SELECT_LINKPAGE_ORDER = "SELECT linkpage_order FROM linkpages_portlet WHERE id_portlet = ? AND id_linkpage = ? ";
57      private static final String SQL_QUERY_DELETE = "DELETE FROM linkpages_portlet WHERE id_portlet = ?";
58      private static final String SQL_QUERY_SELECT = "SELECT id_portlet, id_page FROM core_portlet WHERE id_portlet = ?";
59      private static final String SQL_QUERY_SELECT_LINKPAGE_ID_BY_ORDER = "SELECT id_linkpage FROM linkpages_portlet WHERE id_portlet = ? AND linkpage_order = ?";
60      private static final String SQL_QUERY_UPDATE_LINKPAGE_ORDER = "UPDATE linkpages_portlet SET linkpage_order = ? WHERE id_portlet = ? AND id_linkpage = ? ";
61      private static final String SQL_QUERY_DELETE_LINKPAGE = "DELETE FROM linkpages_portlet WHERE id_portlet=? AND id_linkpage =?";
62      private static final String SQL_QUERY_DELETE_LINKPAGE_ALL = "DELETE FROM portlet_link_pages WHERE id_portlet=?";
63      private static final String SQL_QUERY_CHECK_DUPLICATE = "SELECT id_linkpage FROM linkpages_portlet WHERE  id_portlet = ? AND  id_linkpage = ?";
64      private static final String SQL_QUERY_INSERT_LINKPAGE = "INSERT INTO linkpages_portlet ( id_portlet, id_linkpage, linkpage_order ) VALUES (?,?,?)";
65  
66      ////////////////////////////////////////////////////////////////////////////
67      //Access methods to data
68      public void insert( Portlet portlet )
69      {
70      }
71  
72      /**
73       * Deletes a record from the table
74       *
75       * @param nPortletId Identifier portlet
76       */
77      public void delete( int nPortletId )
78      {
79          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
80          daoUtil.setInt( 1, nPortletId );
81          daoUtil.executeUpdate(  );
82          daoUtil.free(  );
83      }
84  
85      /**
86       * Loads the data of a LinkdPagesPortlet whose identifier is specified in parameter from the table
87       *
88       * @param nPortletId The LinkdPagesPortlet identifier
89       * @return the LinkdPagesPortlet object
90       */
91      public Portlet load( int nPortletId )
92      {
93          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
94          daoUtil.setInt( 1, nPortletId );
95          daoUtil.executeQuery(  );
96  
97          LinkPagesPortletges/business/portlet/LinkPagesPortlet.html#LinkPagesPortlet">LinkPagesPortlet portlet = new LinkPagesPortlet(  );
98  
99          if ( daoUtil.next(  ) )
100         {
101             portlet.setId( daoUtil.getInt( 1 ) );
102             portlet.setPageId( daoUtil.getInt( 2 ) );
103         }
104 
105         daoUtil.free(  );
106 
107         return portlet;
108     }
109 
110     public void store( Portlet portlet ) throws AppException
111     {
112     }
113 
114     /**
115      * Load the list of all the pages of the website
116      *
117      * @return the list in form of a ReferenceList object
118      */
119     public ReferenceList selectLinkPagesList(  )
120     {
121         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINKPAGES_LIST );
122         daoUtil.executeQuery(  );
123 
124         ReferenceList list = new ReferenceList(  );
125 
126         while ( daoUtil.next(  ) )
127         {
128             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
129         }
130 
131         daoUtil.free(  );
132 
133         return list;
134     }
135 
136     /**
137      * Calculate a new primary key to add a new linkPage
138      *
139      * @param nPortletId The identifier of the portlet
140      * @return The new key.
141      */
142     public int selectMaxOrder( int nPortletId )
143     {
144         int nOrder = 0;
145         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_ORDER );
146         daoUtil.setInt( 1, nPortletId );
147         daoUtil.executeQuery(  );
148 
149         if ( daoUtil.next(  ) )
150         {
151             nOrder = daoUtil.getInt( 1 );
152         }
153 
154         daoUtil.free(  );
155 
156         return nOrder;
157     }
158 
159     /**
160      * Return a list of linkpages which belong to a specified portlet
161      *
162      * @param nPortletId The identifier of the portlet
163      * @return A list of linkpages objects
164      */
165     public List<Page> selectLinkPagesInPortletList( int nPortletId )
166     {
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINKPAGE_IN_PORTLET );
168         daoUtil.setInt( 1, nPortletId );
169         daoUtil.executeQuery(  );
170 
171         List<Page> list = new ArrayList<Page>(  );
172 
173         while ( daoUtil.next(  ) )
174         {
175             Page page = new Page(  );
176             page.setId( daoUtil.getInt( 1 ) );
177             page.setName( daoUtil.getString( 2 ) );
178             page.setDescription( daoUtil.getString( 3 ) );
179             page.setOrder( daoUtil.getInt( 4 ) );
180             page.setStatus( daoUtil.getInt( 5 ) );
181             page.setRole( daoUtil.getString( 6 ) );
182             page.setCodeTheme( daoUtil.getString( 7 ) );
183             page.setImageContent( daoUtil.getBytes( 8 ) );
184             list.add( page );
185         }
186 
187         daoUtil.free(  );
188 
189         return list;
190     }
191 
192     /**
193      * Return the order of a link page in a specified portlet
194      *
195      * @param nPortletId The identifier of the portlet to check
196      * @param nPageId The identifier of the page
197      * @return The order of the page in the portlet
198      */
199     public int selectLinkPageOrder( int nPortletId, int nPageId )
200     {
201         int nOrder = 0;
202         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINKPAGE_ORDER );
203         daoUtil.setInt( 1, nPortletId );
204         daoUtil.setInt( 2, nPageId );
205         daoUtil.executeQuery(  );
206 
207         if ( daoUtil.next(  ) )
208         {
209             nOrder = daoUtil.getInt( 1 );
210         }
211 
212         daoUtil.free(  );
213 
214         return nOrder;
215     }
216 
217     /**
218      * Returns the id of a link page wich has a specified order in a specified portlet
219      *
220      * @param nPortletId The identifier of the portlet
221      * @param nOrder The link page's order
222      * @return The identifier of the link page
223      */
224     public int selectLinkPageIdByOrder( int nPortletId, int nOrder )
225     {
226         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINKPAGE_ID_BY_ORDER );
227         daoUtil.setInt( 1, nPortletId );
228         daoUtil.setInt( 2, nOrder );
229         daoUtil.executeQuery(  );
230 
231         if ( !daoUtil.next(  ) )
232         {
233             throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + "(PortletId = " + nPortletId + "; Order = " +
234                 nOrder + ")" );
235         }
236 
237         int nId = daoUtil.getInt( 1 );
238         daoUtil.free(  );
239 
240         return nId;
241     }
242 
243     /**
244      * Update the order of a specified link page in a specified portlet
245      *
246      * @param nPortletId The identifier of the portlet
247      * @param nLinkPageId The identifier of the link page
248      * @param nOrder The new order
249      */
250     public void storeLinkPageOrder( int nOrder, int nPortletId, int nLinkPageId )
251     {
252         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_LINKPAGE_ORDER );
253         daoUtil.setInt( 1, nOrder );
254         daoUtil.setInt( 2, nPortletId );
255         daoUtil.setInt( 3, nLinkPageId );
256         daoUtil.executeUpdate(  );
257         daoUtil.free(  );
258     }
259 
260     /**
261      * Remove a specified link page from a specified portlet
262      *
263      * @param nPortletId The identifier of the portlet
264      * @param nLinkPageId The identifier of the link page
265      */
266     public void deleteLinkPage( int nPortletId, int nLinkPageId )
267     {
268         if ( ( nLinkPageId != 0 ) )
269         {
270             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINKPAGE );
271             daoUtil.setInt( 1, nPortletId );
272             daoUtil.setInt( 2, nLinkPageId );
273             daoUtil.executeUpdate(  );
274             daoUtil.free(  );
275         }
276     }
277 
278     /**
279     * Remove all link pages from a specified portlet
280     *
281     * @param nPortletId The identifier of the portlet
282     */
283     public void deleteAllLinkPages( int nPortletId )
284     {
285         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINKPAGE_ALL );
286         daoUtil.setInt( 1, nPortletId );
287         daoUtil.executeUpdate(  );
288         daoUtil.free(  );
289     }
290 
291     /**
292      * Check if a specified link page is not already registered in a specified portlet
293      *
294      * @param nPortletId The identifier of the portlet
295      * @param nLinkPageId The identifier of the link page
296      * @return The result(boolean)
297      */
298     public boolean testDuplicate( int nPortletId, int nLinkPageId )
299     {
300         boolean bDuplicate = false;
301         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_DUPLICATE );
302         daoUtil.setInt( 1, nPortletId );
303         daoUtil.setInt( 2, nLinkPageId );
304         daoUtil.executeQuery(  );
305 
306         if ( daoUtil.next(  ) )
307         {
308             bDuplicate = true;
309         }
310 
311         daoUtil.free(  );
312 
313         return bDuplicate;
314     }
315 
316     /**
317      * Insert a new record in the table.
318      *
319      * @param nPortletId The identifier of the portlet
320      * @param nLinkPageId The identifier of the link page
321      * @param nOrder The order of the portlet to insert
322      */
323     public void insertLinkPage( int nPortletId, int nLinkPageId, int nOrder )
324     {
325         if ( ( nLinkPageId != 0 ) )
326         {
327             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_LINKPAGE );
328             daoUtil.setInt( 1, nPortletId );
329             daoUtil.setInt( 2, nLinkPageId );
330             daoUtil.setInt( 3, nOrder );
331             daoUtil.executeUpdate(  );
332             daoUtil.free(  );
333         }
334     }
335 }