As a novice JSP (and non-programmer!) I've bee doing some customisation to a browsers driven application which provides a users with CTI functionality on their desktop.

My task was to create a custom "tab" on their page, which gave a series of checkboxes, with an "Apply" button.

The purpose of the "Apply" was to submit a 3-digit "code" to a database field, depending on which checkbox was ticked.

All of this is working fine... however, for the 14 (or thereabouts) different checkboxes, each sends the correct code - but there is another requirement I have to code! Immediately after doing the first database update, I have to do a second database update. However, the second update varies according to what checkbox is selected.

Sounds easy (in theory) but the way the checkboxes are coded gives me (I think!) a boolean string, and I'm not able to write an if statement around it!

The JSP file which displays the checkboxes:

<%@ page language="java" buffer="none" contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.genesyslab.ail.Enumerator" %>
<%@ page import="com.genesyslab.ail.EnumValue" %>
<%@ page import="com.genesyslab.ail.AilLoader" %>
<%@ page import="com.genesyslab.ail.AilFactory" %>
<%@ page import="com.genesyslab.ail.Interaction" %>

<%
    /* get data from session
    */
    String idInteraction = (String)request.getParameter( "idInteraction" );

    AilFactory factory = AilLoader.getAilFactory();

    /* Class used for the processing of this panel
    */
    
    /* Get category list from a configuration bundle
       Here we use the Business Attribute 'disposition code' from CME
    */
    Enumerator dispositionCodeEnum = factory.getEnumerator("DispositionCode");
    String[] codeIds = null;
    String[] codeLabels = null;
    if (dispositionCodeEnum!=null) {
        Collection dispositionCodes = dispositionCodeEnum.getValues();
        codeIds = new String[dispositionCodes.size()];
        codeLabels = new String[dispositionCodes.size()];
        int i=0;
        EnumValue dispositionCode;
        for (Iterator it=dispositionCodes.iterator();it.hasNext();) {
            dispositionCode = (EnumValue)it.next();
            codeIds[i] = dispositionCode.getId();
            codeLabels[i] = dispositionCode.getDisplayName();
            i++;
        }
    }
    
    /* Get the current categories for the current interaction
       Data is stored in attached data / interaction properties depending on the interaction type
       This part can be changed to recover the disposition from another container as a file, a database...
    */
    boolean[] codeInInteraction = new boolean[codeIds.length];
    Interaction interaction = factory.getInteraction(idInteraction);
    Map uData = interaction.getAttachedData();
    Object dataValue = uData.get("DispositionCode");
    if (dataValue!=null) {
        for (int i=0;i<codeIds.length;i++) {
            codeInInteraction[i] = (codeIds[i].equals(dataValue));
        }
    }
%>

<html>
  <head>
    <meta http-equiv="imagetoolbar" content="no">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="style.css"/>
  </head>

  <body class="scrollBar backgroundRoundedPanel" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onload="javascript:onLoad()" onunload="javascript:onUnload()">

    <table class="fontName" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
      <tr valign="top">
        <td class="editablePanelDarkShadow" width="1" align="left" rowspan="3"><img src="empty.gif"></td>
        <td class="editablePanelDarkShadow" height="1"><img src="empty.gif"></td>
        <td class="editablePanelLightShadow" width="1" align="right" rowspan="3"><img src="empty.gif"></td>
      </tr>
      <tr valign="top">
        <td class="editablePanelBackground" width="100%" align="left">
          <form  name="applyCategoriesForm" action="apply-codes.jsp" >
             <input type="hidden" name="idInteraction" value="<%=idInteraction%>" >
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
<%
    String labelTmp = "";
    String imgName = "imgCheckCategory";
    String imgNameTmp ="";
    int i=0;
    if (codeIds != null) {
%>
            <tr height="4"><td colspan="2"></td></tr>
<%
        for (i=0;i<codeIds.length;i++) {
            labelTmp = codeLabels[i];
            imgNameTmp = imgName + i;
%>
            <tr>
              <td width="25" nowrap align="center">
<%
            if (codeInInteraction[i]) { //tbd: use current interaction data
%>
                <a href='javascript:modifyCheckImage(<%= i %>);'><img name="<%= imgNameTmp %>" src="checku.gif" border="0" width="15" height="15"></a>
                <input type="hidden" name="codeId" value="<%=codeIds[i]%>" >
                <input type="hidden" name="codeChecked" value="true" >
<%
            } else {
%>
                <a href='javascript:modifyCheckImage(<%= i %>);'><img name="<%= imgNameTmp %>" src="unchecku.gif" border="0" width="15" height="15"></a>
                <input type="hidden" name="codeId" value="<%=codeIds[i]%>" >
                <input type="hidden" name="codeChecked" value="false" >
<%
            }
%>
              </td>
              <td class="editablePanelText" nowrap><%=labelTmp%></td>
            </tr>
<%
        }
%>
            <tr height="4"><td colspan="2"></td></tr>
<%
    }

    if (i == 0) {
%>
            <tr><td>&nbsp;</td></tr>
<%
    }
