View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "DataTypeGenerator.java".  Description: 
10  "Generates skeletal source code for Datatype classes based on the 
11    HL7 database" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s):  James Agnew 
17  
18  Alternatively, the contents of this file may be used under the terms of the 
19  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
20  applicable instead of those above.  If you wish to allow use of your version of this 
21  file only under the terms of the GPL and not to allow others to use your version 
22  of this file under the MPL, indicate your decision by deleting  the provisions above 
23  and replace  them with the notice and other provisions required by the GPL License.  
24  If you do not delete the provisions above, a recipient may use your version of 
25  this file under either the MPL or the GPL. 
26  
27  */
28  
29  package ca.uhn.hl7v2.mvnplugin;
30  
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.project.MavenProject;
38  
39  import ca.uhn.hl7v2.HL7Exception;
40  import ca.uhn.hl7v2.database.NormativeDatabase;
41  import ca.uhn.hl7v2.sourcegen.DataTypeGenerator;
42  import ca.uhn.hl7v2.sourcegen.EventMapGenerator;
43  import ca.uhn.hl7v2.sourcegen.SourceGenerator;
44  
45  /**
46   * Maven Plugin Mojo for generating HAPI HL7 message/segment/etc source files
47   * 
48   * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
49   * @goal xrefgen
50   * @phase generate-sources
51   * @requiresDependencyResolution runtime
52   * @requiresProject
53   * @inheritedByDefault false
54   */
55  public class XrefGenMojo extends AbstractMojo
56  {
57      private static final Set<String> alreadyMade = new HashSet<String>();
58      
59      /**
60       * The maven project.
61       * 
62       * @parameter property="project"
63       * @required
64       * @readonly
65       */
66      @SuppressWarnings("unused")
67  	private MavenProject project;
68  
69      
70      /**
71       * The target directory for the generated source
72       * 
73       * @parameter
74       * @required
75       */
76      private String targetDirectory;
77  
78      /**
79       * The version for the generated source
80       * 
81       * @parameter
82       */
83      private String version;
84      
85      
86      /**
87       * The JDBC URL for the HL7 database
88       * 
89       * @parameter
90       */
91      private String jdbcUrl;
92  
93      /**
94       * The JDBC User for the HL7 database
95       * 
96       * @parameter
97       */
98      private String jdbcUser;
99  
100     /**
101      * The JDBC Password for the HL7 database
102      * 
103      * @parameter
104      */
105     private String jdbcPassword;
106 
107     
108     /**
109      * Should build be skipped
110      *
111      * @parameter
112      */
113     private boolean skip;
114 
115     /**
116      * The package from which to load the templates
117      * 
118      * @parameter default="ca.uhn.hl7v2.sourcegen.templates"
119      */
120     private String templatePackage = "ca.uhn.hl7v2.sourcegen.templates.json";
121 
122     /**
123      * {@inheritDoc}
124      */
125     public void execute() throws MojoExecutionException, MojoFailureException {
126 
127         if (skip) {
128             getLog().warn("Configured to skip");
129         }
130 
131     	if (!alreadyMade.contains(version)) {
132 
133     		// I haven't entirely figured out why, but Maven runs this plugin 
134     		// several times for each version, which takes forever, so we assume
135     		// that if the directory exists, we don't need to generate again
136             alreadyMade.add(version);
137             
138             if (jdbcUser == null) {
139                 jdbcUser = "";
140             }
141             if (jdbcPassword == null) {
142                 jdbcPassword = "";
143             }
144             
145            System.setProperty(NormativeDatabase.PROP_DATABASE_USER, jdbcUser);
146            System.setProperty(NormativeDatabase.PROP_DATABASE_PASSWORD, jdbcPassword);
147            System.setProperty(NormativeDatabase.PROP_DATABASE_URL, jdbcUrl);
148             
149            DataTypeGenerator.setMakeAll(true);
150            
151             try {
152                 SourceGenerator.makeAll(targetDirectory, version, false, templatePackage, "json");
153                 EventMapGenerator.generateEventDesc(targetDirectory, version, templatePackage);
154 			} catch (HL7Exception e) {
155 				e.printStackTrace();
156 			    throw new MojoExecutionException("Failed to build source ", e);
157 			} catch (Exception e) {
158                 e.printStackTrace();
159                 throw new MojoExecutionException("Failed to build source ", e);
160             }
161             
162         } else {
163             getLog().warn("Already made version " + version + ", skipping!");
164         }
165         
166     }
167 
168 }