View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  /***
18   * PrintSupport.java
19   *
20   */
21  
22  package org.apache.jdo.impl.model.jdo.util;
23  
24  import java.util.*;
25  
26  import org.apache.jdo.model.jdo.JDOArray;
27  import org.apache.jdo.model.jdo.JDOClass;
28  import org.apache.jdo.model.jdo.JDOCollection;
29  import org.apache.jdo.model.jdo.JDOExtension;
30  import org.apache.jdo.model.jdo.JDOField;
31  import org.apache.jdo.model.jdo.JDOIdentityType;
32  import org.apache.jdo.model.jdo.JDOMap;
33  import org.apache.jdo.model.jdo.JDOModel;
34  import org.apache.jdo.model.jdo.JDOReference;
35  import org.apache.jdo.model.jdo.JDORelationship;
36  import org.apache.jdo.model.jdo.NullValueTreatment;
37  import org.apache.jdo.model.jdo.PersistenceModifier;
38  
39  public class PrintSupport 
40  {
41      public static void printJDOModel(JDOModel jdoModel) 
42      {
43          JDOClass[] jdoClasses = jdoModel.getDeclaredClasses();
44          for (int i = 0; i < jdoClasses.length; i++) {
45              printJDOClass(0, jdoClasses[i]);
46          }
47      }
48      
49      public static void printJDOClass(JDOClass jdoClass)
50      {
51          printJDOClass(0, jdoClass);
52      }
53      
54      public static void printJDOFields(JDOField[] jdoFields)
55      {
56          printJDOFields(0, jdoFields);
57      }
58  
59      public static void printJDOField(JDOField jdoField) 
60      {
61          printJDOField(0, jdoField);
62      }
63      
64      public static void printJDORelationship(JDORelationship jdoRelationship) 
65      {
66          printJDORelationship(0, jdoRelationship);
67      }
68  
69      public static void printJDOExtensions(JDOExtension[] jdoExtensions)
70      {
71          printJDOExtensions(0, jdoExtensions);
72      }
73  
74      public static void printJDOExtension(JDOExtension jdoExtension) 
75      {
76          printJDOExtension(0, jdoExtension);
77      }
78  
79      // ----- methods taking indent level -----
80  
81      private static void printJDOClass(int indent, JDOClass jdoClass) 
82      {
83          if (jdoClass == null)
84              return;
85  
86          println(indent, "--> JDOClass "); //NOI18N
87          println(indent+1, "name                       = " + jdoClass.getName()); //NOI18N
88          println(indent+1, "shortName                  = " + jdoClass.getShortName()); //NOI18N
89          println(indent+1, "packagePrefix              = " + jdoClass.getPackagePrefix()); //NOI18N
90          println(indent+1, "identityType               = " + JDOIdentityType.toString(jdoClass.getIdentityType())); //NOI18N
91          println(indent+1, "objectIdClass              = " + jdoClass.getObjectIdClass()); //NOI18N
92          println(indent+1, "declaredObjectIdClassName  = " + jdoClass.getDeclaredObjectIdClassName()); //NOI18N
93          println(indent+1, "requiresExtent             = " + jdoClass.requiresExtent()); //NOI18N
94          println(indent+1, "pcSuperclassName           = " + jdoClass.getPersistenceCapableSuperclassName()); //NOI18N
95          println(indent+1, "pcSuperclass               = " + jdoClass.getPersistenceCapableSuperclass()); //NOI18N
96          println(indent+1, "pcRootClass                = " + jdoClass.getPersistenceCapableRootClass()); //NOI18N
97          println(indent+1, "javaType                   = " + jdoClass.getJavaType()); //NOI18N
98          println(indent+1, "declaredManagedFieldCount  = " + jdoClass.getDeclaredManagedFieldCount()); //NOI18N
99          println(indent+1, "inheritedManagedFieldCount = " + jdoClass.getInheritedManagedFieldCount());  //NOI18N
100         println(indent+1, "managedFields              = " + Arrays.asList(jdoClass.getManagedFields())); //NOI18N
101         println(indent+1, "managedFieldNumbers        = " + asList(jdoClass.getManagedFieldNumbers()));  //NOI18N
102         println(indent+1, "persistentFieldNumbers     = " + asList(jdoClass.getPersistentFieldNumbers())); //NOI18N
103         println(indent+1, "primaryKeyFieldNumbers     = " + asList(jdoClass.getPrimaryKeyFieldNumbers())); //NOI18N
104         println(indent+1, "persistentNonPKFieldNs     = " + asList(jdoClass.getPersistentNonPrimaryKeyFieldNumbers())); //NOI18N
105         println(indent+1, "persistentRelshipFieldNs   = " + asList(jdoClass.getPersistentRelationshipFieldNumbers())); //NOI18N
106         println(indent+1, "persistentSerializableFNs  = " + asList(jdoClass.getPersistentSerializableFieldNumbers())); //NOI18N
107         println(indent+1, "declaredFields"); //NOI18N
108         printJDOFields(indent+1, jdoClass.getDeclaredFields());
109         printJDOExtensions(indent+1, jdoClass.getJDOExtensions());
110         println(0, "<-- JDOClass\n "); //NOI18N
111     }
112     
113     private static void printJDOFields(int indent, JDOField[] jdoFields) {
114         if ((jdoFields != null) && (jdoFields.length > 0)) {
115             TreeSet sorted = new TreeSet(new Comparator() {
116                     public int compare(Object o1, Object o2) {
117                         return (((JDOField)o1).getFieldNumber()
118                                 - ((JDOField)o2).getFieldNumber());
119                     }
120                     public boolean equals(Object obj) {
121                         return obj.equals(this);
122                     }
123                 });
124 
125             for (int i = 0; i < jdoFields.length; i++) {
126                 sorted.add(jdoFields[i]);
127             }
128 
129             for (Iterator i = sorted.iterator(); i.hasNext();) {
130                 printJDOField(indent, (JDOField)i.next());
131             }
132         }
133     }
134     
135     private static void printJDOField(int indent, JDOField jdoField) 
136     {
137         if (jdoField == null)
138             return;
139         boolean isProperty = jdoField.isProperty();
140 
141         if (isProperty)
142             println(indent, "--> JDOProperty"); //NOI18N
143         else
144             println(indent, "--> JDOField"); //NOI18N
145         println(indent+1, "name                = " + jdoField.getName()); //NOI18N
146         println(indent+1, "declaringClass      = " + jdoField.getDeclaringClass().getName()); //NOI18N
147         println(indent+1, "persistenceModifier = " + PersistenceModifier.toString(jdoField.getPersistenceModifier())); //NOI18N
148         println(indent+1, "primaryKey          = " + jdoField.isPrimaryKey()); //NOI18N
149         println(indent+1, "nullValue           = " + NullValueTreatment.toString(jdoField.getNullValueTreatment())); //NOI18N
150         println(indent+1, "defaultFetchGroup   = " + jdoField.isDefaultFetchGroup()); //NOI18N
151         println(indent+1, "embedded            = " + jdoField.isEmbedded()); //NOI18N
152         println(indent+1, "type                = " + jdoField.getType()); //NOI18N
153         //println(indent+1, "typeName            = " + jdoField.getTypeName()); //NOI18N
154         println(indent+1, "javaField           = " + jdoField.getJavaField()); //NOI18N
155         println(indent+1, "serializable        = " + jdoField.isSerializable()); //NOI18N
156         println(indent+1, "mappedByName        = " + jdoField.getMappedByName()); //NOI18N
157         println(indent+1, "fieldNumber         = " + jdoField.getFieldNumber()); //NOI18N
158         println(indent+1, "relativeFieldNumber = " + jdoField.getRelativeFieldNumber()); //NOI18N
159         println(indent+1, "isProperty          = " + jdoField.isProperty()); //NOI18N
160         printJDORelationship(indent+1, jdoField.getRelationship()); //NOI18N
161         printJDOExtensions(indent+1, jdoField.getJDOExtensions()); //NOI18N
162         if (isProperty)
163             println(indent, "<-- JDOProperty "); //NOI18N
164         else
165             println(indent, "<-- JDOField "); //NOI18N
166     }
167     
168     private static void printJDORelationship(int indent, 
169                                             JDORelationship jdoRelationship) 
170     {
171         if (jdoRelationship == null) 
172             return;
173 
174         if (jdoRelationship.isJDOReference())
175             printJDOReference(indent, (JDOReference)jdoRelationship);
176         else if (jdoRelationship.isJDOCollection())
177             printJDOCollection(indent, (JDOCollection)jdoRelationship);
178         else if (jdoRelationship.isJDOArray())
179             printJDOArray(indent, (JDOArray)jdoRelationship);
180         else if (jdoRelationship.isJDOMap())
181             printJDOMap(indent, (JDOMap)jdoRelationship);
182     }
183     
184     private static void printJDORelationshipHelper(int indent, JDORelationship jdoRelationship) 
185     {
186         if (jdoRelationship == null)
187             return;
188 
189         JDORelationship mappedBy = jdoRelationship.getMappedBy();
190         JDORelationship inverse = jdoRelationship.getInverseRelationship();
191         println(indent+1, "declaringField  = " + jdoRelationship.getDeclaringField().getName()); //NOI18N
192         println(indent+1, "bounds          = " + jdoRelationship.getLowerBound() + " / " +  jdoRelationship.getUpperBound()); //NOI18N
193         println(indent+1, "mappedBy        = " + ((mappedBy==null) ? "null" : //NOI18N
194             mappedBy.getDeclaringField().getDeclaringClass().getName() + "." + //NOI18N
195             mappedBy.getDeclaringField().getName()));
196         println(indent+1, "inverseName     = " + jdoRelationship.getInverseRelationshipName());
197         println(indent+1, "inverse         = " + ((inverse==null) ? "null" : //NOI18N
198             inverse.getDeclaringField().getDeclaringClass().getName() + "." + //NOI18N
199             inverse.getDeclaringField().getName()));
200     }
201     
202     private static void printJDOReference(int indent, JDOReference jdoReference) 
203     {
204         if (jdoReference == null)
205             return;
206 
207         println(indent, "--> JDOReference"); //NOI18N
208         printJDORelationshipHelper(indent, jdoReference);
209         printJDOExtensions(indent+1, jdoReference.getJDOExtensions());
210         println(indent, "<-- JDOReference"); //NOI18N
211     }
212     
213     private static void printJDOCollection(int indent, JDOCollection jdoCollection) 
214     {
215         if (jdoCollection == null)
216             return;
217 
218         println(indent, "--> JDOCollection"); //NOI18N
219         printJDORelationshipHelper(indent, jdoCollection);
220         println(indent+1, "embeddedElement = " + jdoCollection.isEmbeddedElement()); //NOI18N
221         println(indent+1, "elementType     = " + jdoCollection.getElementType()); //NOI18N
222         println(indent+1, "elementTypeName = " + jdoCollection.getElementTypeName()); //NOI18N
223         printJDOExtensions(indent+1, jdoCollection.getJDOExtensions()); //NOI18N
224         println(indent, "<-- JDOCollection"); //NOI18N
225     }
226     
227     private static void printJDOArray(int indent, JDOArray jdoArray) 
228     {
229         if (jdoArray == null)
230             return;
231 
232         println(indent, "--> JDOArray"); //NOI18N
233         printJDORelationshipHelper(indent, jdoArray);
234         println(indent+1, "embeddedElement = " + jdoArray.isEmbeddedElement()); //NOI18N
235         printJDOExtensions(indent+1, jdoArray.getJDOExtensions());
236         println(indent, "<-- JDOArray"); //NOI18N
237     }
238     
239     private static void printJDOMap(int indent, JDOMap jdoMap) 
240     {
241         if (jdoMap == null)
242             return;
243 
244         println(indent, "--> JDOMap"); //NOI18N
245         printJDORelationshipHelper(indent, jdoMap);
246         println(indent+1, "embeddedKey     = " + jdoMap.isEmbeddedKey()); //NOI18N
247         println(indent+1, "keyType         = " + jdoMap.getKeyType()); //NOI18N
248         println(indent+1, "keyTypeName     = " + jdoMap.getKeyTypeName()); //NOI18N
249         println(indent+1, "embeddedValue   = " + jdoMap.isEmbeddedValue()); //NOI18N
250         println(indent+1, "valueType       = " + jdoMap.getValueType()); //NOI18N
251         println(indent+1, "valueTypeName   = " + jdoMap.getValueTypeName()); //NOI18N
252         printJDOExtensions(indent+1, jdoMap.getJDOExtensions());
253         println(indent, "<-- JDOMap"); //NOI18N
254     }
255     
256     private static void printJDOExtensions(int indent, JDOExtension[] jdoExtensions) 
257     {
258         if ((jdoExtensions != null) && (jdoExtensions.length > 0)) {
259             TreeSet sorted = new TreeSet(new Comparator() {
260                     public int compare(Object o1, Object o2) {
261                         return ((JDOExtension)o1).getKey().compareTo(
262                           ((JDOExtension)o2).getKey());
263                     }
264                     public boolean equals(Object obj) {
265                         return obj.equals(this);
266                     }
267                 });
268 
269             for (int i = 0; i < jdoExtensions.length; i++) {
270                 sorted.add(jdoExtensions[i]);
271             }
272 
273             for (Iterator i = sorted.iterator(); i.hasNext();) {
274                 printJDOExtension(indent, (JDOExtension)i.next());
275             }
276         }
277     }
278     
279     private static void printJDOExtension(int indent, JDOExtension jdoExtension) 
280     {
281         if (jdoExtension == null)
282             return;
283         
284         println(indent, "--> JDOExtension"); //NOI18N
285         println(indent+1, "vendorName = " + jdoExtension.getVendorName()); //NOI18N
286         println(indent+1, "key        = " + jdoExtension.getKey()); //NOI18N
287         println(indent+1, "value      = " + jdoExtension.getValue()); //NOI18N
288         println(indent, "<-- JDOExtension"); //NOI18N
289     }
290     
291     // ----- helper methods -----
292     
293     static void println(int indent, String text) {
294         for (int i = 0; i < indent; i++) {
295             System.out.print("    "); //NOI18N
296         }
297         System.out.println(text);
298     }
299 
300     public static List asList(int[] array) 
301     {
302         int length = (array == null) ? 0 : array.length;
303         List list = new ArrayList(length);
304         for (int i = 0; i < length; i++) {
305             list.add(new Integer(array[i]));
306         }
307         return list;
308     }
309 }