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.ILocalCardChapterDAO;
37  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalCardChapterDTO;
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 IChapitreFicheLocaleDAO
49   */
50  public class LocalCardChapterDAO implements ILocalCardChapterDAO, Serializable
51  {
52      /** Serial ID */
53      private static final long serialVersionUID = 1871080044687399057L;
54      private static final String SQL_QUERY_NEW_PK = "SELECT max(id) FROM dila_local_card_chapter";
55      private static final String SQL_QUERY_INSERT = " INSERT INTO dila_local_card_chapter "
56              + "( id, title, content, position, local_card_id ) VALUES ( ?, ? ,?, ?, ?)";
57      private static final String SQL_QUERY_DELETE_BY_CARD_ID = " DELETE FROM dila_local_card_chapter "
58              + "WHERE local_card_id = ?";
59      private static final String SQL_QUERY_FIND_BY_CARD_ID = " SELECT id, title, content, position from dila_local_card_chapter WHERE local_card_id = ? ORDER BY id ASC";
60  
61      /**
62       * Generates a new primary key
63       * @return The new primary key
64       */
65      private Long newPrimaryKey( )
66      {
67          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
68          daoUtil.executeQuery( );
69  
70          Long nKey = 1L;
71  
72          if ( daoUtil.next( ) )
73          {
74              nKey = daoUtil.getLong( 1 ) + 1L;
75          }
76  
77          daoUtil.free( );
78  
79          return nKey;
80      }
81  
82      @Override
83      public void create( LocalCardChapterDTO chapter )
84      {
85          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
86  
87          chapter.setLocalCardChapterId( newPrimaryKey( ) );
88  
89          daoUtil.setLong( 1, chapter.getLocalCardChapterId( ) );
90          daoUtil.setString( 2, chapter.getTitle( ) );
91          daoUtil.setString( 3, chapter.getContent( ) );
92          daoUtil.setInt( 4, chapter.getPosition( ) );
93          daoUtil.setLong( 5, chapter.getLocalCard( ).getId( ) );
94  
95          daoUtil.executeUpdate( );
96          daoUtil.free( );
97      }
98  
99      @Override
100     public void deleteByCardId( Long ficheId )
101     {
102         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_CARD_ID, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
103 
104         daoUtil.setLong( 1, ficheId );
105 
106         daoUtil.executeUpdate( );
107         daoUtil.free( );
108     }
109 
110     @Override
111     public List<LocalCardChapterDTO> findByCardId( Long cardId )
112     {
113         List<LocalCardChapterDTO> results = new ArrayList<LocalCardChapterDTO>( );
114         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_CARD_ID, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
115 
116         daoUtil.setLong( 1, cardId );
117         daoUtil.executeQuery( );
118 
119         while ( daoUtil.next( ) )
120         {
121             LocalCardChapterDTO chapter = new LocalCardChapterDTO( );
122 
123             chapter.setLocalCardChapterId( daoUtil.getLong( 1 ) );
124             chapter.setTitle( daoUtil.getString( 2 ) );
125             chapter.setContent( daoUtil.getString( 3 ) );
126             chapter.setPosition( daoUtil.getInt( 4 ) );
127 
128             results.add( chapter );
129         }
130 
131         daoUtil.free( );
132 
133         return results;
134     }
135 }