2 Replies Latest reply: Dec 13, 2012 2:13 AM by Tshifhiwa RSS

    validate when adding values to a bean

    Tshifhiwa
      hi i have a situation where am adding values from a view to a bean,my use case is i have a list of user in my view where am adding them to my bean by clicking the button,but now i what to validate when adding a user in such a way that if i added a use in my bean and i try to add that user again i must have validating error saying the user have been added already my code is
           public RichTable getEmpTable() {
               return empTable;
           }
          RichTable tempTable;
           public void setTempTable(RichTable tempTable) {
               this.tempTable = tempTable;
           }
      
      
           public RichTable getTempTable() {
               return tempTable;
           }
       public void addMember(javax.faces.event.ActionEvent actionEvent) {
              List<String> tempTable = new ArrayList<String>();
              //Code to get the bindings for TargetVO :
                   RowKeySet selectedEmps = getEmpTable().getSelectedRowKeys();    
                     Iterator selectedEmpIter = selectedEmps.iterator();
                     DCBindingContainer bindings =
                                       (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
                     DCIteratorBinding empIter = bindings.findIteratorBinding("UserDetailsViewVO1Iterator");
                     RowSetIterator empRSIter = empIter.getRowSetIterator();
                      while(selectedEmpIter.hasNext()){
                        Key key = (Key)((List)selectedEmpIter.next()).get(0);
                        Row currentRow = empRSIter.getRow(key);
                           onRowCreate(currentRow);
                      }
                   
      
          }
         
              public void emptytable(){
              
              AddmemberBean emp = new AddmemberBean();
          
          }
          
          public String onRowCreate( Row currentRow ) {
           BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
           //access the name of the iterator the table is bound to. 
           DCIteratorBinding dciter = (DCIteratorBinding) bindings.get("addmemberBeanIterator");
           //access the underlying RowSetIterator
           RowSetIterator rsi = dciter.getRowSetIterator();
           //get handle to the last row
           Row lastRow = rsi.last();
           //obtain the index of the last row
           int lastRowIndex = rsi.getRangeIndexOf(lastRow);
           //create a new row
           Row newRow = rsi.createRow();
           String f = (String)currentRow.getAttribute("Firstname");
           String s = (String)currentRow.getAttribute("Surname");
           String u = (String)currentRow.getAttribute("Username");  
           String n = (String)currentRow.getAttribute("Emailaddress");
              newRow.setAttribute("firstname", f);
              newRow.setAttribute("surname", s);
              newRow.setAttribute("name", u);
              newRow.setAttribute("emailaddress", n);
           //initialize the row
           newRow.setNewRowState(Row.STATUS_INITIALIZED);
           //add row to last index + 1 so it becomes last in the range set
           rsi.insertRowAtRangeIndex(lastRowIndex +1,  newRow); 
           //make row the current row so it is displayed correctly
           rsi.setCurrentRow(newRow);    
              System.out.println("Username " + u);
              System.out.println("firstname " + f);
              System.out.println("surname " + s);
              System.out.println("email " + n);
           return null;
          }  
      am in jdeveloper 11.1.1.6.0

      Edited by: adf0994 on 2012/12/13 9:44 AM

      Edited by: adf0994 on 2012/12/13 9:45 AM

      Edited by: adf0994 on 2012/12/13 9:46 AM
        • 1. Re: validate when adding values to a bean
          Frank Nimphius-Oracle
          Hi,

          an easy option would have been to identify users by a unique key (mail address or similar) and then provide validation on the entity level. Some comments to your coding

          1. "onRowCreate" could be simplified by creating a "CreateWithParams" method binding for the "addmemberBeanIterator" view object (assuming that this is a view object and not a manual binding to a managed bean). See: http://www.oracle.com/technetwork/developer-tools/adf/learnmore/13-create-with-params-169140.pdf . Once you created this, you can call OperationBinding for this new method binding
           OperationBinding oper = (OperationBinding) bindings.get("the method binding in PageDeF");
           oper.getParamsMap().put("attributeName1", value1);
           oper.getParamsMap().put("attributeName2", value2);
           ...
           oper.execute();
          As you can see form the above, this makes the code better readable and shorter

          2. For checking whether a user exists, I suggest you expose a method on the view object you want to populate with the new data (create a client interface method on the view object) and then invoke it from a method binding (PageDef file). If the method rteurns true/false whether or not the user exists the code would be

          OperationBinding oper2 = (OperationBinding) bindings.get("check if user exists method binding");
          oper2.getParamsMap().put("attributeName1", uniqueUserAttributeValue);
          Object ret = oper2.execute();
          if(ret != null){
             if(((Boolean) ret) == Boolean.TRUE){
                //alert that user exists
             }
             else{
                //create user
             }
          }
          Frank
          • 2. Re: validate when adding values to a bean
            Tshifhiwa
            hi frank i don't what to check if the user exist in my view object ,i what to check if the user has been currently added in my bean,during current selection,where is this method define .get("check if user exists method binding");
            =OperationBinding oper2 = (OperationBinding) bindings.get("check if user exists method binding");
            and am not using Insert inside CreateWithParams am using method above to copy values from my view to my bean
            this is where i what to check if the coming value already exist
            Row newRow = rsi.createRow();
                 String f = (String)currentRow.getAttribute("Firstname");
                 String s = (String)currentRow.getAttribute("Surname");
                 String u = (String)currentRow.getAttribute("Username");  
                 String n = (String)currentRow.getAttribute("Emailaddress");
                    newRow.setAttribute("firstname", f);
                    newRow.setAttribute("surname", s);
                    newRow.setAttribute("name", u);
                    newRow.setAttribute("emailaddress", n);
            Edited by: adf0994 on 2012/12/13 10:49 AM

            Edited by: adf0994 on 2012/12/13 11:04 AM

            Edited by: adf0994 on 2012/12/13 11:12 AM