View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.sql;
18  
19  import COM.FutureTense.Interfaces.IList;
20  import com.fatwire.cs.core.db.Util;
21  import org.apache.commons.lang3.StringUtils;
22  
23  import java.util.Date;
24  
25  /**
26   * Implements a Row.
27   *
28   * @author Dolf Dijkstra
29   */
30  class SingleRow implements Row {
31  
32      private final IList list;
33  
34      /**
35       * @param list iterable list
36       */
37      public SingleRow(final IList list) {
38          super();
39          this.list = list;
40      }
41  
42      /*
43       * (non-Javadoc)
44       * 
45       * @see "tools.gsf.facade.sql.Row#getBytes(java.lang.String)"
46       */
47      public byte[] getBytes(final String key) {
48          try {
49              return (byte[]) list.getObject(key);
50          } catch (final NoSuchFieldException e) {
51              throw new RuntimeException(e);
52          }
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see "tools.gsf.facade.sql.Row#getChar(java.lang.String)"
59       */
60      public Character getChar(final String key) {
61          try {
62              final String s = list.getValue(key);
63              if (StringUtils.isNotBlank(s)) {
64                  return s.charAt(0);
65              }
66              return null;
67          } catch (final NoSuchFieldException e) {
68              throw new RuntimeException(e);
69          }
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see "tools.gsf.facade.sql.Row#getDate(java.lang.String)"
76       */
77      public Date getDate(final String key) {
78          try {
79              final String s = list.getValue(key);
80              if (StringUtils.isNotBlank(s)) {
81                  return Util.parseJdbcDate(s);
82              }
83              return null;
84          } catch (final NoSuchFieldException e) {
85              throw new RuntimeException(e);
86          }
87      }
88  
89      /*
90       * (non-Javadoc)
91       * 
92       * @see "tools.gsf.facade.sql.Row#getLong(java.lang.String)"
93       */
94      public Long getLong(final String key) {
95          try {
96              final String s = list.getValue(key);
97              if (StringUtils.isNotBlank(s)) {
98                  return Long.parseLong(s);
99              }
100             return null;
101         } catch (final NoSuchFieldException e) {
102             throw new RuntimeException(e);
103         }
104     }
105 
106     /*
107      * (non-Javadoc)
108      * 
109      * @see
110      * tools.gsf.facade.sql.Row#getString(java.lang.String)
111      */
112     public String getString(final String key) {
113         try {
114             return list.getValue(key);
115         } catch (final NoSuchFieldException e) {
116             throw new RuntimeException(e);
117         }
118     }
119 
120     /**
121      * @param key string value for key
122      * @return boolean if a field exists for given key
123      */
124     public boolean isField(String key) {
125         for (int i = 0; i < list.numColumns(); i++) {
126             if (key.equalsIgnoreCase(list.getColumnName(i))) {
127                 return true;
128             }
129 
130         }
131         return false;
132     }
133 
134     @Override
135     public Integer getInt(String key) {
136         try {
137             final String s = list.getValue(key);
138             if (StringUtils.isNotBlank(s)) {
139                 return Integer.parseInt(s);
140             }
141             return null;
142         } catch (final NoSuchFieldException e) {
143             throw new RuntimeException(e);
144         }
145 
146     }
147 }