View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of 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.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   * This class provides the user interface to manage DocumentList Portlet
66   */
67  public class DocumentListPortletJspBean extends PortletJspBean
68  {
69      // Right
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      // Constants
84      private static final long serialVersionUID = 1L;
85  
86      ////////////////////////////////////////////////////////////////////////////
87      // Class attributes
88  
89      /**
90       * Returns portlet's properties prefix
91       *
92       * @return prefix
93       */
94      public String getPropertiesPrefix(  )
95      {
96          return "portlet.document";
97      }
98  
99      /**
100      * Returns the Download portlet creation form
101      *
102      * @param request The http request
103      * @return The HTML form
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      * Returns the Download portlet modification form
120      *
121      * @param request The Http request
122      * @return The HTML form
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         // Format the specific modify form (composed of the list of articles and buttons to modify the order of the articles)
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      * Process portlet's creation
141      *
142      * @param request The Http request
143      * @return The Jsp management URL of the process result
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         //gets the identifier of the parent page
152         String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
153 
154         // get portlet common attributes
155         String strErrorUrl = setPortletCommonData( request, portlet );
156 
157         if ( strErrorUrl != null )
158         {
159             return strErrorUrl;
160         }
161 
162         portlet.setPageId( nIdPage );
163 
164         //gets the specific parameters
165         portlet.setDocumentTypeCode( strDocumentTypeCode );
166 
167         //Categories
168         portlet.setIdCategory( setIdCategory( request ) );
169 
170         //Portlet creation
171         DocumentListPortletHome.getInstance(  ).create( portlet );
172 
173         //Displays the page with the new Portlet
174         return getPageUrl( nIdPage );
175     }
176 
177     /**
178      * Process portlet's modification
179      *
180      * @param request The http request
181      * @return Management's Url
182      */
183     public String doModify( HttpServletRequest request )
184     {
185         // recovers portlet attributes
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         // retrieve portlet common attributes
213         String strErrorUrl = setPortletCommonData( request, portlet );
214 
215         if ( strErrorUrl != null )
216         {
217             return strErrorUrl;
218         }
219 
220         portlet.setDocumentTypeCode( strDocumentTypeCode );
221 
222         //Categories
223         portlet.setIdCategory( arrayIdCategories );
224 
225         // updates the portlet
226         portlet.update(  );
227 
228         // displays the page withe the potlet updated
229         return getPageUrl( portlet.getPageId(  ) );
230     }
231 
232     /**
233      * Set the array of id categories
234      * @param request The http servlet request
235      * @return The array of id categories
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      * Returns an html template containing the list of the portlet types
259      * @param strPortletId The Portet Identifier
260      * @return The html code
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      * Returns an html template containing the list of the categories
289      * @param strPortletId The Portet Identifier
290      * @param user The current user
291      * @return The html code
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 }