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. Velocity模板中的注释

    Velocity ——VTL模板中的注释 注释允许在模板中包含描述文字,而这些文字不会被放置到模板引擎的输出中.注释是一种有效的提醒自己和向别人解释你的VTL语句要做什么事情的方法.你也可以把注释用来 ...

  2. POJ 1037 (计数 + DP) 一个美妙的栅栏

    这道题总算勉勉强强看懂了,DP和计数都很不好想 DP部分: 称i根木棒的合法方案集合为S(i),第二根木棒比第一根长的方案称作UP方案,反之叫做DOWN方案 C[i][k][DOWN] 是S(i)中以 ...

  3. 报错:对象必须实现 IConvertible;以分隔符进行分割链接concat_ws的使用方法;mysql数据类型转换cast,convert

    错误故障,mysql  服务器上用 concat_ws 函数,连接了一串数字,最后 服务器返回的字段就变成了一个 byte ,而我们想要的类型是  string 类型,那么转换的时候,就报错了. 正确 ...

  4. BZOJ 2157 旅行

    裸链剖. 这大概是我第一份两百行左右的代码吧. 然而我把题看错了233333333调了将近两天. #include<iostream> #include<cstdio> #in ...

  5. 【django】django学得好迷茫啊 来个学习规划吧

    http://www.zhihu.com/question/26235428

  6. Android手机部分名词浅谈

    1.什么是Radio? Radio是无线通信模块的驱动程序,ROM是系统程序,radio负责网络通信,ROM和Radio可以分开刷,互不影响.如果您的手机刷新了ROM后有通讯方面的问题可以刷新radi ...

  7. 看人家如何拿到腾讯阿里的offer

    我是如何同时拿到阿里和腾讯offer的: https://segmentfault.com/a/1190000002627927

  8. K2 Blackpearl开发技术要点(Part2)

    转:http://www.cnblogs.com/dannyli/archive/2012/09/14/2685282.html K2 Blackpearl开发技术要点(Part2)  

  9. POJ 1844 Sum

    题意:给一个整数n,求当n由1到k的连续整数加或减组成时的最小的k. 解法:一开始觉得dp……后来觉得复杂度太大了……GG……百度了一下是个数学题orz. 如果n全部由加法组成,那么k可以组成k(k+ ...

  10. the server responded with a status of 404 (Not Found)

    1.出现这种问题,第一时间检查文件路径是否正确,相对路径或者绝对路径是否正确 2.某些后缀的文件是否能够找到,我现在碰到的就是.md文件找不到,需要配置web.config <system.we ...