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.domain;
22  
23  import org.slf4j.*;
24  import org.synchronoss.cpo.*;
25  import org.synchronoss.cpo.helper.*;
26  import org.synchronoss.cpo.meta.CpoMetaDescriptor;
27  import org.synchronoss.cpo.meta.bean.CpoClassBean;
28  
29  import java.util.*;
30  
31  public abstract class CpoClass extends CpoClassBean implements Comparable<CpoClass>, MetaDFVisitable {
32  
33    private static final Logger logger = LoggerFactory.getLogger(CpoClass.class);
34    private Class<?> metaClass = null;
35    /**
36     * javaMap contains a Map of CpoAttribute Objects the key is the javaName of the attribute
37     */
38    private Map<String, CpoAttribute> javaMap = new HashMap<>();
39    /**
40     * dataMap contains a Map of CpoAttribute Objects the key is the dataName of the attribute
41     */
42    private Map<String, CpoAttribute> dataMap = new HashMap<>();
43    /**
44     * functionGroups is a hashMap that contains a hashMap of CpoFunctionGroup Lists that are used by this object to persist and
45     * retrieve it into a datasource.
46     */
47    private Map<String, CpoFunctionGroup> functionGroups = new HashMap<>();
48  
49    public CpoClass() {
50    }
51  
52    public Class<?> getMetaClass() {
53      return metaClass;
54    }
55  
56    public abstract void addDataNameToMap(String dataName, CpoAttribute cpoAttribute);
57  
58    public abstract void removeDataNameFromMap(String dataName);
59  
60    public abstract CpoAttribute getAttributeData(String dataName);
61  
62    protected Map<String, CpoAttribute> getDataMap() {
63      return dataMap;
64    }
65  
66    public void addAttribute(CpoAttribute cpoAttribute) {
67      if (cpoAttribute != null) {
68        logger.debug("Adding Attribute: " + cpoAttribute.getJavaName() + ":" + cpoAttribute.getDataName());
69        javaMap.put(cpoAttribute.getJavaName(), cpoAttribute);
70        addDataNameToMap(cpoAttribute.getDataName(), cpoAttribute);
71      }
72    }
73  
74    public void removeAttribute(CpoAttribute cpoAttribute) {
75      if (cpoAttribute != null) {
76        logger.debug("Removing Attribute: " + cpoAttribute.getJavaName() + ":" + cpoAttribute.getDataName());
77        javaMap.remove(cpoAttribute.getJavaName());
78        removeDataNameFromMap(cpoAttribute.getDataName());
79      }
80    }
81  
82    public Map<String, CpoFunctionGroup> getFunctionGroups() {
83      return this.functionGroups;
84    }
85  
86    public CpoFunctionGroup getFunctionGroup(String groupType, String groupName) throws CpoException {
87      String key = buildFunctionGroupKey(groupType, groupName);
88      CpoFunctionGroup group = functionGroups.get(key);
89      if (group == null) {
90        throw new CpoException("Function Group Not Found: " + groupType + ":" + groupName);
91      }
92      return group;
93    }
94  
95    public boolean existsFunctionGroup(String groupType, String groupName) throws CpoException {
96      String key = buildFunctionGroupKey(groupType, groupName);
97      CpoFunctionGroup group = functionGroups.get(key);
98      return (group != null);
99    }
100 
101   public CpoFunctionGroup addFunctionGroup(CpoFunctionGroup group) {
102     if (group == null) {
103       return null;
104     }
105 
106     String key = buildFunctionGroupKey(group.getType(), group.getName());
107     logger.debug("Adding function group: " + key);
108     return this.functionGroups.put(key, group);
109   }
110 
111   public void removeFunctionGroup(CpoFunctionGroup group) {
112     if (group != null) {
113       String key = buildFunctionGroupKey(group.getType(), group.getName());
114       functionGroups.remove(key);
115     }
116   }
117 
118   private String buildFunctionGroupKey(String groupType, String groupName) {
119     StringBuilder builder = new StringBuilder();
120     if (groupType != null) {
121       builder.append(groupType);
122     }
123     builder.append("@");
124     if (groupName != null) {
125       builder.append(groupName);
126     }
127     return builder.toString();
128   }
129 
130   @Override
131   public int compareTo(CpoClass anotherCpoClass) {
132     return getName().compareTo(anotherCpoClass.getName());
133   }
134 
135   @Override
136   public void acceptMetaDFVisitor(MetaVisitor visitor) {
137     visitor.visit(this);
138 
139     // visit attributes -- need these sorted
140     TreeMap<String, CpoAttribute> attributeMap = new TreeMap<>(javaMap);
141     for (CpoAttribute cpoAttribute : attributeMap.values()) {
142       visitor.visit(cpoAttribute);
143     }
144 
145     // visit function groups -- need these sorted
146     TreeMap<String, CpoFunctionGroup> functionGroupMap = new TreeMap<>(functionGroups);
147     for (CpoFunctionGroup cpoFunctionGroup : functionGroupMap.values()) {
148       visitor.visit(cpoFunctionGroup);
149 
150       // visit the functions
151       List<CpoFunction> functions = cpoFunctionGroup.getFunctions();
152       if (functions != null) {
153         for (CpoFunction cpoFunction : functions) {
154           visitor.visit(cpoFunction);
155 
156           // visit the arguments
157           List<CpoArgument> arguments = cpoFunction.getArguments();
158           if (arguments != null) {
159             for (CpoArgument cpoArgument : arguments) {
160               visitor.visit(cpoArgument);
161             }
162           }
163         }
164       }
165     }
166   }
167 
168   synchronized public void loadRunTimeInfo(CpoMetaDescriptor metaDescriptor) throws CpoException {
169     if (metaClass==null) {
170       Class<?> tmpMetaClass = null;
171 
172       try {
173         logger.debug("Loading runtimeinfo for "+getName());
174         tmpMetaClass = CpoClassLoader.forName(getName());
175       } catch (ClassNotFoundException cnfe) {
176         throw new CpoException("Class not found: " + getName() + ": " + ExceptionHelper.getLocalizedMessage(cnfe));
177       }
178 
179       for (CpoAttribute attribute : javaMap.values()) {
180         attribute.loadRunTimeInfo(metaDescriptor, tmpMetaClass);
181       }
182       logger.debug("Loaded runtimeinfo for "+getName());
183 
184       metaClass = tmpMetaClass;
185     }
186   }
187 
188   public CpoAttribute getAttributeJava(String javaName) {
189     if (javaName == null) {
190       return null;
191     }
192     return javaMap.get(javaName);
193   }
194 
195   @Override
196   public String toString() {
197     return this.getName();
198   }
199 
200   public String toStringFull() {
201     return super.toString();
202   }
203 
204   public void emptyMaps() {
205     javaMap.clear();
206     dataMap.clear();
207     functionGroups.clear();
208   }
209 }