KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > dl > dbloader > ValueRowMetaData


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.setup.impl.dl.dbloader;
10
11 import org.jboss.portal.setup.PortalSetupException;
12
13 import org.jboss.portal.setup.util.PropertyConverter;
14
15 import java.util.List JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import java.util.ArrayList JavaDoc;
18
19
20 /**
21  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
22  * Date: Apr 14, 2005; Time: 3:48:03 PM
23  * @since JBoss portal 2.0
24  * Class org.jboss.portal.setup.impl.dl.dbloader.ValueRowMetaData
25  */

26 public class ValueRowMetaData
27 {
28    private List JavaDoc m_values = new ArrayList JavaDoc();
29
30    public ValueRowMetaData(String JavaDoc values, List JavaDoc columns) throws PortalSetupException
31    {
32       if (null != values)
33       {
34          StringTokenizer JavaDoc strTk = new StringTokenizer JavaDoc(values, ",", false);
35          int i = 0;
36          while (strTk.hasMoreTokens())
37          {
38
39             Object JavaDoc value = null;
40             String JavaDoc token = strTk.nextToken();
41             if (token != null)
42             {
43                token = token.trim();
44                //strip quotes
45
int indexFirst = token.indexOf("'");
46                int indexLast = token.lastIndexOf("'");
47
48                if (indexFirst == -1 && indexLast == -1)
49                {
50                   //check if function
51
if (isFunction(token))
52                   {
53                      //fo special handling later
54
value = new SqlFunctionMetaData(token);
55                   }
56                   else
57                   {
58                      //what to do ? got unquated value
59
value = resolveValue(token, (ColumnMetaData)columns.get(i));
60                   }
61                }
62                else
63                {
64                   String JavaDoc stippedValueStr = token.substring(indexFirst + 1, indexLast);
65                   value = resolveValue(stippedValueStr, (ColumnMetaData)columns.get(i));
66
67                }
68
69                m_values.add(value);
70                i++;
71             }
72          }
73       }
74       else
75       {
76          throw new PortalSetupException("Value row cannot be null");
77       }
78    }
79
80    private Object JavaDoc resolveValue(String JavaDoc oldValue, ColumnMetaData column) throws PortalSetupException
81    {
82       Object JavaDoc value = null;
83       Class JavaDoc javaClass = null;
84       try
85       {
86          javaClass = Class.forName(column.getJavaType());
87
88
89       }
90       catch (ClassNotFoundException JavaDoc cnfe)
91       {
92          throw new PortalSetupException("Invalid java type specified", cnfe);
93       }
94       try
95       {
96
97          value = PropertyConverter.convertToValue(oldValue, javaClass);
98
99       }
100       catch (IllegalArgumentException JavaDoc iae)
101       {
102          throw new PortalSetupException("Fail to convert a string value = '" + oldValue + "' to java object of class '" +
103             column.getJavaType() + "'.", iae);
104       }
105       return value;
106    }
107
108    public List JavaDoc getValues()
109    {
110       return m_values;
111    }
112
113    private boolean isFunction(String JavaDoc strValue)
114    {
115       boolean retVal = false;
116       int index = strValue.indexOf("(");
117       if (index > 0)
118       {
119          String JavaDoc funcName = strValue.substring(0, index);
120          retVal = true;
121          //isSupported ()
122
//resolveFunctionValue(strValue, funcName);
123
}
124       return retVal;
125    }
126
127
128 }
129
Popular Tags