package org.william.First;


import org.hibernate.usertype.*;
import java.io.Serializable;
import java.sql.Types;
import java.util.List;
import java.util.ArrayList;
import java.util.Vector;
import org.hibernate.type.*;
import org.hibernate.*;

import javax.sql.*;
import java.sql.*;

/**
 * @author wangliang138840
 *
 */
public class EMailList implements UserType{
 private List eMails;
 private static final char SPLITE = ';';
 //各个字段的类型
 public static final int[] TYPES = new int[]{Types.VARCHAR };
 //本实例是否可变
 public boolean isMutable(){
  return false;
 }
 //返回字段类型
 public int[] sqlTypes(){
  return TYPES;
 }
 //返回字段类型类
 public Class returnedClass(){
  return List.class;
 }
 /* (non-Javadoc)
  * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
  * 完全拷贝一个变量实体;
  */
 public Object deepCopy(Object value) throws HibernateException{
  List targetList = new ArrayList();
  if(value == null)return targetList;
  List sourceList = (List)value;
  targetList.addAll(sourceList);
  return targetList;
 }
 /* (non-Javadoc)
  * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
  * 判断两个实体是否相等
  */
 public boolean equals(Object x, Object y) throws HibernateException{
  if( x == y) return true;
  if(x != null && y != null){
   List listX = (List)x;
   List listY = (List)y;
   if(listX.size() != listY.size()) return false;
   for(int i = 0; i< listX.size(); i++){
    String str1 = (String)listX.get(i);
    String str2 = (String)listY.get(i);
    if(!equals(str1,str2)) return false;
   }
   return true;
  }
  return false;
 }
 
 public int hashCode(Object x)
  throws HibernateException{
  return -1;
 }
 public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
  throws HibernateException, SQLException{
  String value = (String)Hibernate.STRING.nullSafeGet(rs, names[0]);
  if(value != null)return db2oo(value);
  else return new ArrayList();
 }
 
 /* (non-Javadoc)
  * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
  */
 public void nullSafeSet(PreparedStatement st, Object value, int index)
  throws HibernateException, SQLException{
  if(value != null){
   Object str = oo2db(value);
   Hibernate.STRING.nullSafeSet(st, str, index);
  }else{
   Hibernate.STRING.nullSafeSet(st, value, index);
  }
 }
 
 /**
  * @param value
  * @return
  */
 private Object oo2db(Object value){
  StringBuffer strBuf = new StringBuffer();
  List list = (List)value;
  for(int i = 0; i<list.size() -1; i++){
   strBuf.append(list.get(i)).append(SPLITE);
  }
  strBuf.append(list.get(list.size()-1));
  return (Object)strBuf.toString();
 }
 
 /**
  * @param value
  * @return
  */
 private List db2oo(String value){
  Vector strs = org.apache.tools.ant.util.StringUtils.split(value, SPLITE);
  List emailList = new ArrayList();
  for(int i = 0; i<strs.size(); i++){
   emailList.add(strs.get(i));
  }
  return emailList;
 }

 /* (non-Javadoc)
  * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
  */
 public Object assemble(Serializable cached, Object owner)
  throws HibernateException{
  return cached;
 }
 
 public Serializable disassemble(Object value)
  throws HibernateException{
  return (Serializable)value;
 }
 
 public Object replace(Object original, Object target, Object owner)
  throws HibernateException{
  return null;
 }
 
}