Ah... maintenance of old code

  • Sorry for the long post, but I really want to share this. This is an old ASP page I just got my hands on.

    Stuff between [** .. **] is mine (should be obvious). I erased some of the indentation because I just couldn't keep it there.

    'Procedures'  is what you call use cases in this framework.


    <font color="#990000">[** SNIP: standard html opening tag and head section with style and scripts ]</font>

        <body> <font color="#990000">[
    1 single tab before the tag ]</font>
        <font color="#990000">[
    And suddenly 9 tabs of indentation. Don't ask why, I don't know! ]</font>

        on error resume next
        Set MySort = server.CreateObject("w4com.TWFsort")
        Set fields1 = server.CreateObject ("w4com.TWFname")
        Set fields2 = server.CreateObject ("w4com.TWFname")
        Set fields3 = server.CreateObject ("w4com.TWFname")
        Set fields4 = server.CreateObject ("w4com.TWFname")
        Set criterion = Server.CreateObject("w4com.TWFcriterion")
        Set operator = Server.CreateObject("w4com.TWFoperator")
        Set sortMode = Server.CreateObject("w4com.TWFsortMode")
        Set sortBy = Server.CreateObject("w4com.TWFsortBy")
        Set mystate = Server.CreateObject("w4com.TWFstate")
        Set Tselection = server.CreateObject ("w4com.TWFTselection")
        Set var1 = server.CreateObject ("w4com.TWFname")
        Set var2 = server.CreateObject ("w4com.TWFname")

        <font color="#990000">[
    None of the variables declared before are used, except
        at the very end of the file, where they're set to Nothing ]

        <font color="#999999">'<font color="#000066"> Verify wich process the user can start ================================================</font></font>
        Dim arrStartProcess
        Dim procSort, procSortMode, TSelectValues, procCriteria, procOperator, TProc

        Set fieldsX = Server.CreateObject ("w4com.TWFname")     <font color="#990000">[
    Not used anywhere ]</font>

        set procSortMode = server.CreateObject("w4com.TWFsortMode")
        set TSelectValues = server.CreateObject ("w4com.TWFTselection")
        set procCriteria = Server.CreateObject("w4com.TWFcriterion")
        set procOperator =  Server.CreateObject("w4com.TWFoperator")
        set TProc = Server.CreateObject("w4com.TWFTprocedure")
        set Proc = Server.CreateObject("w4com.TWFProcedure")
        arrStartProcess = <font color="#996600">""     <font color="#990000">[
    This is going to be the list of procedures the user can initiate ]</font></font><font color="#990000"></font>

        <font color="#990000">[
    This part here is actually done right ]</font>
        set TSelectValues = server.CreateObject ("w4com.TWFTselection")
        call TSelectValues.AddStringSelection(procCriteria.CRIT_NAME, procOperator.EQUAL, "Texp_
        call TSelectValues.AddIntegerSelection (procCriteria.CRIT_STATE, procOperator.EQUAL, 1)
        Set TProc = mysession.WFSearchProcedure(WF_BY_CREATION_DATE,procSortMode.ASCENDING ,TSelectValues,0)

        <font color="#990000">[
    * OK, so now we create a sort of XML containing the procedure names ]</font>
        arrParticipants = <font color="#9966cc">"<RESUME>"</font>

            <font color="#990000">[
    Add a level of indentation here, because we can]</font>
            For Each Proc in TProc
                    if HasRole(mActor, Proc.RoleInitiator.Str) = 0 Then
                        arrStartProcess = arrStartProcess & Proc.Name & <font color="#996600">"||"</font> & getPortalDictionary("name." & Proc.Name)  & <font color="#996600">"##"</font>
                    End if

                    if HasRole(mActor, getPortalDictionary("role." & Proc.Name)) = 0 Then
                                arrParticipants = arrParticipants & <font color="#9966ff">"<PROCEDURE>"</font>
                                arrParticipants = arrParticipants & <font color="#9966ff">"<STR>"</font> & Proc.Name & <font color="#9966ff">"</STR>"</font>
                                arrParticipants = arrParticipants & <font color="#9966ff">"<STATE></STATE>"</font>
                                arrParticipants = arrParticipants & <font color="#9966ff">"</PROCEDURE>"</font>
                    end if

        arrParticipants = arrParticipants & <font color="#9966ff">"</RESUME>"</font>
        if len(arrStartProcess) > 3 then
            arrStartProcess = Mid(arrStartProcess, 1,(len(arrStartProcess)-2))
        end if
        set procSortMode = nothing
        set TSelectValues = nothing
        set procCriteria = nothing
        set procOperator =  nothing
        set TProc = nothing
        set Proc = nothing
        Set mActor = nothing

        <font color="#990000">[** SNIP: Three nested tables with inline style attributes, for layout ]</font>

            <font color="#990000">[
    13 tabs of indentation in the original ! ]</font>
            <font color="#000066">' Procedures List ===============================================================</font>
            dim oDom, oNodes, oNode
            set oDom = Server.CreateObject("Msxml2.DOMDocument.3.0")
            oDom.setProperty "SelectionLanguage","XPath"
            Dim sXML
                sXML = "<RESUME>"
            for each fullTask in TfullTask    <font color="#990000">[
    TfullTask is not declared anywhere ]</font>
                if (fullTask.Task.Priority <>10) then
                        sXML = sXML & "<PROCEDURE>"
                        sXML = sXML & "<STR>" & fullTask.Workcase.Procedure.Str & "</STR>"
                        sXML = sXML & "<STATE>" & fullTask.Task.State & "</STATE>"
                        sXML = sXML & "</PROCEDURE>"
                end if
            sXML = sXML & "</RESUME>"         <font color="#990000">[
    And never again do we hear about sXML... ]</font>
            oDom.loadXML arrParticipants
            set oNodes = oDom.selectNodes("//PROCEDURE/STR[not(.=preceding::PROCEDURE/STR)]")
            dim iWait, iExecute, iAvaliable   <font color="#990000">[
    Not used anywhere ]</font>
            for each oNode in oNodes
                if len(oNode.text) > 0 then
                    appDir = "-----/workItems.asp?" & mysession.Context.WriteCtx & "&inv1=CheckLogin&Prioridade=100"
                    appName = getPortalDictionary("name." & oNode.text)
                    if len(appName)=0 then
                        appName = oNode.text
                    end if
                        <font color="#000066">'if(lCase(Left(oNode.text,5)) <> "timp_") then</font>
                            <td><a href="
    <%=appDir%>" name="<%=oNode.text%>"><%=appName%></a></td>

                        <font color="#000066">'end if</font>
                end if
            <font color="#990000">[
    So we just parsed the XML we created above to generate an HTML table!  ]</font>       
            <font color="#000066">'==========================================================================</font>
        <font color="#990000">[
    Close two table, down to 7 tabs ]</font>
        <button onclick="startProcess()" ID="btnStartProc">Iniciar</button>
        <font color="#990000">[
    Close the last table, down to 4 tabs ]</font>
        <form name="frmStartProc" id="frmStartProc" method="post" target="_top" >
            <input type="hidden" name="pb" ID="pb">
        <script language="javascript">

        <font color="#006666">var oNodes = ("</font><%=arrStartProcess%><font color="#006666">").split("##");
        function startProcess(){
                var Process2Start ="";
                newProcOpt = showStartPanel();
                if (newProcOpt == 0){
                    return false;
                    Process2Start = (newProcOpt).toString().toLowerCase();
                switch (Process2Start){     <font color="#990000">[
    Now, interesting things happen. Look! ]</font>
                    Dim myArr, myArr2
                    myArr = Split(arrStartProcess, "##")
                    For i=0 to UBound(myArr)
                        myArr2 = Split(myArr(i), "||")
                        appDir = getPortalDictionary("app." & myArr2(0) & ".dir")
                        Response.Write("<font style="font-style: italic;" color="#009966">case ""</font>" & LCase(myArr2(0)) & ""<font style="font-style: italic;" color="#009966">":</font>" & vbCr)
                        Response.Write(vbTab & vbTab & vbTab & vbTab & vbTab & "<font style="font-style: italic;" color="#009966">document.getElementById(""frmStartProc"").action =""</font>")
                        Response.Write("<font style="font-style: italic;" color="#009966">-----/router.asp?activity=@inv1.activitySubProcedure&inv1=Init&inv1.Command=NewCaseWithInitiator&</font>")
                        Response.Write("<font style="font-style: italic;" color="#009966">inv1.prefixCaseName=</font>" & myArr2(0))
                        Response.Write("<font style="font-style: italic;" color="#009966">&inv1.initiator.id=</font>" & mysession.Context.ActorId)
                        Response.Write("<font style="font-style: italic;" color="#009966">&inv1.makesUniqueName=WF_TRUE</font>")
                        Response.Write("<font style="font-style: italic;" color="#009966">&inv1.procedure=</font>" & myArr2(0))
                        Response.Write("<font style="font-style: italic;" color="#009966">&inv2=processTask&inv2.taskName.id=@inv1.name.id&</font>" & mysession.Context.WriteCtx)
                        Response.Write("<font style="font-style: italic;" color="#009966">"";</font>")
                        <font color="#006666">document.getElementById("frmStartProc").submit();</font>
                        Response.Write(vbCr & vbTab & vbTab & vbTab & vbTab & vbTab &  "<font color="#009999">break</font>" & vbCr & vbCr)
                <font color="#006666">}
                alert("startProcess ERROR: " + e.description);

        function showStartPanel(){
            return window.showModalDialog("-----/showStartProcess.html", oNodes, "dialogWidth:400px;dialogHeight:300px;scroll:no;status:no;");
        <script  language="javascript">
    <font color="#006666"> document.title += " </font><%=Session("usr")%><font color="#006666"> "; </font></script>

        <font color="#990000">[
    SNIP: close body and html ]</font>

    <font color="#990000"></font>
        <font color="#990000">[
    1 tab of indentation **]</font>
        Set mysession = Nothing
        Set Tsort = Nothing
        Set Tfields = Nothing
        Set TcaseVariables = Nothing
        Set TtaskVariables = Nothing
        Set MyWhere = Nothing
        Set MySort = Nothing
        Set fields1 = Nothing
        Set fields2 = Nothing
        Set fields3 = Nothing
        Set fields4 = Nothing
        Set criterion = Nothing
        Set operator = Nothing
        Set sortMode = Nothing
        Set sortBy = Nothing
        Set mystate = Nothing
        Set Tselection = Nothing
        Set var1 = Nothing
        Set var2 = Nothing

    So, what's the deal with startProcess() ?

    Well, it's a client-side javascript function that runs when you press the "Start new procedure" button on the page. It calls a "modal" browser window that is generated based on the arrStartProcess array. The "return value" of this window is then used on a switch statement that was generated server-side (with indentation!) through vbscript.
    In the switch statement we set the value of the 'action' attribute of the 'frmStartProc' form and submit it.

    Simple, isn't it? 

  • To keep indentation in this forum software, you need a wooden table approach:


    1. Paste your code into Word

    2.  Change the font to "Courier New"

    3. Copy the code from Word and come back to the forum's post page

    4. Use "paste from Word" in the editor's toolbar

  • What I meant was I couldn't waste precious forum horizontal space.

     Think of the wasted bandwidth!  ;) 

  • I uses teh Microsoft Words!!!


Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.