View Javadoc

1   package org.codehaus.modello.model;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
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.metadata.AssociationMetadata;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  /**
29   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
30   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
31   */
32  public class ModelAssociation
33      extends ModelField
34  {
35      public static final String ONE_MULTIPLICITY = "1";
36  
37      public static final String MANY_MULTIPLICITY = "*";
38  
39      // ----------------------------------------------------------------------
40      // Configuration
41      // ----------------------------------------------------------------------
42  
43      private String to;
44  
45      private String multiplicity;
46  
47      private ModelClass toClass;
48  
49      // ----------------------------------------------------------------------
50      //
51      // ----------------------------------------------------------------------
52  
53      /**
54       * @param to The to to set.
55       */
56      public void setTo( String to )
57      {
58          this.to = to;
59      }
60  
61      /**
62       * @return Returns the to.
63       */
64      public String getTo()
65      {
66          return to;
67      }
68  
69      public String getType()
70      {
71          if ( ONE_MULTIPLICITY.equals( getMultiplicity() ) )
72          {
73              return getTo();
74          }
75          else
76          {
77              return super.getType();
78          }
79      }
80  
81      /**
82       * @return Returns the multiplicity.
83       */
84      public String getMultiplicity()
85      {
86          return multiplicity;
87      }
88  
89      /**
90       * @param multiplicity The multiplicity to set.
91       */
92      public void setMultiplicity( String multiplicity )
93      {
94          this.multiplicity = multiplicity;
95      }
96  
97      public boolean isManyMultiplicity()
98      {
99          return MANY_MULTIPLICITY.equals( multiplicity );
100     }
101 
102     public boolean isOneMultiplicity()
103     {
104         return ONE_MULTIPLICITY.equals( multiplicity );
105     }
106 
107     /**
108      * @return Returns the to ModelClass.
109      */
110     public ModelClass getToClass()
111     {
112         return toClass;
113     }
114 
115     public AssociationMetadata getAssociationMetadata( String key )
116     {
117         return getMetadata( AssociationMetadata.class, key );
118     }
119 
120     // ----------------------------------------------------------------------
121     // BaseElement overrides
122     // ----------------------------------------------------------------------
123 
124     public void validateElement()
125         throws ModelValidationException
126     {
127         validateFieldNotEmpty( "Association", "name", getName() );
128 
129         validateFieldNotEmpty( "Association '" + getName() + "'", "to", to );
130 
131         if ( isEmpty( to ) )
132         {
133             throw new ModelValidationException( "You must define the type of association." );
134         }
135 
136         if ( !"String".equals( to ) )
137         {
138             toClass = getModelClass().getModel().getClass( to, getVersionRange() );
139 
140             if ( toClass == null )
141             {
142                 throw new ModelValidationException( "Association '" + getName() + "': Could not find to class." );
143             }
144         }
145 
146         if ( isEmpty( multiplicity ) )
147         {
148             multiplicity = ONE_MULTIPLICITY;
149         }
150 
151         if ( "n".equals( multiplicity ) || "*".equals( multiplicity ) )
152         {
153             multiplicity = MANY_MULTIPLICITY;
154         }
155 
156         if ( !ONE_MULTIPLICITY.equals( multiplicity ) && !MANY_MULTIPLICITY.equals( multiplicity ) )
157         {
158             throw new ModelValidationException( "Association multiplicity '" + getName() + "' is incorrect: "
159                                                 + "Possible values are '1', '*' or 'n'." );
160         }
161 
162         if ( isEmpty( getType() ) )
163         {
164             ModelDefault modelDefault = getModelClass().getModel().getDefault( ModelDefault.LIST );
165 
166             setType( modelDefault.getKey() );
167 
168             setDefaultValue( getDefaultValue( modelDefault ) );
169         }
170         else
171         {
172             if ( isManyMultiplicity() )
173             {
174                 if ( "Set".equalsIgnoreCase( getType() ) )
175                 {
176                     setType( ModelDefault.SET );
177                 }
178                 else if ( "List".equalsIgnoreCase( getType() ) )
179                 {
180                     setType( ModelDefault.LIST );
181                 }
182                 else if ( "Map".equalsIgnoreCase( getType() ) )
183                 {
184                     setType( ModelDefault.MAP );
185                 }
186                 else if ( "Properties".equalsIgnoreCase( getType() ) )
187                 {
188                     setType( ModelDefault.PROPERTIES );
189                 }
190                 else
191                 {
192                     throw new ModelValidationException(
193                         "The type of element '" + getName() + "' must be List, Map, Properties or Set." );
194                 }
195 
196                 if ( isEmpty( getDefaultValue() ) )
197                 {
198                     ModelDefault modelDefault = getModelClass().getModel().getDefault( getType() );
199 
200 //                    setType( modelDefault.getKey() );
201 
202                     setDefaultValue( getDefaultValue( modelDefault ) );
203                 }
204             }
205         }
206     }
207 
208     public boolean isGenericType()
209     {
210         return getType().equals( ModelDefault.LIST ) || getType().equals( ModelDefault.SET );
211     }
212 
213     private String getDefaultValue( ModelDefault modelDefault )
214     {
215         String value = modelDefault.getValue();
216 
217         if ( isGenericType() )
218         {
219             value = StringUtils.replace( value, "<?>", "/*<" + getTo() + ">*/" );
220         }
221 
222         return value;
223     }
224 }