1 package org.apache.ojb.broker.accesslayer.conversions; 2 3 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.commons.lang.StringUtils; 22 23 31 public class IntList2VarcharFieldConversion implements FieldConversion 32 { 33 34 private static final String NULLVALUE = "#NULL#"; 35 private static final String EMPTYCOLLEC = "#EMTPY#"; 36 37 public IntList2VarcharFieldConversion() 38 { 39 } 40 41 44 public Object javaToSql(Object source) throws ConversionException 45 { 46 if (source == null) 47 { 48 return NULLVALUE; 49 } 50 51 try 52 { 53 List intList = (List ) source; 54 if (intList.isEmpty()) 55 { 56 return EMPTYCOLLEC; 57 } 58 59 StringBuffer result = new StringBuffer (); 60 for (int i = 0; i < intList.size(); i++) 61 { 62 Integer obj = (Integer ) intList.get(i); 63 String newSt = obj.toString(); 64 newSt = StringUtils.replace(newSt, "#", "##"); 65 result.append(newSt); 66 result.append("#"); 67 } 68 return result.toString(); 69 } 70 catch (ClassCastException e) 71 { 72 throw new ConversionException("Object is not a List of Integer it is a" 73 + source.getClass().getName()); 74 } 75 } 76 77 80 public Object sqlToJava(Object source) throws ConversionException 81 { 82 83 if (source == null) 84 { 85 return null; 86 } 87 if (!(source instanceof String )) 88 { 89 throw new ConversionException("Object is not a String it is a" 90 + source.getClass().getName()); 91 } 92 if (source.toString().equals(NULLVALUE)) 93 { 94 return null; 95 } 96 if (source.toString().equals(EMPTYCOLLEC)) 97 { 98 return new ArrayList (); 99 } 100 101 List v = new ArrayList (); 102 String input = source.toString(); 103 int pos = input.indexOf("#"); 104 105 while (pos >= 0) 106 { 107 if (pos == 0) 108 { 109 v.add(""); 110 } 111 else 112 { 113 v.add(Integer.valueOf(input.substring(0, pos))); 114 } 115 116 if (pos + 1 > input.length()) 117 { 118 break; 120 } 121 122 input = input.substring(pos + 1, input.length()); 123 pos = input.indexOf("#"); 124 } 125 return v; 126 } 127 128 } 129 | Popular Tags |