%>
          </table>
          <a href='javascript:applyCodes();'><img name="apply" src="btnapplys.gif" border="0"></a>
          </form>
        </td>
      </tr>
      <tr valign="bottom">
        <td class="editablePanelLightShadow" height="1"><img src="empty.gif"></td>
      </tr>
    </table>
    <script language="JavaScript">
      var idInteraction = "<%=idInteraction%>";
      var rootImgName = "<%=imgName%>";

      function modifyCheckImage(idx) {
        // Get the current image to know the action to launch

        if (document.forms.applyCategoriesForm.elements.codeChecked[idx].value=="true") {
          setCheckBox(idx,"false");
        } else {
          // uncheck other checkboxes
          var j;
          for (j=0;j<document.forms.applyCategoriesForm.elements.codeChecked.length;j++) {
            if (j!=idx) {
              setCheckBox(j,"false");
            }
          }
          setCheckBox(idx,"true");
        }
      }

      function setCheckBox(idx,isChecked) {
          var name = rootImgName+idx;
          if (isChecked=="true") {
            if (document.forms.applyCategoriesForm.elements.codeChecked[idx].value!="true") {
              // Update the image
              document.images[name].src = "checku.gif";
              // Update the form
              document.forms.applyCategoriesForm.elements.codeChecked[idx].value="true";
            }
          } else {
            if (document.forms.applyCategoriesForm.elements.codeChecked[idx].value!="false") {
              // Update the image
              document.images[name].src = "unchecku.gif";
              // Update the form
              document.forms.applyCategoriesForm.elements.codeChecked[idx].value="false";
            }
          }
      }

      function onLoad() {
        // The disposition code checked is recovered in the custom context
         var context = window.parent.getCustomContext();
         if (context!=null && context.codeChecked!=null) {
           var j;
           for (j=0;j<context.codeChecked.length;j++) {
             setCheckBox(j,context.codeChecked[j]);
           }
         }
      }

      function onUnload() {
        // The disposition code checked is stored in the custom context
         var context = new Object();
         context.codeChecked = new Array();
         var j;
           if(document.forms.applyCategoriesForm.elements.codeChecked!=null){
           for (j=0;j<document.forms.applyCategoriesForm.elements.codeChecked.length;j++) {
             context.codeChecked[j] = document.forms.applyCategoriesForm.elements.codeChecked[j].value;
           }
         }
         window.parent.setCustomContext(context);
      }

      function applyCodes() {
        document.forms.applyCategoriesForm.submit();
      }

      // This line disable the IE context menu
      document.oncontextmenu = function(){return true;}
    </script>
  </body>
</html>

This fetches a "list" of items to create the checkboxes.

Then in the JSP which peforms the "apply":

<%@ page language="java" buffer="none" %>
<%@ page import="com.genesyslab.ail.AilLoader" %>
<%@ page import="com.genesyslab.ail.AilFactory" %>
<%@ page import="com.genesyslab.ail.Interaction" %>
<%@ page import="com.genesyslab.ail.exception.RequestFailedException" %>
<%@ page import="com.genesyslab.ail.InteractionVoiceOutbound" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<%@ taglib uri="struts-action" prefix="action" %>
<%@ taglib uri="uadthin" prefix="uadthin" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.genesyslab.ail.*" %>

<%
    /* Get data from request
     */

    AilFactory factory = AilLoader.getAilFactory();

    String idInteraction = (String)request.getParameter( "idInteraction" );
 Interaction interaction = factory.getInteraction( idInteraction );
    String[] codeIds = (String[])request.getParameterValues( "codeId" );
    String[] codeChecked = (String[])request.getParameterValues( "codeChecked" );
	

	

    /* Initialize AIL related data
     */
   

    /* Apply disposition code in interaction
       Here set attached data
       This part can be changed to store the disposition on another container
       as a file, a database...
     */
    
try {
		
				//it is an outbound call. 
				Place p = interaction.getPlace();
				OutboundService os = p.getOutboundService();
				OutboundChain ixnChain = os.getChain(interaction);
				OutboundRecord recordToProcess = ixnChain.getActiveRecord();
				
for (int i=0;i<codeIds.length;i++) {
            if ("true".equals(codeChecked[i])) {            
recordToProcess.setCustomField("business_outcome_code",codeIds[i]);
                break;



			}
            }
out.println("Your Outcome Code has been submitted. Thanks.");
			
			
}



	
	catch(Exception ex) {
		out.println("Error at submit.");
	}



%>

Depending on the value of "business_outcome_code", this is the trigger to perform another action.

Given this code, what is the easiest way to perform some "if" processing to get my desired result?

don't use scriptlets. Move to jstl and I may take a look.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.