抽象一個Matter。然後把這個Matter和相關的部分現實映射去一個管理Licence的系統。然後一個Matter可以實現一個Licence,或者一個Authorized Supervisor。資料庫內,所有的Matter的頂層抽象由一張表維護,Licence和AS的Code由一張表維護,AS和Licence的關係由一張表維護。
Business Analyst又告訴我一個Licence只能有最多2個AS associated;然後 the same situation applies the other way around...
之後遇到了一個問題,就是防範措施放在哪裡,肯定不能在presentation tier;如果放入Struts Action,放在哪個方法內呢?
而且防範有4種情況:
拿到一個current Matter,要判斷它是AS還是Licence;之後,要拿到這個matter的一個List,裡面是associated matters.
除了這個current matter,還有使用者想去associate with的 target matter。同樣,那個target matter也有一個自己的List,裡面是target matter association(of matter).
那麼就要4種判斷。
2 2 一組,之後我把代碼寫的好重複好冗餘,但事實上,真正的工作環境下,誰會1天像7小時考試一樣把代碼做到agile, beautiful,沒有腦子也沒有時間。
如果要重構當然可以,但是現實和理想往往是兩回事。
Code Snippet as follows:
AirMatterActionForm frm = (AirMatterActionForm)form;
MtrMatSuperTypeCustom matter = frm.getMatter();
List<MtrMatterXrefCustom> matters = matter.getMtrMatters();
ActionMessages messages = new ActionMessages();
Connection conn = (TransactionConnection)request.getAttribute("CONNECTION");
// check the related matter that the current matter is going to associate with first, there are together four conditions
if ( !(frm.getMtrMatterId2() == null || frm.getMtrMatterId2().equals("") || frm.getMtrXrefType() == null || frm.getMtrXrefType().equals("")) )
{
MtrMatSuperTypeCustom relatedMatter = new MtrMatSuperTypeCustom();
MtrMatSuperTypeQuery.query(Long.parseLong(frm.getMtrMatterId2()),relatedMatter,"VIEW",conn);
List<MtrMatterXrefCustom> relatedMatters = relatedMatter.getMtrMatters();
//Find out if this matter is already related to two AS application...
int counter = 0;
if(relatedMatters.size() > 1)
{
// if the matter is a Licence type
if(relatedMatter.getMtrMidCd() == "1" || "1".equals(relatedMatter.getMtrMidCd()))
{
Iterator iter = relatedMatters.iterator();
while(iter.hasNext())
{
MtrMatterXrefCustom matterX = (MtrMatterXrefCustom) iter.next();
if(matterX.getMtrXrefType() == "AS" || "AS".equals(matterX.getMtrXrefType()))
{
counter ++;
}
if(counter >= 2)
{
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.detail", "Warning - Warning - The Licence you are intend to associate with has already 2 Authorised Supervisors associated. The actioin is not permitted. "));
saveMessages(request, messages);
return mapping.findForward("RefMtr");
}
}
} else if (relatedMatter.getMtrMidCd() == "5" || "5".equals(relatedMatter.getMtrMidCd()))
{
// if the matter is an AS type
Iterator iter = relatedMatters.iterator();
while(iter.hasNext())
{
MtrMatterXrefCustom matterX = (MtrMatterXrefCustom) iter.next();
if(matterX.getMtrXrefType() == "LIC" || "LIC".equals(matterX.getMtrXrefType()))
{
counter++;
}
if(counter >= 2)
{
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.detail", "Warning - The Authorised Supervisor you are intend to associate with has already 2 licenses associated. The actioin is not permitted. "));
saveMessages(request, messages);
return mapping.findForward("RefMtr");
}
}
}
}
//so need of this breaking line ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
// then check the current matter.
//Find out if this matter is already related to two AS application...
int counter = 0;
if(matters.size() > 1)
{
// if the matter is a Licence type
if(matter.getMtrMidCd() == "1" || "1".equals(matter.getMtrMidCd()))
{
Iterator iter = matters.iterator();
while(iter.hasNext())
{
MtrMatterXrefCustom matterX = (MtrMatterXrefCustom) iter.next();
if(matterX.getMtrXrefType() == "AS" || "AS".equals(matterX.getMtrXrefType()))
{
counter ++;
}
if(counter >= 2)
{
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.detail", "Warning - This License already has 2 Authorised Supervisor related to it. Another Authorised Supervisor is not permitted to be assigned. "));
saveMessages(request, messages);
return mapping.findForward("RefMtr");
}
}
} else if (matter.getMtrMidCd() == "5" || "5".equals(matter.getMtrMidCd()))
{
// if the matter is an AS type
Iterator iter = matters.iterator();
while(iter.hasNext())
{
MtrMatterXrefCustom matterX = (MtrMatterXrefCustom) iter.next();
if(matterX.getMtrXrefType() == "LIC" || "LIC".equals(matterX.getMtrXrefType()))
{
counter++;
}
if(counter >= 2)
{
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.detail", "Warning - This Authorised Supervisor already has 2 other associated licenses. Please make another selection. "));
saveMessages(request, messages);
return mapping.findForward("RefMtr");
}
}
}
}
//end of the if-else
}
if (frm.getMtrMatterId2() == null || frm.getMtrMatterId2().equals("") || frm.getMtrXrefType() == null || frm.getMtrXrefType().equals(""))
return mapping.findForward("RefMtr");
MtrMatterXrefCustom ref = new MtrMatterXrefCustom();
ref.setMtrMatterId1(matter.getMtrMatterId());
ref.setMtrMatterId2(new Long(frm.getMtrMatterId2()));
ref.setMtrXrefType(frm.getMtrXrefType());
try
{
ref.setMtrNumberFormatted(MtrMatterXrefQueryCustom.queryNumber(ref.getMtrMatterId2(), conn));
ref.setMtrCitedName(MtrMatterXrefQueryCustom.queryCitedName(ref.getMtrMatterId2(), conn));
}
catch (Exception e)
{
ProjectLogger.debug(this.getClass().getName() + ".addref() :: exception Caught! " + e);
}
Calendar nowCal = Calendar.getInstance();
Date nowDate = new Date();
nowCal.setTime(nowDate);
ref.setMtrRefDate(new Timestamp(nowDate.getTime()));
if (!ref.isContain(matters, ref))
{
matters.add(ref); //add it to collection
ProjectLogger.debug(this.getClass().getName() + ".addref() :: ref = " + ref);
}
return mapping.findForward("RefMtr");
... End of this snippet
雖然冗餘,難看,但是可以用,真的,工作中沒有多少精力來做邏輯抽象和美化。會累。架構師可以拿筆一畫,抽象的說一筆;可實現不還是由辛苦又吃力不討好的CODER來實現。It is a bit unfair sometimes.....
So the only way to step out of the misery is.... you be the architect.
^^