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