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.library.web;
35
36 import fr.paris.lutece.plugins.document.business.Document;
37 import fr.paris.lutece.plugins.document.business.DocumentType;
38 import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
39 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
40 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
41 import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
42 import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
43 import fr.paris.lutece.plugins.document.business.workflow.DocumentAction;
44 import fr.paris.lutece.plugins.document.business.workflow.DocumentActionHome;
45 import fr.paris.lutece.plugins.document.modules.metadatadublincore.business.DublinCoreMetadata;
46 import fr.paris.lutece.plugins.document.service.DocumentException;
47 import fr.paris.lutece.plugins.document.service.DocumentService;
48 import fr.paris.lutece.plugins.document.service.DocumentTypeResourceIdService;
49 import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
50 import fr.paris.lutece.plugins.library.business.LibraryMapping;
51 import fr.paris.lutece.plugins.library.business.LibraryMapping.AttributeAssociation;
52 import fr.paris.lutece.plugins.library.business.LibraryMappingHome;
53 import fr.paris.lutece.plugins.library.business.LibraryMedia;
54 import fr.paris.lutece.plugins.library.business.LibraryMediaHome;
55 import fr.paris.lutece.plugins.library.business.MediaAttribute;
56 import fr.paris.lutece.plugins.library.business.MediaAttributeHome;
57 import fr.paris.lutece.portal.business.user.AdminUser;
58 import fr.paris.lutece.portal.service.admin.AdminUserService;
59 import fr.paris.lutece.portal.service.html.XmlTransformerService;
60 import fr.paris.lutece.portal.service.i18n.I18nService;
61 import fr.paris.lutece.portal.service.message.AdminMessage;
62 import fr.paris.lutece.portal.service.message.AdminMessageService;
63 import fr.paris.lutece.portal.service.plugin.Plugin;
64 import fr.paris.lutece.portal.service.plugin.PluginService;
65 import fr.paris.lutece.portal.service.template.AppTemplateService;
66 import fr.paris.lutece.portal.service.util.AppLogService;
67 import fr.paris.lutece.portal.service.util.AppPathService;
68 import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
69 import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
70 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
71 import fr.paris.lutece.util.UniqueIDGenerator;
72 import fr.paris.lutece.util.html.HtmlTemplate;
73 import fr.paris.lutece.util.xml.XmlUtil;
74
75 import org.apache.commons.lang3.StringEscapeUtils;
76
77 import java.io.FileInputStream;
78 import java.io.IOException;
79
80 import java.util.Collection;
81 import java.util.HashMap;
82 import java.util.Map;
83
84 import javax.servlet.http.HttpServletRequest;
85
86 import javax.xml.transform.Source;
87 import javax.xml.transform.stream.StreamSource;
88
89
90
91
92
93
94
95 public class UploadInsertServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
96 {
97 private static final long serialVersionUID = -7071548876864568789L;
98 private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
99
100
101 private static final String TEMPLATE_MEDIATYPE_SELECTOR = "admin/plugins/library/upload_mediatype_selector.html";
102 private static final String TEMPLATE_SPACE_SELECTOR = "admin/plugins/library/upload_space_selector.html";
103 private static final String TEMPLATE_MEDIA_CREATOR = "admin/plugins/library/media_creator.html";
104 private static final String TEMPLATE_EDIT_MEDIA = "admin/plugins/library/mediaedition_selector.html";
105
106
107 private static final String JSP_INSERT_MEDIA = "jsp/admin/plugins/library/DoInsertMediaByUpload.jsp";
108
109
110 private static final String MARK_FIELDS = "fields";
111 private static final String MARK_DOCUMENT_TYPE = "document_type";
112 private static final String MARK_MEDIA_TYPES = "mediatypes";
113 private static final String MARK_METADATA = "metadata";
114 private static final String MARK_MEDIA = "media";
115 private static final String MARK_INPUT = "input";
116 private static final String MARK_MEDIA_TYPE = "mediatype";
117 private static final String MARK_MEDIA_TYPE_ATTRIBUTES = "media_attributes";
118 private static final String MARK_MEDIA_ATTRIBUTES_ASSOCIATIONS = "attributes_associations";
119 private static final String MARK_MEDIA_DOC = "document";
120 private static final String MARK_JSP_INSERT = "jsp_insert";
121 private static final String MARK_SPACE = "space";
122 private static final String MARK_SPACES_TREE = "spaces_tree";
123 private static final String MARK_SPACE_ID = "space_id";
124 private static final String MARK_LOCALE = "locale";
125 private static final String MARK_WEBAPP_URL = "webapp_url";
126 private static final String MARK_IS_IMAGE = "is_image";
127 private static final String MARK_USER_LAST_NAME = "user_last_name";
128
129
130 private static final String PARAMETER_DOCUMENT_TYPE_CODE = "document_type_code";
131 private static final String PARAMETER_INPUT = "input";
132 private static final String PARAMETER_MEDIA_TYPE = "media_type";
133 private static final String PARAMETER_MEDIA_ID = "media";
134 private static final String PARAMETER_SPACE_ID = "space_id";
135
136
137 private static final String XML_TAG_MEDIA = "media";
138 private static final String XSL_UNIQUE_PREFIX_ID = UniqueIDGenerator.getNewId( ) + "library-";
139 private static final String XSL_PARAMETER_CURRENT_SPACE = "current-space-id";
140 private static final String SPACE_TREE_XSL_UNIQUE_PREFIX = UniqueIDGenerator.getNewId( ) + "SpacesTree";
141 private static final String PATH_XSL = "/WEB-INF/plugins/library/xsl/";
142 private static final String FILE_TREE_XSL = "document_spaces_tree.xsl";
143
144
145 private static final String MESSAGE_DOCUMENT_ERROR = "library.uploadinsertservice.message.documentError";
146 private static final String MESSAGE_NOT_AUTHORIZED = "library.uploadinsertservice.message.notAuthorized";
147 private static final String SPACE_ID_SESSION = "spaceIdSession";
148
149 private AdminUser _user;
150 private Plugin _plugin;
151 private String _input;
152 private static final int IMAGE_MEDIA_TYPE_ID = 2 ;
153
154
155
156
157
158
159 @Override
160 public String getInsertServiceSelectorUI( HttpServletRequest request )
161 {
162 init( request );
163
164 Collection<LibraryMedia> libraryMedia = LibraryMediaHome.findAllMedia( _plugin );
165 HashMap<String, Object> model = getDefaultModel( );
166 model.put( MARK_MEDIA_TYPES, libraryMedia );
167
168 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MEDIATYPE_SELECTOR, _user.getLocale( ), model );
169
170 return template.getHtml( );
171 }
172
173 private void init( HttpServletRequest request )
174 {
175 String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
176 _user = AdminUserService.getAdminUser( request );
177 _plugin = PluginService.getPlugin( strPluginName );
178 _input = request.getParameter( PARAMETER_INPUT );
179 }
180
181 private HashMap<String, Object> getDefaultModel( )
182 {
183 HashMap<String, Object> model = new HashMap<String, Object>( );
184 model.put( PARAMETER_PLUGIN_NAME, _plugin.getName( ) );
185 model.put( PARAMETER_INPUT, _input );
186
187 return model;
188 }
189
190
191
192
193
194
195 public String getCreateMedia( HttpServletRequest request )
196 {
197 init( request );
198
199 String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
200
201 String strSpaceId = request.getParameter( PARAMETER_SPACE_ID );
202 int nSpaceId = -1;
203
204 if ( ( strSpaceId != null ) && ( strSpaceId.length( ) > 0 ) )
205 {
206 nSpaceId = Integer.parseInt( strSpaceId );
207 request.getSession( ).setAttribute( SPACE_ID_SESSION, nSpaceId );
208 }
209 else
210 {
211 Integer nSpaceIdTmp = (Integer) request.getSession( ).getAttribute( SPACE_ID_SESSION );
212
213 if ( nSpaceIdTmp != null )
214 {
215 nSpaceId = nSpaceIdTmp.intValue( );
216 }
217 }
218
219 LibraryMedia mediaType = LibraryMediaHome.findByPrimaryKey( Integer.parseInt( strMediaType ), _plugin );
220 Collection<LibraryMapping> libraryMappingList = LibraryMappingHome.findAllMappingsByMedia( mediaType.getMediaId( ), _plugin);
221
222
223 LibraryMapping mapping = null;
224 for (LibraryMapping libMap : libraryMappingList)
225 {
226 mapping = LibraryMappingHome.findByPrimaryKey( libMap.getIdMapping( ), _plugin );
227 }
228
229 DocumentType documentType = DocumentTypeHome.findByPrimaryKey( mapping.getCodeDocumentType( ) );
230
231 if ( ( nSpaceId == -1 ) ||
232 ( !DocumentService.getInstance( )
233 .isAuthorizedAdminDocument( nSpaceId, documentType.getCode( ),
234 DocumentTypeResourceIdService.PERMISSION_CREATE, _user ) ) )
235 {
236 return getSelectSpace( request );
237 }
238
239 HashMap<String, Object> model = getDefaultModel( );
240 model.put( MARK_DOCUMENT_TYPE, documentType.getCode( ) );
241 model.put( MARK_FIELDS,
242 DocumentService.getInstance( )
243 .getCreateForm( documentType.getCode( ), _user.getLocale( ),
244 AppPathService.getBaseUrl( request ) ) );
245 model.put( MARK_METADATA, new DublinCoreMetadata( ) );
246 model.put( MARK_MEDIA, mediaType.getMediaId( ) );
247 model.put( MARK_INPUT, _input );
248 model.put( MARK_SPACE_ID, nSpaceId );
249 model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
250 model.put( MARK_LOCALE, _user.getLocale( ).getLanguage( ) );
251 model.put( MARK_IS_IMAGE, Document.CODE_DOCUMENT_TYPE_IMAGE.equals( mediaType.getName() ) ) ;
252 model.put( MARK_USER_LAST_NAME, _user.getLastName( ) );
253
254 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MEDIA_CREATOR, _user.getLocale( ), model );
255
256 return template.getHtml( );
257 }
258
259
260
261
262
263
264 public String getSelectSpace( HttpServletRequest request )
265 {
266 init( request );
267
268 String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
269
270 LibraryMedia mediaType = LibraryMediaHome.findByPrimaryKey( Integer.parseInt( strMediaType ), _plugin );
271 Collection<LibraryMapping> libraryMappingList = LibraryMappingHome.findAllMappingsByMedia( mediaType.getMediaId( ), _plugin);
272
273
274 LibraryMapping mapping = null;
275 for (LibraryMapping libMap : libraryMappingList)
276 {
277 mapping = LibraryMappingHome.findByPrimaryKey( libMap.getIdMapping( ), _plugin );
278 }
279 DocumentType documentType = DocumentTypeHome.findByPrimaryKey( mapping.getCodeDocumentType( ) );
280
281
282 String strXmlSpaces = DocumentSpacesService.getInstance( ).getXmlSpacesList( _user, documentType.getCode( ) );
283
284 FileInputStream fis = AppPathService.getResourceAsStream( PATH_XSL, FILE_TREE_XSL );
285 Source sourceXsl = new StreamSource( fis );
286
287 String strSpaceIdRequest = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
288 int nSpaceId = -1;
289
290 if ( ( strSpaceIdRequest != null ) && ( strSpaceIdRequest.length( ) > 0 ) )
291 {
292 nSpaceId = Integer.parseInt( strSpaceIdRequest );
293 request.getSession( ).setAttribute( SPACE_ID_SESSION, nSpaceId );
294 }
295 else
296 {
297 Integer nSpaceIdTmp = (Integer) request.getSession( ).getAttribute( SPACE_ID_SESSION );
298
299 if ( nSpaceIdTmp != null )
300 {
301 nSpaceId = nSpaceIdTmp.intValue( );
302 }
303 }
304
305 HashMap<String, String> htXslParameters = new HashMap<String, String>( );
306 htXslParameters.put( XSL_PARAMETER_CURRENT_SPACE, nSpaceId + "" );
307
308 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
309 String strSpacesTree = xmlTransformerService.transformBySourceWithXslCache( strXmlSpaces, sourceXsl,
310 SPACE_TREE_XSL_UNIQUE_PREFIX, htXslParameters, null );
311
312 try
313 {
314 fis.close( );
315 }
316 catch ( IOException e )
317 {
318 AppLogService.error( e.getMessage( ), e );
319 }
320
321 DocumentSpace currentSpace = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
322
323 HashMap<String, Object> model = getDefaultModel( );
324
325 model.put( MARK_INPUT, _input );
326 model.put( MARK_SPACES_TREE, strSpacesTree );
327 model.put( MARK_SPACE, currentSpace );
328 model.put( MARK_MEDIA_TYPE, strMediaType );
329
330 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SPACE_SELECTOR, _user.getLocale( ), model );
331
332 return template.getHtml( );
333 }
334
335
336
337
338
339
340 public String createDocument( HttpServletRequest request , Document document )
341 {
342
343 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
344
345 String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
346 String strSpaceIdRequest = request.getParameter( PARAMETER_SPACE_ID );
347 int nSpaceId;
348
349 try
350 {
351 nSpaceId = Integer.parseInt( strSpaceIdRequest );
352 }
353 catch ( NumberFormatException e )
354 {
355 nSpaceId = -1;
356 }
357
358 if ( !DocumentService.getInstance( )
359 .isAuthorizedAdminDocument( nSpaceId, strDocumentTypeCode,
360 DocumentTypeResourceIdService.PERMISSION_CREATE, _user ) )
361 {
362 return AdminMessageService.getMessageUrl( request, MESSAGE_NOT_AUTHORIZED, AdminMessage.TYPE_ERROR );
363 }
364
365 document.setCodeDocumentType( strDocumentTypeCode );
366
367 String strError = DocumentService.getInstance( )
368 .getDocumentData( multipartRequest, document, _user.getLocale( ) );
369
370 if ( strError != null )
371 {
372 return strError;
373 }
374
375 document.setSpaceId( nSpaceId );
376 document.setStateId( 1 );
377 document.setCreatorId( _user.getUserId( ) );
378
379 try
380 {
381 DocumentService.getInstance( ).createDocument( document, _user );
382
383 DocumentAction action = DocumentActionHome.findByPrimaryKey( DocumentAction.ACTION_VALIDATE );
384 DocumentService.getInstance( )
385 .validateDocument( document, _user, action.getFinishDocumentState( ).getId( ) );
386 }
387 catch ( DocumentException e )
388 {
389 return AdminMessageService.getMessageUrl( request, MESSAGE_DOCUMENT_ERROR,
390 new String[] { I18nService.getLocalizedString( e.getI18nMessage( ), _user.getLocale( ) ) },
391 AdminMessage.TYPE_ERROR );
392 }
393
394
395 return null;
396 }
397
398
399
400
401
402
403 public String doCreateDocument( HttpServletRequest request )
404 {
405 Document document = new Document( ) ;
406 String url = createDocument(request, document) ;
407
408 if ( url != null )
409 {
410 return url;
411 }
412 else
413 {
414 return getEditSelectedMedia( request, document );
415 }
416 }
417
418
419
420
421
422
423 public String doCreateImage ( HttpServletRequest request )
424 {
425 Document document = new Document( ) ;
426 String url = createDocument(request, document) ;
427
428 if ( url != null )
429 {
430 return url;
431 }
432 else
433 {
434 return doInsertUrl( request , document);
435 }
436
437 }
438
439
440
441
442
443
444 public String doInsertUrl( HttpServletRequest request )
445 {
446 return doInsertUrl ( request, null ) ;
447
448 }
449
450
451
452
453
454
455 public String doInsertUrl( HttpServletRequest request , Document document )
456 {
457 init( request );
458
459 String strMediaTypeId = request.getParameter( PARAMETER_MEDIA_ID );
460 LibraryMedia mediaType = LibraryMediaHome.findByPrimaryKey( Integer.parseInt( strMediaTypeId ), _plugin );
461 mediaType.setMediaAttributeList( MediaAttributeHome.findAllAttributesForMedia( mediaType.getMediaId( ), _plugin ) );
462
463 Collection<LibraryMapping> allMappings = LibraryMappingHome.findAllMappingsByMedia( mediaType.getMediaId( ), _plugin );
464 LibraryMapping mapping = allMappings.iterator( ).next( );
465
466 Map<String,String> _listAttributesAssociation = getAttributesFromMapping(mapping) ;
467
468 StringBuffer sbXml = new StringBuffer( );
469 XmlUtil.beginElement( sbXml, XML_TAG_MEDIA );
470
471 for ( MediaAttribute attribute : mediaType.getMediaAttributeList( ) )
472 {
473 String strValue = request.getParameter( attribute.getCode( ) );
474
475
476 if ( strValue == null ) {
477 if (document.getAttribute( attribute.getCode( ) ) != null )
478 {
479 strValue = (document.getAttribute( attribute.getCode( ) ) ).getTextValue() ;
480 }
481 else
482 {
483
484 if ( attribute.getTypeId( ) == IMAGE_MEDIA_TYPE_ID && _listAttributesAssociation != null ) {
485 String docType_code = _listAttributesAssociation.get( attribute.getCode( ) ) ;
486 DocumentAttribute _docAttr = document.getAttribute( docType_code );
487 if ( _docAttr != null ) strValue = "document?id=" + document.getId() + "&id_attribute=" + _docAttr.getId() ;
488 }
489
490 }
491 }
492
493 if ( attribute.getTypeId( ) == MediaAttribute.ATTRIBUTE_TYPE_BINARY )
494 {
495 strValue = StringEscapeUtils.escapeHtml4( strValue );
496 }
497
498 XmlUtil.addElement( sbXml, attribute.getCode( ), strValue );
499 }
500
501 XmlUtil.endElement( sbXml, XML_TAG_MEDIA );
502
503 String strHtml = "";
504
505 try
506 {
507 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
508 String strXslUniqueId = XSL_UNIQUE_PREFIX_ID + strMediaTypeId;
509 String strPreHtml = xmlTransformerService.transformBySourceWithXslCache( sbXml.toString( ),
510 mediaType.getStyleSheet( ).getSource( ), strXslUniqueId, null, null );
511
512 strHtml = StringEscapeUtils.escapeEcmaScript( strPreHtml );
513 }
514 catch ( Exception e )
515 {
516 AppLogService.error( sbXml.toString( ) );
517 AppLogService.error( getClass( ).getClassLoader( ).getResource( "testlibrary.xsl" ) );
518 AppLogService.error( e.getMessage( ), e );
519 }
520
521 return insertUrl( request, _input, strHtml );
522 }
523
524
525
526
527
528
529
530 public String getEditSelectedMedia( HttpServletRequest request, Document document )
531 {
532 String strMediaTypeId = request.getParameter( PARAMETER_MEDIA_ID );
533
534 LibraryMedia mediaType = LibraryMediaHome.findByPrimaryKey( Integer.parseInt( strMediaTypeId ), _plugin );
535 mediaType.setMediaAttributeList( MediaAttributeHome.findAllAttributesForMedia( mediaType.getMediaId( ), _plugin ) );
536
537 Collection<LibraryMapping> allMappings = LibraryMappingHome.findAllMappingsByMedia( mediaType.getMediaId( ),
538 _plugin );
539 LibraryMapping mapping = allMappings.iterator( ).next( );
540
541 Map<String, Object> model = getDefaultModel( );
542 model.put( MARK_MEDIA_TYPE, mediaType );
543 model.put( MARK_MEDIA_TYPE_ATTRIBUTES, mediaType.getMediaAttributeList( ) );
544 model.put( MARK_MEDIA_ATTRIBUTES_ASSOCIATIONS, getAttributesFromMapping( mapping ) );
545 model.put( MARK_MEDIA_DOC, document );
546 model.put( MARK_JSP_INSERT, JSP_INSERT_MEDIA );
547
548 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EDIT_MEDIA, _user.getLocale( ), model );
549
550 return template.getHtml( );
551 }
552
553 private Map<String, String> getAttributesFromMapping( LibraryMapping m )
554 {
555 HashMap<String, String> result = new HashMap<String, String>( );
556
557 for ( AttributeAssociation assoc : m.getAttributeAssociationList( ) )
558 {
559 result.put( MediaAttributeHome.findByPrimaryKey( assoc.getIdMediaAttribute( ), _plugin ).getCode( ),
560 DocumentAttributeHome.findByPrimaryKey( assoc.getIdDocumentAttribute( ) ).getCode( ) );
561 }
562
563 return result;
564 }
565 }