0 Replies Latest reply on May 13, 2008 5:42 PM by asookazian

    using valueChangeListener with Richfaces and Seam

    asookazian

      Seam 2.0.1.GA
      RF 3.2.0.SR1

      I have a Seam app that is using valueChangeListener as follows:

      <h:inputText id="barcode" maxlength="5" size="10" value="#{list.o1.coxBarcode}" valueChangeListener="#{addHardware.processValueChange}">
       <a4j:support event="onblur" oncomplete="checkBarCode();" ajaxSingle="true" reRender="tempcompid, tempmsg, displayMessageLabel, errmsgheader, submit"/>
       </h:inputText>


      The method in the SFSB is:

      public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
       try {
       myInput = (HtmlInputText)event.getComponent();
       fc = FacesContext.getCurrentInstance();
       fc.getExternalContext().getRequestMap().put("focusId",(event.getComponent().getClientId(FacesContext.getCurrentInstance())));
       boolean dupOnScreenExists = false;
       String atLineNo=getLineNo(event.getComponent().getId());
       String dupEntryFoundAtLineNo = "";
      
       if (myInput.getValue().toString().trim().length() == 0) {
       msg = atLineNo +" Message for Barcode - Barcode is Required";
       msgConfirm = "YES";
       myInput.setStyle("border-right-color: Red; border-bottom-color: Red; border-top-width: medium; border-top-color: Red; border-right-width: medium; border-left-width: medium; border-left-color: Red; border-bottom-width: medium");
       log.info("focusId = "+fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompId(fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompMsg(msgConfirm);
       setMsg(msg);
       isSubmitDisabled = true;
       }
       else if (!"".trim().equalsIgnoreCase((String)event.getNewValue()) && myInput.getValue().toString().trim().length()>0) {
       String newVal = "";
       newVal = (String)event.getNewValue().toString().trim();
       for (int i=0;i<duplicateBarCodeOnScreenList.size();i++) {
       if (event.getComponent().getId().equalsIgnoreCase(duplicateBarCodeOnScreenList.get(i).getValue().toString())) {
       duplicateBarCodeOnScreenList.remove(i);
       }
       else if (!event.getComponent().getId().equalsIgnoreCase(duplicateBarCodeOnScreenList.get(i).getValue().toString()) &&
       newVal.equalsIgnoreCase(duplicateBarCodeOnScreenList.get(i).getLabel().toString())) {
       dupEntryFoundAtLineNo = duplicateBarCodeOnScreenList.get(i).getValue().toString();
       dupOnScreenExists = true;
       }
       }
       duplicateBarCodeOnScreenList.add(new SelectItem(event.getComponent().getId(), newVal));
      
       for (int i=0;i<duplicateBarCodeOnScreenList.size();i++) {
       String coxBarCode = duplicateBarCodeOnScreenList.get(i).getLabel().toString();
      
       isSubmitDisabled = false;
      
       if (coxBarCode.trim().length() == 5) {
       if ( Integer.valueOf(coxBarCode) >= 1000 && Integer.valueOf(coxBarCode) <= 99999) {
       if (!dupOnScreenExists) {
      
       //AS 05-07-08 - check to see if barcode already exists in tbHardware...
       //TO DO: refactor this into a private method...
      
       List<TbHardware> tbHardware = entityManager.createQuery("SELECT h FROM TbHardware h WHERE h.coxBarcode = :coxBarcode")
       .setParameter("coxBarcode", coxBarCode)
       .getResultList();
      
       if ( tbHardware!=null && (tbHardware.size() > 0) ) {
       String dupMessageInDB = findMessage(SHIMSConstants.DUPLICATE_ERROR_MSG_ID);
       if (dupMessageInDB != null && dupMessageInDB.trim().length() > 0) {
       msg = atLineNo +" : "+dupMessageInDB + " in Database";
       msgConfirm = "YES";
       myInput.setStyle("border-right-color: Red; border-bottom-color: Red; border-top-width: medium; border-top-color: Red; border-right-width: medium; border-left-width: medium; border-left-color: Red; border-bottom-width: medium");
       setCompId(fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompMsg(msgConfirm);
       isSubmitDisabled = true;
       }
       }
      
       }
       else {
       msg = "Duplicate Barcode "+coxBarCode.trim()+" entry exists on Screen at "+atLineNo + " & "+getLineNo(dupEntryFoundAtLineNo);
       msgConfirm = "YES";
       myInput.setStyle("border-right-color: Red; border-bottom-color: Red; border-top-width: medium; border-top-color: Red; border-right-width: medium; border-left-width: medium; border-left-color: Red; border-bottom-width: medium");
       setCompId(fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompMsg(msgConfirm);
       isSubmitDisabled = true;
       }
       }
       else {
       msg = atLineNo + ": Invalid Format for Barcode "+coxBarCode.trim();
       msgConfirm = "YES";
       myInput.setStyle("border-right-color: Red; border-bottom-color: Red; border-top-width: medium; border-top-color: Red; border-right-width: medium; border-left-width: medium; border-left-color: Red; border-bottom-width: medium");
       setCompId(fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompMsg(msgConfirm);
       isSubmitDisabled = true;
       }
       }
       else {
       msg = atLineNo + ": Invalid Barcode "+coxBarCode.trim()+ " - Permissable Barcode length MUST be 5";
       msgConfirm = "YES";
       myInput.setStyle("border-right-color: Red; border-bottom-color: Red; border-top-width: medium; border-top-color: Red; border-right-width: medium; border-left-width: medium; border-left-color: Red; border-bottom-width: medium");
       setCompId(fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompMsg(msgConfirm);
       isSubmitDisabled = true;
       }
       }
       }
      
       if ("YES".equalsIgnoreCase(msgConfirm)) {
       errMgHeader = "Please correct following error(s) before Submission";
       }
       }
       catch (NumberFormatException nfe) {
       msg = getLineNo(event.getComponent().getId()) + ": Invalid Barcode.";
       msgConfirm = "YES";
       errMgHeader = "Please correct following error(s) before Submission";
       myInput.setStyle("border-right-color: Red; border-bottom-color: Red; border-top-width: medium; border-top-color: Red; border-right-width: medium; border-left-width: medium; border-left-color: Red; border-bottom-width: medium");
       setCompId(fc.getExternalContext().getRequestMap().get("focusId").toString());
       setCompMsg(msgConfirm);
       isSubmitDisabled = true;
       }
       }


      Is there a better alternative than using valueChangeListener in this scenario? e.g. is there any advantage to using a4j:support onchange?

      I need to be able to validate certain field values (some of these I am using Hibernate Validator annotations in the SFSB or entity class) and set the focus and color appropriately for a particular cell in a dataTable after the validation. thx.