2011-10-21 48 views
1

我正在使用Spring批處理實現的數據加載器,即讀取多個平面文件,處理並將提交間隔爲1000的pojo列表寫入數據庫。
從文件讀取的每一行都被轉換爲包含需要在處理結果上設置的屬性的pojo對象。用於過濾和修改列表項的Lambdaj或apache謂詞

我有一個包含三列180行的查找表。我將每個列值保存在一個單獨的列表中,並在謂詞中迭代列表以與每個POJO項目屬性匹配。如果在所有列表中找到匹配項,則會設置一個屬性。以下是我使用的謂詞,

public class LogicProcessor<I, O> implements ItemProcessor<I, O> { 

    private Map[] params ; 

    public void setParams(Map[] params) 
    { 
     this.params = params; 
    } 

    public O process(I item) throws Exception 
    { 
     System.out.println(params ); 

     List container  = (List) params[1].get("SRVC"); 
     final List callInd  = (List) container.get(0); 
     final List totaltype  = (List) container.get(1); 
     final List servicetype = (List) container.get(2); 

     Predicate<I> callIndipredicate = new Predicate<I>() { 
      public boolean apply(I input) 
      { 
       boolean flag=false; 
       for (int i=0;i<callInd.size();i++) 
       { 
        if ("*".equals(callInd.get(i))) 
        { 
         flag= true; 
         break; 
        } else 
        { 
         try 
         { 
          if (BeanUtils.getProperty(input,"CALLINDICATOR").equals(callInd.get(i))) 
          { 
           flag = true; 
           break; 
          } else 
          { 
           flag= false; 
           break; 
          } 
         } catch (IllegalAccessException e) { 
          e.printStackTrace(); 
         } catch (InvocationTargetException e) { 
          e.printStackTrace(); 
         } catch (NoSuchMethodException e) { 
          e.printStackTrace(); 
         } 

        } 
       } 
       return flag; 
      } 
     }; 

     Predicate<I> totaltyppredicate = new Predicate<I>() { 
      public boolean apply(I input) 
      { 
       boolean flag=false; 
       for (int i=0;i<totaltype.size();i++) 
       { 
        try 
        { 
         if (BeanUtils.getProperty(input,"TOTALTYPE").equals(totaltype.get(i))) 
         { 
          flag = true; 
          break; 
         } else 
         { 
          flag= false; 
          break; 
         } 
        } catch (IllegalAccessException e) { 
         e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
         e.printStackTrace(); 
        } catch (NoSuchMethodException e) { 
         e.printStackTrace(); 
        } 
       } 
       return flag; 
      } 
     }; 

     Predicate<I>srvctypepredicate = new Predicate<I>() { 
      public boolean apply(I input) 
      { 
       boolean flag=false; 
       for (int i=0;i<servicetype.size();i++) 
       { 
        try 
        { 
         if (BeanUtils.getProperty(input,"SERVICETYPE").equals(servicetype.get(i))) 
         { 
          flag = true; 
          break; 
         } else 
         { 
          flag= false; 
          break; 
         } 
        } catch (IllegalAccessException e) { 
         e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
         e.printStackTrace(); 
        } catch (NoSuchMethodException e) { 
         e.printStackTrace(); 
        } 
       } 
       return flag;     
      } 
     }; 

     Predicate<I> isFound = Predicates.and(callIndipredicate,totaltyppredicate,srvctypepredicate); 
     int preorPost= Iterables.indexOf(Arrays.asList(item), isFound) ; 
     System.out.println(preorPost); 
     if (preorPost >=0) 
     { 
      BeanUtils.setProperty(item, "PREPOST", '0'); 
     } else 
     { 
      BeanUtils.setProperty(item, "PREPOST", 'X'); 
     } 
     return (O) item; 
    } 

} 

有沒有更好的方法來過濾項目和修改使用番石榴。

+1

比什麼好? –

+0

@dave我的問題是它是如何在lambdaj –

+1

@SureshSakaDeadProgrammer:我認爲戴夫指出你沒有提供任何代碼,所以我們不知道結果應該是什麼「比」更好。 – StriplingWarrior

回答