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.document.web.portlet;
35
36 import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
37 import fr.paris.lutece.plugins.document.business.portlet.DocumentPortlet;
38 import fr.paris.lutece.plugins.document.business.portlet.DocumentPortletHome;
39 import fr.paris.lutece.plugins.document.service.category.CategoryService;
40 import fr.paris.lutece.plugins.document.service.category.CategoryService.CategoryDisplay;
41 import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
42 import fr.paris.lutece.plugins.document.utils.IntegerUtils;
43 import fr.paris.lutece.portal.business.portlet.PortletHome;
44 import fr.paris.lutece.portal.business.user.AdminUser;
45 import fr.paris.lutece.portal.service.message.AdminMessage;
46 import fr.paris.lutece.portal.service.message.AdminMessageService;
47 import fr.paris.lutece.portal.service.template.AppTemplateService;
48 import fr.paris.lutece.portal.web.portlet.PortletJspBean;
49 import fr.paris.lutece.util.html.HtmlTemplate;
50
51 import org.apache.commons.lang3.StringUtils;
52
53 import java.util.ArrayList;
54 import java.util.Arrays;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.Map;
58
59 import javax.servlet.http.HttpServletRequest;
60
61
62
63
64
65 public class DocumentPortletJspBean extends PortletJspBean
66 {
67
68
69
70
71 public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
72
73
74
75
76 private static final long serialVersionUID = -5162094873312077653L;
77 private static final String MARK_DOCUMENT_TYPE_LIST = "document_type_list";
78 private static final String MARK_CATEGORY_LIST = "category_list";
79 private static final String MARK_CODE_TYPE_DOCUMENT = "code_type_document";
80 private static final String PARAMETER_DOCUMENT_TYPE_CODE = "document_type_code";
81 private static final String PARAMETER_CATEGORY = "categories";
82 private static final String COMBO_DOCUMENT_TYPE_LIST = "@combo_document_type@";
83 private static final String COMBO_DOCUMENT_CATEGORY_LIST = "@combo_category_document@";
84 private static final String TEMPLATE_DOCUMENT_TYPE_LIST = "admin/plugins/document/portlet/document_type_list.html";
85 private static final String TEMPLATE_CATEGORY_DOCUMENT_LIST = "admin/plugins/document/portlet/category_document_list.html";
86 private static final String MESSAGE_ERROR_DOCUMENTS_MUST_BE_UNASSIGNED = "document.message.errorDocumentsMustBeUnassigned";
87
88
89
90
91
92
93
94
95
96 public String getPropertiesPrefix( )
97 {
98 return "portlet.document";
99 }
100
101
102
103
104
105
106
107 public String getCreate( HttpServletRequest request )
108 {
109 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
110 String strIdPortletType = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
111 AdminUser user = getUser( );
112
113 HtmlTemplate template = getCreateTemplate( strIdPage, strIdPortletType );
114 template.substitute( COMBO_DOCUMENT_TYPE_LIST, getDocumentTypesList( "" ) );
115 template.substitute( COMBO_DOCUMENT_CATEGORY_LIST, getCategoryList( "", user ) );
116
117 return template.getHtml( );
118 }
119
120
121
122
123
124
125
126 public String getModify( HttpServletRequest request )
127 {
128 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
129 int nPortletId = IntegerUtils.convert( strPortletId );
130 DocumentPortlet./../../../fr/paris/lutece/plugins/document/business/portlet/DocumentPortlet.html#DocumentPortlet">DocumentPortlet portlet = (DocumentPortlet) PortletHome.findByPrimaryKey( nPortletId );
131 AdminUser user = getUser( );
132
133 HtmlTemplate template = getModifyTemplate( portlet );
134
135 template.substitute( COMBO_DOCUMENT_TYPE_LIST, getDocumentTypesList( strPortletId ) );
136 template.substitute( COMBO_DOCUMENT_CATEGORY_LIST, getCategoryList( strPortletId, user ) );
137
138 return template.getHtml( );
139 }
140
141
142
143
144
145
146
147 public String doCreate( HttpServletRequest request )
148 {
149 DocumentPortletent/business/portlet/DocumentPortlet.html#DocumentPortlet">DocumentPortlet portlet = new DocumentPortlet( );
150 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
151 int nIdPage = IntegerUtils.convert( strIdPage );
152
153
154 String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
155
156
157 String strErrorUrl = setPortletCommonData( request, portlet );
158
159 if ( strErrorUrl != null )
160 {
161 return strErrorUrl;
162 }
163
164 portlet.setPageId( nIdPage );
165
166
167 portlet.setDocumentTypeCode( strDocumentTypeCode );
168
169
170 portlet.setIdCategory( setIdCategory( request ) );
171
172
173 DocumentPortletHome.getInstance( ).create( portlet );
174
175
176 return getPageUrl( nIdPage );
177 }
178
179
180
181
182
183
184
185 public String doModify( HttpServletRequest request )
186 {
187
188 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
189 String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
190 int nPortletId = IntegerUtils.convert( strPortletId );
191 DocumentPortlet./../../../fr/paris/lutece/plugins/document/business/portlet/DocumentPortlet.html#DocumentPortlet">DocumentPortlet portlet = (DocumentPortlet) DocumentPortletHome.findByPrimaryKey( nPortletId );
192 int[] arrayIdCategories = setIdCategory( request );
193 int[] arrayOldIdCategories = portlet.getIdCategory( );
194
195 if ( arrayIdCategories != null )
196 {
197 Arrays.sort( arrayIdCategories );
198 }
199
200 if ( arrayOldIdCategories != null )
201 {
202 Arrays.sort( arrayOldIdCategories );
203 }
204
205 if ( ( !Arrays.equals( arrayIdCategories, arrayOldIdCategories ) ||
206 !portlet.getDocumentTypeCode( ).equals( strDocumentTypeCode ) ) &&
207 ( PublishingService.getInstance( ).getAssignedDocumentsByPortletId( nPortletId ).size( ) != 0 ) )
208 {
209 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENTS_MUST_BE_UNASSIGNED,
210 AdminMessage.TYPE_STOP );
211 }
212
213
214 String strErrorUrl = setPortletCommonData( request, portlet );
215
216 if ( strErrorUrl != null )
217 {
218 return strErrorUrl;
219 }
220
221 portlet.setDocumentTypeCode( strDocumentTypeCode );
222
223
224 portlet.setIdCategory( arrayIdCategories );
225
226
227 portlet.update( );
228
229
230 return getPageUrl( portlet.getPageId( ) );
231 }
232
233
234
235
236
237
238 private int[] setIdCategory( HttpServletRequest request )
239 {
240 String[] strArrayIdCategory = request.getParameterValues( PARAMETER_CATEGORY );
241
242 if ( strArrayIdCategory != null )
243 {
244 int[] nArrayIdCategory = new int[strArrayIdCategory.length];
245 int i = 0;
246
247 for ( String strIdCategory : strArrayIdCategory )
248 {
249 nArrayIdCategory[i++] = IntegerUtils.convert( strIdCategory );
250 }
251
252 return nArrayIdCategory;
253 }
254
255 return new int[0];
256 }
257
258
259
260
261
262
263 private String getDocumentTypesList( String strPortletId )
264 {
265 String strCodeTypeDocument = StringUtils.EMPTY;
266
267 if ( IntegerUtils.isNumeric( strPortletId ) )
268 {
269 DocumentPortlet./../../../fr/paris/lutece/plugins/document/business/portlet/DocumentPortlet.html#DocumentPortlet">DocumentPortlet portlet = (DocumentPortlet) PortletHome.findByPrimaryKey( IntegerUtils.convert(
270 strPortletId ) );
271
272 if ( portlet != null )
273 {
274 strCodeTypeDocument = portlet.getDocumentTypeCode( );
275 }
276 }
277
278 Map<String, Object> model = new HashMap<String, Object>( );
279
280 model.put( MARK_CODE_TYPE_DOCUMENT, strCodeTypeDocument );
281 model.put( MARK_DOCUMENT_TYPE_LIST, DocumentTypeHome.getDocumentTypesList( ) );
282
283 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DOCUMENT_TYPE_LIST, getLocale( ), model );
284
285 return template.getHtml( );
286 }
287
288
289
290
291
292
293
294 private String getCategoryList( String strPortletId, AdminUser user )
295 {
296 Collection<CategoryDisplay> listCategoriesDisplay = new ArrayList<CategoryDisplay>( );
297
298 if ( IntegerUtils.isNumeric( strPortletId ) )
299 {
300 DocumentPortlet./../../../fr/paris/lutece/plugins/document/business/portlet/DocumentPortlet.html#DocumentPortlet">DocumentPortlet portlet = (DocumentPortlet) PortletHome.findByPrimaryKey( IntegerUtils.convert(
301 strPortletId ) );
302
303 if ( portlet != null )
304 {
305 listCategoriesDisplay = CategoryService.getAllCategoriesDisplay( portlet.getIdCategory( ), user );
306 }
307 }
308 else
309 {
310 listCategoriesDisplay = CategoryService.getAllCategoriesDisplay( user );
311 }
312
313 Map<String, Object> model = new HashMap<String, Object>( );
314
315 model.put( MARK_CATEGORY_LIST, listCategoriesDisplay );
316
317 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CATEGORY_DOCUMENT_LIST, getLocale( ), model );
318
319 return template.getHtml( );
320 }
321 }