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.meta;
22  
23  import org.slf4j.*;
24  import org.synchronoss.cpo.CpoException;
25  import org.synchronoss.cpo.core.cpoCoreMeta.*;
26  import org.synchronoss.cpo.meta.domain.*;
27  
28  import java.util.*;
29  
30  /**
31   *
32   * @author dberry
33   */
34  public abstract class AbstractCpoMetaAdapter implements CpoMetaAdapter {
35  
36    private static final Logger logger = LoggerFactory.getLogger(AbstractCpoMetaAdapter.class);
37    /**
38     * The map of classes in this metaAdapter
39     */
40    private Map<String, CpoClass> classMap = new HashMap<String, CpoClass>();
41    private CpoClass currentClass = null;
42  
43    protected AbstractCpoMetaAdapter() {
44      super();
45    }
46  
47    /**
48     * DOCUMENT ME!
49     *
50     * @param obj DOCUMENT ME!
51     * @return DOCUMENT ME!
52     * @throws CpoException DOCUMENT ME!
53     */
54    @Override
55    public <T> CpoClass getMetaClass(T obj) throws CpoException {
56      CpoClass cpoClass = null;
57      String className;
58      String requestedName;
59      Class<?> classObj;
60      Class<?> requestedClass;
61  
62      if (obj != null) {
63        requestedClass = obj.getClass();
64        classObj = requestedClass;
65        requestedName = requestedClass.getName();
66        className = requestedName;
67        cpoClass = classMap.get(className);
68  
69        while (cpoClass == null && classObj != null) {
70          classObj = classObj.getSuperclass();
71          className = classObj == null ? null : classObj.getName();
72          if (className != null) {
73            cpoClass = classMap.get(className);
74          }
75        }
76        if (cpoClass == null) {
77          throw new CpoException("No Metadata found for class:" + requestedName);
78        }
79      }
80  
81      return cpoClass;
82    }
83  
84    @Override
85    public List<CpoClass> getCpoClasses() {
86      List<CpoClass> result = new ArrayList<CpoClass>();
87      result.addAll(classMap.values());
88      return result;
89    }
90  
91    protected void loadCpoMetaDataDocument(CpoMetaDataDocument metaDataDoc, boolean caseSensitive) throws CpoException {
92      for (CtClass ctClass : metaDataDoc.getCpoMetaData().getCpoClassArray()) {
93  
94        CpoClass cpoClass = getCpoClass(ctClass.getName());
95        if (cpoClass == null) {
96          cpoClass = createCpoClass(caseSensitive);
97          cpoClass.setName(ctClass.getName());
98          cpoClass.setDescription(ctClass.getDescription());
99          loadCpoClass(cpoClass, ctClass);
100         addCpoClass(cpoClass);
101       } else {
102         loadCpoClass(cpoClass, ctClass);
103       }
104     }
105   }
106 
107   protected void loadCpoClass(CpoClass cpoClass, CtClass ctClass) throws CpoException {
108     logger.debug("Loading class: " + ctClass.getName());
109 
110     currentClass = cpoClass;
111 
112     for (CtAttribute ctAttribute : ctClass.getCpoAttributeArray()) {
113       CpoAttribute cpoAttribute = cpoClass.getAttributeJava(ctAttribute.getJavaName());
114       
115       if (cpoAttribute == null) {
116         cpoAttribute = createCpoAttribute();
117         loadCpoAttribute(cpoAttribute, ctAttribute);
118         cpoClass.addAttribute(cpoAttribute);
119       } else {
120         loadCpoAttribute(cpoAttribute, ctAttribute);
121       }
122     }
123 
124     for (CtFunctionGroup ctFunctionGroup : ctClass.getCpoFunctionGroupArray()) {
125       CpoFunctionGroup functionGroup = null;
126       
127       try {
128         functionGroup = cpoClass.getFunctionGroup(ctFunctionGroup.getType().toString(), ctFunctionGroup.getName());
129       } catch (Exception e){
130         // this a runtime exception that we can ignore during load time.
131         if (logger.isTraceEnabled()) {
132           logger.trace(e.getLocalizedMessage());
133         }
134       }
135       
136       if (functionGroup == null) {
137         functionGroup = createCpoFunctionGroup();
138         loadCpoFunctionGroup(functionGroup, ctFunctionGroup);
139         cpoClass.addFunctionGroup(functionGroup);
140       } else {
141         functionGroup.clearFunctions();
142         loadCpoFunctionGroup(functionGroup, ctFunctionGroup);
143       }
144       logger.debug("Added Function Group: " + functionGroup.getName());
145     }
146   }
147 
148   protected void loadCpoAttribute(CpoAttribute cpoAttribute, CtAttribute ctAttribute) {
149     cpoAttribute.setDataName(ctAttribute.getDataName());
150     cpoAttribute.setDataType(ctAttribute.getDataType());
151     cpoAttribute.setDescription(ctAttribute.getDescription());
152     cpoAttribute.setJavaName(ctAttribute.getJavaName());
153     cpoAttribute.setJavaType(ctAttribute.getJavaType());
154     cpoAttribute.setTransformClassName(ctAttribute.getTransformClass());
155   }
156 
157   protected void loadCpoFunctionGroup(CpoFunctionGroup cpoFunctionGroup, CtFunctionGroup ctFunctionGroup) {
158     cpoFunctionGroup.setDescription(ctFunctionGroup.getDescription());
159     if (ctFunctionGroup.isSetName()) {
160       cpoFunctionGroup.setName(ctFunctionGroup.getName());
161     }
162     cpoFunctionGroup.setType(ctFunctionGroup.getType().toString());
163 
164     for (CtFunction ctFunction : ctFunctionGroup.getCpoFunctionArray()) {
165       CpoFunction cpoFunction = createCpoFunction();
166       cpoFunctionGroup.addFunction(cpoFunction);
167       loadCpoFunction(cpoFunction, ctFunction);
168     }
169 
170   }
171 
172   protected void loadCpoFunction(CpoFunction cpoFunction, CtFunction ctFunction) {
173     cpoFunction.setName(ctFunction.getName());
174     cpoFunction.setExpression(ctFunction.getExpression());
175     cpoFunction.setDescription(ctFunction.getDescription());
176 
177     for (CtArgument ctArgument : ctFunction.getCpoArgumentArray()) {
178       CpoArgument cpoArgument = createCpoArgument();
179       cpoFunction.addArgument(cpoArgument);
180       loadCpoArgument(cpoArgument, ctArgument);
181     }
182   }
183 
184   protected void loadCpoArgument(CpoArgument cpoArgument, CtArgument ctArgument) {
185     cpoArgument.setAttributeName(ctArgument.getAttributeName());
186     cpoArgument.setDescription(ctArgument.getDescription());
187 
188     cpoArgument.setAttribute(currentClass.getAttributeJava(ctArgument.getAttributeName()));
189   }
190 
191   protected CpoClass createCpoClass(boolean caseSensitive) {
192     if (caseSensitive)
193       return new CpoClassCaseSensitive();
194     else 
195       return new CpoClassCaseInsensitive();
196   }
197 
198   protected CpoAttribute createCpoAttribute() {
199     return new CpoAttribute();
200   }
201 
202   protected CpoFunctionGroup createCpoFunctionGroup() {
203     return new CpoFunctionGroup();
204   }
205 
206   protected CpoFunction createCpoFunction() {
207     return new CpoFunction();
208   }
209 
210   protected CpoArgument createCpoArgument() {
211     return new CpoArgument();
212   }
213 
214   protected CpoClass getCpoClass(String name) {
215     return classMap.get(name);
216   }
217 
218   protected void addCpoClass(CpoClass metaClass) {
219     CpoClass oldMetaClass = classMap.put(metaClass.getName(), metaClass);
220     if (oldMetaClass != null)
221       logger.debug("Overwrote class: " + metaClass.getName());
222     else 
223       logger.debug("Added class: " + metaClass.getName());
224 
225   }
226 
227   protected void removeCpoClass(CpoClass metaClass) {
228     if (metaClass != null) {
229       logger.debug("Removing class: " + metaClass.getName());
230       metaClass.emptyMaps();
231       classMap.remove(metaClass.getName());
232     }
233   }
234 }