1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33 public abstract class AbstractCpoMetaAdapter implements CpoMetaAdapter {
34
35 private static final Logger logger = LoggerFactory.getLogger(AbstractCpoMetaAdapter.class);
36
37
38
39 private Map<String, CpoClass> classMap = new HashMap<>();
40 private CpoClass currentClass = null;
41
42 protected AbstractCpoMetaAdapter() {
43 super();
44 }
45
46
47
48
49
50
51
52
53 @Override
54 public <T> CpoClass getMetaClass(T obj) throws CpoException {
55 CpoClass cpoClass = null;
56 String className;
57 String requestedName;
58 List<Class<?>> classList = new ArrayList<>();
59 Class<?> requestedClass;
60
61 if (obj != null) {
62 requestedClass = obj.getClass();
63 classList.add(requestedClass);
64 requestedName = requestedClass.getName();
65 className = requestedName;
66 cpoClass = classMap.get(className);
67
68 while (cpoClass == null && !classList.isEmpty()) {
69 classList = getSuperClasses(classList);
70 for (Class<?> clazz : classList) {
71 className = clazz.getName();
72 cpoClass = classMap.get(className);
73 if (cpoClass != null)
74 break;
75 }
76 }
77 if (cpoClass == null) {
78 throw new CpoException("No Metadata found for class:" + requestedName);
79 }
80 }
81
82 return cpoClass;
83 }
84
85 private List<Class<?>> getSuperClasses(List<Class<?>> classList) {
86 List<Class<?>> superClassList = new ArrayList<>();
87
88 for (Class<?> clazz : classList) {
89 Class<?> superClass = clazz.getSuperclass();
90 if (superClass != null) {
91 superClassList.add(superClass);
92 }
93 superClassList.addAll(Arrays.asList(clazz.getInterfaces()));
94 }
95 return superClassList;
96 }
97
98
99 @Override
100 public List<CpoClass> getCpoClasses() {
101 List<CpoClass> result = new ArrayList<>();
102 result.addAll(classMap.values());
103 return result;
104 }
105
106 @Override
107 public String getDataTypeName(CpoAttribute attribute) {
108 String clazzName = getDataTypeJavaClass(attribute).getName();
109 byte[] b = new byte[0];
110 char[] c = new char[0];
111
112 if (b.getClass().getName().equals(clazzName)) {
113 clazzName = "byte[]";
114 } else if (c.getClass().getName().equals(clazzName)) {
115 clazzName = "char[]";
116 }
117 return clazzName;
118 }
119
120 @Override
121 public Class<?> getDataTypeJavaClass(CpoAttribute attribute) {
122 Class<?> clazz = String.class;
123 DataTypeMapEntry<?> dataTypeMapEntry = getDataTypeMapper().getDataTypeMapEntry(attribute.getDataType());
124
125 if (attribute.getTransformInMethod() != null) {
126 clazz = attribute.getTransformInMethod().getReturnType();
127 } else if (dataTypeMapEntry != null) {
128 clazz = dataTypeMapEntry.getJavaClass();
129 }
130
131 return clazz;
132 }
133
134 @Override
135 public int getDataTypeInt(String dataTypeName) throws CpoException {
136 return getDataTypeMapper().getDataTypeInt(dataTypeName);
137 }
138
139 @Override
140 public DataTypeMapEntry<?> getDataTypeMapEntry(int dataTypeInt) throws CpoException {
141 return getDataTypeMapper().getDataTypeMapEntry(dataTypeInt);
142 }
143
144 @Override
145 public List<String> getAllowableDataTypes() throws CpoException {
146 return getDataTypeMapper().getDataTypeNames();
147 }
148
149 protected abstract DataTypeMapper getDataTypeMapper();
150
151 protected void loadCpoMetaDataDocument(CpoMetaDataDocument metaDataDoc, boolean caseSensitive) throws CpoException {
152 for (CtClass ctClass : metaDataDoc.getCpoMetaData().getCpoClassArray()) {
153
154 CpoClass cpoClass = getCpoClass(ctClass.getName());
155 if (cpoClass == null) {
156 cpoClass = createCpoClass(caseSensitive);
157 cpoClass.setName(ctClass.getName());
158 cpoClass.setDescription(ctClass.getDescription());
159 loadCpoClass(cpoClass, ctClass);
160 addCpoClass(cpoClass);
161 } else {
162 loadCpoClass(cpoClass, ctClass);
163 }
164 }
165 }
166
167 protected void loadCpoClass(CpoClass cpoClass, CtClass ctClass) throws CpoException {
168 logger.debug("Loading class: " + ctClass.getName());
169
170 currentClass = cpoClass;
171
172 for (CtAttribute ctAttribute : ctClass.getCpoAttributeArray()) {
173 CpoAttribute cpoAttribute = cpoClass.getAttributeJava(ctAttribute.getJavaName());
174
175 if (cpoAttribute == null) {
176 cpoAttribute = createCpoAttribute();
177 loadCpoAttribute(cpoAttribute, ctAttribute);
178 cpoClass.addAttribute(cpoAttribute);
179 } else {
180 loadCpoAttribute(cpoAttribute, ctAttribute);
181 }
182 }
183
184 for (CtFunctionGroup ctFunctionGroup : ctClass.getCpoFunctionGroupArray()) {
185 CpoFunctionGroup functionGroup = null;
186
187 try {
188 functionGroup = cpoClass.getFunctionGroup(ctFunctionGroup.getType().toString(), ctFunctionGroup.getName());
189 } catch (Exception e) {
190
191 if (logger.isTraceEnabled()) {
192 logger.trace(e.getLocalizedMessage());
193 }
194 }
195
196 if (functionGroup == null) {
197 functionGroup = createCpoFunctionGroup();
198 loadCpoFunctionGroup(functionGroup, ctFunctionGroup);
199 cpoClass.addFunctionGroup(functionGroup);
200 } else {
201 functionGroup.clearFunctions();
202 loadCpoFunctionGroup(functionGroup, ctFunctionGroup);
203 }
204 logger.debug("Added Function Group: " + functionGroup.getName());
205 }
206 }
207
208 protected void loadCpoAttribute(CpoAttribute cpoAttribute, CtAttribute ctAttribute) {
209 cpoAttribute.setDataName(ctAttribute.getDataName());
210 cpoAttribute.setDataType(ctAttribute.getDataType());
211 cpoAttribute.setDescription(ctAttribute.getDescription());
212 cpoAttribute.setJavaName(ctAttribute.getJavaName());
213 cpoAttribute.setJavaType(ctAttribute.getJavaType());
214 cpoAttribute.setTransformClassName(ctAttribute.getTransformClass());
215 }
216
217 protected void loadCpoFunctionGroup(CpoFunctionGroup cpoFunctionGroup, CtFunctionGroup ctFunctionGroup) {
218 cpoFunctionGroup.setDescription(ctFunctionGroup.getDescription());
219 if (ctFunctionGroup.isSetName()) {
220 cpoFunctionGroup.setName(ctFunctionGroup.getName());
221 }
222 cpoFunctionGroup.setType(ctFunctionGroup.getType().toString());
223
224 for (CtFunction ctFunction : ctFunctionGroup.getCpoFunctionArray()) {
225 CpoFunction cpoFunction = createCpoFunction();
226 cpoFunctionGroup.addFunction(cpoFunction);
227 loadCpoFunction(cpoFunction, ctFunction);
228 }
229 }
230
231 protected void loadCpoFunction(CpoFunction cpoFunction, CtFunction ctFunction) {
232 cpoFunction.setName(ctFunction.getName());
233 cpoFunction.setExpression(ctFunction.getExpression());
234 cpoFunction.setDescription(ctFunction.getDescription());
235
236 for (CtArgument ctArgument : ctFunction.getCpoArgumentArray()) {
237 CpoArgument cpoArgument = createCpoArgument();
238 cpoFunction.addArgument(cpoArgument);
239 loadCpoArgument(cpoArgument, ctArgument);
240 }
241 }
242
243 protected void loadCpoArgument(CpoArgument cpoArgument, CtArgument ctArgument) {
244 cpoArgument.setAttributeName(ctArgument.getAttributeName());
245 cpoArgument.setDescription(ctArgument.getDescription());
246
247 cpoArgument.setAttribute(currentClass.getAttributeJava(ctArgument.getAttributeName()));
248 }
249
250 protected CpoClass createCpoClass(boolean caseSensitive) {
251 if (caseSensitive) {
252 return new CpoClassCaseSensitive();
253 } else {
254 return new CpoClassCaseInsensitive();
255 }
256 }
257
258 protected CpoAttribute createCpoAttribute() {
259 return new CpoAttribute();
260 }
261
262 protected CpoFunctionGroup createCpoFunctionGroup() {
263 return new CpoFunctionGroup();
264 }
265
266 protected CpoFunction createCpoFunction() {
267 return new CpoFunction();
268 }
269
270 protected CpoArgument createCpoArgument() {
271 return new CpoArgument();
272 }
273
274 protected CpoClass getCpoClass(String name) {
275 return classMap.get(name);
276 }
277
278 protected void addCpoClass(CpoClass metaClass) {
279 CpoClass oldMetaClass = classMap.put(metaClass.getName(), metaClass);
280 if (oldMetaClass != null) {
281 logger.debug("Overwrote class: " + metaClass.getName());
282 } else {
283 logger.debug("Added class: " + metaClass.getName());
284 }
285 }
286
287 protected void removeCpoClass(CpoClass metaClass) {
288 if (metaClass != null) {
289 logger.debug("Removing class: " + metaClass.getName());
290 metaClass.emptyMaps();
291 classMap.remove(metaClass.getName());
292 }
293 }
294
295 protected void removeAllCpoClass() {
296 classMap.clear();
297 }
298 }