View Javadoc
1   package org.exolab.castor.mapping.loader;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   
6   import org.exolab.castor.mapping.ClassDescriptor;
7   import org.exolab.castor.mapping.FieldDescriptor;
8   import org.exolab.castor.mapping.MappingException;
9   
10  public class ClassDescriptorHelper {
11  
12    /**
13     * Get all the {@link FieldDescriptor}s for non-identity fields, including all the fields in base
14     * classes.
15     * 
16     * @param classDescriptor ClassMapping instance.
17     * @return An array.
18     * @throws MappingException
19     */
20    public static FieldDescriptor[] getFullFields(final ClassDescriptor classDescriptor)
21        throws MappingException {
22      ClassDescriptor baseClassDescriptor = classDescriptor.getExtends();
23      ArrayList<FieldDescriptor> fullFields = new ArrayList<FieldDescriptor>();
24  
25      if (baseClassDescriptor != null) {
26        ClassDescriptor origin = baseClassDescriptor;
27  
28        while (origin.getExtends() != null) {
29          origin = origin.getExtends();
30        }
31  
32        // recursive call to obtain full fields for base class.
33        FieldDescriptor[] fullBaseFieldDescriptors = getFullFields(baseClassDescriptor);
34        FieldDescriptor[] currentFields = classDescriptor.getFields();
35  
36        // add all base field descriptors
37        for (FieldDescriptor baseFieldDescriptor : fullBaseFieldDescriptors) {
38          fullFields.add(baseFieldDescriptor);
39        }
40        // add all fields of the current class
41        for (FieldDescriptor currentFieldDescriptor : currentFields) {
42          fullFields.add(currentFieldDescriptor);
43        }
44      } else {
45        FieldDescriptor[] fieldDescriptors = ((ClassDescriptorImpl) classDescriptor).getFields();
46        fullFields.addAll(Arrays.asList(fieldDescriptors));
47      }
48  
49      return fullFields.toArray(new FieldDescriptor[fullFields.size()]);
50    }
51  
52  
53    /**
54     * Get the all the id fields of a class If the class, C, is a dependent class, then the depended
55     * class', D, id fields will be appended at the back and returned. If the class is an extended
56     * class, the id fields of the extended class will be returned.
57     */
58    public static FieldDescriptor[] getIdFields(final ClassDescriptor classDescriptor)
59        throws MappingException {
60  
61      // start with the extended class
62      ClassDescriptor base = classDescriptor;
63      while (base.getExtends() != null) {
64        base = base.getExtends();
65      }
66  
67      // fmDepended = null;
68  
69      FieldDescriptor[] identities = ((ClassDescriptorImpl) base).getIdentities();
70  
71      if (identities == null || identities.length == 0) {
72        throw new MappingException("Identity is null!");
73      }
74  
75      // //INBESTIGATE[WG]: what's the use fo this code
76      // fmIds = new FieldMapping[identities.length];
77      // fmBase = base.getClassChoice().getFieldMapping();
78      // for (int i = 0; i < fmBase.length; i++) {
79      // for (int k = 0; k < identities.length; k++) {
80      // if (fmBase[i].getName().equals(identities[k])) {
81      // fmIds[k] = fmBase[i];
82      // break;
83      // }
84      // }
85      // }
86      // if (fmDepended == null) {
87      return identities;
88      // }
89  
90      // TODO[INVESTIGATE]: look at this dead code
91      // // join depend ids and class id
92      // fmResult = new FieldMapping[fmDepended.length + identities.length];
93      // System.arraycopy(fmIds, 0, fmResult, 0, fmIds.length);
94      // System.arraycopy(fmDepended, 0, fmResult, fmIds.length,
95      // fmDepended.length);
96      // return fmIds;
97    }
98  
99  }
100 
101