代码笔记 - Licence与Authorized Supervisor

抽象一个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.

^^ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值