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 extends ModelField {
33      public static final String ONE_MULTIPLICITY = "1";
34  
35      public static final String MANY_MULTIPLICITY = "*";
36  
37      // ----------------------------------------------------------------------
38      // Configuration
39      // ----------------------------------------------------------------------
40  
41      private String to;
42  
43      private String multiplicity;
44  
45      private ModelClass toClass;
46  
47      // ----------------------------------------------------------------------
48      //
49      // ----------------------------------------------------------------------
50  
51      /**
52       * @param to The to to set.
53       */
54      public void setTo(String to) {
55          this.to = to;
56      }
57  
58      /**
59       * @return Returns the to.
60       */
61      public String getTo() {
62          return to;
63      }
64  
65      public String getType() {
66          if (ONE_MULTIPLICITY.equals(getMultiplicity())) {
67              return getTo();
68          } else {
69              return super.getType();
70          }
71      }
72  
73      /**
74       * @return Returns the multiplicity.
75       */
76      public String getMultiplicity() {
77          return multiplicity;
78      }
79  
80      /**
81       * @param multiplicity The multiplicity to set.
82       */
83      public void setMultiplicity(String multiplicity) {
84          this.multiplicity = multiplicity;
85      }
86  
87      public boolean isManyMultiplicity() {
88          return MANY_MULTIPLICITY.equals(multiplicity);
89      }
90  
91      public boolean isOneMultiplicity() {
92          return ONE_MULTIPLICITY.equals(multiplicity);
93      }
94  
95      /**
96       * @return Returns the to ModelClass.
97       */
98      public ModelClass getToClass() {
99          return toClass;
100     }
101 
102     public AssociationMetadata getAssociationMetadata(String key) {
103         return getMetadata(AssociationMetadata.class, key);
104     }
105 
106     // ----------------------------------------------------------------------
107     // BaseElement overrides
108     // ----------------------------------------------------------------------
109 
110     public void validateElement() throws ModelValidationException {
111         validateFieldNotEmpty("Association", "name", getName());
112 
113         validateFieldNotEmpty("Association '" + getName() + "'", "to", to);
114 
115         if (isEmpty(to)) {
116             throw new ModelValidationException("You must define the type of association.");
117         }
118 
119         if (!"String".equals(to)) {
120             toClass = getModelClass().getModel().getClass(to, getVersionRange());
121 
122             if (toClass == null) {
123                 throw new ModelValidationException("Association '" + getName() + "': Could not find to class.");
124             }
125         }
126 
127         if (isEmpty(multiplicity)) {
128             multiplicity = ONE_MULTIPLICITY;
129         }
130 
131         if ("n".equals(multiplicity) || "*".equals(multiplicity)) {
132             multiplicity = MANY_MULTIPLICITY;
133         }
134 
135         if (!ONE_MULTIPLICITY.equals(multiplicity) && !MANY_MULTIPLICITY.equals(multiplicity)) {
136             throw new ModelValidationException("Association multiplicity '" + getName() + "' is incorrect: "
137                     + "Possible values are '1', '*' or 'n'.");
138         }
139 
140         if (isEmpty(getType())) {
141             ModelDefault modelDefault = getModelClass().getModel().getDefault(ModelDefault.LIST);
142 
143             setType(modelDefault.getKey());
144 
145             setDefaultValue(getDefaultValue(modelDefault));
146         } else {
147             if (isManyMultiplicity()) {
148                 if ("Set".equalsIgnoreCase(getType())) {
149                     setType(ModelDefault.SET);
150                 } else if ("List".equalsIgnoreCase(getType())) {
151                     setType(ModelDefault.LIST);
152                 } else if ("Map".equalsIgnoreCase(getType())) {
153                     setType(ModelDefault.MAP);
154                 } else if ("Properties".equalsIgnoreCase(getType())) {
155                     setType(ModelDefault.PROPERTIES);
156                 } else {
157                     throw new ModelValidationException(
158                             "The type of element '" + getName() + "' must be List, Map, Properties or Set.");
159                 }
160 
161                 if (isEmpty(getDefaultValue())) {
162                     ModelDefault modelDefault = getModelClass().getModel().getDefault(getType());
163 
164                     //                    setType( modelDefault.getKey() );
165 
166                     setDefaultValue(getDefaultValue(modelDefault));
167                 }
168             }
169         }
170     }
171 
172     public boolean isGenericType() {
173         return getType().equals(ModelDefault.LIST) || getType().equals(ModelDefault.SET);
174     }
175 
176     private String getDefaultValue(ModelDefault modelDefault) {
177         String value = modelDefault.getValue();
178 
179         if (isGenericType()) {
180             value = StringUtils.replace(value, "<?>", "/*<" + getTo() + ">*/");
181         }
182 
183         return value;
184     }
185 }