0 Replies Latest reply on Oct 13, 2010 8:00 AM by shwetank sharma

    Automatically Update DataBase Value at Time of validation

    shwetank sharma Newbie

      Hi , I want to validate some text fields at before persist / merge


      But form show validate error and also update database value .


      This Function use to render data Model Panel and also fetch the data from database and show into Data Table




      @SuppressWarnings("unchecked")
           @Begin(join = true,flushMode=FlushModeType.MANUAL)
           public void loadMachineryListForRH() {
                log.info("Entering getMachineryList() method");
      
                List<Machinery> machList = new ArrayList<Machinery>(0);
      
                machList = tenantEntityManager.createQuery(
                          "select mc from Machinery mc where mc.flgDeleted = 0"
                                    + "and mc.shipMaster.ishipId="
                                    + Contexts.getSessionContext().get("shipId"))
                          .getResultList();
                tenantEntityManager.flush();
      
                machineryRList = machList;
      
                prevMachineryRList = new ArrayList<Machinery>(0);
      
                for (Machinery mc : machList) {
                     Machinery m = new Machinery();
                     m.setImachineryId(mc.getImachineryId());
                     m.setIrunningHours(mc.getIrunningHours());
                     prevMachineryRList.add(m);
                }
      
                showOverlay = true;
                log.info("Exiting getMachineryList() method with size of list=="
                          + machineryRList.size());
           }




      And This Function Use For Merging




      @SuppressWarnings("unchecked")
           @Begin(join = true,flushMode=FlushModeType.MANUAL)
           public void persistRuningHoursForAll() {
      
                log.info("Enter in persist running hours For All method");
                String numeric_regex = "[0-9]*"; 
                String machineryName = "";
                Integer oldRunningHrs = 0;
                Integer newRunningHrs = 0;
      
                SeamResourceBundle srb = new SeamResourceBundle();
                int DISABLE_EDIT_JOB_DONE_DAYS = Integer.parseInt(srb
                          .getString("DISABLE_EDIT_JOB_DONE_DAYS"));
                
                
                Date today = new Date();
                boolean validate = false;
                boolean subValidate = false;
                for (Machinery mach : machineryRList) {
                     for (Machinery prevMach : prevMachineryRList) {
                          if (prevMach.getImachineryId() == mach.getImachineryId()) {
                               if (mach.getIrunningHours() < prevMach.getIrunningHours()) {
      
                                    machineryName = mach.getStrName();
                                    oldRunningHrs = prevMach.getIrunningHours();
                                    newRunningHrs = mach.getIrunningHours();
                                    int days = compareToDate(mach.getDtUpdated(), today); 
                                    if(days > DISABLE_EDIT_JOB_DONE_DAYS){ 
                                    
                                         facesMessages.addToControl(
                                              "addRunningHoursPanel",
                                              "MacineryName"+machineryName+" We can not decrease running hrs after seven days");
                                    validate = true;
                                    break;
                                    }
                               }
                          }
                     }
                }
      
                
                if (!validate){
                     for (Machinery mach : machineryRList) {
      
                          log.info("Machinery running hours" + mach.getIrunningHours());
      
                          if (mach.getIrunningHours() != null) {
                               if (!(mach.getIrunningHours().toString()
                                         .matches(numeric_regex))) {
                                    facesMessages.addToControlFromResourceBundle(
                                              "addRunningHoursPanel", "validator.numeric");
                                    subValidate = true;
                                    break;
                               }
                          }
      
                          if (mach.getIrunningHours() == null) {
                               mach.setIrunningHours(0);
                               tenantEntityManager.merge(mach);
                               tenantEntityManager.flush();
                          }
      
                          for (Machinery prevMach : prevMachineryRList) {
      
                               if (mach.getIrunningHours() != prevMach.getIrunningHours()) {
                                    mach.setDtUpdated(new Date());
                               }
                               if (prevMach.getImachineryId() == mach.getImachineryId()) {
      
                                    int componentRh = mach.getIrunningHours()
                                              - prevMach.getIrunningHours();
      
                                    String compQl = " select comp from Component comp where comp.flgIsInUse = 1 and "
                                              + " comp.machinery.imachineryId = "
                                              + mach.getImachineryId()
                                              + "and comp.shipMaster.ishipId="
                                              + Contexts.getSessionContext().get("shipId");
      
                                    List<Component> compList = tenantEntityManager
                                              .createQuery(compQl).getResultList();
      
                                    for (Component cmp : compList) {
                                         cmp.setIrunningHours(cmp.getIrunningHours()
                                                   + componentRh);
                                         tenantEntityManager.merge(cmp);
                                         tenantEntityManager.flush();
                                    }
                               }
                          }
                          mach.setFlgIsDirty(2);
                          tenantEntityManager.merge(mach);
                          tenantEntityManager.flush();
                          this.showOverlay = false;
                     }
                     
                }     
                
                if(!validate && !subValidate){
                     this.status = "due";
                     setStatus("due");
      
                     facesMessages
                               .addFromResourceBundle("message.runninghrs.updated.successful");
                     fetchMaintenanceJobsTypeDetails();
                }
                
           }




      Thanks In Advance ...........