View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc.exporter;
22  
23  import org.synchronoss.cpo.core.cpoCoreMeta.CtArgument;
24  import org.synchronoss.cpo.core.cpoCoreMeta.CtAttribute;
25  import org.synchronoss.cpo.exporter.CoreMetaXmlObjectExporter;
26  import org.synchronoss.cpo.exporter.MetaXmlObjectExporter;
27  import org.synchronoss.cpo.jdbc.JdbcCpoArgument;
28  import org.synchronoss.cpo.jdbc.JdbcCpoAttribute;
29  import org.synchronoss.cpo.jdbc.cpoJdbcMeta.CtJdbcArgument;
30  import org.synchronoss.cpo.jdbc.cpoJdbcMeta.CtJdbcAttribute;
31  import org.synchronoss.cpo.meta.CpoMetaDescriptor;
32  import org.synchronoss.cpo.meta.domain.CpoArgument;
33  import org.synchronoss.cpo.meta.domain.CpoAttribute;
34  
35  /**
36   * XmlObject exporter for jdbc meta objects
37   *
38   * @author Michael Bellomo
39   * @since 4/18/12
40   */
41  public class JdbcMetaXmlObjectExporter extends CoreMetaXmlObjectExporter implements MetaXmlObjectExporter {
42  
43    public JdbcMetaXmlObjectExporter(CpoMetaDescriptor metaDescriptor) {
44      super(metaDescriptor);
45    }
46  
47    @Override
48    public void visit(CpoAttribute cpoAttribute) {
49  
50      // shouldn't happen, but if what we got wasn't a JdbcAttribute...
51      if (!(cpoAttribute instanceof JdbcCpoAttribute)) {
52        super.visit(cpoAttribute);
53        return;
54      }
55  
56      JdbcCpoAttribute jdbcAttribute = (JdbcCpoAttribute) cpoAttribute;
57  
58      if (currentCtClass != null) {
59  
60        // CtClass.addNewCpoAttribute() can't be used here because it returns a CtAttribute, not a CtJdbcAttribute
61        CtJdbcAttribute ctJdbcAttribute = CtJdbcAttribute.Factory.newInstance();
62  
63        ctJdbcAttribute.setJavaName(jdbcAttribute.getJavaName());
64        ctJdbcAttribute.setJavaType(jdbcAttribute.getJavaType());
65        ctJdbcAttribute.setDataName(jdbcAttribute.getDataName());
66        ctJdbcAttribute.setDataType(jdbcAttribute.getDataType());
67  
68        if (jdbcAttribute.getTransformClassName() != null && jdbcAttribute.getTransformClassName().length() > 0) {
69          ctJdbcAttribute.setTransformClass(jdbcAttribute.getTransformClassName());
70        }
71  
72        if (jdbcAttribute.getDescription() != null && jdbcAttribute.getDescription().length() > 0) {
73          ctJdbcAttribute.setDescription(jdbcAttribute.getDescription());
74        }
75  
76        if (jdbcAttribute.getDbTable() != null && jdbcAttribute.getDbTable().length() > 0) {
77          ctJdbcAttribute.setDbTable(jdbcAttribute.getDbTable());
78        }
79  
80        if (jdbcAttribute.getDbColumn() != null && jdbcAttribute.getDbColumn().length() > 0) {
81          ctJdbcAttribute.setDbColumn(jdbcAttribute.getDbColumn());
82        }
83  
84        // add it to the class
85        CtAttribute ctAttribute = currentCtClass.addNewCpoAttribute();
86        ctAttribute.set(ctJdbcAttribute);
87      }
88    }
89  
90    @Override
91    public void visit(CpoArgument cpoArgument) {
92  
93      // shouldn't happen, but if what we got wasn't a JdbcArgument...
94      if (!(cpoArgument instanceof JdbcCpoArgument)) {
95        super.visit(cpoArgument);
96        return;
97      }
98  
99      JdbcCpoArgument jdbcArgument = (JdbcCpoArgument) cpoArgument;
100 
101     if (currentCtFunction != null) {
102 
103       // CtFunction.addNewCpoArgument() can't be used here because it returns a CtArgument, not a CtJdbcArgument
104       CtJdbcArgument ctJdbcArgument = CtJdbcArgument.Factory.newInstance();
105 
106       ctJdbcArgument.setAttributeName(jdbcArgument.getAttributeName());
107 
108       if (jdbcArgument.getDescription() != null && jdbcArgument.getDescription().length() > 0) {
109         ctJdbcArgument.setDescription(jdbcArgument.getDescription());
110       }
111 
112       if (jdbcArgument.isInParameter() && jdbcArgument.isOutParameter()) {
113         ctJdbcArgument.setScope(CtJdbcArgument.Scope.BOTH);
114       } else if (jdbcArgument.isInParameter()) {
115         ctJdbcArgument.setScope(CtJdbcArgument.Scope.IN);
116       } else if (jdbcArgument.isOutParameter()) {
117         ctJdbcArgument.setScope(CtJdbcArgument.Scope.OUT);
118       }
119 
120       if (jdbcArgument.getTypeInfo() != null && !jdbcArgument.getTypeInfo().isEmpty()) {
121         ctJdbcArgument.setTypeInfo(jdbcArgument.getTypeInfo());
122       }
123 
124       CtArgument ctArgument = currentCtFunction.addNewCpoArgument();
125       ctArgument.set(ctJdbcArgument);
126     }
127   }
128 }