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 com.fatwire.gst.foundation.facade.sql;
18  
19  import java.util.Date;
20  
21  import COM.FutureTense.Interfaces.IList;
22  
23  import com.fatwire.cs.core.db.Util;
24  
25  import org.apache.commons.lang.StringUtils;
26  
27  /**
28   * Implements a Row.
29   * 
30   * @author Dolf Dijkstra
31   * 
32   */
33  class SingleRow implements Row {
34  
35      private final IList list;
36  
37      /**
38       * @param list
39       */
40      public SingleRow(final IList list) {
41          super();
42          this.list = list;
43      }
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see com.fatwire.gst.foundation.facade.sql.Row#getBytes(java.lang.String)
49       */
50      public byte[] getBytes(final String key) {
51          try {
52              return (byte[]) list.getObject(key);
53          } catch (final NoSuchFieldException e) {
54              throw new RuntimeException(e);
55          }
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see com.fatwire.gst.foundation.facade.sql.Row#getChar(java.lang.String)
62       */
63      public Character getChar(final String key) {
64          try {
65              final String s = list.getValue(key);
66              if (StringUtils.isNotBlank(s)) {
67                  return s.charAt(0);
68              }
69              return null;
70          } catch (final NoSuchFieldException e) {
71              throw new RuntimeException(e);
72          }
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see com.fatwire.gst.foundation.facade.sql.Row#getDate(java.lang.String)
79       */
80      public Date getDate(final String key) {
81          try {
82              final String s = list.getValue(key);
83              if (StringUtils.isNotBlank(s)) {
84                  return Util.parseJdbcDate(s);
85              }
86              return null;
87          } catch (final NoSuchFieldException e) {
88              throw new RuntimeException(e);
89          }
90      }
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see com.fatwire.gst.foundation.facade.sql.Row#getLong(java.lang.String)
96       */
97      public Long getLong(final String key) {
98          try {
99              final String s = list.getValue(key);
100             if (StringUtils.isNotBlank(s)) {
101                 return Long.parseLong(s);
102             }
103             return null;
104         } catch (final NoSuchFieldException e) {
105             throw new RuntimeException(e);
106         }
107     }
108 
109     /*
110      * (non-Javadoc)
111      * 
112      * @see
113      * com.fatwire.gst.foundation.facade.sql.Row#getString(java.lang.String)
114      */
115     public String getString(final String key) {
116         try {
117             return list.getValue(key);
118         } catch (final NoSuchFieldException e) {
119             throw new RuntimeException(e);
120         }
121     }
122 
123     /**
124      * @param key
125      * @return
126      */
127     public boolean isField(String key) {
128         for (int i = 0; i < list.numColumns(); i++) {
129             if (key.equalsIgnoreCase(list.getColumnName(i))) {
130                 return true;
131             }
132 
133         }
134         return false;
135     }
136 
137     @Override
138     public Integer getInt(String key) {
139         try {
140             final String s = list.getValue(key);
141             if (StringUtils.isNotBlank(s)) {
142                 return Integer.parseInt(s);
143             }
144             return null;
145         } catch (final NoSuchFieldException e) {
146             throw new RuntimeException(e);
147         }
148 
149     }
150 }