VoHelper

package com.isoftstone.pcis.policy.core.helper;

import com.isoftstone.fwk.dao.CommonDao;
import com.isoftstone.fwk.service.BusinessServiceException;
import com.isoftstone.fwk.util.BeanTool;
import com.isoftstone.fwk.util.SpringUtils;
import com.isoftstone.pcis.policy.core.exception.VoManipulateException;
import com.isoftstone.pcis.policy.vo.AbstractCvrgVO;
import com.isoftstone.pcis.prod.exception.ProductDefinitionException;
import com.isoftstone.pcis.prod.service.ProdService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class VoHelper
{
  public static String VO_TYPE_APP = "App";
  public static String VO_TYPE_PLY = "Ply";

  public static String ELEMENT_NAME = "Name";
  public static String ELEMENT_CAPTION = "Caption";
  public static String ELEMENT_NAME_PRODEF = "CNmeEn";
  public static String ELEMENT_CAPTION_PRODEF = "CNmeCn";

  private static final Log logger = LogFactory.getLog(VoHelper.class);

  private static String voPkg = "com.isoftstone.pcis.policy.vo";
  private static String voUdrPkg = "com.isoftstone.pcis.policy.app.underwrite.vo";

  public static Class getVoClass(String componentName, String voType) {
    String className = voPkg + "." + getVoClassName(componentName, voType);
    try {
      return Class.forName(className);
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }return null;
  }

  public static Class getAppVoClass(String componentName)
  {
    String className = voPkg + "." + getAppVoClassName(componentName);
    try {
      return Class.forName(className);
    } catch (ClassNotFoundException e) {
      logger.error("Class.forName(" + className + ") 出现异常。");
    }return null;
  }

  public static Class getPlyVoClass(String componentName)
  {
    String className = voPkg + "." + getPlyVoClassName(componentName);
    try {
      return Class.forName(className);
    } catch (ClassNotFoundException e) {
      logger.error("Class.forName(" + className + ") 出现异常。");
    }return null;
  }

  public static Class getUdrVoClass(String componentName)
  {
    String className = voUdrPkg + "." + getUdrVoClassName(componentName);
    try {
      return Class.forName(className);
    } catch (ClassNotFoundException e) {
      logger.error("Class.forName(" + className + ") 出现异常。");
    }return null;
  }

  public static Object createAppEntryVo(String componentName)
  {
    Class c = getAppVoClass(componentName);
    try {
      if (c != null) {
        return c.newInstance();
      }
      return null;
    } catch (InstantiationException e) {
      logger.error(e.getMessage());
      return null;
    } catch (IllegalAccessException e) {
      logger.error(e.getMessage());
    }return null;
  }

  public static Object createPlyEntryVo(String componentName)
  {
    Class c = getPlyVoClass(componentName);
    try {
      if (c != null) {
        return c.newInstance();
      }
      return null;
    } catch (InstantiationException e) {
      logger.error(e.getMessage());
      return null;
    } catch (IllegalAccessException e) {
      logger.error(e.getMessage());
    }return null;
  }

  public static Object createUdrEntryVo(String componentName)
  {
    Class c = getUdrVoClass(componentName);
    try {
      if (c != null) {
        return c.newInstance();
      }
      return null;
    } catch (InstantiationException e) {
      logger.error(e.getMessage());
      return null;
    } catch (IllegalAccessException e) {
      logger.error(e.getMessage());
    }return null;
  }

  public static String getVoClassName(String componentName, String voType)
  {
    return voType + componentName + "VO";
  }

  public static String getAppVoClassName(String componentName)
  {
    return "App" + componentName + "VO";
  }

  public static String getPlyVoClassName(String componentName)
  {
    return "Ply" + componentName + "VO";
  }

  public static String getUdrVoClassName(String componentName)
  {
    return "Undr" + componentName + "VO";
  }

  public static String getComponentNameOfVo(Object entry)
  {
    String[] pathArray = entry.getClass().getName().split("\\.");
    String voName = pathArray[(pathArray.length - 1)];
    String featureName = null;
    if ((voName.matches("^App")) || (!voName.matches("VO$")))
      featureName = voName.replaceAll("^App", "").replaceAll("VO$", "");
    else if ((voName.matches("^Ply")) || (!voName.matches("VO$"))) {
      featureName = voName.replaceAll("^Ply", "").replaceAll("VO$", "");
    }

    return featureName;
  }

  public static void serializePlyVo() {
    CommonDao dao = (CommonDao)SpringUtils.getSpringBean("commonDao");
  }

  public static boolean hasPkId(Object vo) throws VoManipulateException
  {
    try {
      Map propertyMap = BeanUtils.describe(vo);
      if (propertyMap.containsKey("CPkId")) {
        return true;
      }
      return false;
    }
    catch (Exception e) {
      throw new VoManipulateException("判断vo是否有CPkId字段时出错", e);
    }
  }

  public static String getOrigPkId(Object vo)
    throws VoManipulateException
  {
    try
    {
      String origRowId = BeanUtils.getProperty(vo, "CRowId");
      if (origRowId != null) {
        return origRowId;
      }
      return BeanUtils.getProperty(vo, "CPkId");
    }
    catch (Exception e)
    {
      throw new VoManipulateException("获得vo[" + vo.getClass().getName() + "]原始id时出错", e);
    }
  }

  public static Long getSeqNoIgnoreException(Object vo) {
    try {
      return (Long)PropertyUtils.getProperty(vo, "NSeqNo");
    } catch (Exception e) {
      logger.warn("忽略获得seqno时出错");
    }return null;
  }

  public static Map<String, Map<String, Object>> compareEntry(Object oldEntry, Object newEntry, Set<String> propSet)
    throws VoManipulateException
  {
    try
    {
      propSet.remove("class");
      Map diffMap = new HashMap();

      for (String propName : propSet) {
        Object oldValue = PropertyUtils.getProperty(oldEntry, propName);
        Object newValue = PropertyUtils.getProperty(newEntry, propName);

        if ((oldValue != null) || (newValue != null))
        {
          if ((oldValue != null) && (newValue != null))
          {
            if ((oldValue instanceof Date)) {
              if (DateUtils.isSameDay((Date)oldValue, (Date)newValue)) {
                continue;
              }
            }
            else {
              if ((oldValue instanceof Double) ?
                Double.compare(((Double)oldValue).doubleValue(), ((Double)newValue).doubleValue()) == 0 :
                oldValue.equals(newValue))
              {
                continue;
              }
            }
          }
          Map diff = new HashMap();
          diff.put("old", oldValue);
          diff.put("new", newValue);
          diffMap.put(propName, diff);
        }
      }

      return diffMap;
    } catch (Exception e) {
      throw new VoManipulateException("比较失败", e);
    }
  }

  public static Map<String, Map<String, Object>> compareEntryEx(Object oldEntry, Object newEntry, Set<String> excludePropSet)
    throws VoManipulateException
  {
    try
    {
      Map propMap = BeanUtils.describe(oldEntry);
      Set propSet = new HashSet();
      propSet.addAll((Collection)propMap.keySet());
      propSet.removeAll(excludePropSet);

      return compareEntry(oldEntry, newEntry, propSet);
    } catch (Exception e) {
      throw new VoManipulateException("比较失败", e);
    }
  }

  public static Map<String, Map<String, Object>> compareEntry(Object oldEntry, Object newEntry)
    throws VoManipulateException
  {
    try
    {
      Map propMap = BeanUtils.describe(oldEntry);
      Set propSet = new HashSet();
      propSet.addAll((Collection)propMap.keySet());

      return compareEntry(oldEntry, newEntry, propSet);
    } catch (Exception e) {
      throw new VoManipulateException("比较失败", e);
    }
  }

  public static List<String> getKeyNameList(String prodNo, String componentName)
    throws ProductDefinitionException
  {
    try
    {
      ProdService prodService = (ProdService)SpringUtils.getSpringBean("prodService");
      return prodService.getProdEleNmeEn(prodNo, componentName);
    } catch (BusinessServiceException e) {
      throw new ProductDefinitionException("获得保单要素分类关键字异常", e);
    }
  }

  public static List<String> getCoverageKeyNameList(String prodNo, String cvrgNo)
    throws ProductDefinitionException
  {
    try
    {
      ProdService prodService = (ProdService)SpringUtils.getSpringBean("prodService");
      return prodService.getProdEleNmeEn(prodNo, "Cvrg",
        cvrgNo);
    } catch (BusinessServiceException e) {
      throw new ProductDefinitionException("获得保单要素分类关键字异常", e);
    }
  }

  public static List<Map<String, String>> getKeyNameCaptionMapList(String prodNo, String componentName)
    throws ProductDefinitionException
  {
    try
    {
      ProdService prodService = (ProdService)SpringUtils.getSpringBean("prodService");
      List elemNameMapList = prodService.getProdEleNme(prodNo, componentName, null);
      List nameLabelMapList = new ArrayList();
      for (Map elemNameMap : elemNameMapList) {
        String complexName = (String)elemNameMap.get("CNmeEn");
        String[] nameSects = complexName.split("\\.");
        Map nameLabelMap = new HashMap();
        nameLabelMap.put(ELEMENT_NAME_PRODEF, nameSects[1]);
        nameLabelMap.put(ELEMENT_CAPTION_PRODEF, (String)elemNameMap.get(ELEMENT_CAPTION_PRODEF));
        nameLabelMap.put(ELEMENT_NAME, nameSects[1]);
        nameLabelMap.put(ELEMENT_CAPTION, (String)elemNameMap.get(ELEMENT_CAPTION_PRODEF));

        nameLabelMapList.add(nameLabelMap);
      }
      return nameLabelMapList;
    } catch (BusinessServiceException e) {
      throw new ProductDefinitionException("获得保单要素分类关键字异常", e);
    }
  }

  public static List<Map<String, String>> getCoverageKeyNameCaptionMapList(String prodNo, String cvrgNo)
    throws ProductDefinitionException
  {
    try
    {
      ProdService prodService = (ProdService)SpringUtils.getSpringBean("prodService");
      List elemNameMapList = prodService.getProdEleNme(prodNo,
        "Cvrg", cvrgNo);
      List nameLabelMapList = new ArrayList();
      for (Map elemNameMap : elemNameMapList) {
        String complexName = (String)elemNameMap.get("CNmeEn");
        String[] nameSects = complexName.split("\\.");
        Map nameLabelMap = new HashMap();
        nameLabelMap.put(ELEMENT_NAME_PRODEF, nameSects[1]);
        nameLabelMap.put(ELEMENT_CAPTION_PRODEF, (String)elemNameMap.get(ELEMENT_CAPTION_PRODEF));
        nameLabelMap.put(ELEMENT_NAME, nameSects[1]);
        nameLabelMap.put(ELEMENT_CAPTION, (String)elemNameMap.get(ELEMENT_CAPTION_PRODEF));

        nameLabelMapList.add(nameLabelMap);
      }
      return nameLabelMapList;
    } catch (BusinessServiceException e) {
      throw new ProductDefinitionException("获得保单要素分类关键字异常", e);
    }
  }

  public static String getKeyValueAsStr(Object entry, List<String> keyNameList)
    throws VoManipulateException
  {
    try
    {
      if ((keyNameList != null) && (!keyNameList.isEmpty())) {
        String[] elemValues = new String[keyNameList.size()];
        for (int i = 0; i < keyNameList.size(); i++) {
          String attrName = ((String)keyNameList.get(i)).substring(
            ((String)keyNameList.get(i)).indexOf(".") + 1);
          String aValue = BeanTool.getAttributeValueAsString(entry, attrName);
          elemValues[i] = aValue;
        }
        return StringUtils.join(elemValues, "-%-");
      }

      if ((entry instanceof AbstractCvrgVO)) {
        return ((AbstractCvrgVO)entry).getCCvrgNo();
      }
      Long seqNo = getSeqNoIgnoreException(entry);
      if (seqNo != null) {
        return seqNo.toString();
      }
      return null;
    }
    catch (Exception e)
    {
      throw new VoManipulateException("生成要素关键标识描述时异常", e);
    }
  }
}

