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.document.web.rules;
35
36 import fr.paris.lutece.plugins.document.business.rules.Rule;
37 import fr.paris.lutece.plugins.document.business.rules.RuleHome;
38 import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
39 import fr.paris.lutece.plugins.document.utils.IntegerUtils;
40 import fr.paris.lutece.portal.business.user.AdminUser;
41 import fr.paris.lutece.portal.service.message.AdminMessage;
42 import fr.paris.lutece.portal.service.message.AdminMessageService;
43 import fr.paris.lutece.portal.service.template.AppTemplateService;
44 import fr.paris.lutece.portal.service.util.AppPathService;
45 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
46 import fr.paris.lutece.portal.web.constants.Messages;
47 import fr.paris.lutece.util.html.HtmlTemplate;
48 import fr.paris.lutece.util.url.UrlItem;
49
50 import org.apache.commons.lang3.StringUtils;
51
52 import java.util.ArrayList;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Map;
56
57 import javax.servlet.http.HttpServletRequest;
58
59
60
61
62
63 public class DocumentRulesJspBean extends PluginAdminPageJspBean
64 {
65 public static final String RIGHT_RULES_MANAGEMENT = "DOCUMENT_RULES_MANAGEMENT";
66
67
68
69
70 private static final long serialVersionUID = -1195569288911640639L;
71 private static final String JSP_DO_DELETE_RULE = "jsp/admin/plugins/document/DoDeleteRule.jsp";
72 private static final String JSP_SELECT_SPACE = "jsp/admin/plugins/document/SelectSpace.jsp";
73 private static final String JSP_CREATE_RULE_SESSION = "jsp/admin/plugins/document/CreateRuleSession.jsp";
74 private static final String TEMPLATE_MANAGE_RULES = "/admin/plugins/document/rules/manage_rules.html";
75 private static final String TEMPLATE_CREATE_RULE = "/admin/plugins/document/rules/create_rule.html";
76 private static final String TEMPLATE_SELECT_SPACE = "/admin/plugins/document/rules/select_space_rule.html";
77 private static final String MARK_RULES_LIST = "rules_list";
78 private static final String MARK_RULE_TYPES_LIST = "rule_types_list";
79 private static final String MARK_CREATE_FORM = "create_form";
80 private static final String MARK_RULE_TYPE_ID = "rule_type_id";
81 private static final String MARK_SPACES_BROWSER = "spaces_browser";
82 private static final String MARK_SUBMIT_BUTTON_DISABLED = "submit_button_disabled";
83 private static final String PARAMETER_RULE_TYPE_ID = "id_rule_type";
84 private static final String PARAMETER_RULE_ID = "id_rule";
85 private static final String PARAMETER_RULE_ATTRIBUTE_SELECTED = "rule_attribute_selected";
86 private static final String PARAMETER_CANCEL = "cancel";
87 private static final String PROPERTY_CONFIRM_RULE_DELETE = "document.rules.message.confirmRuleDelete";
88 private static final String PROPERTY_RULE_ALREADY_EXISTS = "document.rules.message.ruleAlreadyExists";
89 private static final String PROPERTY_CREATE_RULE_PAGE_TITLE = "document.create_rule.pageTitle";
90 private static final String PROPERTY_SELECT_SPACE_PAGE_TITLE = "document.create_rule.selectSpace";
91 private Rule _rule;
92 private String _StrRuleAttributeSelected;
93
94
95
96
97
98 public Rule getRuleSession( )
99 {
100 return _rule;
101 }
102
103
104
105
106
107 public void setRuleSession( Rule rule )
108 {
109 _rule = rule;
110 }
111
112
113
114
115
116 public String getRuleAttributeSelectedSession( )
117 {
118 return _StrRuleAttributeSelected;
119 }
120
121
122
123
124
125 public void setRuleAttributeSelectedSession( String ruleAttributeSelected )
126 {
127 _StrRuleAttributeSelected = ruleAttributeSelected;
128 }
129
130
131
132
133
134
135 public String getManageRules( HttpServletRequest request )
136 {
137 setPageTitleProperty( null );
138
139 AdminUser user = getUser( );
140
141 List<Rule> listRules = RuleHome.findAll( getLocale( ) );
142 List<Rule> listRulesAuthorized = new ArrayList<Rule>( );
143
144 for ( Rule rule : listRules )
145 {
146 if ( rule.isAuthorized( user ) )
147 {
148 rule.setUser( user );
149 listRulesAuthorized.add( rule );
150 }
151 }
152
153 Map<String, Object> model = new HashMap<String, Object>( );
154 model.put( MARK_RULES_LIST, listRulesAuthorized );
155 model.put( MARK_RULE_TYPES_LIST, RuleHome.getRuleTypesList( getLocale( ) ) );
156
157 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_RULES, getLocale( ), model );
158
159 return getAdminPage( template.getHtml( ) );
160 }
161
162
163
164
165
166
167 public String getSelectSpace( HttpServletRequest request )
168 {
169 setPageTitleProperty( PROPERTY_SELECT_SPACE_PAGE_TITLE );
170
171 String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
172 boolean bSubmitButtonDisabled = Boolean.TRUE;
173
174 if ( StringUtils.isNotBlank( strSpaceId ) )
175 {
176 bSubmitButtonDisabled = Boolean.FALSE;
177 }
178
179 Map<String, Object> model = new HashMap<String, Object>( );
180
181 model.put( MARK_SUBMIT_BUTTON_DISABLED, bSubmitButtonDisabled );
182 model.put( MARK_SPACES_BROWSER,
183 DocumentSpacesService.getInstance( ).getSpacesBrowser( request, getUser( ), getLocale( ), false, true ) );
184
185 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECT_SPACE, getLocale( ), model );
186
187 return getAdminPage( template.getHtml( ) );
188 }
189
190
191
192
193
194
195 public String doSelectSpace( HttpServletRequest request )
196 {
197 String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
198
199 if ( strSpaceId == null )
200 {
201 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
202 }
203
204 int nSpaceId = IntegerUtils.convert( strSpaceId );
205 getRuleSession( ).setAttribute( getRuleAttributeSelectedSession( ), Integer.toString( nSpaceId ) );
206
207 return AppPathService.getBaseUrl( request ) + JSP_CREATE_RULE_SESSION;
208 }
209
210
211
212
213
214
215 public String getCreateRule( HttpServletRequest request )
216 {
217 setPageTitleProperty( PROPERTY_CREATE_RULE_PAGE_TITLE );
218
219 String strRuleTypeId;
220 Rule rule;
221
222 if ( getRuleSession( ) == null )
223 {
224 strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
225 rule = RuleHome.newInstance( strRuleTypeId );
226 }
227 else
228 {
229 rule = getRuleSession( );
230 strRuleTypeId = rule.getRuleTypeId( );
231 }
232
233 Map<String, Object> model = new HashMap<String, Object>( );
234 model.put( MARK_CREATE_FORM, rule.getCreateForm( getUser( ), getLocale( ) ) );
235 model.put( MARK_RULE_TYPE_ID, strRuleTypeId );
236
237 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_RULE, getLocale( ), model );
238
239 return getAdminPage( template.getHtml( ) );
240 }
241
242
243
244
245
246
247 public String doCreateRule( HttpServletRequest request )
248 {
249 if ( request.getParameter( PARAMETER_CANCEL ) == null )
250 {
251 String strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
252 Rule rule = RuleHome.newInstance( strRuleTypeId );
253 rule.readAttributes( request );
254
255 String strValidation = rule.validateRule( );
256
257 if ( strValidation != null )
258 {
259 return AdminMessageService.getMessageUrl( request, strValidation, AdminMessage.TYPE_STOP );
260 }
261
262
263 for ( Rule checkRule : RuleHome.findByRuleTypeKey( rule.getRuleTypeId( ) ) )
264 {
265 if ( rule.equals( checkRule ) )
266 {
267 return AdminMessageService.getMessageUrl( request, PROPERTY_RULE_ALREADY_EXISTS,
268 AdminMessage.TYPE_STOP );
269 }
270 }
271
272 RuleHome.create( rule );
273 }
274
275 return getHomeUrl( request );
276 }
277
278
279
280
281
282
283 public String doSaveRuleSession( HttpServletRequest request )
284 {
285 String strRuleAttributeNameSelected = request.getParameter( PARAMETER_RULE_ATTRIBUTE_SELECTED );
286 String strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
287 Rule rule = RuleHome.newInstance( strRuleTypeId );
288 rule.readAttributes( request );
289 setRuleSession( rule );
290 setRuleAttributeSelectedSession( strRuleAttributeNameSelected );
291
292 return AppPathService.getBaseUrl( request ) + JSP_SELECT_SPACE + "?" +
293 DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID + "=" +
294 rule.getAttribute( strRuleAttributeNameSelected );
295 }
296
297
298
299
300
301
302 public String deleteRule( HttpServletRequest request )
303 {
304 String strRuleId = request.getParameter( PARAMETER_RULE_ID );
305 Rule rule = RuleHome.findByPrimaryKey( IntegerUtils.convert( strRuleId ) );
306 UrlItem url = new UrlItem( JSP_DO_DELETE_RULE );
307 url.addParameter( PARAMETER_RULE_ID, strRuleId );
308 rule.setLocale( getLocale( ) );
309
310 String[] args = { rule.getRule( ) };
311
312 return AdminMessageService.getMessageUrl( request, PROPERTY_CONFIRM_RULE_DELETE, args, url.getUrl( ),
313 AdminMessage.TYPE_CONFIRMATION );
314 }
315
316
317
318
319
320
321 public String doDeleteRule( HttpServletRequest request )
322 {
323 String strRuleId = request.getParameter( PARAMETER_RULE_ID );
324 RuleHome.remove( IntegerUtils.convert( strRuleId ) );
325
326 return getHomeUrl( request );
327 }
328 }