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.exporter;
22  
23  import org.synchronoss.cpo.MetaVisitor;
24  import org.synchronoss.cpo.core.cpoCoreMeta.*;
25  import org.synchronoss.cpo.meta.CpoMetaDescriptor;
26  import org.synchronoss.cpo.meta.domain.*;
27  
28  /**
29   * XmlObject exporter for meta objects
30   *
31   * @author Michael Bellomo
32   * @since 4/17/12
33   */
34  public class CoreMetaXmlObjectExporter implements MetaXmlObjectExporter, MetaVisitor {
35  
36    protected CpoMetaDataDocument cpoMetaDataDocument = null;
37    protected CtClass currentCtClass;
38    protected CtFunctionGroup currentCtFunctionGroup;
39    protected CtFunction currentCtFunction;
40  
41    public CoreMetaXmlObjectExporter(CpoMetaDescriptor metaDescriptor) {
42      cpoMetaDataDocument = CpoMetaDataDocument.Factory.newInstance();
43      CtCpoMetaData ctCpoMetaData = cpoMetaDataDocument.addNewCpoMetaData();
44      ctCpoMetaData.setMetaDescriptor(metaDescriptor.getClass().getName());
45      ctCpoMetaData.setDefaultPackageName(metaDescriptor.getDefaultPackageName());
46    }
47  
48    @Override
49    public CpoMetaDataDocument getCpoMetaDataDocument() {
50      return cpoMetaDataDocument;
51    }
52  
53    @Override
54    public void visit(CpoClass cpoClass) {
55      CtClass ctClass = cpoMetaDataDocument.getCpoMetaData().addNewCpoClass();
56      ctClass.setName(cpoClass.getName());
57  
58      if (cpoClass.getDescription() != null && cpoClass.getDescription().length() > 0) {
59        ctClass.setDescription(cpoClass.getDescription());
60      }
61  
62      // save the reference
63      currentCtClass = ctClass;
64    }
65  
66    @Override
67    public void visit(CpoAttribute cpoAttribute) {
68      if (currentCtClass != null) {
69        CtAttribute ctAttribute = currentCtClass.addNewCpoAttribute();
70  
71        ctAttribute.setJavaName(cpoAttribute.getJavaName());
72        ctAttribute.setJavaType(cpoAttribute.getJavaType());
73        ctAttribute.setDataName(cpoAttribute.getDataName());
74        ctAttribute.setDataType(cpoAttribute.getDataType());
75  
76        if (cpoAttribute.getTransformClassName() != null && cpoAttribute.getTransformClassName().length() > 0) {
77          ctAttribute.setTransformClass(cpoAttribute.getTransformClassName());
78        }
79  
80        if (cpoAttribute.getDescription() != null && cpoAttribute.getDescription().length() > 0) {
81          ctAttribute.setDescription(cpoAttribute.getDescription());
82        }
83      }
84    }
85  
86    @Override
87    public void visit(CpoFunctionGroup cpoFunctionGroup) {
88      if (currentCtClass != null) {
89        CtFunctionGroup ctFunctionGroup = currentCtClass.addNewCpoFunctionGroup();
90  
91        if (cpoFunctionGroup.getName() != null && cpoFunctionGroup.getName().length() > 0) {
92          ctFunctionGroup.setName(cpoFunctionGroup.getName());
93        }
94  
95        ctFunctionGroup.setType(StFunctionGroupType.Enum.forString(cpoFunctionGroup.getType()));
96  
97        if (cpoFunctionGroup.getDescription() != null && cpoFunctionGroup.getDescription().length() > 0) {
98          ctFunctionGroup.setDescription(cpoFunctionGroup.getDescription());
99        }
100 
101       // save the reference
102       currentCtFunctionGroup = ctFunctionGroup;
103     }
104   }
105 
106   @Override
107   public void visit(CpoFunction cpoFunction) {
108     if (currentCtFunctionGroup != null) {
109       CtFunction ctFunction = currentCtFunctionGroup.addNewCpoFunction();
110 
111       ctFunction.setName(cpoFunction.getName());
112       ctFunction.setExpression(cpoFunction.getExpression());
113 
114       if (cpoFunction.getDescription() != null && cpoFunction.getDescription().length() > 0) {
115         ctFunction.setDescription(cpoFunction.getDescription());
116       }
117 
118       // save the reference
119       currentCtFunction = ctFunction;
120     }
121   }
122 
123   @Override
124   public void visit(CpoArgument cpoArgument) {
125     if (currentCtFunction != null) {
126       CtArgument ctArgument = currentCtFunction.addNewCpoArgument();
127 
128       ctArgument.setAttributeName(cpoArgument.getAttribute().getJavaName());
129 
130       if (cpoArgument.getDescription() != null && cpoArgument.getDescription().length() > 0) {
131         ctArgument.setDescription(cpoArgument.getDescription());
132       }
133     }
134   }
135 }