1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
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 }