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.links.business.portlet;
35  
36  import fr.paris.lutece.plugins.links.business.Link;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  
44  
45  /**
46   * This class provides Data Access methods for LinksPortlet objects
47   */
48  public final class LinksPortletDAO implements ILinksPortletDAO
49  {
50      // Constants
51      private static final String SQL_QUERY_SELECT = " SELECT id_portlet FROM core_portlet WHERE id_portlet = ? ";
52      private static final String SQL_QUERY_INSERT = " INSERT INTO link_list_portlet ( id_portlet, id_link, link_order ) VALUES ( ? , ? , ? )";
53      private static final String SQL_QUERY_DELETE = " DELETE FROM link_list_portlet WHERE id_portlet = ? ";
54      private static final String SQL_QUERY_DELETE_LINK_PORTLET = " DELETE FROM link_portlet WHERE id_portlet = ? ";
55      private static final String SQL_QUERY_DELETE_LINK = " DELETE FROM link_list_portlet WHERE id_portlet=? AND id_link = ? ";
56      private static final String SQL_QUERY_SELECT_LINK = " SELECT id_link, name, url FROM link ORDER BY name ";
57      private static final String SQL_QUERY_SELECT_ID_LINK = " SELECT id_link FROM link_list_portlet WHERE  id_portlet = ? AND  id_link = ? ";
58      private static final String SQL_QUERY_SELECT_LINK_IN_PORTLET_LIST = " SELECT a.id_link, a.name, a.url, a.description, a.image_content, a.mime_type, a.workgroup_key " +
59          " FROM link a, link_list_portlet b WHERE a.id_link = b.id_link AND b.id_portlet = ? " +
60          " ORDER BY b.link_order";
61      private static final String SQL_QUERY_SELECT_LINK_ORDER = " SELECT link_order FROM link_list_portlet WHERE id_portlet = ? AND id_link = ? ";
62      private static final String SQL_QUERY_SELECT_MAX_ORDER = " SELECT max( link_order ) FROM link_list_portlet WHERE id_portlet= ? ";
63      private static final String SQL_QUERY_SELECT_PORTLET_MAX_ORDER = " SELECT max( portlet_link_order ) FROM link_portlet ";
64      private static final String SQL_QUERY_UPDATE_LINK_ORDER = " UPDATE link_list_portlet SET link_order = ? WHERE id_portlet = ? AND id_link = ? ";
65      private static final String SQL_QUERY_SELECT_LINK_ID_BY_ORDER = " SELECT id_link FROM link_list_portlet WHERE id_portlet = ? AND link_order = ? ";
66      private static final String SQL_QUERY_SELECT_UNSELECTED_PORTLET = " SELECT a.id_portlet, a.name FROM core_portlet a " +
67          " LEFT JOIN link_portlet b ON a.id_portlet=b.id_portlet WHERE b.id_portlet is NULL " +
68          " AND a.id_portlet_type= ? ";
69      private static final String SQL_QUERY_SELECT_PORTLET_LINK_PAGE = " SELECT a.id_portlet, a.portlet_link_order, b.name " +
70          " FROM link_portlet a, core_portlet b WHERE a.portlet_link_order > -1 " +
71          " AND a.id_portlet=b.id_portlet ORDER BY a.portlet_link_order ";
72      private static final String SQL_QUERY_SELECT_PORTLET_LINK_ORDER = " SELECT portlet_link_order FROM link_portlet WHERE id_portlet = ? ";
73      private static final String SQL_QUERY_DELETE_PORTLET = " DELETE FROM link_portlet WHERE id_portlet= ? ";
74      private static final String SQL_QUERY_DELETE_LINK_FROM_PORTLET = " DELETE FROM link_list_portlet WHERE id_link= ? ";
75      private static final String SQL_QUERY_INSERT_INTO_PORTLET = " INSERT INTO link_portlet ( id_portlet, portlet_link_order ) VALUES ( ? , ? ) ";
76      private static final String SQL_QUERY_SELECT_PORTLET_ID = " SELECT id_portlet FROM link_portlet WHERE portlet_link_order = ? ";
77      private static final String SQL_QUERY_UPDATE_PORTLET_LINK = " UPDATE link_portlet SET portlet_link_order = ? WHERE id_portlet = ? ";
78      private static final String SQL_QUERY_SELECT_URL_LIST = " SELECT virtual_host_key, url FROM link_virtual_host WHERE id_link = ? ";
79  
80      ///////////////////////////////////////////////////////////////////////////////////////
81      //Access methods to data
82  
83      /**
84       * Insert a new record in the table.
85       *
86       * @param portlet The identifier of the portlet
87       */
88      public void insert( Portlet portlet )
89      {
90      }
91  
92      /**
93       * Insert a new record in the table.
94       *
95       * @param nPortletId The identifier of the portlet
96       * @param nLinkId The identifier of the link
97       * @param nOrder The order of the portlet to insert
98       */
99      public void insertLink( int nPortletId, int nLinkId, int nOrder )
100     {
101         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
102         {
103             daoUtil.setInt( 1, nPortletId );
104             daoUtil.setInt( 2, nLinkId );
105             daoUtil.setInt( 3, nOrder );
106     
107             daoUtil.executeUpdate(  );
108         }
109     }
110 
111     /**
112      * Remove a specified link from a specified portlet
113      *
114      * @param nPortletId The identifier of the portlet
115      * @param nLinkId The identifier of the link
116      */
117     public void deleteLink( int nPortletId, int nLinkId )
118     {
119         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK ) )
120         {
121             daoUtil.setInt( 1, nPortletId );
122             daoUtil.setInt( 2, nLinkId );
123     
124             daoUtil.executeUpdate(  );
125         }
126     }
127 
128     /**
129      * Delete a record from the table
130      * @param nPortletId The identifier of the portlet
131      */
132     public void delete( int nPortletId )
133     {
134         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
135         {
136             daoUtil.setInt( 1, nPortletId );
137             daoUtil.executeUpdate(  );
138         }
139 
140         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK_PORTLET ) )
141         {
142             daoUtil.setInt( 1, nPortletId );
143             daoUtil.executeUpdate(  );
144         }
145     }
146 
147     /**
148      * Gets the data from database
149      * @param nPortletId The identifier of the portlet
150      * @return portlet The instance of the object portlet
151      */
152     public Portlet load( int nPortletId )
153     {
154         LinksPortletnks/business/portlet/LinksPortlet.html#LinksPortlet">LinksPortlet portlet = new LinksPortlet(  );
155         
156         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
157         {
158             daoUtil.setInt( 1, nPortletId );
159             daoUtil.executeQuery(  );
160     
161             if ( daoUtil.next(  ) )
162             {
163                 portlet.setId( daoUtil.getInt( 1 ) );
164             }
165         }
166 
167         return portlet;
168     }
169 
170     /**
171      * Update the record in the table
172      * @param portlet The instance of the object portlet
173      */
174     public void store( Portlet portlet )
175     {
176     }
177 
178     /**
179      * Returns a list of all the links
180      * @return A list of links in form of a ReferenceList object
181      */
182     public ReferenceList selectLinksList(  )
183     {
184         ReferenceList list = new ReferenceList(  );
185         
186         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK ) )
187         {
188             daoUtil.executeQuery(  );
189     
190             while ( daoUtil.next(  ) )
191             {
192                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) + " " + daoUtil.getString( 3 ) );
193             }
194         }
195 
196         return list;
197     }
198 
199     /**
200      * Check if a specified links is not already registered in a specified portlet
201      * @param nPortletId The identifier of the portlet
202      * @param nLinkId The identifier of the link
203      * @return The result(boolean)
204      */
205     public boolean testDuplicate( int nPortletId, int nLinkId )
206     {
207         boolean bResult;
208         
209         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ID_LINK ) )
210         {    
211             daoUtil.setInt( 1, nPortletId );
212             daoUtil.setInt( 2, nLinkId );
213             daoUtil.executeQuery(  );
214     
215             bResult = daoUtil.next(  );
216         }
217 
218         return bResult;
219     }
220 
221     /**
222      * Return a list of links wich belong to a specified portlet
223      *
224      * @param nPortletId The identifier of the portlet
225      * @return A collection of links objects
226      */
227     public Collection<Link> selectLinksInPortletList( int nPortletId )
228     {
229         ArrayList<Link> list = new ArrayList<>(  );
230         
231         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK_IN_PORTLET_LIST ) )
232         {    
233             daoUtil.setInt( 1, nPortletId );
234             daoUtil.executeQuery(  );
235     
236             while ( daoUtil.next(  ) )
237             {
238                 Link/plugins/links/business/Link.html#Link">Link link = new Link(  );
239                 link.setId( daoUtil.getInt( 1 ) );
240                 link.setName( daoUtil.getString( 2 ) );
241                 link.setUrl( daoUtil.getString( 3 ) );
242                 link.setDescription( daoUtil.getString( 4 ) );
243                 link.setImageContent( daoUtil.getBytes( 5 ) );
244                 link.setMimeType( daoUtil.getString( 6 ) );
245                 link.setWorkgroupKey( daoUtil.getString( 7 ) );
246                 link.setOptionalUrls( this.selectUrlsList( link.getId(  ) ) );
247                 list.add( link );
248             }
249         }
250 
251         return list;
252     }
253 
254     /**
255      * Return the order of a specified link in a specified portlet
256      *
257      * @param nPortletId The identifier of the portlet
258      * @param nLinkId The identifier of the link
259      * @return The link's order
260      */
261     public int selectLinkOrder( int nPortletId, int nLinkId )
262     {
263         int nOrder = 0;
264         
265         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK_ORDER ) )
266         {
267             daoUtil.setInt( 1, nPortletId );
268             daoUtil.setInt( 2, nLinkId );
269             daoUtil.executeQuery(  );
270     
271             if ( daoUtil.next(  ) )
272             {
273                 nOrder = daoUtil.getInt( 1 );
274             }
275         }
276 
277         return nOrder;
278     }
279 
280     /**
281      * Calculate a new primary key to add a new link
282      * @param nPortletId The identifier of the portlet
283      * @return The new key.
284      */
285     public int selectMaxOrder( int nPortletId )
286     {
287         int nOrder = 0;
288         
289         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_ORDER ) )
290         {
291             daoUtil.setInt( 1, nPortletId );
292             daoUtil.executeQuery(  );
293     
294             if ( daoUtil.next(  ) )
295             {
296                 nOrder = daoUtil.getInt( 1 );
297             }
298         }
299 
300         return nOrder;
301     }
302 
303     /**
304      * Returns the maximum order of the portlets in the links page
305      * @return the max order
306      */
307     public int selectPortletMaxOrder(  )
308     {
309         int nOrder = 0;
310         
311         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_MAX_ORDER ) )
312         {
313             daoUtil.executeQuery(  );
314     
315             if ( daoUtil.next(  ) )
316             {
317                 nOrder = daoUtil.getInt( 1 );
318             }
319         }
320 
321         return nOrder;
322     }
323 
324     /**
325      * Update the order of a specified link in a specified portlet
326      *
327      * @param nPortletId The identifier of the portlet
328      * @param nLinkId The identifier of the link
329      * @param nOrder The new order
330      */
331     public void storeLinkOrder( int nOrder, int nPortletId, int nLinkId )
332     {
333         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_LINK_ORDER ) )
334         {
335             daoUtil.setInt( 1, nOrder );
336             daoUtil.setInt( 2, nPortletId );
337             daoUtil.setInt( 3, nLinkId );
338     
339             daoUtil.executeUpdate(  );
340         }
341     }
342 
343     /**
344      * Returns the id of an link wich has a specified order in a specified portlet
345      * @param nPortletId The identifier of the portlet
346      * @param nOrder The link's order
347      * @return The identifier of the link
348      */
349     public int selectLinkIdByOrder( int nPortletId, int nOrder )
350     {
351         int nResult = 0;
352         
353         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK_ID_BY_ORDER ) )
354         {    
355             daoUtil.setInt( 1, nPortletId );
356             daoUtil.setInt( 2, nOrder );
357             daoUtil.executeQuery(  );
358     
359             if ( daoUtil.next(  ) )
360             {
361                 nResult = daoUtil.getInt( 1 );
362             }
363         }
364 
365         return nResult;
366     }
367 
368     /**
369      * Finds the portlets which have not been selected in the links page
370      * @return the list of the unselected portlets
371      */
372     public ReferenceList findUnselectedPortlets(  )
373     {
374         ReferenceList list = new ReferenceList(  );
375         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_UNSELECTED_PORTLET ) )
376         {
377             String strPortletTypeId = LinksPortletHome.getInstance(  ).getPortletTypeId(  );
378     
379             daoUtil.setString( 1, strPortletTypeId );
380             daoUtil.executeQuery(  );
381     
382             while ( daoUtil.next(  ) )
383             {
384                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
385             }
386         }
387 
388         return list;
389     }
390 
391     /**
392      * Selects the list of the portlets in the links page
393      *
394      * @return a collection of the unselected portlets
395      */
396     public Collection<Portlet> selectPortletsInLinksPage(  )
397     {
398         ArrayList<Portlet> list = new ArrayList<>(  );
399         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_LINK_PAGE ) )
400         {
401             daoUtil.executeQuery(  );
402     
403             while ( daoUtil.next(  ) )
404             {
405                 LinksPortletnks/business/portlet/LinksPortlet.html#LinksPortlet">LinksPortlet portlet = new LinksPortlet(  );
406                 portlet.setId( daoUtil.getInt( 1 ) );
407                 portlet.setPortletOrder( daoUtil.getInt( 2 ) );
408                 portlet.setName( daoUtil.getString( 3 ) );
409                 list.add( portlet );
410             }
411         }
412 
413         return list;
414     }
415 
416     /**
417      * Selects the order of a portlet in the links page
418      * @param nPortletId The identifier of the portlet
419      * @return the order
420      */
421     public int selectPortletOrder( int nPortletId )
422     {
423         int nOrder = 0;
424         
425         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_LINK_ORDER ) )
426         {    
427             daoUtil.setInt( 1, nPortletId );
428             daoUtil.executeQuery(  );
429     
430             if ( daoUtil.next(  ) )
431             {
432                 nOrder = daoUtil.getInt( 1 );
433             }
434         }
435 
436         return nOrder;
437     }
438 
439     /**
440      * Remove a portlet from the links page
441      * @param nPortletId The identifier of the portlet
442      */
443     public void removePortlet( int nPortletId )
444     {
445         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_PORTLET ) )
446         {
447             daoUtil.setInt( 1, nPortletId );
448             daoUtil.executeUpdate(  );
449         }
450     }
451 
452     /**
453      * Removes a link from all the portlets
454      * @param nLinkId The identifier of the link
455      */
456     public void removeLinkFromPortlets( int nLinkId )
457     {
458         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK_FROM_PORTLET ) )
459         {
460             daoUtil.setInt( 1, nLinkId );
461             daoUtil.executeUpdate(  );
462         }
463     }
464 
465     /**
466      * Insert a new portlet in the links page
467      * @param nPortletId The identifier of the portlet
468      * @param nOrder The order of the portlet
469      */
470     public void insertPortlet( int nPortletId, int nOrder )
471     {
472         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_INTO_PORTLET ) )
473         {
474             daoUtil.setInt( 1, nPortletId );
475             daoUtil.setInt( 2, nOrder );
476             daoUtil.executeUpdate(  );
477         }
478     }
479 
480     /**
481      * Selects a portlet Id from the links page by its order
482      * @param nOrder The order of the portlet
483      * @return the portlet Id
484      */
485     public int selectPortletIdByOrder( int nOrder )
486     {
487         int nResult = 0;
488         
489         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_ID ) )
490         {
491             daoUtil.setInt( 1, nOrder );
492             daoUtil.executeQuery(  );
493     
494             if ( daoUtil.next(  ) )
495             {
496                 nResult = daoUtil.getInt( 1 );
497             }
498         }
499 
500         return nResult;
501     }
502 
503     /**
504      * Stores the order of a portlet in the links page
505     
506      * @param nOrder The order of the portlet
507      * @param nPortletId The identifier of the portlet
508      */
509     public void storePortletOrder( int nOrder, int nPortletId )
510     {
511         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_PORTLET_LINK ) )
512         {
513             daoUtil.setInt( 1, nOrder );
514             daoUtil.setInt( 2, nPortletId );
515             daoUtil.executeUpdate(  );
516         }
517     }
518 
519     /**
520      * load all the optional urls
521      *
522      * @param idLink the link's id
523      * @return the optional urls ReferenceList
524      */
525     private ReferenceList selectUrlsList( int idLink )
526     {
527         ReferenceList list = new ReferenceList(  );
528         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_URL_LIST ) )
529         {
530             daoUtil.setInt( 1, idLink );
531             daoUtil.executeQuery(  );
532     
533             while ( daoUtil.next(  ) )
534             {
535                 list.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
536             }
537         }
538 
539         return list;
540     }
541 }