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.calendar.modules.document.web;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentFilter;
38  import fr.paris.lutece.plugins.document.business.DocumentHome;
39  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortletHome;
40  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
41  import fr.paris.lutece.portal.business.page.Page;
42  import fr.paris.lutece.portal.business.page.PageHome;
43  import fr.paris.lutece.portal.business.portlet.Portlet;
44  import fr.paris.lutece.portal.business.portlet.PortletHome;
45  import fr.paris.lutece.portal.business.user.AdminUser;
46  import fr.paris.lutece.portal.service.admin.AdminUserService;
47  import fr.paris.lutece.portal.service.i18n.I18nService;
48  import fr.paris.lutece.portal.service.message.AdminMessage;
49  import fr.paris.lutece.portal.service.message.AdminMessageService;
50  import fr.paris.lutece.portal.service.template.AppTemplateService;
51  import fr.paris.lutece.portal.service.util.AppPathService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
54  import fr.paris.lutece.portal.web.constants.Messages;
55  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
56  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  import fr.paris.lutece.util.url.UrlItem;
59  
60  import org.apache.commons.lang.StringEscapeUtils;
61  
62  import java.util.ArrayList;
63  import java.util.Collection;
64  import java.util.HashMap;
65  import java.util.Map;
66  
67  import javax.servlet.http.HttpServletRequest;
68  
69  
70  /**
71   * This class provides the user interface to insert an id of a document
72   *
73   */
74  public class CalendarDocInsertServiceJspBean
75  {
76      private static final long serialVersionUID = 2694692453596836769L;
77  
78      ////////////////////////////////////////////////////////////////////////////
79      // Constants
80      private static final String REGEX_ID = "^[\\d]+$";
81  
82      // Templates
83      private static final String TEMPLATE_INSERT_INTO_ELEMENT = "admin/plugins/calendar/modules/document/admin/insert/insert_into_element.html";
84      private static final String TEMPLATE_SELECTOR_DOCUMENT = "admin/plugins/calendar/modules/document/admin/insert/document_selector.html";
85  
86      // Parameters
87      private static final String PARAMETER_DOCUMENT_ID = "document_id";
88      private static final String PARAMETER_INPUT = "input";
89  
90      // Marker
91      private static final String MARK_DOCUMENTS_LIST = "documents_list";
92      private static final String MARK_INPUT = "input";
93      private static final String MARK_INSERT = "insert";
94      private static final String PROPERTY_DOCUMENT_CALENDAR_TYPE = "calendar-document.calendar.document.type";
95  
96      // private
97      private AdminUser _user;
98      private String _input;
99  
100     /**
101      * Initialize data
102      *
103      * @param request The HTTP request
104      */
105     public void init( HttpServletRequest request )
106     {
107         _user = AdminUserService.getAdminUser( request );
108         _input = request.getParameter( PARAMETER_INPUT );
109     }
110 
111     /**
112      * Entry point of the insert service
113      * Return the html form for document selection.
114      *
115      * @param request The HTTP request
116      * @return The html form of the document selection page
117      */
118     public String getSelectDocument( HttpServletRequest request )
119     {
120         init( request );
121 
122         DocumentFilter docFilter = new DocumentFilter(  );
123         docFilter.setCodeDocumentType( AppPropertiesService.getProperty( PROPERTY_DOCUMENT_CALENDAR_TYPE ) );
124 
125         Collection<Document> listDocuments = DocumentHome.findByFilter( docFilter, I18nService.getDefaultLocale(  ) );
126 
127         HashMap model = getDefaultModel(  );
128         model.put( MARK_INPUT, _input );
129         model.put( MARK_DOCUMENTS_LIST, listDocuments );
130 
131         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_DOCUMENT, _user.getLocale(  ), model );
132 
133         return template.getHtml(  );
134     }
135 
136     /**
137     * Insert the specified url into HTML content
138     *
139     * @param request The HTTP request
140     * @return The url
141     */
142     public String doInsertUrl( HttpServletRequest request )
143     {
144         init( request );
145 
146         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
147 
148         if ( ( strDocumentId == null ) || !strDocumentId.matches( REGEX_ID ) )
149         {
150             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
151         }
152 
153         String strInsert = request.getParameter( PARAMETER_DOCUMENT_ID );
154 
155         Map<String, String> model = new HashMap<String, String>(  );
156         model.put( MARK_INPUT, _input );
157         model.put( MARK_INSERT, strInsert );
158 
159         HtmlTemplate template;
160 
161         template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT, request.getLocale(  ), model );
162 
163         return template.getHtml(  );
164     }
165 
166     /**
167      * Get the default model for selection templates
168      *
169      * @return The default model
170      */
171     private HashMap getDefaultModel(  )
172     {
173         HashMap model = new HashMap(  );
174         model.put( MARK_INPUT, _input );
175 
176         return model;
177     }
178 }