一般業務系統中總會存在一些基礎數據,在其他的業務單據中會被套引用。因此,系統必須保證這些被業務單據引用的基礎數據不能任意的刪除。最常見的做法就是,在刪除基礎數據時,預先校驗該類數據是否在相關業務表中存在,若不存在才允許用戶刪除,否則給用戶以提示。
但這樣的處理方法,有些缺點,就是需要編碼對每個業務類提供查詢方法,或在刪除邏輯中增加判斷邏輯。因此,每次引用關系變化,增加或減少時免不了要修改原來的邏輯,時間越長,系統的維護成本就越來越高了。因此,有必要對系統進行重構,將這類的處理邏輯進行抽象,單獨封裝成一個服務,當引用關系有變更時,不用再修改原有邏輯,通過配置就可以完成變更。
通用引用關系查詢服務,主要就是通過db表或xml配置文件,對系統中每個基礎數據有引用的所有關系進行定義,定義屬性主要是引用的表及字段名稱。查詢時,從配置文件中讀取指定類別的引用關系,并逐一查詢這些表中的記錄,以確定數據是否被引用。這種處理方法的優點為,易擴展、可維護性強,引用關系變更時,僅通過維護配置文件,不必進行編碼,就能實現,這樣能大大的提高系統的穩定性。
xml配置文件如下:
<rule bizName='product' desc="產品關聯項定義">
<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="客戶關聯項定義">
<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>
通用業務引用查詢類代碼片段如下:
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";
... ...
/**
* 查詢指定業務數據是否被其他業務表關聯依賴.
* @param bizName 關聯業務名稱
* @param bizId 關聯業務ID.
* @param extParam 擴展條件
* @return true 被關聯/false 未被關聯.
*/
public boolean isBizReference(String bizName,String bizId,Map<String,Object>extParam) throws ServiceException {
Assert.notNull(bizName, "業務名稱不能為空,bizName is NULL。");
Assert.notNull(bizId, "記錄ID不能為空,bizId is NULL。");
try {
//逐個檢查依賴項是否有數據關聯.
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 != 0) return true;
}
return false;
}
catch(Exception ex){
logger.error("調用業務關聯服務錯誤。"+bizName+",bizId:"+bizId+",extParam"+LogUtil.parserBean(extParam),ex);
throw new ServiceException("調用業務關聯服務錯誤。");
}
}
/**
* 組裝擴展查詢條件的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("動態參數值未設置! param:"+param+",extParam:"+LogUtil.parserBean(extParam));
}
condition = condition.replaceAll(param, "'"+String.valueOf(extParam.get(simpleParam))+"'");
}
return condition;
}
/**
* 解析擴展查詢條件中的動態參數名.
* @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;
}
/**
* 獲取業務關聯查詢規則.
*/
private List<BizReferenceRule> getBizRelationRule(String bizName){
Assert.notNull(bizName, "業務名稱不能為空,bizName is NULL。");
//配置定義未加載到內存時,讀取配置文件
if(ruleMaps == null){
parseRuleConfig();
if(ruleMaps == null) return null;
}
return ruleMaps.get(bizName);
}
/**
* 讀取業務關聯規則配置文件
*/
@SuppressWarnings("unchecked")
private synchronized void parseRuleConfig(){
if(ruleMaps != null){
return;
}
//解析業務引用定義文件.

}
/**
* 讀取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. 在客戶表增加引用計數字段;
需額外維護引用計數字段,在引用的業務邏輯增加或刪除記錄時,需對該字段的數值進行更新。適用于需要直接查詢記錄被引用次數的場景,但在集群環境下,需注意并發問題。
posted on 2009-07-14 14:42
josson 閱讀(384)
評論(0) 編輯 收藏 所屬分類:
java 開發