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.portal.business.style;
35
36 import fr.paris.lutece.util.sql.DAOUtil;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41
42
43
44
45 public final class PageTemplateDAO implements IPageTemplateDAO
46 {
47
48 private static final String SQL_QUERY_NEW_PK = " SELECT max( id_template ) FROM core_page_template";
49 private static final String SQL_QUERY_SELECT = " SELECT id_template, description, file_name, picture FROM core_page_template WHERE id_template = ?";
50 private static final String SQL_QUERY_INSERT = " INSERT INTO core_page_template ( id_template, description, file_name, picture ) VALUES ( ?, ?, ?, ? )";
51 private static final String SQL_QUERY_DELETE = " DELETE FROM core_page_template WHERE id_template = ?";
52 private static final String SQL_QUERY_UPDATE = " UPDATE core_page_template SET id_template = ?, description = ?, file_name = ?, picture = ? " +
53 " WHERE id_template = ?";
54 private static final String SQL_QUERY_SELECTALL = " SELECT id_template , description, file_name, picture FROM core_page_template ORDER BY id_template ";
55 private static final String SQL_CHECK_PAGE_TEMPLATE_IS_USED = " SELECT id_template FROM core_page WHERE id_template = ? ";
56
57
58
59
60
61
62
63
64 int newPrimaryKey( )
65 {
66 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
67 daoUtil.executeQuery( );
68
69 int nKey;
70
71 if ( !daoUtil.next( ) )
72 {
73
74 nKey = 1;
75 }
76
77 nKey = daoUtil.getInt( 1 ) + 1;
78
79 daoUtil.free( );
80
81 return nKey;
82 }
83
84
85
86
87
88 public synchronized void insert( PageTemplate pageTemplate )
89 {
90 pageTemplate.setId( newPrimaryKey( ) );
91
92 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
93
94 daoUtil.setInt( 1, pageTemplate.getId( ) );
95 daoUtil.setString( 2, pageTemplate.getDescription( ) );
96 daoUtil.setString( 3, pageTemplate.getFile( ) );
97 daoUtil.setString( 4, pageTemplate.getPicture( ) );
98
99 daoUtil.executeUpdate( );
100 daoUtil.free( );
101 }
102
103
104
105
106
107
108
109 public PageTemplate load( int nPageTemplateId )
110 {
111 PageTemplate pageTemplate = null;
112 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
113 daoUtil.setInt( 1, nPageTemplateId );
114
115 daoUtil.executeQuery( );
116
117 if ( daoUtil.next( ) )
118 {
119 pageTemplate = new PageTemplate( );
120 pageTemplate.setId( daoUtil.getInt( 1 ) );
121 pageTemplate.setDescription( daoUtil.getString( 2 ) );
122 pageTemplate.setFile( daoUtil.getString( 3 ) );
123 pageTemplate.setPicture( daoUtil.getString( 4 ) );
124 }
125
126 daoUtil.free( );
127
128 return pageTemplate;
129 }
130
131
132
133
134
135 public void delete( int nPageTemplateId )
136 {
137 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
138 daoUtil.setInt( 1, nPageTemplateId );
139 daoUtil.executeUpdate( );
140 daoUtil.free( );
141 }
142
143
144
145
146
147 public void store( PageTemplate pageTemplate )
148 {
149 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
150
151 daoUtil.setInt( 1, pageTemplate.getId( ) );
152 daoUtil.setString( 2, pageTemplate.getDescription( ) );
153 daoUtil.setString( 3, pageTemplate.getFile( ) );
154 daoUtil.setString( 4, pageTemplate.getPicture( ) );
155 daoUtil.setInt( 5, pageTemplate.getId( ) );
156
157 daoUtil.executeUpdate( );
158 daoUtil.free( );
159 }
160
161
162
163
164
165 public List<PageTemplate> selectPageTemplatesList( )
166 {
167 List<PageTemplate> listPageTemplates = new ArrayList<PageTemplate>( );
168 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
169 daoUtil.executeQuery( );
170
171 while ( daoUtil.next( ) )
172 {
173 PageTemplate pageTemplate = new PageTemplate( );
174
175 pageTemplate.setId( daoUtil.getInt( 1 ) );
176 pageTemplate.setDescription( daoUtil.getString( 2 ) );
177 pageTemplate.setFile( daoUtil.getString( 3 ) );
178 pageTemplate.setPicture( daoUtil.getString( 4 ) );
179 listPageTemplates.add( pageTemplate );
180 }
181
182 daoUtil.free( );
183
184 return listPageTemplates;
185 }
186
187
188
189
190
191
192 public boolean checkPageTemplateIsUsed( int nPageTemplateId )
193 {
194 DAOUtil daoUtil = new DAOUtil( SQL_CHECK_PAGE_TEMPLATE_IS_USED );
195
196 daoUtil.setInt( 1, nPageTemplateId );
197 daoUtil.executeQuery( );
198
199 if ( !daoUtil.next( ) )
200 {
201 daoUtil.free( );
202
203 return true;
204 }
205
206 daoUtil.free( );
207
208 return false;
209 }
210 }