1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
52
53 public class ComplementaryDataLinkDAO implements IComplementaryDataLinkDAO, Serializable
54 {
55
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
68
69
70
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
86
87
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
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 }