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.forms.modules.template.business;
35
36 import java.sql.Statement;
37
38 import org.apache.commons.lang3.StringUtils;
39
40 import fr.paris.lutece.plugins.genericattributes.business.Entry;
41 import fr.paris.lutece.plugins.genericattributes.business.EntryType;
42 import fr.paris.lutece.portal.service.plugin.Plugin;
43 import fr.paris.lutece.util.sql.DAOUtil;
44
45
46
47
48 public final class TemplateEntryDAO implements ITemplateEntryDAO
49 {
50
51 private static final String SQL_QUERY_SELECT_LIST = "ent.id_type,typ.title,typ.is_group,typ.is_comment,typ.class_name,typ.is_mylutece_user,typ.icon_name,"
52 + "ent.id_entry,ent.code,ent.title,ent.help_message, ent.comment,ent.mandatory,ent.fields_in_line,"
53 + "ent.pos,ent.field_unique, ent.css_class, ent.pos_conditional, ent.error_message, " + "ent.is_only_display_back, ent.is_indexed ";
54 private static final String SQL_QUERY_SELECT_ENTRY_ATTRIBUTES = "SELECT " + SQL_QUERY_SELECT_LIST
55 + "FROM template_entry ent,genatt_entry_type typ WHERE ent.id_type=typ.id_type ";
56 private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES + " AND ent.id_entry = ? ";
57 private static final String SQL_QUERY_INSERT = "INSERT INTO template_entry ( id_type,code,title,help_message, comment,mandatory,fields_in_line,"
58 + "pos,field_unique,css_class, pos_conditional, error_message, is_only_display_back, is_indexed ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
59 private static final String SQL_QUERY_DELETE = "DELETE FROM template_entry WHERE id_entry = ? ";
60 private static final String SQL_QUERY_UPDATE = "UPDATE template_entry SET id_entry=?,id_type=?,code=?,title=?,help_message=?,"
61 + "comment=?,mandatory=?, fields_in_line=?,pos=?,field_unique=?,css_class=?, pos_conditional=?, "
62 + "error_message=?, is_only_display_back = ?, is_indexed = ? WHERE id_entry=?";
63 private static final int CONSTANT_ZERO = 0;
64
65
66
67
68 @Override
69 public int insert( Entry entry, Plugin plugin )
70 {
71 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
72 {
73 int nIndex = 1;
74 daoUtil.setInt( nIndex++, entry.getEntryType( ).getIdType( ) );
75 daoUtil.setString( nIndex++, entry.getCode( ) );
76 daoUtil.setString( nIndex++, trimEntryTitle( entry ) );
77 daoUtil.setString( nIndex++, entry.getHelpMessage( ) );
78 daoUtil.setString( nIndex++, entry.getComment( ) );
79 daoUtil.setBoolean( nIndex++, entry.isMandatory( ) );
80 daoUtil.setBoolean( nIndex++, entry.isFieldInLine( ) );
81 daoUtil.setInt( nIndex++, CONSTANT_ZERO );
82 daoUtil.setBoolean( nIndex++, entry.isUnique( ) );
83 daoUtil.setString( nIndex++, ( entry.getCSSClass( ) == null ) ? StringUtils.EMPTY : entry.getCSSClass( ) );
84 daoUtil.setInt( nIndex++, CONSTANT_ZERO );
85 daoUtil.setString( nIndex++, entry.getErrorMessage( ) );
86 daoUtil.setBoolean( nIndex++, entry.isOnlyDisplayInBack( ) );
87 daoUtil.setBoolean( nIndex++, entry.isIndexed( ) );
88
89 daoUtil.executeUpdate( );
90
91 if ( daoUtil.nextGeneratedKey( ) )
92 {
93 entry.setIdEntry( daoUtil.getGeneratedKeyInt( 1 ) );
94 }
95 }
96
97 return entry.getIdEntry( );
98 }
99
100
101
102
103 @Override
104 public Entry load( int nId, Plugin plugin )
105 {
106 Entry entry = null;
107 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
108 {
109 daoUtil.setInt( 1, nId );
110 daoUtil.executeQuery( );
111
112 if ( daoUtil.next( ) )
113 {
114 entry = getEntryValues( daoUtil );
115 }
116
117 }
118 return entry;
119 }
120
121
122
123
124 @Override
125 public void delete( int nIdEntry, Plugin plugin )
126 {
127 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
128 {
129 daoUtil.setInt( 1, nIdEntry );
130 daoUtil.executeUpdate( );
131 }
132 }
133
134
135
136
137 @Override
138 public void store( Entry entry, Plugin plugin )
139 {
140 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
141 {
142 int nIndex = 1;
143 daoUtil.setInt( nIndex++, entry.getIdEntry( ) );
144 daoUtil.setInt( nIndex++, entry.getEntryType( ).getIdType( ) );
145 daoUtil.setString( nIndex++, entry.getCode( ) );
146 daoUtil.setString( nIndex++, trimEntryTitle( entry ) );
147 daoUtil.setString( nIndex++, entry.getHelpMessage( ) );
148 daoUtil.setString( nIndex++, entry.getComment( ) );
149 daoUtil.setBoolean( nIndex++, entry.isMandatory( ) );
150 daoUtil.setBoolean( nIndex++, entry.isFieldInLine( ) );
151 daoUtil.setInt( nIndex++, CONSTANT_ZERO );
152 daoUtil.setBoolean( nIndex++, entry.isUnique( ) );
153 daoUtil.setString( nIndex++, ( entry.getCSSClass( ) == null ) ? StringUtils.EMPTY : entry.getCSSClass( ) );
154 daoUtil.setInt( nIndex++, CONSTANT_ZERO );
155 daoUtil.setString( nIndex++, entry.getErrorMessage( ) );
156 daoUtil.setBoolean( nIndex++, entry.isOnlyDisplayInBack( ) );
157 daoUtil.setBoolean( nIndex++, entry.isIndexed( ) );
158
159 daoUtil.setInt( nIndex++, entry.getIdEntry( ) );
160
161 daoUtil.executeUpdate( );
162 }
163 }
164
165
166
167
168
169
170
171
172 private Entry getEntryValues( DAOUtil daoUtil )
173 {
174 Entry entry = new Entry( );
175
176 int nIndex = 1;
177 EntryType entryType = new EntryType( );
178 entryType.setIdType( daoUtil.getInt( nIndex++ ) );
179 entryType.setTitle( daoUtil.getString( nIndex++ ) );
180 entryType.setGroup( daoUtil.getBoolean( nIndex++ ) );
181 entryType.setComment( daoUtil.getBoolean( nIndex++ ) );
182 entryType.setBeanName( daoUtil.getString( nIndex++ ) );
183 entryType.setMyLuteceUser( daoUtil.getBoolean( nIndex++ ) );
184 entryType.setIconName( daoUtil.getString( nIndex++ ) );
185 entry.setEntryType( entryType );
186 entry.setIdEntry( daoUtil.getInt( nIndex++ ) );
187
188 entry.setCode( daoUtil.getString( nIndex++ ) );
189 entry.setTitle( daoUtil.getString( nIndex++ ) );
190 entry.setHelpMessage( daoUtil.getString( nIndex++ ) );
191 entry.setComment( daoUtil.getString( nIndex++ ) );
192 entry.setMandatory( daoUtil.getBoolean( nIndex++ ) );
193 entry.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
194 entry.setPosition( daoUtil.getInt( nIndex++ ) );
195 entry.setUnique( daoUtil.getBoolean( nIndex++ ) );
196 entry.setCSSClass( daoUtil.getString( nIndex++ ) );
197
198 if ( daoUtil.getInt( nIndex++ ) > 0 )
199 {
200 entry.setPosition( daoUtil.getInt( nIndex - 1 ) );
201 }
202
203 entry.setErrorMessage( daoUtil.getString( nIndex++ ) );
204 entry.setOnlyDisplayInBack( daoUtil.getBoolean( nIndex++ ) );
205 entry.setIndexed( daoUtil.getBoolean( nIndex ) );
206
207 return entry;
208 }
209
210
211
212
213
214
215
216
217 private String trimEntryTitle( Entry entry )
218 {
219 String strEntryTitle = entry.getTitle( );
220
221 if ( strEntryTitle != null )
222 {
223 strEntryTitle = strEntryTitle.trim( );
224 }
225
226 return strEntryTitle;
227 }
228 }