View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.forms;
20  
21  import bsh.Parser;
22  import groovy.lang.GroovyShell;
23  import org.apache.struts.action.ActionErrors;
24  import org.apache.struts.action.ActionMapping;
25  import org.apache.struts.action.ActionMessage;
26  import org.apache.struts.util.LabelValueBean;
27  import org.apache.struts.validator.ValidatorForm;
28  import org.itracker.core.resources.ITrackerResources;
29  import org.itracker.model.WorkflowScript;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import java.io.StringReader;
35  import java.util.ArrayList;
36  import java.util.List;
37  import java.util.Locale;
38  
39  import static org.itracker.model.util.WorkflowUtilities.*;
40  /**
41   * This is the LoginForm Struts Form. It is used by Login form.
42   *
43   * @author ready
44   */
45  public class WorkflowScriptForm extends ValidatorForm {
46      /**
47       *
48       */
49      private static final Logger log = LoggerFactory.getLogger(WorkflowScriptForm.class);
50      private static final long serialVersionUID = 1L;
51      String action;
52      Integer id;
53      String name;
54      Integer event;
55      String script;
56      String language;
57  
58      private List<LabelValueBean> eventOptions;
59  
60      public void initEventOptions(Locale locale) {
61          if (null != eventOptions) {
62              return;
63          }
64          eventOptions = new ArrayList<>(5);
65          String[] eventTypes = new String[]{String.valueOf(EVENT_FIELD_ONPOPULATE),
66                  String.valueOf(EVENT_FIELD_ONSETDEFAULT),
67                  String.valueOf(EVENT_FIELD_ONVALIDATE),
68                  String.valueOf(EVENT_FIELD_ONPRESUBMIT),
69                  String.valueOf(EVENT_FIELD_ONPOSTSUBMIT),
70          };
71  
72          for (String eventType : eventTypes) {
73              eventOptions.add(new LabelValueBean(
74                      ITrackerResources.getString(ITrackerResources.KEY_BASE_WORKFLOW_EVENT + eventType, locale),
75                      eventType));
76          }
77  
78      }
79  
80      public List<LabelValueBean> getEventOptions() {
81          return eventOptions;
82      }
83  
84  
85      public void reset(ActionMapping mapping, HttpServletRequest request) {
86          action = null;
87          id = null;
88          name = null;
89          event = null;
90          script = null;
91      }
92  
93      public ActionErrors validate(ActionMapping mapping,
94                                   HttpServletRequest request) {
95          ActionErrors errors = super.validate(mapping, request);
96  
97          if (WorkflowScript.ScriptLanguage.Groovy != WorkflowScript.ScriptLanguage.valueOf(getLanguage())) {
98              validateBeanShellScript(errors);
99          } else {
100             validateGroovyScript(errors);
101         }
102         return errors;
103     }
104 
105     private void validateBeanShellScript(ActionErrors errors) {
106         try {
107             Parser parser = new Parser(new StringReader(getScript()));
108             boolean eof;
109             while (!(eof = parser.Line())) {
110             }
111         } catch (Exception e) {
112             addScriptError(errors, e);
113         }
114     }
115 
116     private void validateGroovyScript(ActionErrors errors) {
117         try {
118             new GroovyShell().parse(getScript());
119         } catch (Exception e) {
120             addScriptError(errors, e);
121         }
122     }
123 
124     private void addScriptError(ActionErrors errors, Exception e) {
125         errors.add("script", new ActionMessage("itracker.web.error.invalidscriptdata", e.getMessage()));
126     }
127 
128     public String getAction() {
129         return action;
130     }
131 
132     public void setAction(String action) {
133         this.action = action;
134     }
135 
136     public Integer getEvent() {
137         return event;
138     }
139 
140     public void setEvent(Integer event) {
141         this.event = event;
142     }
143 
144     public Integer getId() {
145         return id;
146     }
147 
148     public void setId(Integer id) {
149         this.id = id;
150     }
151 
152     public String getName() {
153         return name;
154     }
155 
156     public void setName(String name) {
157         this.name = name;
158     }
159 
160     public String getScript() {
161         return script;
162     }
163 
164     public void setScript(String script) {
165         this.script = script;
166     }
167 
168     public String getLanguage() {
169         return language;
170     }
171 
172     public void setLanguage(String language) {
173         this.language = language;
174     }
175 }