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.plugins.dila.business.fichelocale.dao.impl;
35  
36  import fr.paris.lutece.plugins.dila.business.fichelocale.dao.ILocalFolderLinkDAO;
37  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalFolderLinkDTO;
38  import fr.paris.lutece.plugins.dila.service.DilaPlugin;
39  import fr.paris.lutece.portal.service.plugin.PluginService;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  import java.io.Serializable;
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  
47  /**
48   * Implementation of {@link ILocalFolderLinkDAO}
49   */
50  public class LocalFolderLinkDAO implements ILocalFolderLinkDAO, Serializable
51  {
52      /** Serial ID */
53      private static final long serialVersionUID = 3635002841395718887L;
54      private static final String SQL_QUERY_NEW_PK = "SELECT max(id) FROM dila_local_folder_link";
55      private static final String SQL_QUERY_INSERT = " INSERT INTO dila_local_folder_link "
56              + "( id, title, position, card_id, local_folder_id ) VALUES ( ?, ? ,?, ?, ?)";
57      private static final String SQL_QUERY_DELETE_BY_FOLDER_ID = " DELETE FROM dila_local_folder_link "
58              + "WHERE local_folder_id = ?";
59      private static final String SQL_QUERY_SELECT_BY_FOLDER_ID = " SELECT title, position, card_id FROM dila_local_folder_link "
60              + "WHERE local_folder_id = ? ORDER BY position";
61  
62      /**
63       * Generates a new primary key
64       * @return The new primary key
65       */
66      private Long newPrimaryKey( )
67      {
68          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
69          daoUtil.executeQuery( );
70  
71          Long nKey = 1L;
72  
73          if ( daoUtil.next( ) )
74          {
75              // if the table is empty
76              nKey = daoUtil.getLong( 1 ) + 1L;
77          }
78  
79          daoUtil.free( );
80  
81          return nKey;
82      }
83  
84      @Override
85      public void insert( LocalFolderLinkDTO link )
86      {
87          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
88  
89          link.setId( newPrimaryKey( ) );
90  
91          daoUtil.setLong( 1, link.getId( ) );
92          daoUtil.setString( 2, link.getTitle( ) );
93          daoUtil.setInt( 3, link.getPosition( ) );
94          daoUtil.setString( 4, link.getCardId( ) );
95          daoUtil.setLong( 5, link.getLocalFolderId( ) );
96  
97          daoUtil.executeUpdate( );
98          daoUtil.free( );
99      }
100 
101     @Override
102     public void deleteByFolderId( Long localFolderId )
103     {
104         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_FOLDER_ID, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
105         daoUtil.setLong( 1, localFolderId );
106         daoUtil.executeUpdate( );
107         daoUtil.free( );
108     }
109 
110     @Override
111     public List<LocalFolderLinkDTO> findByFolderId( Long id )
112     {
113         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_FOLDER_ID, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
114 
115         daoUtil.setLong( 1, id );
116         daoUtil.executeQuery( );
117 
118         List<LocalFolderLinkDTO> result = new ArrayList<LocalFolderLinkDTO>( );
119 
120         while ( daoUtil.next( ) )
121         {
122             LocalFolderLinkDTO link = new LocalFolderLinkDTO( );
123 
124             link.setTitle( daoUtil.getString( 1 ) );
125             link.setPosition( daoUtil.getInt( 2 ) );
126             link.setCardId( daoUtil.getString( 3 ) );
127 
128             result.add( link );
129         }
130 
131         daoUtil.free( );
132 
133         return result;
134     }
135 }