View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  
20  /**
21   * 
22   */
23  package org.org.usurper.handlers.additional;
24  
25  import java.util.Map;
26  
27  import org.org.usurper.UsurperGenerator;
28  import org.org.usurper.handlers.IHandler;
29  import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
30  import org.org.usurper.model.HandledBeanProperty;
31  import org.org.usurper.model.HandledConstructorArg;
32  import org.org.usurper.model.ITargetDefinition;
33  import org.org.usurper.model.PropertyTypeDefinition;
34  import org.org.usurper.setup.UsurperGeneratorSetup;
35  
36  /**
37   * This handler allows for a redundant ;) generation of ValueObjects.
38   * Handled Value Object types have to be passed to the constructor(s).
39   * 
40   * @author pagregoire
41   */
42  public class ValueObjectPropertyTypeHandler extends AbstractPropertyTypeHandler {
43      /**
44       * This constructor takes a series of Supported types' PropertyTypeDefinitions as a parameter.
45       * @param supportedTypes
46       */
47      public ValueObjectPropertyTypeHandler(PropertyTypeDefinition... supportedTypes) {
48          super(supportedTypes);
49      }
50  
51      /**
52       * This constructor takes a series of Supported types' Classes as a parameter.
53       * @param supportedTypes
54       */
55      public ValueObjectPropertyTypeHandler(Class<?>... supportedTypes) {
56          super(supportedTypes);
57      }
58  
59      /**
60       * @see org.org.usurper.handlers.basic.AbstractPropertyTypeHandler#handle()
61       */
62      public Object handle(HandledBeanProperty handledBeanProperty) {
63          return doHandle(handledBeanProperty.getPropertyClass(), handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers());
64      }
65  
66      /**
67       * @see org.org.usurper.handlers.basic.AbstractPropertyTypeHandler#handle()
68       */
69      public Object handle(HandledConstructorArg handledConstructorArg) {
70          return doHandle(handledConstructorArg.getConstructorArgClass(), handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers());
71      }
72  
73      /**
74       * This method does the proper handling.
75       * @return
76       */
77      @SuppressWarnings("unchecked")
78      private Object doHandle(Class<?> entityClass, Map<ITargetDefinition, IHandler> parentHandlers) {
79          UsurperGeneratorSetup usurperGeneratorSetup = new UsurperGeneratorSetup();
80          usurperGeneratorSetup.setAllHandlers(parentHandlers);
81          UsurperGenerator usurper = new UsurperGenerator(entityClass, usurperGeneratorSetup);
82          return usurper.generateUsurper();
83      }
84  
85  }