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.phraseanet.business.template;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.util.sql.DAOUtil;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42
43
44
45
46 public final class TemplateDAO implements ITemplateDAO
47 {
48
49 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_template ) FROM phraseanet_template";
50 private static final String SQL_QUERY_SELECT = "SELECT id_template, name, default_template, media_type FROM phraseanet_template WHERE media_type = ?";
51 private static final String SQL_QUERY_INSERT = "INSERT INTO phraseanet_template ( id_template, name, default_template, media_type ) VALUES ( ?, ?, ?, ? ) ";
52 private static final String SQL_QUERY_DELETE = "DELETE FROM phraseanet_template WHERE id_template = ? ";
53 private static final String SQL_QUERY_UPDATE = "UPDATE phraseanet_template SET name = ?, default_template = ? WHERE id_template = ?";
54 private static final String SQL_QUERY_SELECTALL = "SELECT id_template, name, default_template, media_type FROM phraseanet_template";
55
56
57
58
59
60
61 public int newPrimaryKey( Plugin plugin )
62 {
63 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
64 daoUtil.executeQuery( );
65
66 int nKey;
67
68 if ( !daoUtil.next( ) )
69 {
70
71 nKey = 1;
72 }
73
74 nKey = daoUtil.getInt( 1 ) + 1;
75 daoUtil.free( );
76
77 return nKey;
78 }
79
80
81
82
83
84
85 public void insert( Template template, Plugin plugin )
86 {
87 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
88
89 template.setId( newPrimaryKey( plugin ) );
90
91 daoUtil.setInt( 1, template.getId( ) );
92 daoUtil.setString( 2, template.getName( ) );
93 daoUtil.setString( 3, template.getTemplate( ) );
94
95 daoUtil.executeUpdate( );
96 daoUtil.free( );
97 }
98
99
100
101
102
103
104
105 public Template load( String strMediaType, Plugin plugin )
106 {
107 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
108 daoUtil.setString( 1, strMediaType );
109 daoUtil.executeQuery( );
110
111 Template template = null;
112
113 if ( daoUtil.next( ) )
114 {
115 template = new Template( );
116
117 template.setId( daoUtil.getInt( 1 ) );
118 template.setName( daoUtil.getString( 2 ) );
119 template.setTemplate( daoUtil.getString( 3 ) );
120 template.setMediaType( daoUtil.getString( 4 ) );
121 }
122
123 daoUtil.free( );
124
125 return template;
126 }
127
128
129
130
131
132
133 public void delete( int nTemplateId, Plugin plugin )
134 {
135 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
136 daoUtil.setInt( 1, nTemplateId );
137 daoUtil.executeUpdate( );
138 daoUtil.free( );
139 }
140
141
142
143
144
145
146 public void store( Template template, Plugin plugin )
147 {
148 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
149
150 daoUtil.setString( 1, template.getName( ) );
151 daoUtil.setString( 2, template.getTemplate( ) );
152 daoUtil.setInt( 3, template.getId( ) );
153
154 daoUtil.executeUpdate( );
155 daoUtil.free( );
156 }
157
158
159
160
161
162
163 public List<Template> selectTemplatesList( Plugin plugin )
164 {
165 List<Template> templateList = new ArrayList<Template>( );
166 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
167 daoUtil.executeQuery( );
168
169 while ( daoUtil.next( ) )
170 {
171 Template template = new Template( );
172
173 template.setId( daoUtil.getInt( 1 ) );
174 template.setName( daoUtil.getString( 2 ) );
175 template.setTemplate( daoUtil.getString( 3 ) );
176
177 templateList.add( template );
178 }
179
180 daoUtil.free( );
181
182 return templateList;
183 }
184 }