View Javadoc
1   package org.nuiton.eugene.java;
2   
3   /*
4    * #%L
5    * EUGene :: Java templates
6    * %%
7    * Copyright (C) 2012 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.ImmutableSet;
26  import org.apache.commons.lang3.StringUtils;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.nuiton.eugene.Template;
29  import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata;
30  import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil;
31  import org.nuiton.eugene.models.extension.tagvalue.matcher.EqualsTagValueNameMatcher;
32  import org.nuiton.eugene.models.extension.tagvalue.provider.DefaultTagValueMetadatasProvider;
33  import org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider;
34  import org.nuiton.eugene.models.object.ObjectModel;
35  import org.nuiton.eugene.models.object.ObjectModelClassifier;
36  import org.nuiton.eugene.models.object.ObjectModelPackage;
37  
38  import java.util.Set;
39  
40  import static org.nuiton.i18n.I18n.n;
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * Defines all tag values managed by Java templates.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 2.5.6
48   */
49  @Component(role = TagValueMetadatasProvider.class, hint = "bean")
50  public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider {
51  
52      @Override
53      public String getDescription() {
54          return t("eugene.bean.tagvalues");
55      }
56  
57      public enum Store implements TagValueMetadata {
58  
59          /**
60           * To generate a utility class around the bean. this classe offers functions, predicates, copy methods, ...
61           *
62           * You must use it on the complete model.
63           *
64           * @see #isGenerateHelper(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
65           * @since 3.0
66           */
67          generateHelper(n("eugene.bean.tagvalue.generateHelper"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
68  
69  
70          /**
71           * To generate or not guava predicates on each property of the bean.
72           *
73           * You can globally use it on the complete model or to a specific classifier.
74           *
75           * @see #isGenerateHelperPredicates(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
76           * @since 3.0
77           */
78          generateHelperPredicates(n("eugene.bean.tagvalue.generateHelperPredicates"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
79  
80          /**
81           * To generate or not guava functions on each property of the bean.
82           *
83           * You can globally use it on the complete model or to a specific classifier.
84           *
85           * @see #isGenerateHelperFunctions(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
86           * @since 3.0
87           */
88          generateHelperFunctions(n("eugene.bean.tagvalue.generateHelperFunctions"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
89  
90          /**
91           * To generate or not constructors methods on Default classes.
92           *
93           * You can globally use it on the complete model, package or on a specific classifier.
94           *
95           * @see #isGenerateHelperConstructors(ObjectModelClassifier, ObjectModelPackage, ObjectModel)}
96           * @since 3.0
97           */
98          generateHelperConstructors(n("eugene.bean.tagvalue.generateHelperConstructors"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
99  
100         /**
101          * Tag value to generate property change support on generated beans.
102          *
103          * You can globally use it on the complete model, on packages, or to a specific classifier.
104          *
105          * @see #isGeneratePropertyChangeSupport(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
106          * @since 3.0
107          */
108         generatePropertyChangeSupport(n("eugene.bean.tagvalue.generatePropertyChangeSupport"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
109 
110         /**
111          * Tag value to generate lazy instantiation of any collection to avoid NPEs.
112          *
113          * You can globally use it on the complete model or a package, or to a specific classifier.
114          *
115          * @see #isGenerateNotEmptyCollections(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
116          * @since 3.0
117          */
118         generateNotEmptyCollections(n("eugene.bean.tagvalue.generateNotEmptyCollections"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
119 
120         /**
121          * Tag value to use a super class for generated bean.
122          *
123          * If the bean needs Property change support (says you use the tag-value {@link Store#generatePropertyChangeSupport},
124          * then your class must provide everything for it.
125          *
126          * More over, if you use some collections in your bean you must also define
127          * two method named {@code getChild(Collection list, int index)} and
128          * {@code getChild(List list, int index)}
129          *
130          * See new code to know minimum stuff to add in your class for this purpose.
131          * <pre>
132          * public abstract class BeanSupport implements Serializable {
133          *
134          *     private static final long serialVersionUID = 1L;
135          *
136          *     protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
137          *
138          *     public void addPropertyChangeListener(PropertyChangeListener listener) {
139          *         pcs.addPropertyChangeListener(listener);
140          *     }
141          *
142          *     public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
143          *         pcs.addPropertyChangeListener(propertyName, listener);
144          *     }
145          *
146          *     public void removePropertyChangeListener(PropertyChangeListener listener) {
147          *         pcs.removePropertyChangeListener(listener);
148          *     }
149          *
150          *     public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
151          *         pcs.removePropertyChangeListener(propertyName, listener);
152          *     }
153          *
154          *     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
155          *         pcs.firePropertyChange(propertyName, oldValue, newValue);
156          *     }
157          *
158          *     protected void firePropertyChange(String propertyName, Object newValue) {
159          *         firePropertyChange(propertyName, null, newValue);
160          *     }
161          *
162          *     protected &lt;T&gt; T getChild(Collection&lt;T&gt; list, int index) {
163          *         return CollectionUtil.getOrNull(list, index);
164          *     }
165          *
166          *     protected &lt;T&gt; T getChild(List&lt;T&gt; list, int index) {
167          *         return CollectionUtil.getOrNull(list, index);
168          *     }
169          * }
170          * </pre>
171          *
172          * You can globally use it on the complete model or to a specific classifier.
173          *
174          * @see #getSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
175          * @since 3.0
176          */
177         superClass(n("eugene.bean.tagvalue.superClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
178 
179         /**
180          * Tag value to use a super super-class for generated defaults class of a simple bean.
181          *
182          * You can globally use it on the complete model or to a specific classifier.
183          *
184          * @see #getHelperSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
185          * @since 3.0
186          */
187         helperSuperClass(n("eugene.bean.tagvalue.helperSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
188 
189         /**
190          * To add a prefix on the name of each generated bean class.
191          *
192          * You can globally use it on the complete model or to a specific classifier.
193          *
194          * @see #getClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
195          * @since 3.0
196          */
197         classNamePrefix(n("eugene.bean.tagvalue.classNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
198 
199         /**
200          * To add a prefix on the name of each generated bean class.
201          *
202          * You can globally use it on the complete model or to a specific classifier.
203          *
204          * @see #getClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
205          * @since 3.0
206          */
207         classNameSuffix(n("eugene.bean.tagvalue.classNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
208 
209         /**
210          * To add a prefix on the name of each generated bean class.
211          *
212          * You can globally use it on the complete model or to a specific classifier.
213          *
214          * @see #getHelperClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
215          * @since 3.0
216          */
217         helperClassNamePrefix(n("eugene.bean.tagvalue.helperClassNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
218 
219         /**
220          * To add a suffix on the name of each generated bean class.
221          *
222          * You can globally use it on the complete model or to a specific classifier.
223          *
224          * @see #getHelperClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel)
225          * @since 3.0
226          */
227         helperClassNameSuffix(n("eugene.bean.tagvalue.helperClassNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class);
228 
229         private final Set<Class<?>> targets;
230         private final Class<?> type;
231         private final String i18nDescriptionKey;
232         private final String defaultValue;
233 
234         Store(String i18nDescriptionKey, Class<?> type, String defaultValue, Class<?>... targets) {
235             this.targets = ImmutableSet.copyOf(targets);
236             this.type = type;
237             this.i18nDescriptionKey = i18nDescriptionKey;
238             this.defaultValue = defaultValue;
239         }
240 
241         @Override
242         public String getName() {
243             return name();
244         }
245 
246         @Override
247         public Set<Class<?>> getTargets() {
248             return targets;
249         }
250 
251         @Override
252         public Class<?> getType() {
253             return type;
254         }
255 
256         @Override
257         public Class<EqualsTagValueNameMatcher> getMatcherClass() {
258             return EqualsTagValueNameMatcher.class;
259         }
260 
261         @Override
262         public String getDescription() {
263             return t(i18nDescriptionKey);
264         }
265 
266         @Override
267         public String getDefaultValue() {
268             return defaultValue;
269         }
270 
271         @Override
272         public boolean isDeprecated() {
273             return false;
274         }
275 
276     }
277 
278     public BeanTransformerTagValues() {
279         super((TagValueMetadata[]) Store.values());
280     }
281 
282     /**
283      * Obtain the value of the {@link Store#superClass} tag value on the given model or classifier.
284      *
285      * It will first look on the model, and then in the given classifier.
286      *
287      * @param classifier classifier to seek
288      * @param model      model to seek
289      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
290      * @see Store#superClass
291      * @since 3.0
292      */
293     public String getSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
294         return TagValueUtil.findTagValue(Store.superClass, classifier, aPackage, model);
295     }
296 
297     /**
298      * Obtain the value of the {@link Store#helperSuperClass} tag value on the given model or classifier.
299      *
300      * It will first look on the model, and then in the given classifier.
301      *
302      * @param classifier classifier to seek
303      * @param model      model to seek
304      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
305      * @see Store#helperSuperClass
306      * @since 3.0
307      */
308     public String getHelperSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
309         return TagValueUtil.findTagValue(Store.helperSuperClass, classifier, aPackage, model);
310     }
311 
312     /**
313      * Obtain the value of the {@link Store#classNamePrefix} tag value on the given model or classifier.
314      *
315      * It will first look on the model, and then in the given classifier.
316      *
317      * @param classifier classifier to seek
318      * @param model      model to seek
319      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
320      * @see Store#classNamePrefix
321      * @since 3.0
322      */
323     public String getClassNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
324         return TagValueUtil.findTagValue(Store.classNamePrefix, classifier, aPackage, model);
325     }
326 
327     /**
328      * Obtain the value of the {@link Store#classNameSuffix} tag value on the given model or classifier.
329      *
330      * It will first look on the model, and then in the given classifier.
331      *
332      * @param classifier classifier to seek
333      * @param model      model to seek
334      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
335      * @see Store#classNameSuffix
336      * @since 3.0
337      */
338     public String getClassNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
339         return TagValueUtil.findTagValue(Store.classNameSuffix, classifier, aPackage, model);
340     }
341 
342     /**
343      * Obtain the value of the {@link Store#helperClassNamePrefix} tag value on the given model or classifier.
344      *
345      * It will first look on the model, and then in the given classifier.
346      *
347      * @param classifier classifier to seek
348      * @param model      model to seek
349      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
350      * @see Store#helperClassNamePrefix
351      * @since 3.0
352      */
353     public String getHelperClassNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
354         return TagValueUtil.findTagValue(Store.helperClassNamePrefix, classifier, aPackage, model);
355     }
356 
357     /**
358      * Obtain the value of the {@link Store#helperClassNameSuffix} tag value on the given model or classifier.
359      *
360      * It will first look on the model, and then in the given classifier.
361      *
362      * <strong>If not filled, then use default {@code s} value.</strong>
363      *
364      * @param classifier classifier to seek
365      * @param model      model to seek
366      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
367      * @see Store#helperClassNameSuffix
368      * @since 3.0
369      */
370     public String getHelperClassNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
371         String value = TagValueUtil.findTagValue(Store.helperClassNameSuffix, classifier, aPackage, model);
372         if (StringUtils.isBlank(value)) {
373             value = "Helper";
374         }
375         return value;
376     }
377 
378     /**
379      * Obtain the value of the {@link Store#generateHelper} tag value on the given model or classifier.
380      *
381      * It will first look on the model, and then in the given classifier.
382      *
383      * @param classifier classifier to seek
384      * @param model      model to seek
385      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
386      * @see Store#generateHelper
387      * @since 3.0
388      */
389     public boolean isGenerateHelper(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
390         return TagValueUtil.findBooleanTagValue(Store.generateHelper, classifier, aPackage, model);
391     }
392 
393     /**
394      * Obtain the value of the {@link Store#generateHelperPredicates} tag value on the given model or classifier.
395      *
396      * It will first look on the model, and then in the given classifier.
397      *
398      * <strong>If not filled, then use default {@code s} value.</strong>
399      *
400      * @param classifier classifier to seek
401      * @param model      model to seek
402      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
403      * @see Store#generateHelperPredicates
404      * @since 3.0
405      */
406     public boolean isGenerateHelperPredicates(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
407         return TagValueUtil.findBooleanTagValue(Store.generateHelperPredicates, classifier, aPackage, model);
408     }
409 
410     /**
411      * Obtain the value of the {@link Store#generateHelperFunctions} tag value on the given model or classifier.
412      *
413      * It will first look on the model, and then in the given classifier.
414      *
415      * <strong>If not filled, then use default {@code s} value.</strong>
416      *
417      * @param classifier classifier to seek
418      * @param model      model to seek
419      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
420      * @see Store#generateHelperFunctions
421      * @since 3.0
422      */
423     public boolean isGenerateHelperFunctions(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
424         return TagValueUtil.findBooleanTagValue(Store.generateHelperFunctions, classifier, aPackage, model);
425     }
426 
427     /**
428      * Obtain the value of the {@link Store#generateHelperConstructors} tag value on the given model or classifier.
429      *
430      * It will first look on the model, and then in the given classifier.
431      *
432      * <strong>If not filled, then use default {@code s} value.</strong>
433      *
434      * @param classifier classifier to seek
435      * @param model      model to seek
436      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
437      * @see Store#generateHelperConstructors
438      * @since 3.0
439      */
440     public boolean isGenerateHelperConstructors(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
441         return TagValueUtil.findBooleanTagValue(Store.generateHelperConstructors, classifier, aPackage, model);
442     }
443 
444     /**
445      * Obtain the value of the {@link Store#generatePropertyChangeSupport} tag value on the given model, package or classifier.
446      *
447      * It will first look on the model, then and package and then in the given classifier.
448      *
449      * If no value found, then will use the default value of the tag value.
450      *
451      * @param classifier classifier to seek
452      * @param aPackage   package to seek
453      * @param model      model to seek
454      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
455      * @see Store#generatePropertyChangeSupport
456      * @since 3.0
457      */
458     public boolean isGeneratePropertyChangeSupport(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
459         return TagValueUtil.findBooleanTagValue(Store.generatePropertyChangeSupport, classifier, aPackage, model);
460     }
461 
462     /**
463      * Obtain the value of the {@link Store#generateNotEmptyCollections} tag value on the given model, package or classifier.
464      *
465      * It will first look on the model, then and package and then in the given classifier.
466      *
467      * If no value found, then will use the default value of the tag value.
468      *
469      * @param classifier classifier to seek
470      * @param aPackage   package to seek
471      * @param model      model to seek
472      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
473      * @see Store#generateNotEmptyCollections
474      * @since 3.0
475      */
476     public boolean isGenerateNotEmptyCollections(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
477         return TagValueUtil.findBooleanTagValue(Store.generateNotEmptyCollections, classifier, aPackage, model);
478     }
479 }