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.donneescomplementaires.dao.impl;
35  
36  import fr.paris.lutece.plugins.dila.business.donneescomplementaires.dao.IComplementaryDataLinkDAO;
37  import fr.paris.lutece.plugins.dila.business.donneescomplementaires.dto.ComplementaryDataLearnMoreDTO;
38  import fr.paris.lutece.plugins.dila.business.donneescomplementaires.dto.ComplementaryDataLinkDTO;
39  import fr.paris.lutece.plugins.dila.business.donneescomplementaires.dto.ComplementaryDataTeleserviceDTO;
40  import fr.paris.lutece.plugins.dila.business.enums.ComplementaryLinkTypeEnum;
41  import fr.paris.lutece.plugins.dila.service.DilaPlugin;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  import java.io.Serializable;
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  
50  /**
51   * Implementation of {@link IComplementaryDataLinkDAO}
52   */
53  public class ComplementaryDataLinkDAO implements IComplementaryDataLinkDAO, Serializable
54  {
55      /** Serial ID */
56      private static final long serialVersionUID = 3317028742153081496L;
57      private static final String TELESERVICE_TABLE = "dila_complementary_data_teleservice";
58      private static final String LINK_TABLE = "dila_complementary_data_learn_more";
59      private static final String SQL_QUERY_NEW_PK = "SELECT max(id) FROM <%TABLE%>";
60      private static final String SQL_QUERY_SELECT_BY_ID = "SELECT id, title, url, position"
61              + " FROM <%TABLE%> WHERE complementary_data_id = ? ORDER BY position ASC";
62      private static final String SQL_QUERY_INSERT = " INSERT INTO <%TABLE%> "
63              + "( id, title, url, position, complementary_data_id ) VALUES (?, ?, ?, ?, ? )";
64      private static final String SQL_QUERY_DELETE_BY_ID = " DELETE FROM <%TABLE%> " + "WHERE complementary_data_id = ?";
65  
66      /**
67       * Generate query
68       * @param strQuery the query to generate
69       * @param type the type of dto
70       * @return the formatted query
71       */
72      private String getQuery( String strQuery, ComplementaryLinkTypeEnum type )
73      {
74          if ( type.equals( ComplementaryLinkTypeEnum.LEARN_MORE ) )
75          {
76              return strQuery.replace( "<%TABLE%>", LINK_TABLE );
77          }
78          else
79          {
80              return strQuery.replace( "<%TABLE%>", TELESERVICE_TABLE );
81          }
82      }
83  
84      /**
85       * Generates a new primary key
86       * @param type the type of dto to add
87       * @return The new primary key
88       */
89      private Long newPrimaryKey( ComplementaryLinkTypeEnum type )
90      {
91          DAOUtil daoUtil = new DAOUtil( getQuery( SQL_QUERY_NEW_PK, type ),
92                  PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
93          daoUtil.executeQuery( );
94  
95          Long nKey = 1L;
96  
97          if ( daoUtil.next( ) )
98          {
99              // if the table is empty
100             nKey = daoUtil.getLong( 1 ) + 1L;
101         }
102 
103         daoUtil.free( );
104 
105         return nKey;
106     }
107 
108     @Override
109     public void insert( ComplementaryDataLinkDTO dto )
110     {
111         DAOUtil daoUtil = new DAOUtil( getQuery( SQL_QUERY_INSERT, dto.getType( ) ),
112                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
113 
114         dto.setId( newPrimaryKey( dto.getType( ) ) );
115 
116         daoUtil.setLong( 1, dto.getId( ) );
117         daoUtil.setString( 2, dto.getTitle( ) );
118         daoUtil.setString( 3, dto.getURL( ) );
119         daoUtil.setInt( 4, dto.getPosition( ) );
120         daoUtil.setLong( 5, dto.getIdComplementaryData( ) );
121 
122         daoUtil.executeUpdate( );
123         daoUtil.free( );
124     }
125 
126     @Override
127     public void delete( Long id, ComplementaryLinkTypeEnum type )
128     {
129         DAOUtil daoUtil = new DAOUtil( getQuery( SQL_QUERY_DELETE_BY_ID, type ),
130                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
131 
132         daoUtil.setLong( 1, id );
133         daoUtil.executeUpdate( );
134         daoUtil.free( );
135     }
136 
137     @Override
138     public List<ComplementaryDataLinkDTO> findByDataId( Long idDonneeComplementaire, ComplementaryLinkTypeEnum type )
139     {
140         DAOUtil daoUtil = new DAOUtil( getQuery( SQL_QUERY_SELECT_BY_ID, type ),
141                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
142 
143         daoUtil.setLong( 1, idDonneeComplementaire );
144 
145         daoUtil.executeQuery( );
146 
147         List<ComplementaryDataLinkDTO> result = new ArrayList<ComplementaryDataLinkDTO>( );
148 
149         while ( daoUtil.next( ) )
150         {
151             ComplementaryDataLinkDTO complement = null;
152 
153             if ( type.equals( ComplementaryLinkTypeEnum.TELESERVICE ) )
154             {
155                 complement = new ComplementaryDataTeleserviceDTO( );
156             }
157             else
158             {
159                 complement = new ComplementaryDataLearnMoreDTO( );
160             }
161 
162             complement.setId( daoUtil.getLong( 1 ) );
163             complement.setTitle( daoUtil.getString( 2 ) );
164             complement.setURL( daoUtil.getString( 3 ) );
165             complement.setPosition( daoUtil.getInt( 4 ) );
166             complement.setIdComplementaryData( idDonneeComplementaire );
167 
168             result.add( complement );
169         }
170 
171         daoUtil.free( );
172 
173         return result;
174     }
175 }