posts - 122,  comments - 25,  trackbacks - 0
          一般業(yè)務(wù)系統(tǒng)中總會存在一些基礎(chǔ)數(shù)據(jù),在其他的業(yè)務(wù)單據(jù)中會被套引用。因此,系統(tǒng)必須保證這些被業(yè)務(wù)單據(jù)引用的基礎(chǔ)數(shù)據(jù)不能任意的刪除。最常見的做法就是,在刪除基礎(chǔ)數(shù)據(jù)時,預(yù)先校驗該類數(shù)據(jù)是否在相關(guān)業(yè)務(wù)表中存在,若不存在才允許用戶刪除,否則給用戶以提示。

          但這樣的處理方法,有些缺點,就是需要編碼對每個業(yè)務(wù)類提供查詢方法,或在刪除邏輯中增加判斷邏輯。因此,每次引用關(guān)系變化,增加或減少時免不了要修改原來的邏輯,時間越長,系統(tǒng)的維護成本就越來越高了。因此,有必要對系統(tǒng)進行重構(gòu),將這類的處理邏輯進行抽象,單獨封裝成一個服務(wù),當(dāng)引用關(guān)系有變更時,不用再修改原有邏輯,通過配置就可以完成變更。

          通用引用關(guān)系查詢服務(wù),主要就是通過db表或xml配置文件,對系統(tǒng)中每個基礎(chǔ)數(shù)據(jù)有引用的所有關(guān)系進行定義,定義屬性主要是引用的表及字段名稱。查詢時,從配置文件中讀取指定類別的引用關(guān)系,并逐一查詢這些表中的記錄,以確定數(shù)據(jù)是否被引用。這種處理方法的優(yōu)點為,易擴展、可維護性強,引用關(guān)系變更時,僅通過維護配置文件,不必進行編碼,就能實現(xiàn),這樣能大大的提高系統(tǒng)的穩(wěn)定性。

          xml配置文件如下:
          <rule bizName='product' desc="產(chǎn)品關(guān)聯(lián)項定義">
              
          <item>
                  
          <refTable>sale_item</refTable>
                  
          <refField>product_id</refField>
                  <!-- 用于查詢條件的擴展,允許為空 -->
                  <extCondition>CORP_ID = #corpId#</extCondition>
              
          </item>
              
          <item>
                  
          <refTable>sale_order_item</refTable>
                  
          <refField>product_id</refField>
                  
          <extCondition>CORP_ID = #corpId#</extCondition>
              
          </item>
          </rule>
          <rule bizName='customer' desc="客戶關(guān)聯(lián)項定義">
              
          <item>
                  
          <refTable>sale_order</refTable>
                  
          <refField>cust_id</refField>
                  
          <extCondition>CORP_ID = #corpId#</extCondition>
              
          </item>
              
          <item>
                  
          <refTable>sale_bill</refTable>
                  
          <refField>cust_id</refField>
                  
          <extCondition></extCondition>
              
          </item>
              ... ...

          </rule>

          通用業(yè)務(wù)引用查詢類代碼片段如下:
          public class BizReferenceService implements IBizReferenceService {

              
          private static Map<String,List<BizReferenceRule>> ruleMaps;
              
              
          private static final String PATTERN = "#[\\w]+#";
              
          private static final String CFG_FILE = "bizReferenceRule.xml";
              ... ...

            
              
          /**
               * 查詢指定業(yè)務(wù)數(shù)據(jù)是否被其他業(yè)務(wù)表關(guān)聯(lián)依賴.
               * 
          @param bizName 關(guān)聯(lián)業(yè)務(wù)名稱
               * 
          @param bizId 關(guān)聯(lián)業(yè)務(wù)ID.
               * 
          @param extParam 擴展條件
               * 
          @return true 被關(guān)聯(lián)/false 未被關(guān)聯(lián).
               
          */
              
          public boolean isBizReference(String bizName,String bizId,Map<String,Object>extParam) throws ServiceException {
                  Assert.notNull(bizName, 
          "業(yè)務(wù)名稱不能為空,bizName is NULL。");
                  Assert.notNull(bizId, 
          "記錄ID不能為空,bizId is NULL。");

                  
          try {
                      
          //逐個檢查依賴項是否有數(shù)據(jù)關(guān)聯(lián).
                      List<BizReferenceRule> rules = getBizRelationRule(bizName);
                      
          for(BizReferenceRule rule : rules){
                          StringBuilder sqlBuilder 
          = new StringBuilder();
                          sqlBuilder.append(
          "select count(*) from ").append(rule.getRelTable()).append(" where ")
                              .append(rule.getRelField()).append(
          "='").append(bizId).append("");
                          String extConditon 
          = rule.getExtCondition();
                          
          if(StringUtil.isNotBlank(extConditon)){
                              initTenantParam(extParam);
                              sqlBuilder.append(
          " and ").append(getExtParamSql(extConditon,extParam));
                          }
                          logger.debug(sqlBuilder);
                          
          int nCount = bizReferenceDao.getBizRelationCount(sqlBuilder.toString());
                          
          if (nCount != 0return true;
                      }
                      
          return false;
                  }
                  
          catch(Exception ex){
                      logger.error(
          "調(diào)用業(yè)務(wù)關(guān)聯(lián)服務(wù)錯誤。"+bizName+",bizId:"+bizId+",extParam"+LogUtil.parserBean(extParam),ex);
                      
          throw new ServiceException("調(diào)用業(yè)務(wù)關(guān)聯(lián)服務(wù)錯誤。");
                  }
              }
              
              
          /**
               * 組裝擴展查詢條件的sql
               * 
          @param condition
               * 
          @param extParam
               * 
          @return
               * 
          @throws Exception
               
          */
              
          private String getExtParamSql(String condition,Map<String,Object>extParam) throws Exception {
                  List
          <String> paramList = parseDyncParam(condition);
                  
          for(String param : paramList){
                      String simpleParam 
          = simpleName(param);
                      
          if(!extParam.containsKey(simpleParam)){
                          
          throw new ServiceException("動態(tài)參數(shù)值未設(shè)置! param:"+param+",extParam:"+LogUtil.parserBean(extParam));
                      }
                      condition 
          = condition.replaceAll(param, "'"+String.valueOf(extParam.get(simpleParam))+"'");
                  }
                  
          return condition;
              }
              
              
          /**
               * 解析擴展查詢條件中的動態(tài)參數(shù)名.
               * 
          @param condition
               * 
          @return
               * 
          @throws Exception
               
          */
              
          private List<String> parseDyncParam(String condition) throws Exception {
                  PatternCompiler compiler 
          = new Perl5Compiler();
                  PatternMatcher matcher 
          = new Perl5Matcher();
                  MatchResult result 
          = null;
                  PatternMatcherInput input 
          = null;
                  List
          <String> paramList = new ArrayList<String>();
                  input 
          = new PatternMatcherInput(condition);
                  Pattern pattern 
          = compiler.compile(PATTERN,Perl5Compiler.CASE_INSENSITIVE_MASK);
                  
          while (matcher.contains(input, pattern)){
                      result 
          = matcher.getMatch();
                      input.setBeginOffset(result.length());
                      paramList.add(result.group(
          0));
                  }
                  
          return paramList;
              }
              
              
          /**
               * 獲取業(yè)務(wù)關(guān)聯(lián)查詢規(guī)則.
               
          */
              
          private List<BizReferenceRule> getBizRelationRule(String bizName){
                  Assert.notNull(bizName, 
          "業(yè)務(wù)名稱不能為空,bizName is NULL。");
                  
                  
          //配置定義未加載到內(nèi)存時,讀取配置文件
                  if(ruleMaps == null){
                      parseRuleConfig();
                      
          if(ruleMaps == nullreturn null;
                  }
                  
                  
          return ruleMaps.get(bizName);
              }
              
              
          /**
               * 讀取業(yè)務(wù)關(guān)聯(lián)規(guī)則配置文件
               
          */
              @SuppressWarnings(
          "unchecked")
              
          private synchronized void parseRuleConfig(){
                  
          if(ruleMaps != null){
                      
          return;
                  }
                  
                  
          //解析業(yè)務(wù)引用定義文件.
                   
              }
              
              
          /**
               * 讀取Xml文檔
               * 
          @return
               
          */
              
          private Document getXmlDocument(){
                  InputStream is 
          = null;
                  
          try {
                      ClassLoader loader 
          = Thread.currentThread().getContextClassLoader();
                      is 
          = loader.getResourceAsStream(CFG_FILE);
                      SAXBuilder sb 
          = new SAXBuilder();
                      
          return sb.build(new BufferedInputStream(is));
                  }
                  
          catch(Exception ex) {
                      logger.error(
          "讀取配置文件錯誤. file:"+CFG_FILE, ex);
                      
          return null;
                  }
                  
          finally {
                      
          try {
                          
          if(is != null){
                              is.close();
                              is 
          = null;
                          }
                      }
                      
          catch(Exception ex) {
                          logger.error(ex);
                      }
                  }
              }

               
          }

          其他的一些可選處理方法:
          b. 在客戶表增加引用計數(shù)字段;
          需額外維護引用計數(shù)字段,在引用的業(yè)務(wù)邏輯增加或刪除記錄時,需對該字段的數(shù)值進行更新。適用于需要直接查詢記錄被引用次數(shù)的場景,但在集群環(huán)境下,需注意并發(fā)問題。
          posted on 2009-07-14 14:42 josson 閱讀(386) 評論(0)  編輯  收藏 所屬分類: java 開發(fā)
          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汽车| 太仆寺旗| 富民县| 友谊县| 平利县| 景东| 商丘市| 拉萨市| 大竹县| 琼海市| 普宁市| 南康市| 铜陵市| 界首市| 太原市| 湟源县| 罗源县| 康保县| 龙南县| 大港区| 新密市| 南溪县| 清水河县| 宁陵县| 木里| 阳曲县| 泉州市| 公安县| 双桥区| 德令哈市| 惠安县| 鄂州市| 蕉岭县| 惠水县| 阳原县| 梁河县| 呈贡县| 铅山县| 平湖市| 乌拉特后旗| 贡觉县|