代码笔记 - 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.

^^ 

需求响应动态冰蓄冷系统需求响应策略的优化研究(Matlab代码实现)内容概要:本文围绕“需求响应动态冰蓄冷系统需求响应策略的优化研究”展开,基于Matlab代码实现,重点探讨了冰蓄冷系统在电力需求响应背景下的动态建模优化调度策略。研究结合实际电力负荷电价信号,构建系统能耗模型,利用优化算法对冰蓄冷系统的运行策略进行求解,旨在降低用电成本、平衡电网负荷,并提升能源利用效率。文中还提及该研究为博士论文复现,涉及系统建模、优化算法应用仿真验证等关键技术环节,配套提供了完整的Matlab代码资源。; 适合人群:具备一定电力系统、能源管理或优化算法基础,从事科研或工程应用的研究生、高校教师及企业研发人员,尤其适合开展需求响应、综合能源系统优化等相关课题研究的人员。; 使用场景及目标:①复现博士论文中的冰蓄冷系统需求响应优化模型;②学习Matlab在能源系统建模优化中的具体实现方法;③掌握需求响应策略的设计思路仿真验证流程,服务于科研项目、论文写作或实际工程方案设计。; 阅读建议:建议结合提供的Matlab代码逐模块分析,重点关注系统建模逻辑优化算法的实现细节,按文档目录顺序系统学习,并尝试调整参数进行仿真对比,以深入理解不同需求响应策略的效果差异。
综合能源系统零碳优化调度研究(Matlab代码实现)内容概要:本文围绕“综合能源系统零碳优化调度研究”,提供了基于Matlab代码实现的完整解决方案,重点探讨了在高比例可再生能源接入背景下,如何通过优化调度实现零碳排放目标。文中涉及多种先进优化算法(如改进遗传算法、粒子群优化、ADMM等)在综合能源系统中的应用,涵盖风光场景生成、储能配置、需求响应、微电网协同调度等多个关键技术环节,并结合具体案例(如压缩空气储能、光热电站、P2G技术等)进行建模仿真分析,展示了从问题建模、算法设计到结果验证的全流程实现过程。; 适合人群:具备一定电力系统、能源系统或优化理论基础,熟悉Matlab/Simulink编程,从事新能源、智能电网、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①开展综合能源系统低碳/零碳调度的科研建模算法开发;②复现高水平期刊(如SCI/EI)论文中的优化模型仿真结果;③学习如何将智能优化算法(如遗传算法、灰狼优化、ADMM等)应用于实际能源系统调度问题;④掌握Matlab在能源系统仿真优化中的典型应用方法。; 阅读建议:建议结合文中提供的Matlab代码网盘资源,边学习理论模型边动手调试程序,重点关注不同优化算法在调度模型中的实现细节参数设置,同时可扩展应用于自身研究课题中,提升科研效率模型精度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值