View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2015 LegSem.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the GNU Lesser Public License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7    * 
8    * Contributors:
9    *     LegSem - initial API and implementation
10   ******************************************************************************/
11  package com.legstar.coxb.impl;
12  
13  import java.lang.reflect.InvocationTargetException;
14  import java.lang.reflect.Method;
15  import java.sql.Timestamp;
16  import java.util.Date;
17  
18  import com.legstar.coxb.CobolElement;
19  import com.legstar.coxb.ICobolComplexBinding;
20  import com.legstar.coxb.common.CBinding;
21  import com.legstar.coxb.host.HostException;
22  
23  /**
24   * A generic binding for alphanumeric bindings. All alphanumeric values are
25   * stored in a String.
26   */
27  public abstract class AbstractAlphaNumericBinding extends CBinding {
28  
29      /** The current value for this element. */
30      private String mValue = null;
31  
32      /**
33       * Constructor for a cobol element to java binding.
34       * 
35       * @param bindingName the identifier for this binding
36       * @param jaxbName the name of the bound java property
37       * @param jaxbType the type of the bound java property
38       * @param cobolAnnotations the cobol annotations for this element
39       * @param parentBinding a reference to the parent binding
40       */
41      public AbstractAlphaNumericBinding(final String bindingName,
42              final String jaxbName, final Class < ? > jaxbType,
43              final CobolElement cobolAnnotations,
44              final ICobolComplexBinding parentBinding) {
45          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
46      }
47  
48      /** {@inheritDoc} */
49      public String getStringValue() throws HostException {
50          return mValue;
51      }
52  
53      /** {@inheritDoc} */
54      public void setStringValue(final String value) throws HostException {
55          mValue = value;
56      }
57  
58      /** {@inheritDoc} */
59      public Object getObjectValue(final Class < ? > type) throws HostException {
60          if (type.equals(String.class)) {
61              return mValue;
62          } else if (type.equals(Date.class)) {
63              return new Date(Timestamp.valueOf(mValue).getTime());
64          } else if (type.isEnum()) {
65              /*
66               * If result is request to be an enum, return instance corresponding
67               * to this value
68               */
69              try {
70                  Method fromValue = getFromValueMethod(type);
71                  return fromValue.invoke(null, mValue.trim());
72              } catch (SecurityException e) {
73                  throw new HostException(e);
74              } catch (IllegalArgumentException e) {
75                  throw new HostException(e);
76              } catch (IllegalAccessException e) {
77                  throw new HostException(e);
78              } catch (InvocationTargetException e) {
79                  throw new HostException(e);
80              }
81  
82          } else {
83              throw new HostException("Attempt to get binding "
84                      + getBindingName() + " as an incompatible type " + type);
85          }
86      }
87  
88      /**
89       * Enum classes can be JAXB Enum wrappers with a "fromValue" method or plain
90       * POJO Enums.
91       * <p/>
92       * Here we lookup a method that takes a String representation of the Enum
93       * and returns the Enum item.
94       * 
95       * @param type an Enum type
96       * @return the method that turns a String into an Enum item
97       * @throws HostException if method cannot be found
98       */
99      protected Method getFromValueMethod(Class < ? > type) throws HostException {
100         try {
101             Method fromValueMethod = null;
102             try {
103                 fromValueMethod = type.getMethod("fromValue", String.class);
104             } catch (NoSuchMethodException e) {
105                 fromValueMethod = type.getMethod("valueOf", String.class);
106             }
107             return fromValueMethod;
108         } catch (SecurityException e) {
109             throw new HostException(e);
110         } catch (NoSuchMethodException e) {
111             throw new HostException(e);
112         }
113     }
114 
115     /** {@inheritDoc} */
116     public void setObjectValue(final Object value) throws HostException {
117         if (value == null) {
118             mValue = null;
119             return;
120         }
121         if (value instanceof String) {
122             mValue = (String) value;
123         } else if (value instanceof Date) {
124             mValue = new Timestamp(((Date) value).getTime()).toString();
125         } else if (value instanceof Enum < ? >) {
126             try {
127                 Method valueMethod = getValueMethod(value.getClass());
128                 mValue = (String) valueMethod.invoke(value);
129             } catch (SecurityException e) {
130                 throw new HostException(e);
131             } catch (IllegalArgumentException e) {
132                 throw new HostException(e);
133             } catch (IllegalAccessException e) {
134                 throw new HostException(e);
135             } catch (InvocationTargetException e) {
136                 throw new HostException(e);
137             }
138         } else {
139             throw new HostException("Attempt to set binding "
140                     + getBindingName() + " from an incompatible value " + value);
141         }
142     }
143 
144     /**
145      * Enum classes can be JAXB Enum wrappers with a "value" method or plain
146      * POJO Enums.
147      * <p/>
148      * Here we lookup a method that returns a String representation of the Enum
149      * value by first trying the JAXB type, then the POJO type.
150      * 
151      * @param type the Enum type
152      * @return the method that returns a String representation of that enum item
153      * @throws HostException if method cannot be found
154      */
155     protected Method getValueMethod(Class < ? > type) throws HostException {
156         try {
157             Method valueMethod = null;
158             try {
159                 valueMethod = type.getMethod("value");
160             } catch (NoSuchMethodException e) {
161                 valueMethod = type.getMethod("toString");
162             }
163             return valueMethod;
164         } catch (SecurityException e) {
165             throw new HostException(e);
166         } catch (NoSuchMethodException e) {
167             throw new HostException(e);
168         }
169     }
170 
171     /** {@inheritDoc} */
172     public boolean isSet() {
173         return (mValue != null);
174     }
175 }