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