View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.form.web;
35  
36  import fr.paris.lutece.plugins.form.business.Form;
37  import fr.paris.lutece.plugins.form.business.FormAction;
38  import fr.paris.lutece.plugins.form.business.FormActionHome;
39  import fr.paris.lutece.plugins.form.business.FormFilter;
40  import fr.paris.lutece.plugins.form.business.FormHome;
41  import fr.paris.lutece.plugins.form.business.FormSubmitHome;
42  import fr.paris.lutece.plugins.form.service.FormPlugin;
43  import fr.paris.lutece.plugins.form.service.FormResourceIdService;
44  import fr.paris.lutece.plugins.genericattributes.business.ResponseFilter;
45  import fr.paris.lutece.portal.business.rbac.RBAC;
46  import fr.paris.lutece.portal.business.right.Right;
47  import fr.paris.lutece.portal.business.right.RightHome;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.dashboard.DashboardComponent;
50  import fr.paris.lutece.portal.service.database.AppConnectionService;
51  import fr.paris.lutece.portal.service.plugin.Plugin;
52  import fr.paris.lutece.portal.service.rbac.RBACService;
53  import fr.paris.lutece.portal.service.template.AppTemplateService;
54  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  import fr.paris.lutece.util.url.UrlItem;
57  
58  import org.apache.commons.lang.StringUtils;
59  
60  import java.util.ArrayList;
61  import java.util.HashMap;
62  import java.util.List;
63  import java.util.Locale;
64  import java.util.Map;
65  
66  import javax.servlet.http.HttpServletRequest;
67  
68  /**
69   * Calendar Dashboard Component This component displays directories
70   */
71  public class FormDashboardComponent extends DashboardComponent
72  {
73      private static final String MARK_URL = "url";
74      private static final String MARK_ICON = "icon";
75      private static final String MARK_FORM_LIST = "form_list";
76      private static final String MARK_PERMISSION_CREATE_FORM = "permission_create_form";
77      private static final String MARK_RESPONSE_COUNT_MAP = "response_count_map";
78      private static final String MARK_AUTHORIZED_FORM_MODIFICATION_LIST = "authorized_form_modification_list";
79      private static final String MARK_PERMISSION_CREATE = "permission_create";
80      private static final String TEMPLATE_DASHBOARD = "/admin/plugins/form/form_dashboard.html";
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public String getDashboardData( AdminUser user, HttpServletRequest request )
87      {
88          Plugin plugin = getPlugin( );
89  
90          if ( !plugin.isInstalled( ) || StringUtils.isBlank( plugin.getDbPoolName( ) ) || AppConnectionService.NO_POOL_DEFINED.equals( plugin.getDbPoolName( ) ) )
91          {
92              return StringUtils.EMPTY;
93          }
94  
95          Right right = RightHome.findByPrimaryKey( getRight( ) );
96          Locale locale = user.getLocale( );
97          List<FormAction> listActionsForFormEnable;
98          List<FormAction> listActionsForFormDisable;
99          List<FormAction> listActions;
100 
101         UrlItem url = new UrlItem( right.getUrl( ) );
102         url.addParameter( FormPlugin.PLUGIN_NAME, right.getPluginName( ) );
103 
104         // build Filter
105         FormFilter formFilter = new FormFilter( );
106 
107         List<Form> listForm = FormHome.getFormList( formFilter, getPlugin( ) );
108         listForm = (List<Form>) AdminWorkgroupService.getAuthorizedCollection( listForm, user );
109 
110         Map<String, Object> model = new HashMap<String, Object>( );
111 
112         listActionsForFormEnable = FormActionHome.selectActionsByFormState( Form.STATE_ENABLE, plugin, locale );
113         listActionsForFormDisable = FormActionHome.selectActionsByFormState( Form.STATE_DISABLE, plugin, locale );
114 
115         Map<String, Object> nCountResponseMap = new HashMap<String, Object>( );
116         List<Integer> nAuthorizedModificationList = new ArrayList<Integer>( );
117 
118         for ( Form form : listForm )
119         {
120             if ( form.isActive( ) )
121             {
122                 listActions = listActionsForFormEnable;
123             }
124             else
125             {
126                 listActions = listActionsForFormDisable;
127             }
128 
129             listActions = (List<FormAction>) RBACService.getAuthorizedActionsCollection( listActions, form, user );
130             form.setActions( listActions );
131 
132             ResponseFilter responseFilter = new ResponseFilter( );
133             responseFilter.setIdResource( form.getIdForm( ) );
134             nCountResponseMap.put( form.getIdForm( ) + "", FormSubmitHome.getCountFormSubmit( responseFilter, plugin ) );
135 
136             if ( RBACService.isAuthorized( form, FormResourceIdService.PERMISSION_MODIFY, user ) )
137             {
138                 nAuthorizedModificationList.add( form.getIdForm( ) );
139             }
140         }
141 
142         model.put( MARK_FORM_LIST, listForm );
143 
144         if ( RBACService.isAuthorized( Form.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, FormResourceIdService.PERMISSION_CREATE, user ) )
145         {
146             model.put( MARK_PERMISSION_CREATE_FORM, true );
147         }
148         else
149         {
150             model.put( MARK_PERMISSION_CREATE_FORM, false );
151         }
152 
153         model.put( MARK_URL, url.getUrl( ) );
154         model.put( MARK_ICON, plugin.getIconUrl( ) );
155         model.put( MARK_RESPONSE_COUNT_MAP, nCountResponseMap );
156         model.put( MARK_AUTHORIZED_FORM_MODIFICATION_LIST, nAuthorizedModificationList );
157         model.put( MARK_PERMISSION_CREATE,
158                 RBACService.isAuthorized( Form.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, FormResourceIdService.PERMISSION_CREATE, user ) );
159 
160         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD, user.getLocale( ), model );
161 
162         return t.getHtml( );
163     }
164 }