Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Changeset 4764

Show
Ignore:
Timestamp:
07/02/08 09:16:16 (5 months ago)
Author:
r0bby
Message:

groovyforms: I now use jquery for AJAX. AJAX is used to check the syntax and relay any errors iin a clean way.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/groovyforms/web/module/createGroovyForms.jsp

    r4746 r4764  
    1919 
    2020<%@ include file="/WEB-INF/template/header.jsp" %> 
    21  
    2221<openmrs:htmlInclude file="/dwr/interface/DWRGroovyFormsService.js"/> 
    2322<openmrs:htmlInclude file="/dwr/engine.js"/> 
    2423<openmrs:htmlInclude file="/dwr/util.js"/> 
    2524 
     25<script type="text/javascript" src="${pageContext.request.contextPath}/moduleResources/groovyforms/jquery-1.2.6.js"> </script> 
    2626<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) 
    3338 
    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" 
    4046        } 
    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    }     
    6748</script> 
    6849 
     
    7253 
    7354<br/> 
    74 <!--<form action="${pageContext.request.contextPath}/moduleServlet/groovyforms/createGroovyForm" method="post"> --
     55<form action="${pageContext.request.contextPath}/moduleServlet/groovyforms/createGroovyForm" method="post"
    7556    <b><spring:message code="groovyforms.label.title"/></b> 
    7657    <br/><br/> 
     
    9879    <div id="stepTwo" style="display:none"> 
    9980        <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"> 
    10282        </textarea> 
    103         <input type="submit" onclick="generateForm()" value="<spring:message code="groovyforms.generate-form"/>"/> 
    10483    </div> 
    10584    <br/> 
     
    11089        <br /> 
    11190        <b><spring:message code="groovyforms.label.controller"/> </b> 
    112         <textarea id="controller" rows="80" cols="10"> 
     91        <textarea id="controller" rows="10" cols="80"> 
    11392        </textarea> 
    114         <input type="submit" onclick="saveForm()" value="<spring:message code="groovyforms.finalize"/>"/>         
     93        <input type="submit" value="<spring:message code="groovyforms.finalize"/>"/> 
    11594    </div> 
    11695    <br/> 
    117     <input type="submit" onclick="checkSyntax()" value="<spring:message code="groovyforms.checkSyntax"/>"/> 
    118     &nbsp;&nbsp; 
    119     <input type="submit" value="Foo"/> 
    120     &nbsp;&nbsp; 
    121     <input type="submit" onclick="generateTemplate()" value="<spring:message code="groovyforms.generate-template"/>" /> 
    122 <!--</form> --> 
     96</form> 
    12397    <br/> 
    12498    <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  
    2020import org.apache.commons.logging.LogFactory 
    2121import org.codehaus.groovy.control.CompilationFailedException 
    22 import org.codehaus.groovy.control.CompilerConfiguration 
    2322 
    2423class CreateGroovyFormServlet extends HttpServlet { 
     
    2928    @Override 
    3029    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")) 
    3234        def name = request.getParameter("formName") 
    3335        def version = request.getParameter("version") 
    3436        def res = this.checkSyntax(clazz) 
    3537        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            } 
    3947        } else { 
    40             response.setContentType "text/xml" 
     48            response.contentType = "text/xml" 
    4149            response.setHeader "Cache-Control", "no-cache" 
    4250            response.writer.write "<result>\n\tPlease fill in the Form Model\n</result>" 
     
    4654    @Override 
    4755    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     56        doGet(request,response) 
    4857    } 
    4958 
     
    5665 
    5766    def getClassLoader() { 
    58         def conf = new CompilerConfiguration() 
    59         def gcl = new GroovyClassLoader(this.getClass().getClassLoader(), conf) 
     67        def gcl = new GroovyClassLoader(this.getClass().getClassLoader()) 
    6068        gcl 
    6169    } 
     
    7078        sb << "import org.openmrs.*\n\n\n" 
    7179        sb << clazz 
    72         def res = "" 
     80        def res = null 
    7381        try { 
    74             if (getClassLoader().parseClass(sb.toString())) { 
    75                 res = "Congrats! The syntax is valid" 
    76             } 
     82            getClassLoader().parseClass(sb.toString()) 
     83 
    7784        } catch (CompilationFailedException e) { 
    78             final Writer trace = new StringWriter() 
    79             final PrintWriter printWriter = new PrintWriter(trace) 
    80             e.printStackTrace(printWriter) 
    8185            res = "Exception: ${e.message}" 
    8286        } 
     
    8690 
    8791    /** 
    88      * Check if it is valid groovy code. 
     92     * Check if it is result groovy code. 
    8993     * @param clazz the class 
    90      * @return whether or not it is valid groovy code 
     94     * @return whether or not it is result groovy code 
    9195     */ 
    92     def isValidGroovy(clazz) { 
     96    def isresultGroovy(clazz) { 
    9397        def sb = new StringBuilder() 
    9498        sb << "import org.openmrs.*\n\n\n" 
     
    101105        return true 
    102106    } 
    103  
    104107} 
  • openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/GroovyFormsServlet.groovy

    r4746 r4764  
    1212 * Copyright (C) OpenMRS, LLC.  All Rights Reserved. 
    1313 */ 
    14 package org.openmrs.module.groovyforms.web; 
     14package org.openmrs.module.groovyforms.web 
    1515 
    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; 
     16import javax.servlet.ServletException 
     17import javax.servlet.http.HttpServlet 
     18import javax.servlet.http.HttpServletRequest 
     19import javax.servlet.http.HttpServletResponse 
     20import org.codehaus.groovy.control.CompilationFailedException 
     21import org.codehaus.groovy.control.CompilerConfiguration 
     22import org.openmrs.module.groovyforms.metadata.model.GroovyFormsContainer 
     23import org.openmrs.module.groovyforms.util.GroovyFormsUtil 
    2724 
    2825public class GroovyFormsServlet extends HttpServlet { 
    29     private static final long serialVersionUID = 356197107933621003L; 
     26    private static final long serialVersionUID = 356197107933621003L 
    3027    /** 
    3128     * A list of forms currently in the system 
    3229     */ 
    33     private List<GroovyForm> forms; 
     30    def forms 
    3431 
    3532    /** 
     
    3835    @Override 
    3936    public void init() throws ServletException { 
    40         forms = GroovyFormsContainer.getForms(); 
     37        forms = GroovyFormsContainer.getForms() 
     38        shell = getClassLoader() 
    4139    } 
    4240 
     
    4644    @Override 
    4745    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 
    5054    } 
    5155 
     
    5559    @Override 
    5660    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") 
    5963        // 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) 
    6265    } 
    6366 
     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 
    64116}