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. POJ (Manacher) Palindrome

    多敲几个模板题,加深一下对Manacher算法的理解. 这道题给的时间限制15s,是我见过的最长的时间的了.看来是为了让一些比较朴素的求最大回文子串的算法也能A过去 Manacher算法毕竟给力,运行 ...

  2. 51nod1120 机器人走方格 V3

    跟括号序列是一样的,将向右走看成是左括号向左走看成是右括号就可以了.那么就是卡特兰数了.然后由于n和m太大所以用了lucas定理 //跟括号序列是一样的,将向右走看成是左括号向左走看成是右括号就可以了 ...

  3. 《深入浅出嵌入式底层软件开发》—1. ARM汇编编程基础

    1.1 ARM CPU寄存器 ARM的汇编编程,本质上就是针对CPU寄存器的编程,所以要搞清楚ARM有哪些寄存器:ARM寄存器分为两类:普通寄存器和状态寄存器:普通寄存器一共有16个,分别为R0——R ...

  4. Hibernate-Criteria Queries

    1.实例 接口org.hibernate.Criteria针对特殊持久层类进行查询,Sesion是Criteria的工厂: Criteria crit = sess.createCriteria(Ca ...

  5. 《C++ Primer 4th》读书笔记 序

    注:本系列读书笔记是博主写作于两三年前的,所以是基于<C++ Primer>第四版的,目前该书已更新至第五版,第五版是基于C++11标准的,貌似更新挺多的.博主今年应届硕士毕业,如若过阵子 ...

  6. 扩容盘、SD卡扩容

    内存卡的前世今生回想当年,大家都还在用着非智能机,由于功能单一,需要存储的数据也就是通讯录和短信.虽然那时也有手机游戏,但大多都是几十KB,并不需要太大的存储空间.但随着手机功能的多样化,尤其是音乐. ...

  7. 配置java环境时,java的path地址放在其他地址的前面还是后面?

      PATH环境变量里是按顺序查找的,如果电脑里只有一个jdk,放哪都一样,有多个放前面的会优先.   一般是放在最前面吧,以免其他在path中的程序需要用到Java环境而由于在JAVA_PATH之前 ...

  8. 嵌入式 C 语言的可变参数表函数的设计

    首先在介绍可变参数表函数的设计之前,我们先来介绍一下最经典的可变参数表printf函数的实现原理.一.printf函数的实现原理在C/C++中,对函数参数的扫描是从后向前的.C/C++的函数参数是通过 ...

  9. struct 与 typedef struct

    1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typede ...

  10. 性能测试指标&说明 [解释的灰常清楚哦!!]

    详见: 浅谈软件性能测试中关键指标的监控与分析 http://www.51testing.com/html/18/n-3549018.html