VoHelper的更多相关文章

随机推荐

  1. core--线程调度

    线程的调度和我们用人是同一个道理,假如我们是公司的CEO,你的手下有7名大将,10名美女,100000名普通员工,现在你的任务是在1年内情切慰问完所有的员工.你将如何安排这个行程?先从那些人开始?从哪 ...

  2. HDU 4023 (博弈 贪心 模拟) Game

    如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...

  3. HDU 1850 (Nim博弈 取胜方案数) Being a Good Boy in Spring Festival

    考虑到Bouton定理的证明过程,设n个数的Nim和(异或和)为X,其最高位的1在第k位,那么n个数中一定有个y的第k为也是个1. 将y的数量变为X xor y,那么n的数的Nim和为0,便转为先手必 ...

  4. HDU 2512 一卡通大冒险

    我感觉这更像个数学问题. dp[i][j]表示将i件物品分成j组的方案数. 状态转移方程: dp[i][j] = dp[i-1][j-1] + j * dp[i-1][j]; 将i张卡分成j组可以有之 ...

  5. BZOJ 3631 松鼠的新家

    链剖. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  6. Sqoop的使用(Mysql To HBase)

    最近需要将mysql的数据整合到HBase中,原本使用MapReduce,自己制作job将mysql的数据导入, 查阅资料过程中,发现了开源工具sqoop(关系性数据库与HDFS,HBASE,HIVE ...

  7. IP碎片原理:攻击和防护

    为了加深理解IP协议和一些DoS攻击手段大家有必要看看以下内容,也许对你理解这个概念有所帮助.先来看看IP碎片是如何产生的吧.         一.IP碎片是如何产生的       链路层具有最大传输 ...

  8. Android 注解的一些应用以及原理

    在这边文章之前你首先需要对java 的注解部分有一个基本的了解(不需要太过的深入). 简单来说,注解这个东西就是用于辅助我们开发java代码的,注解本身无法干扰java源代码的执行. 在android ...

  9. nginx的配置,要求根据不同的来路域名,发送到不同的端口去处理

    这一台电脑上既有tomcat 也有 apache,他俩是没有办法同时享用80端口的.我现在让tomcat用8088,apache用8080,然后让nginx用80,这样nginx在收到请求后,根据不同 ...

  10. DOMContentLoaded事件

    今天查看百度空间源代码,发现多了个util.js文件,打开看看.里面里面定义了addDOMLoadEvent.这是干什么用的? 仔细查看代码,发现在Mozilla添加了DOMContentLoaded ...