View Javadoc

1   package org.codehaus.modello.plugin.stax;
2   
3   /*
4    * Copyright (c) 2004, Jason van Zyl
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import org.codehaus.modello.ModelloException;
26  import org.codehaus.modello.model.Model;
27  import org.codehaus.modello.model.ModelAssociation;
28  import org.codehaus.modello.model.ModelClass;
29  import org.codehaus.modello.model.ModelField;
30  import org.codehaus.modello.plugins.xml.AbstractXmlJavaGenerator;
31  import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
32  
33  import java.util.HashSet;
34  import java.util.List;
35  import java.util.Properties;
36  import java.util.Set;
37  
38  /**
39   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
40   */
41  public abstract class AbstractStaxGenerator
42      extends AbstractXmlJavaGenerator
43  {
44      private Set<ModelClass> parts;
45  
46      protected void initialize( Model model, Properties parameters )
47          throws ModelloException
48      {
49          super.initialize( model, parameters );
50  
51          parts = null;
52      }
53  
54      protected ModelField getReferenceIdentifierField( ModelAssociation association )
55          throws ModelloException
56      {
57          XmlAssociationMetadata xmlAssocMetadata =
58              (XmlAssociationMetadata) association.getAssociationMetadata( XmlAssociationMetadata.ID );
59  
60          ModelField referenceIdentifierField = null;
61          if ( xmlAssocMetadata.isReference() )
62          {
63              String associationName = association.getName();
64  
65              ModelClass modelClass = association.getModelClass();
66              if ( !isClassInModel( association.getTo(), modelClass.getModel() ) )
67              {
68                  throw new ModelloException( "Can't use xml.reference on the '" + associationName + "' association of '"
69                      + modelClass.getName() + "' because the target class '" + association.getTo()
70                      + "' is not in the model" );
71              }
72  
73              List<ModelField> identifierFields = association.getToClass().getIdentifierFields( getGeneratedVersion() );
74              if ( identifierFields.size() == 1 )
75              {
76                  referenceIdentifierField = identifierFields.get( 0 );
77              }
78              else
79              {
80                  referenceIdentifierField = new DummyIdModelField();
81                  referenceIdentifierField.setName( "modello.refid" );
82              }
83          }
84          return referenceIdentifierField;
85      }
86  
87      protected boolean isAssociationPartToClass( ModelClass modelClass )
88      {
89          if ( parts == null )
90          {
91              parts = new HashSet<ModelClass>();
92              for ( ModelClass clazz : modelClass.getModel().getClasses( getGeneratedVersion() ) )
93              {
94                  for ( ModelField modelField : clazz.getFields( getGeneratedVersion() ) )
95                  {
96                      if ( modelField instanceof ModelAssociation )
97                      {
98                          ModelAssociation assoc = (ModelAssociation) modelField;
99  
100                         XmlAssociationMetadata xmlAssocMetadata =
101                             (XmlAssociationMetadata) assoc.getAssociationMetadata( XmlAssociationMetadata.ID );
102 
103                         if ( xmlAssocMetadata.isReference() )
104                         {
105                             parts.add( assoc.getToClass() );
106                         }
107                     }
108                 }
109             }
110         }
111         return parts.contains( modelClass );
112     }
113 }