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.reflect;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import com.legstar.coxb.CobolComplexType;
17  import com.legstar.coxb.CobolElement;
18  import com.legstar.coxb.common.CArrayComplexBinding;
19  import com.legstar.coxb.host.HostException;
20  import com.legstar.coxb.ICobolComplexBinding;
21  
22  /**
23   * Cobol/JAXB implementation of an array of complex (record) elements.
24   *
25   * @author Fady Moussallam
26   * 
27   */
28  public class CArrayComplexReflectBinding extends CArrayComplexBinding {
29  
30      /** This is a reference to a JAXB object factory. */
31      private Object mJaxbObjectFactory;
32  
33      /** Java object to which this cobol complex array element is bound. */
34      private List < Object > mJaxbObject;
35  
36      /**
37       * Creates a binding between a Cobol array of complex elements and a
38       * java List.
39       * 
40       * @param bindingName the identifier for this binding
41       * @param jaxbName the name of the bound java property
42       * @param jaxbType the type of the bound java property
43       * @param cobolAnnotations the cobol annotations for this element
44       * @param parentBinding a reference to the parent binding if any
45       * @param complexItemBinding a binding element for array items
46       * @param objectFactory the JAXB object factory
47       */
48      public CArrayComplexReflectBinding(
49              final String bindingName,
50              final String jaxbName,
51              final Class < ? > jaxbType,
52              final CobolElement cobolAnnotations,
53              final ICobolComplexBinding parentBinding,
54              final ICobolComplexBinding complexItemBinding,
55              final Object objectFactory) {
56  
57          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding,
58                  complexItemBinding);
59          mJaxbObjectFactory = objectFactory;
60          /* Assume we are bound to a JAXB object */
61          setValueObjectClassName(jaxbType.getName());
62          setValueObjectsFactoryClassName(objectFactory.getClass().getName());
63          /* Jaxb class might hold an annotation which gives more details
64           * on how to bind*/
65          CobolComplexType cobolComplexType =
66              (CobolComplexType) jaxbType.getAnnotation(CobolComplexType.class);
67          if (cobolComplexType != null
68                  && cobolComplexType.javaClassName() != null
69                  && cobolComplexType.javaClassName().length() > 0) {
70              setValueObjectClassName(cobolComplexType.javaClassName());
71              /* TODO allow more options, such as factory name, to be 
72               * passed as annotations */
73              setValueObjectsFactoryClassName(null);
74          }
75      }
76  
77      /** {@inheritDoc}
78       * @deprecated */
79      public void createJaxbObject() throws HostException {
80          createValueObject();
81      }
82  
83      /** {@inheritDoc} */
84      public void createValueObject() throws HostException {
85          mJaxbObject = new ArrayList < Object >();
86      }
87  
88      /** {@inheritDoc} */
89      public void setItemValue(
90              final int index) throws HostException {
91          /* Make sure there is an associated JAXB object*/
92          if (mJaxbObject == null) {
93              createJaxbObject();
94          }
95          /* The Jaxb list might have less items than expected by the binding.
96           * In this case, we fill the binding with empty items. */
97          if (index < mJaxbObject.size()) {
98              getComplexItemBinding().setObjectValue(mJaxbObject.get(index));
99          } else {
100             getComplexItemBinding().setObjectValue(null);
101         }
102     }
103 
104     /** {@inheritDoc}
105      * @deprecated */
106     public void addJaxbPropertyValue(
107             final int index) throws HostException {
108         addPropertyValue(index);
109     }
110 
111     /** {@inheritDoc} */
112     public void addPropertyValue(
113             final int index) throws HostException {
114         /* Make sure there is an associated JAXB object*/
115         if (mJaxbObject == null) {
116             throw new HostException(
117                     "Binded object not initialized for " + getBindingName());
118         }
119         mJaxbObject.add(getComplexItemBinding().getObjectValue(getJaxbType()));
120     }
121 
122     /**
123      * @return Returns the JAXB Object Factory.
124      */
125     public Object getObjectFactory() {
126         return mJaxbObjectFactory;
127     }
128 
129     /**
130      * @return the List of items
131      */
132     public List < ? > getObjectList() {
133         return mJaxbObject;
134     }
135 
136     /**
137      * @param list the items List to set
138      */
139     @SuppressWarnings("unchecked")
140     public void setObjectList(final List < ? > list) {
141         mJaxbObject = ( List < Object >) list;
142     }
143 
144     /** {@inheritDoc} */
145     public Object getObjectValue(
146             final Class < ? > type) throws HostException {
147         if (type.equals(getJaxbType())) {
148             return mJaxbObject;
149         } else {
150             throw new HostException("Attempt to get binding " + getBindingName()
151                     + " as an incompatible type " + type);
152         }
153     }
154 
155     /** {@inheritDoc} */
156     @SuppressWarnings("unchecked")
157     public void setObjectValue(final Object value) throws HostException {
158         if (value == null) {
159             mJaxbObject = null;
160             return;
161         }
162         if (value instanceof List) {
163             if (((List < ? >) value).size() == 0) {
164                 mJaxbObject = new ArrayList < Object >();
165                 return;
166             }
167             /* We assume all items will have the same type as the first one.
168              * The unchecked cast might break at runtime. */
169             Object item = ((List < ? >) value).get(0);
170             if (item.getClass().equals(getJaxbType())) {
171                 mJaxbObject = (List < Object >) value;
172                 return;
173             }
174         }
175         throw new HostException("Attempt to set binding " + getBindingName()
176                 + " from an incompatible value " + value);
177     }
178 
179     /** {@inheritDoc} */
180     public boolean isSet() {
181         return (mJaxbObject != null);
182     }
183 }