View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.library.web;
35  
36  import fr.paris.lutece.plugins.document.business.DocumentType;
37  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
38  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
39  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
40  import fr.paris.lutece.plugins.library.business.LibraryMapping;
41  import fr.paris.lutece.plugins.library.business.LibraryMapping.AttributeAssociation;
42  import fr.paris.lutece.plugins.library.business.LibraryMappingHome;
43  import fr.paris.lutece.plugins.library.business.LibraryMedia;
44  import fr.paris.lutece.plugins.library.business.LibraryMediaHome;
45  import fr.paris.lutece.plugins.library.business.MediaAttribute;
46  import fr.paris.lutece.plugins.library.business.MediaAttributeHome;
47  import fr.paris.lutece.portal.service.html.XmlTransformerService;
48  import fr.paris.lutece.portal.service.i18n.I18nService;
49  import fr.paris.lutece.portal.service.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.portal.service.template.AppTemplateService;
52  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
53  import fr.paris.lutece.portal.web.constants.Messages;
54  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
55  import fr.paris.lutece.util.ReferenceItem;
56  import fr.paris.lutece.util.ReferenceList;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  import fr.paris.lutece.util.url.UrlItem;
59  
60  import org.apache.commons.fileupload.FileItem;
61  
62  import java.util.ArrayList;
63  import java.util.Collection;
64  import java.util.HashMap;
65  import java.util.Locale;
66  import java.util.Map;
67  
68  import javax.servlet.http.HttpServletRequest;
69  
70  
71  /**
72   * Library management JSP Bean
73   */
74  public class LibraryJspBean extends PluginAdminPageJspBean
75  {
76      /**
77       * Right to manage libray plugin
78       */
79      public static final String LIBRARY_MANAGEMENT = "LIBRARY_MANAGEMENT";
80      private static final long serialVersionUID = -4537118194245494293L;
81  
82      // Jsp
83      private static final String JSP_URL_MEDIA = "ManageMedia.jsp";
84      private static final String JSP_URL_MAPPINGS = "ManageMappings.jsp";
85      private static final String JSP_DO_REMOVE_MAPPING = "jsp/admin/plugins/library/DoRemoveMapping.jsp";
86      private static final String JSP_DO_REMOVE_ATTRIBUTE = "jsp/admin/plugins/library/DoRemoveAttribute.jsp";
87      private static final String JSP_DO_REMOVE_MEDIA = "jsp/admin/plugins/library/DoRemoveMedia.jsp";
88  
89      // Parameters
90      private static final String PARAMETER_MEDIA_ID = "media_id";
91      private static final String PARAMETER_ATTRIBUTE_CODE = "attribute_code";
92      private static final String PARAMETER_ATTRIBUTE_DESCRIPTION = "attribute_description";
93      private static final String PARAMETER_ATTRIBUTE_TYPE = "attribute_type";
94      private static final String PARAMETER_ATTRIBUTE_DEFAULT_VALUE = "attribute_default_value";
95      private static final String PARAMETER_ATTRIBUTE_ID = "attribute_id";
96      private static final String PARAMETER_DOCUMENT_TYPE = "document_type";
97      private static final String PARAMETER_MEDIA_NAME = "media_name";
98      private static final String PARAMETER_MEDIA_DESCRIPTION = "media_description";
99      private static final String PARAMETER_PREFIX_DOCUMENT_ATTRIBUTE_ID = "document_attribute_id_";
100     private static final String PARAMETER_MAPPING_ID = "mapping_id";
101     private static final String PARAMETER_MEDIA_STYLESHEET = "media_stylesheet";
102 
103     // Properties
104     private static final String PROPERTY_PAGE_TITLE_MAPPING_CREATION = "library.pageTitle.mapping.creation";
105     private static final String PROPERTY_PAGE_TITLE_MEDIA_CREATION = "library.pageTitle.media.creation";
106     private static final String PROPERTY_PAGE_TITLE_MEDIA_MODIFICATION = "library.pageTitle.media.modification";
107     private static final String PROPERTY_PAGE_TITLE_ATTRIBUTE_CREATION = "library.pageTitle.attribute.modification";
108     private static final String PROPERTY_PAGE_TITLE_ATTRIBUTE_MODIFICATION = "library.pageTitle.attribute.creation";
109     private static final String PROPERTY_PAGE_TITLE_MAPPING_MANAGEMENT = "library.pageTitle.mapping.management";
110     private static final String PROPERTY_MESSAGE_REMOVE_MEDIA = "library.message.media.remove";
111     private static final String PROPERTY_MESSAGE_REMOVE_MAPPING = "library.message.mapping.remove";
112     private static final String PROPERTY_PAGE_TITLE_MAPPING_MODIFICATION = "library.pageTitle.mapping.modification";
113     private static final String PROPERTY_MESSAGE_REMOVE_ATTRIBUTE = "library.message.attribute.remove";
114 
115     // Marks
116     private static final String MARK_MEDIA_LIST = "media_list";
117     private static final String MARK_MEDIA = "media";
118     private static final String MARK_MEDIA_ID = "mediaId";
119     private static final String MARK_ATTRIBUTE_TYPE_LIST = "attribute_type_list";
120     private static final String MARK_ATTRIBUTE = "attribute";
121     private static final String MARK_MAPPING_LIST = "mapping_list";
122     private static final String MARK_DOCUMENT_TYPE_LIST = "document_type_list";
123     private static final String MARK_DOCUMENT_TYPE = "document_type";
124     private static final String MARK_DOCUMENT_ATTRIBUTE = "document_attribute";
125     private static final String MARK_MAPPING = "mapping";
126     private static final String MARK_MEDIA_MULTIPLE = "media-multiple";
127 
128     // Templates
129     private static final String TEMPLATE_MANAGE_MEDIA = "admin/plugins/library/manage_media.html";
130     private static final String TEMPLATE_CREATE_MEDIA = "admin/plugins/library/create_media.html";
131     private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/library/create_attribute.html";
132     private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/library/modify_attribute.html";
133     private static final String TEMPLATE_MANAGE_LIBRARY = "admin/plugins/library/manage_library.html";
134     private static final String TEMPLATE_MANAGE_MAPPINGS = "admin/plugins/library/manage_mappings.html";
135     private static final String TEMPLATE_CREATE_MAPPING = "admin/plugins/library/create_mapping.html";
136     private static final String TEMPLATE_GENERATE_ASSOCIATION_LABEL = "admin/plugins/library/generate_mapping_association_label.html";
137     private static final String TEMPLATE_MODIFY_MAPPING = "admin/plugins/library/modify_mapping.html";
138 
139     /**
140      * Get the manage library page
141      * @param request The request
142      * @return The HTML content
143      */
144     public String getManageLibrary( HttpServletRequest request )
145     {
146         Map<String, Object> model = new HashMap<String, Object>(  );
147         model.put( MARK_MEDIA_LIST, LibraryMediaHome.findAllMedia( getPlugin(  ) ) );
148 
149         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_LIBRARY, getLocale(  ), model );
150 
151         return getAdminPage( template.getHtml(  ) );
152     }
153 
154     /**
155      * Get the page to create a media
156      * @param request The request
157      * @return The HTML content
158      */
159     public String getCreateMedia( HttpServletRequest request )
160     {
161         setPageTitleProperty( PROPERTY_PAGE_TITLE_MEDIA_CREATION );
162 
163         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MEDIA, getLocale(  ), null );
164 
165         return getAdminPage( template.getHtml(  ) );
166     }
167 
168     /**
169      * Do create a media
170      * @param request The request
171      * @return The next URL to redirect to
172      */
173     public String doCreateMedia( HttpServletRequest request )
174     {
175         String strName = request.getParameter( PARAMETER_MEDIA_NAME );
176         String strDescription = request.getParameter( PARAMETER_MEDIA_DESCRIPTION );
177 
178         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
179         FileItem item = mRequest.getFile( PARAMETER_MEDIA_STYLESHEET );
180 
181         if ( ( strName.length(  ) == 0 ) || ( strDescription.length(  ) == 0 ) || ( item.getSize(  ) == 0 ) )
182         {
183             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
184         }
185 
186         LibraryMediarary/business/LibraryMedia.html#LibraryMedia">LibraryMedia media = new LibraryMedia(  );
187         media.setName( strName );
188         media.setDescription( strDescription );
189         media.setStyleSheetBytes( item.get(  ) );
190         media.setIsMultipleMedia( Boolean.parseBoolean( request.getParameter( MARK_MEDIA_MULTIPLE ) ) );
191 
192         LibraryMediaHome.create( media, getPlugin(  ) );
193 
194         return getHomeUrl( request );
195     }
196 
197     /**
198      * Get the confirmation page before removing a media
199      * @param request The request
200      * @return The next URL to redirect to
201      */
202     public String getRemoveMedia( HttpServletRequest request )
203     {
204         UrlItem url = new UrlItem( JSP_DO_REMOVE_MEDIA );
205         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
206         int nMediaId = Integer.parseInt( strMediaId );
207         url.addParameter( PARAMETER_MEDIA_ID, nMediaId );
208 
209         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_REMOVE_MEDIA, url.getUrl(  ),
210             AdminMessage.TYPE_CONFIRMATION );
211     }
212 
213     /**
214      * Do remove a media
215      * @param request The request
216      * @return The next URL to redirect to
217      */
218     public String doRemoveMedia( HttpServletRequest request )
219     {
220         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
221         int nMediaId = Integer.parseInt( strMediaId );
222 
223         XmlTransformerService.clearXslCache(  );
224 
225         // remove media definition
226         LibraryMediaHome.remove( nMediaId, getPlugin(  ) );
227 
228         // remove all attributes associated with this media
229         MediaAttributeHome.removeAllForMedia( nMediaId, getPlugin(  ) );
230 
231         // delete all the mapping definitions that exist for this media
232         Collection<LibraryMapping> colMappings = LibraryMappingHome.findAllMappingsByMedia( nMediaId, getPlugin(  ) );
233 
234         for ( LibraryMapping mapping : colMappings )
235         {
236             LibraryMappingHome.remove( mapping.getIdMapping(  ), getPlugin(  ) );
237         }
238 
239         return getHomeUrl( request );
240     }
241 
242     /**
243      *
244      * @param request
245      * @return
246      */
247     public String getManageMedia( HttpServletRequest request )
248     {
249         setPageTitleProperty( PROPERTY_PAGE_TITLE_MEDIA_MODIFICATION );
250 
251         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
252         int nMediaId = Integer.parseInt( strMediaId );
253 
254         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( nMediaId, getPlugin(  ) );
255         Collection<MediaAttribute> attributeList = MediaAttributeHome.findAllAttributesForMedia( nMediaId, getPlugin(  ) );
256         media.setMediaAttributeList( I18nService.localizeCollection( attributeList, getLocale(  ) ) );
257 
258         Map<String, Object> model = new HashMap<String, Object>(  );
259         model.put( MARK_MEDIA, media );
260 
261         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_MEDIA, getLocale(  ), model );
262 
263         return getAdminPage( template.getHtml(  ) );
264     }
265 
266     /**
267      *
268      * @param request
269      * @return
270      */
271     public String doModifyMedia( HttpServletRequest request )
272     {
273         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
274         String strName = request.getParameter( PARAMETER_MEDIA_NAME );
275         String strDescription = request.getParameter( PARAMETER_MEDIA_DESCRIPTION );
276 
277         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
278         FileItem item = mRequest.getFile( PARAMETER_MEDIA_STYLESHEET );
279 
280         if ( ( strName.length(  ) == 0 ) || ( strDescription.length(  ) == 0 ) || ( strMediaId.length(  ) == 0 ) )
281         {
282             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
283         }
284 
285         int nMediaId = Integer.parseInt( strMediaId );
286 
287         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( nMediaId, getPlugin(  ) );
288         media.setMediaId( nMediaId );
289         media.setName( strName );
290         media.setDescription( strDescription );
291 
292         if ( ( item != null ) && ( item.getSize(  ) > 0 ) )
293         {
294             XmlTransformerService.clearXslCache(  );
295             media.setStyleSheetBytes( item.get(  ) );
296         }
297 
298         media.setIsMultipleMedia( Boolean.parseBoolean( request.getParameter( MARK_MEDIA_MULTIPLE ) ) );
299 
300         LibraryMediaHome.update( media, getPlugin(  ) );
301 
302         return getHomeUrl( request );
303     }
304 
305     /**
306      *
307      * @param request
308      * @return
309      */
310     public String getCreateAttribute( HttpServletRequest request )
311     {
312         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
313         setPageTitleProperty( PROPERTY_PAGE_TITLE_ATTRIBUTE_CREATION );
314 
315         Map<String, Object> model = new HashMap<String, Object>(  );
316         model.put( MARK_MEDIA_ID, strMediaId );
317         model.put( MARK_ATTRIBUTE_TYPE_LIST, getAttributeTypeReferenceList( getLocale(  ) ) );
318 
319         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_ATTRIBUTE, getLocale(  ), model );
320 
321         return getAdminPage( template.getHtml(  ) );
322     }
323 
324     /**
325      *
326      * @param request
327      * @return
328      */
329     public String doCreateAttribute( HttpServletRequest request )
330     {
331         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
332         String strCode = request.getParameter( PARAMETER_ATTRIBUTE_CODE );
333         String strDescription = request.getParameter( PARAMETER_ATTRIBUTE_DESCRIPTION );
334         String strType = request.getParameter( PARAMETER_ATTRIBUTE_TYPE );
335         String strDefaultValue = request.getParameter( PARAMETER_ATTRIBUTE_DEFAULT_VALUE );
336 
337         if ( ( strMediaId.length(  ) == 0 ) || ( strCode.length(  ) == 0 ) || ( strDescription.length(  ) == 0 ) ||
338                 ( strType.length(  ) == 0 ) )
339         {
340             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
341         }
342 
343         int nMediaId = Integer.parseInt( strMediaId );
344         int nType = Integer.parseInt( strType );
345 
346         MediaAttributeusiness/MediaAttribute.html#MediaAttribute">MediaAttribute attribute = new MediaAttribute(  );
347         attribute.setMediaId( nMediaId );
348         attribute.setCode( strCode );
349         attribute.setDescription( strDescription );
350         attribute.setTypeId( nType );
351         attribute.setDefaultValue( ( ( strDefaultValue != null ) && ( nType == MediaAttribute.ATTRIBUTE_TYPE_TEXT_USER ) )
352             ? strDefaultValue : "" );
353 
354         MediaAttributeHome.create( attribute, getPlugin(  ) );
355 
356         return JSP_URL_MEDIA + "?" + PARAMETER_MEDIA_ID + "=" + nMediaId;
357     }
358 
359     /**
360      *
361      * @param request
362      * @return
363      */
364     public String getModifyAttribute( HttpServletRequest request )
365     {
366         setPageTitleProperty( PROPERTY_PAGE_TITLE_ATTRIBUTE_MODIFICATION );
367 
368         String strAttributeId = request.getParameter( PARAMETER_ATTRIBUTE_ID );
369         int nAttributeId = Integer.parseInt( strAttributeId );
370 
371         MediaAttribute attribute = MediaAttributeHome.findByPrimaryKey( nAttributeId, getPlugin(  ) );
372         HashMap<String, Object> model = new HashMap<String, Object>(  );
373 
374         model.put( MARK_ATTRIBUTE, attribute );
375         model.put( MARK_ATTRIBUTE_TYPE_LIST, getAttributeTypeReferenceList( getLocale(  ) ) );
376 
377         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_ATTRIBUTE, getLocale(  ), model );
378 
379         return getAdminPage( template.getHtml(  ) );
380     }
381 
382     /**
383      *
384      * @param request
385      * @return
386      */
387     public String doModifyAttribute( HttpServletRequest request )
388     {
389         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
390         String strAttributeId = request.getParameter( PARAMETER_ATTRIBUTE_ID );
391         String strCode = request.getParameter( PARAMETER_ATTRIBUTE_CODE );
392         String strDescription = request.getParameter( PARAMETER_ATTRIBUTE_DESCRIPTION );
393         String strType = request.getParameter( PARAMETER_ATTRIBUTE_TYPE );
394         String strDefaultValue = request.getParameter( PARAMETER_ATTRIBUTE_DEFAULT_VALUE );
395 
396         if ( ( strMediaId.length(  ) == 0 ) || ( strAttributeId.length(  ) == 0 ) || ( strCode.length(  ) == 0 ) ||
397                 ( strDescription.length(  ) == 0 ) || ( strType.length(  ) == 0 ) )
398         {
399             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
400         }
401 
402         int nMediaId = Integer.parseInt( strMediaId );
403         int nAttributeId = Integer.parseInt( strAttributeId );
404         int nType = Integer.parseInt( strType );
405 
406         MediaAttributeusiness/MediaAttribute.html#MediaAttribute">MediaAttribute attribute = new MediaAttribute(  );
407         attribute.setMediaId( nMediaId );
408         attribute.setAttributeId( nAttributeId );
409         attribute.setCode( strCode );
410         attribute.setDescription( strDescription );
411         attribute.setTypeId( nType );
412         attribute.setDefaultValue( ( ( strDefaultValue != null ) && ( nType == MediaAttribute.ATTRIBUTE_TYPE_TEXT_USER ) )
413             ? strDefaultValue : "" );
414 
415         MediaAttributeHome.update( attribute, getPlugin(  ) );
416 
417         return JSP_URL_MEDIA + "?" + PARAMETER_MEDIA_ID + "=" + nMediaId;
418     }
419 
420     /**
421      *
422      * @param request
423      * @return
424      */
425     public String getRemoveAttribute( HttpServletRequest request )
426     {
427         UrlItem url = new UrlItem( JSP_DO_REMOVE_ATTRIBUTE );
428         String strAttributeId = request.getParameter( PARAMETER_ATTRIBUTE_ID );
429         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
430         int nMediaId = Integer.parseInt( strMediaId );
431         int nAttributeId = Integer.parseInt( strAttributeId );
432         url.addParameter( PARAMETER_ATTRIBUTE_ID, nAttributeId );
433         url.addParameter( PARAMETER_MEDIA_ID, nMediaId );
434 
435         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_REMOVE_ATTRIBUTE, url.getUrl(  ),
436             AdminMessage.TYPE_CONFIRMATION );
437     }
438 
439     /**
440      *
441      * @param request
442      * @return
443      */
444     public String doRemoveAttribute( HttpServletRequest request )
445     {
446         String strAttributeId = request.getParameter( PARAMETER_ATTRIBUTE_ID );
447         int nAttributeId = Integer.parseInt( strAttributeId );
448         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
449 
450         // remove attribute
451         MediaAttributeHome.remove( nAttributeId, getPlugin(  ) );
452 
453         //remove existing mapping associations found for this attribute
454         LibraryMappingHome.removeMappingAttributeAssociation( nAttributeId, getPlugin(  ) );
455 
456         return JSP_URL_MEDIA + "?" + PARAMETER_MEDIA_ID + "=" + strMediaId;
457     }
458 
459     /**
460      *
461      * @param locale
462      * @return
463      */
464     private ReferenceList getAttributeTypeReferenceList( Locale locale )
465     {
466         ReferenceList refList = new ReferenceList(  );
467         ReferenceItem item = new ReferenceItem(  );
468         item.setCode( String.valueOf( MediaAttribute.ATTRIBUTE_TYPE_TEXT_USER ) );
469         item.setName( I18nService.getLocalizedString( MediaAttribute.PROPERTY_ATTRIBUTE_TYPE_LABEL_TEXT_USER, locale ) );
470         refList.add( item );
471         item = new ReferenceItem(  );
472         item.setCode( String.valueOf( MediaAttribute.ATTRIBUTE_TYPE_TITLE_DOCUMENT ) );
473         item.setName( I18nService.getLocalizedString( MediaAttribute.PROPERTY_ATTRIBUTE_TYPE_LABEL_TITLE_DOCUMENT,
474                 locale ) );
475         refList.add( item );
476         item = new ReferenceItem(  );
477         item.setCode( String.valueOf( MediaAttribute.ATTRIBUTE_TYPE_SUMMARY_DOCUMENT ) );
478         item.setName( I18nService.getLocalizedString( MediaAttribute.PROPERTY_ATTRIBUTE_TYPE_LABEL_SUMMARY_DOCUMENT,
479                 locale ) );
480         refList.add( item );
481         item = new ReferenceItem(  );
482         item.setCode( String.valueOf( MediaAttribute.ATTRIBUTE_TYPE_TEXT_DOCUMENT ) );
483         item.setName( I18nService.getLocalizedString( MediaAttribute.PROPERTY_ATTRIBUTE_TYPE_LABEL_TEXT_DOCUMENT, locale ) );
484         refList.add( item );
485         item = new ReferenceItem(  );
486         item.setCode( String.valueOf( MediaAttribute.ATTRIBUTE_TYPE_BINARY ) );
487         item.setName( I18nService.getLocalizedString( MediaAttribute.PROPERTY_ATTRIBUTE_TYPE_LABEL_BINARY, locale ) );
488         refList.add( item );
489 
490         return refList;
491     }
492 
493     /**
494      *
495      * @param request
496      * @return
497      */
498     public String getManageMappings( HttpServletRequest request )
499     {
500         setPageTitleProperty( PROPERTY_PAGE_TITLE_MAPPING_MANAGEMENT );
501 
502         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
503         int nMediaId = Integer.parseInt( strMediaId );
504 
505         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( nMediaId, getPlugin(  ) );
506         Collection<MediaAttribute> attributeList = MediaAttributeHome.findAllAttributesForMedia( nMediaId, getPlugin(  ) );
507         media.setMediaAttributeList( I18nService.localizeCollection( attributeList, getLocale(  ) ) );
508 
509         Collection<LibraryMapping> colMappings = LibraryMappingHome.findAllMappingsByMedia( nMediaId, getPlugin(  ) );
510 
511         for ( LibraryMapping mapping : colMappings )
512         {
513             if ( mapping.getAttributeAssociationList(  ) == null )
514             {
515                 continue;
516             }
517 
518             for ( AttributeAssociation association : mapping.getAttributeAssociationList(  ) )
519             {
520                 MediaAttribute mediaAttribute = MediaAttributeHome.findByPrimaryKey( association.getIdMediaAttribute(  ),
521                         getPlugin(  ) );
522                 DocumentAttribute documentAttribute = DocumentAttributeHome.findByPrimaryKey( association.getIdDocumentAttribute(  ) );
523 
524                 Map<String, Object> modelLabel = new HashMap<String, Object>(  );
525                 modelLabel.put( MARK_ATTRIBUTE, mediaAttribute );
526                 modelLabel.put( MARK_DOCUMENT_ATTRIBUTE, documentAttribute );
527 
528                 HtmlTemplate templateLabel = AppTemplateService.getTemplate( TEMPLATE_GENERATE_ASSOCIATION_LABEL,
529                         getLocale(  ), modelLabel );
530                 association.setAssociationLabel( templateLabel.getHtml(  ) );
531             }
532         }
533 
534         Map<String, Object> model = new HashMap<String, Object>(  );
535         model.put( MARK_MEDIA, media );
536         model.put( MARK_MAPPING_LIST, colMappings );
537         model.put( MARK_DOCUMENT_TYPE_LIST, DocumentTypeHome.getDocumentTypesList(  ) );
538 
539         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_MAPPINGS, getLocale(  ), model );
540 
541         return getAdminPage( template.getHtml(  ) );
542     }
543 
544     /**
545      *
546      * @param request
547      * @return
548      */
549     public String getCreateMapping( HttpServletRequest request )
550     {
551         setPageTitleProperty( PROPERTY_PAGE_TITLE_MAPPING_CREATION );
552 
553         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
554         int nMediaId = Integer.parseInt( strMediaId );
555         String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE );
556 
557         DocumentType documentType = DocumentTypeHome.findByPrimaryKey( strDocumentTypeCode );
558 
559         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( nMediaId, getPlugin(  ) );
560         Collection<MediaAttribute> attributeList = MediaAttributeHome.findAllAttributesForMedia( nMediaId, getPlugin(  ) );
561         media.setMediaAttributeList( I18nService.localizeCollection( attributeList, getLocale(  ) ) );
562 
563         Map<String, Object> model = new HashMap<String, Object>(  );
564         model.put( MARK_MEDIA, media );
565         model.put( MARK_DOCUMENT_TYPE, documentType );
566 
567         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MAPPING, getLocale(  ), model );
568 
569         return getAdminPage( template.getHtml(  ) );
570     }
571 
572     /**
573      *
574      * @param request
575      * @return
576      */
577     public String doCreateMapping( HttpServletRequest request )
578     {
579         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
580         int nMediaId = Integer.parseInt( strMediaId );
581 
582         String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE );
583 
584         LibraryMapping/business/LibraryMapping.html#LibraryMapping">LibraryMapping mapping = new LibraryMapping(  );
585         mapping.setCodeDocumentType( strDocumentTypeCode );
586         mapping.setIdMedia( nMediaId );
587 
588         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( nMediaId, getPlugin(  ) );
589         Collection<MediaAttribute> colMediaAttribute = MediaAttributeHome.findAllAttributesForMedia( nMediaId,
590                 getPlugin(  ) );
591         media.setMediaAttributeList( colMediaAttribute );
592         mapping.setAttributeAssociationList( new ArrayList<AttributeAssociation>(  ) );
593 
594         for ( MediaAttribute attribute : media.getMediaAttributeList(  ) )
595         {
596             if ( attribute.isAssociableWithDocument(  ) )
597             {
598                 String strParameterDocumentAttributeId = PARAMETER_PREFIX_DOCUMENT_ATTRIBUTE_ID +
599                     attribute.getAttributeId(  );
600                 String strDocumentAttributeId = request.getParameter( strParameterDocumentAttributeId );
601 
602                 if ( strDocumentAttributeId != null )
603                 {
604                     int nDocumentAttributeId = Integer.parseInt( strDocumentAttributeId );
605                     mapping.addAttributeAssociation( attribute.getAttributeId(  ), nDocumentAttributeId );
606                 }
607             }
608         }
609 
610         LibraryMappingHome.create( mapping, getPlugin(  ) );
611 
612         return JSP_URL_MAPPINGS + "?" + PARAMETER_MEDIA_ID + "=" + nMediaId;
613     }
614 
615     /**
616      *
617      * @param request
618      * @return
619      */
620     public String getModifyMapping( HttpServletRequest request )
621     {
622         setPageTitleProperty( PROPERTY_PAGE_TITLE_MAPPING_MODIFICATION );
623 
624         String strMappingId = request.getParameter( PARAMETER_MAPPING_ID );
625         int nMappingId = Integer.parseInt( strMappingId );
626 
627         LibraryMapping mapping = LibraryMappingHome.findByPrimaryKey( nMappingId, getPlugin(  ) );
628 
629         DocumentType documentType = DocumentTypeHome.findByPrimaryKey( mapping.getCodeDocumentType(  ) );
630 
631         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( mapping.getIdMedia(  ), getPlugin(  ) );
632         Collection<MediaAttribute> attributeList = MediaAttributeHome.findAllAttributesForMedia( mapping.getIdMedia(  ),
633                 getPlugin(  ) );
634         media.setMediaAttributeList( I18nService.localizeCollection( attributeList, getLocale(  ) ) );
635 
636         Map<String, Object> model = new HashMap<String, Object>(  );
637         model.put( MARK_MAPPING, mapping );
638         model.put( MARK_MEDIA, media );
639         model.put( MARK_DOCUMENT_TYPE, documentType );
640         model.put( MARK_DOCUMENT_TYPE_LIST, DocumentTypeHome.getDocumentTypesList(  ) );
641 
642         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_MAPPING, getLocale(  ), model );
643 
644         return getAdminPage( template.getHtml(  ) );
645     }
646 
647     /**
648      *
649      * @param request
650      * @return
651      */
652     public String doModifyMappingAssociations( HttpServletRequest request )
653     {
654         String strMappingId = request.getParameter( PARAMETER_MAPPING_ID );
655         int nMappingId = Integer.parseInt( strMappingId );
656 
657         String strMediaId = request.getParameter( PARAMETER_MEDIA_ID );
658         int nMediaId = Integer.parseInt( strMediaId );
659 
660         String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE );
661 
662         LibraryMapping/business/LibraryMapping.html#LibraryMapping">LibraryMapping mapping = new LibraryMapping(  );
663         mapping.setCodeDocumentType( strDocumentTypeCode );
664         mapping.setIdMedia( nMediaId );
665         mapping.setIdMapping( nMappingId );
666 
667         LibraryMedia media = LibraryMediaHome.findByPrimaryKey( nMediaId, getPlugin(  ) );
668         Collection<MediaAttribute> colMediaAttribute = MediaAttributeHome.findAllAttributesForMedia( nMediaId,
669                 getPlugin(  ) );
670         media.setMediaAttributeList( colMediaAttribute );
671 
672         for ( MediaAttribute attribute : media.getMediaAttributeList(  ) )
673         {
674             if ( attribute.isAssociableWithDocument(  ) )
675             {
676                 String strParameterDocumentAttributeId = PARAMETER_PREFIX_DOCUMENT_ATTRIBUTE_ID +
677                     attribute.getAttributeId(  );
678                 String strDocumentAttributeId = request.getParameter( strParameterDocumentAttributeId );
679 
680                 if ( strDocumentAttributeId != null )
681                 {
682                     int nDocumentAttributeId = Integer.parseInt( strDocumentAttributeId );
683                     mapping.addAttributeAssociation( attribute.getAttributeId(  ), nDocumentAttributeId );
684                 }
685             }
686         }
687 
688         LibraryMappingHome.update( mapping, getPlugin(  ) );
689 
690         return JSP_URL_MAPPINGS + "?" + PARAMETER_MEDIA_ID + "=" + nMediaId;
691     }
692 
693     /**
694      *
695      * @param request
696      * @return
697      */
698     public String getRemoveMapping( HttpServletRequest request )
699     {
700         UrlItem url = new UrlItem( JSP_DO_REMOVE_MAPPING );
701         String strMappingId = request.getParameter( PARAMETER_MAPPING_ID );
702         int nMappingId = Integer.parseInt( strMappingId );
703         url.addParameter( PARAMETER_MAPPING_ID, nMappingId );
704 
705         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_REMOVE_MAPPING, url.getUrl(  ),
706             AdminMessage.TYPE_CONFIRMATION );
707     }
708 
709     /**
710      * Do remove a mapping
711      * @param request The request
712      * @return The next URL to redirect to
713      */
714     public String doRemoveMapping( HttpServletRequest request )
715     {
716         String strMappingId = request.getParameter( PARAMETER_MAPPING_ID );
717         int nMappingId = Integer.parseInt( strMappingId );
718         LibraryMapping mapping = LibraryMappingHome.findByPrimaryKey( nMappingId, getPlugin(  ) );
719 
720         LibraryMappingHome.remove( nMappingId, getPlugin(  ) );
721 
722         return JSP_URL_MAPPINGS + "?" + PARAMETER_MEDIA_ID + "=" + mapping.getIdMedia(  );
723     }
724 }