Changeset 4764
- Timestamp:
- 07/02/08 09:16:16 (5 months ago)
- Files:
-
- openmrs-modules/groovyforms/web/module/createGroovyForms.jsp (modified) (4 diffs)
- openmrs-modules/groovyforms/web/module/resources/jquery-1.2.6.js (added)
- openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/CreateGroovyFormServlet.groovy (modified) (7 diffs)
- openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/GroovyFormsServlet.groovy (copied) (copied from openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/GroovyFormsServlet.java) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs-modules/groovyforms/web/module/createGroovyForms.jsp
r4746 r4764 19 19 20 20 <%@ include file="/WEB-INF/template/header.jsp" %> 21 22 21 <openmrs:htmlInclude file="/dwr/interface/DWRGroovyFormsService.js"/> 23 22 <openmrs:htmlInclude file="/dwr/engine.js"/> 24 23 <openmrs:htmlInclude file="/dwr/util.js"/> 25 24 25 <script type="text/javascript" src="${pageContext.request.contextPath}/moduleResources/groovyforms/jquery-1.2.6.js"> </script> 26 26 <script type="text/javascript"> 27 document.body.onload = function() { 28 document.getElementById("formName").focus() 29 } 30 function AjaxValidation(url,callback) { 31 var req = init() 32 req.onreadystatechange = processRequest 27 $(window).ready(function() { 28 $("#groovyModel").bind("blur",function() { 29 $.ajax({ 30 ajaxStart: $("#out").html(""), 31 type:'POST', 32 data: { groovyModel:$("#groovyModel").val() }, 33 url: "${pageContext.request.contextPath}/moduleServlet/groovyforms/createGroovyForm", 34 cache: false, 35 success: function(data) { 36 var res = $(data).find("result").text() 37 $("#out").html(res) 33 38 34 function init() { 35 if(window.XMLHttpRequest) { 36 return new XMLHttpRequest() 37 } else if(window.ActiveXObject) { 38 return new ActiveXObject("Mircosoft.XMLHTTP") 39 } 39 } 40 }) 41 }); 42 }) 43 function stepTwo(valid) { 44 if(valid == "true") { 45 document.getElementById("stepTwo").style.display = "block" 40 46 } 41 42 function processRequest() { 43 if(req.readyState == 4) { 44 if(req.status == 200) { 45 if(callback) { 46 chkSyntaxCallBack(req.responseXML) 47 } 48 } 49 } 50 } 51 this.doGet = function() { 52 req.open("GET",url,true) 53 req.send(null) 54 } 55 } 56 function chkSyntaxCallBack(responseXML) { 57 var res = responseXML.getElementsByTagName("result")[0].firstChild.nodeValue 58 document.getElementById("out").innerHTML = res 59 } 60 61 function checkSyntax() { 62 var target = document.getElementById("groovyModel") 63 var url = "${pageContext.request.contextPath}/moduleServlet/groovyforms/createGroovyForm?groovyModel="+escape(target.value) 64 var ajax = new AjaxValidation(url, chkSyntaxCallBack) 65 ajax.doGet() 66 } 47 } 67 48 </script> 68 49 … … 72 53 73 54 <br/> 74 < !--<form action="${pageContext.request.contextPath}/moduleServlet/groovyforms/createGroovyForm" method="post"> -->55 <form action="${pageContext.request.contextPath}/moduleServlet/groovyforms/createGroovyForm" method="post"> 75 56 <b><spring:message code="groovyforms.label.title"/></b> 76 57 <br/><br/> … … 98 79 <div id="stepTwo" style="display:none"> 99 80 <b><spring:message code="groovyforms.label.template"/><br/></b> 100 <textarea rows="80" cols="10" id="template"> 101 81 <textarea rows="10" cols="80" id="template"> 102 82 </textarea> 103 <input type="submit" onclick="generateForm()" value="<spring:message code="groovyforms.generate-form"/>"/>104 83 </div> 105 84 <br/> … … 110 89 <br /> 111 90 <b><spring:message code="groovyforms.label.controller"/> </b> 112 <textarea id="controller" rows=" 80" cols="10">91 <textarea id="controller" rows="10" cols="80"> 113 92 </textarea> 114 <input type="submit" onclick="saveForm()" value="<spring:message code="groovyforms.finalize"/>"/>93 <input type="submit" value="<spring:message code="groovyforms.finalize"/>"/> 115 94 </div> 116 95 <br/> 117 <input type="submit" onclick="checkSyntax()" value="<spring:message code="groovyforms.checkSyntax"/>"/> 118 119 <input type="submit" value="Foo"/> 120 121 <input type="submit" onclick="generateTemplate()" value="<spring:message code="groovyforms.generate-template"/>" /> 122 <!--</form> --> 96 </form> 123 97 <br/> 124 98 <a href="http://groovy.codehaus.org/Documentation" target="_groovy_doc"><spring:message openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/CreateGroovyFormServlet.groovy
r4746 r4764 20 20 import org.apache.commons.logging.LogFactory 21 21 import org.codehaus.groovy.control.CompilationFailedException 22 import org.codehaus.groovy.control.CompilerConfiguration23 22 24 23 class CreateGroovyFormServlet extends HttpServlet { … … 29 28 @Override 30 29 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 def clazz = request.getParameter("groovyModel") 30 def generateTemplate = request.getParameter("template") 31 def generateController = request.getParameter("controller") 32 def finalMarkup = request.getParameter("markup") 33 def clazz = URLDecoder.decode(request.getParameter("groovyModel")) 32 34 def name = request.getParameter("formName") 33 35 def version = request.getParameter("version") 34 36 def res = this.checkSyntax(clazz) 35 37 if (clazz) { 36 response.setContentType "text/xml" 37 response.setHeader "Cache-Conrol", "no-cache" 38 response.writer.write "<result>\n\t$res\n</result>" 38 if (checkSyntax(clazz)) { 39 response.contentType = "text/xml" 40 response.setHeader "Cache-Conrol", "no-cache" 41 response.writer.write "<result>\n\t$res\n</result>" 42 } else { 43 response.contentType = "text/xml" 44 response.setHeader "Cache-Control", "no-cache" 45 response.writer.write "<result>true</result>" 46 } 39 47 } else { 40 response. setContentType"text/xml"48 response.contentType = "text/xml" 41 49 response.setHeader "Cache-Control", "no-cache" 42 50 response.writer.write "<result>\n\tPlease fill in the Form Model\n</result>" … … 46 54 @Override 47 55 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 56 doGet(request,response) 48 57 } 49 58 … … 56 65 57 66 def getClassLoader() { 58 def conf = new CompilerConfiguration() 59 def gcl = new GroovyClassLoader(this.getClass().getClassLoader(), conf) 67 def gcl = new GroovyClassLoader(this.getClass().getClassLoader()) 60 68 gcl 61 69 } … … 70 78 sb << "import org.openmrs.*\n\n\n" 71 79 sb << clazz 72 def res = ""80 def res = null 73 81 try { 74 if (getClassLoader().parseClass(sb.toString())) { 75 res = "Congrats! The syntax is valid" 76 } 82 getClassLoader().parseClass(sb.toString()) 83 77 84 } catch (CompilationFailedException e) { 78 final Writer trace = new StringWriter()79 final PrintWriter printWriter = new PrintWriter(trace)80 e.printStackTrace(printWriter)81 85 res = "Exception: ${e.message}" 82 86 } … … 86 90 87 91 /** 88 * Check if it is validgroovy code.92 * Check if it is result groovy code. 89 93 * @param clazz the class 90 * @return whether or not it is validgroovy code94 * @return whether or not it is result groovy code 91 95 */ 92 def is ValidGroovy(clazz) {96 def isresultGroovy(clazz) { 93 97 def sb = new StringBuilder() 94 98 sb << "import org.openmrs.*\n\n\n" … … 101 105 return true 102 106 } 103 104 107 } openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/GroovyFormsServlet.groovy
r4746 r4764 12 12 * Copyright (C) OpenMRS, LLC. All Rights Reserved. 13 13 */ 14 package org.openmrs.module.groovyforms.web ;14 package org.openmrs.module.groovyforms.web 15 15 16 import org.openmrs.module.groovyforms.metadata.model.GroovyForm; 17 import org.openmrs.module.groovyforms.metadata.model.GroovyFormsContainer; 18 import org.openmrs.module.groovyforms.util.GroovyFormsUtil; 19 20 import javax.servlet.ServletException; 21 import javax.servlet.http.HttpServlet; 22 import javax.servlet.http.HttpServletRequest; 23 import javax.servlet.http.HttpServletResponse; 24 import java.io.IOException; 25 import java.io.PrintWriter; 26 import java.util.List; 16 import javax.servlet.ServletException 17 import javax.servlet.http.HttpServlet 18 import javax.servlet.http.HttpServletRequest 19 import javax.servlet.http.HttpServletResponse 20 import org.codehaus.groovy.control.CompilationFailedException 21 import org.codehaus.groovy.control.CompilerConfiguration 22 import org.openmrs.module.groovyforms.metadata.model.GroovyFormsContainer 23 import org.openmrs.module.groovyforms.util.GroovyFormsUtil 27 24 28 25 public class GroovyFormsServlet extends HttpServlet { 29 private static final long serialVersionUID = 356197107933621003L ;26 private static final long serialVersionUID = 356197107933621003L 30 27 /** 31 28 * A list of forms currently in the system 32 29 */ 33 private List<GroovyForm> forms;30 def forms 34 31 35 32 /** … … 38 35 @Override 39 36 public void init() throws ServletException { 40 forms = GroovyFormsContainer.getForms(); 37 forms = GroovyFormsContainer.getForms() 38 shell = getClassLoader() 41 39 } 42 40 … … 46 44 @Override 47 45 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 48 PrintWriter out = response.getWriter(); 49 String formId = request.getParameter("formid"); 46 def out = response.writer 47 String formId = request.getParameter("formid") 48 def f = GroovyFormsUtil.getControllerDirectory(formId)+"/FormController.groovy" 49 if(isValidGroovy(f)) { 50 def controller = getClassLoader().parseClass(f).newInstance() 51 controller.onSubmit request, response 52 } 53 50 54 } 51 55 … … 55 59 @Override 56 60 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 57 PrintWriter out = response.getWriter() ;58 String formId = request.getParameter("formid") ;61 PrintWriter out = response.getWriter() 62 String formId = request.getParameter("formid") 59 63 // forward to the form jsp 60 request.getRequestDispatcher(GroovyFormsUtil.getViewDirectory(formId)+"/form.jsp") 61 .forward(request,response); 64 request.getRequestDispatcher(GroovyFormsUtil.getViewDirectory(formId) + "/form.jsp").forward(request, response) 62 65 } 63 66 67 68 def getClassLoader() { 69 def conf = new CompilerConfiguration() 70 def gcl = new GroovyClassLoader(this.getClass().getClassLoader(), conf) 71 gcl 72 } 73 74 /** 75 * This method is used to relay errors to the user 76 * @oaram clazz the class 77 * @return the exception message or null if it was successful 78 */ 79 def checkSyntax(clazz) { 80 def sb = new StringBuilder() 81 sb << "import org.openmrs.*\n\n\n" 82 sb << clazz 83 def res = "" 84 try { 85 if (getClassLoader().parseClass(sb.toString())) { 86 res = "Congrats! The syntax is valid" 87 } 88 } catch (CompilationFailedException e) { 89 final Writer trace = new StringWriter() 90 final PrintWriter printWriter = new PrintWriter(trace) 91 e.printStackTrace(printWriter) 92 res = "Exception: ${e.message}" 93 } 94 res 95 96 } 97 98 /** 99 * Check if it is valid groovy code. 100 * @param clazz the class 101 * @return whether or not it is valid groovy code 102 */ 103 def isValidGroovy(clazz) { 104 def sb = new StringBuilder() 105 sb << "import org.openmrs.*\n\n\n" 106 sb << clazz 107 try { 108 getClassLoader().parseClass(sb.toString()) 109 } catch (CompilationFailedException e) { 110 return false 111 } 112 return true 113 } 114 115 64 116 }