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