View Javadoc

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