View Javadoc

1   /*
2    * Copyright (c) 2002-2013, 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         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         daoUtil.free(  );
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         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK );
120 
121         daoUtil.setInt( 1, nPortletId );
122         daoUtil.setInt( 2, nLinkId );
123 
124         daoUtil.executeUpdate(  );
125         daoUtil.free(  );
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         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
135         daoUtil.setInt( 1, nPortletId );
136         daoUtil.executeUpdate(  );
137         daoUtil.free(  );
138 
139         daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK_PORTLET );
140         daoUtil.setInt( 1, nPortletId );
141         daoUtil.executeUpdate(  );
142         daoUtil.free(  );
143     }
144 
145     /**
146      * Gets the data from database
147      * @param nPortletId The identifier of the portlet
148      * @return portlet The instance of the object portlet
149      */
150     public Portlet load( int nPortletId )
151     {
152         LinksPortlet portlet = new LinksPortlet(  );
153         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
154 
155         daoUtil.setInt( 1, nPortletId );
156         daoUtil.executeQuery(  );
157 
158         if ( daoUtil.next(  ) )
159         {
160             portlet.setId( daoUtil.getInt( 1 ) );
161         }
162 
163         daoUtil.free(  );
164 
165         return portlet;
166     }
167 
168     /**
169      * Update the record in the table
170      * @param portlet The instance of the object portlet
171      */
172     public void store( Portlet portlet )
173     {
174     }
175 
176     /**
177      * Returns a list of all the links
178      * @return A list of links in form of a ReferenceList object
179      */
180     public ReferenceList selectLinksList(  )
181     {
182         ReferenceList list = new ReferenceList(  );
183         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK );
184         daoUtil.executeQuery(  );
185 
186         while ( daoUtil.next(  ) )
187         {
188             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) + " " + daoUtil.getString( 3 ) );
189         }
190 
191         daoUtil.free(  );
192 
193         return list;
194     }
195 
196     /**
197      * Check if a specified links is not already registered in a specified portlet
198      * @param nPortletId The identifier of the portlet
199      * @param nLinkId The identifier of the link
200      * @return The result(boolean)
201      */
202     public boolean testDuplicate( int nPortletId, int nLinkId )
203     {
204         boolean bResult;
205         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ID_LINK );
206 
207         daoUtil.setInt( 1, nPortletId );
208         daoUtil.setInt( 2, nLinkId );
209         daoUtil.executeQuery(  );
210 
211         bResult = daoUtil.next(  );
212         daoUtil.free(  );
213 
214         return bResult;
215     }
216 
217     /**
218      * Return a list of links wich belong to a specified portlet
219      *
220      * @param nPortletId The identifier of the portlet
221      * @return A collection of links objects
222      */
223     public Collection<Link> selectLinksInPortletList( int nPortletId )
224     {
225         ArrayList<Link> list = new ArrayList<Link>(  );
226         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK_IN_PORTLET_LIST );
227 
228         daoUtil.setInt( 1, nPortletId );
229         daoUtil.executeQuery(  );
230 
231         while ( daoUtil.next(  ) )
232         {
233             Link link = new Link(  );
234             link.setId( daoUtil.getInt( 1 ) );
235             link.setName( daoUtil.getString( 2 ) );
236             link.setUrl( daoUtil.getString( 3 ) );
237             link.setDescription( daoUtil.getString( 4 ) );
238             link.setImageContent( daoUtil.getBytes( 5 ) );
239             link.setMimeType( daoUtil.getString( 6 ) );
240             link.setWorkgroupKey( daoUtil.getString( 7 ) );
241             link.setOptionalUrls( this.selectUrlsList( link.getId(  ) ) );
242             list.add( link );
243         }
244 
245         daoUtil.free(  );
246 
247         return list;
248     }
249 
250     /**
251      * Return the order of a specified link in a specified portlet
252      *
253      * @param nPortletId The identifier of the portlet
254      * @param nLinkId The identifier of the link
255      * @return The link's order
256      */
257     public int selectLinkOrder( int nPortletId, int nLinkId )
258     {
259         int nOrder = 0;
260         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK_ORDER );
261         daoUtil.setInt( 1, nPortletId );
262         daoUtil.setInt( 2, nLinkId );
263         daoUtil.executeQuery(  );
264 
265         if ( daoUtil.next(  ) )
266         {
267             nOrder = daoUtil.getInt( 1 );
268         }
269 
270         daoUtil.free(  );
271 
272         return nOrder;
273     }
274 
275     /**
276      * Calculate a new primary key to add a new link
277      * @param nPortletId The identifier of the portlet
278      * @return The new key.
279      */
280     public int selectMaxOrder( int nPortletId )
281     {
282         int nOrder = 0;
283         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_ORDER );
284         daoUtil.setInt( 1, nPortletId );
285         daoUtil.executeQuery(  );
286 
287         if ( daoUtil.next(  ) )
288         {
289             nOrder = daoUtil.getInt( 1 );
290         }
291 
292         daoUtil.free(  );
293 
294         return nOrder;
295     }
296 
297     /**
298      * Returns the maximum order of the portlets in the links page
299      * @return the max order
300      */
301     public int selectPortletMaxOrder(  )
302     {
303         int nOrder = 0;
304         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_MAX_ORDER );
305         daoUtil.executeQuery(  );
306 
307         if ( daoUtil.next(  ) )
308         {
309             nOrder = daoUtil.getInt( 1 );
310         }
311 
312         daoUtil.free(  );
313 
314         return nOrder;
315     }
316 
317     /**
318      * Update the order of a specified link in a specified portlet
319      *
320      * @param nPortletId The identifier of the portlet
321      * @param nLinkId The identifier of the link
322      * @param nOrder The new order
323      */
324     public void storeLinkOrder( int nOrder, int nPortletId, int nLinkId )
325     {
326         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_LINK_ORDER );
327 
328         daoUtil.setInt( 1, nOrder );
329         daoUtil.setInt( 2, nPortletId );
330         daoUtil.setInt( 3, nLinkId );
331 
332         daoUtil.executeUpdate(  );
333 
334         daoUtil.free(  );
335     }
336 
337     /**
338      * Returns the id of an link wich has a specified order in a specified portlet
339      * @param nPortletId The identifier of the portlet
340      * @param nOrder The link's order
341      * @return The identifier of the link
342      */
343     public int selectLinkIdByOrder( int nPortletId, int nOrder )
344     {
345         int nResult = 0;
346         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LINK_ID_BY_ORDER );
347 
348         daoUtil.setInt( 1, nPortletId );
349         daoUtil.setInt( 2, nOrder );
350         daoUtil.executeQuery(  );
351 
352         if ( daoUtil.next(  ) )
353         {
354             nResult = daoUtil.getInt( 1 );
355         }
356 
357         daoUtil.free(  );
358 
359         return nResult;
360     }
361 
362     /**
363      * Finds the portlets which have not been selected in the links page
364      * @return the list of the unselected portlets
365      */
366     public ReferenceList findUnselectedPortlets(  )
367     {
368         ReferenceList list = new ReferenceList(  );
369         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_UNSELECTED_PORTLET );
370         String strPortletTypeId = LinksPortletHome.getInstance(  ).getPortletTypeId(  );
371 
372         daoUtil.setString( 1, strPortletTypeId );
373         daoUtil.executeQuery(  );
374 
375         while ( daoUtil.next(  ) )
376         {
377             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
378         }
379 
380         daoUtil.free(  );
381 
382         return list;
383     }
384 
385     /**
386      * Selects the list of the portlets in the links page
387      *
388      * @return a collection of the unselected portlets
389      */
390     public Collection<Portlet> selectPortletsInLinksPage(  )
391     {
392         ArrayList<Portlet> list = new ArrayList<Portlet>(  );
393         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_LINK_PAGE );
394         daoUtil.executeQuery(  );
395 
396         while ( daoUtil.next(  ) )
397         {
398             LinksPortlet portlet = new LinksPortlet(  );
399             portlet.setId( daoUtil.getInt( 1 ) );
400             portlet.setPortletOrder( daoUtil.getInt( 2 ) );
401             portlet.setName( daoUtil.getString( 3 ) );
402             list.add( portlet );
403         }
404 
405         daoUtil.free(  );
406 
407         return list;
408     }
409 
410     /**
411      * Selects the order of a portlet in the links page
412      * @param nPortletId The identifier of the portlet
413      * @return the order
414      */
415     public int selectPortletOrder( int nPortletId )
416     {
417         int nOrder = 0;
418         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_LINK_ORDER );
419 
420         daoUtil.setInt( 1, nPortletId );
421         daoUtil.executeQuery(  );
422 
423         if ( daoUtil.next(  ) )
424         {
425             nOrder = daoUtil.getInt( 1 );
426         }
427 
428         daoUtil.free(  );
429 
430         return nOrder;
431     }
432 
433     /**
434      * Remove a portlet from the links page
435      * @param nPortletId The identifier of the portlet
436      */
437     public void removePortlet( int nPortletId )
438     {
439         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_PORTLET );
440         daoUtil.setInt( 1, nPortletId );
441         daoUtil.executeUpdate(  );
442 
443         daoUtil.free(  );
444     }
445 
446     /**
447      * Removes a link from all the portlets
448      * @param nLinkId The identifier of the link
449      */
450     public void removeLinkFromPortlets( int nLinkId )
451     {
452         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK_FROM_PORTLET );
453         daoUtil.setInt( 1, nLinkId );
454         daoUtil.executeUpdate(  );
455 
456         daoUtil.free(  );
457     }
458 
459     /**
460      * Insert a new portlet in the links page
461      * @param nPortletId The identifier of the portlet
462      * @param nOrder The order of the portlet
463      */
464     public void insertPortlet( int nPortletId, int nOrder )
465     {
466         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_INTO_PORTLET );
467         daoUtil.setInt( 1, nPortletId );
468         daoUtil.setInt( 2, nOrder );
469         daoUtil.executeUpdate(  );
470 
471         daoUtil.free(  );
472     }
473 
474     /**
475      * Selects a portlet Id from the links page by its order
476      * @param nOrder The order of the portlet
477      * @return the portlet Id
478      */
479     public int selectPortletIdByOrder( int nOrder )
480     {
481         int nResult = 0;
482         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_ID );
483         daoUtil.setInt( 1, nOrder );
484         daoUtil.executeQuery(  );
485 
486         if ( daoUtil.next(  ) )
487         {
488             nResult = daoUtil.getInt( 1 );
489         }
490 
491         daoUtil.free(  );
492 
493         return nResult;
494     }
495 
496     /**
497      * Stores the order of a portlet in the links page
498     
499      * @param nOrder The order of the portlet
500      * @param nPortletId The identifier of the portlet
501      */
502     public void storePortletOrder( int nOrder, int nPortletId )
503     {
504         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_PORTLET_LINK );
505         daoUtil.setInt( 1, nOrder );
506         daoUtil.setInt( 2, nPortletId );
507         daoUtil.executeUpdate(  );
508 
509         daoUtil.free(  );
510     }
511 
512     /**
513      * load all the optional urls
514      *
515      * @param idLink the link's id
516      * @return the optional urls ReferenceList
517      */
518     private ReferenceList selectUrlsList( int idLink )
519     {
520         ReferenceList list = new ReferenceList(  );
521         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_URL_LIST );
522         daoUtil.setInt( 1, idLink );
523         daoUtil.executeQuery(  );
524 
525         while ( daoUtil.next(  ) )
526         {
527             list.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
528         }
529 
530         daoUtil.free(  );
531 
532         return list;
533     }
534 }