1 package org.apache.maven.plugin.eclipse.writers.myeclipse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.Map;
28
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.eclipse.Messages;
31 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
32 import org.apache.maven.plugin.ide.IdeUtils;
33 import org.codehaus.plexus.util.IOUtil;
34
35
36
37
38
39
40 public class MyEclipseHibernateWriter
41 extends AbstractEclipseWriter
42 {
43
44 private static final String MYECLIPSE_HIBERNATE_DATA_FILE = ".myhibernatedata";
45
46 private static final String MYECLIPSE_HB_GEN_COMP_ID = "genBasicCompId";
47
48 private static final String MYECLIPSE_HB_SESSION_FACTORY_NAME = "sessionFactoryName";
49
50 private static final String MYECLIPSE_HB_PROFILE = "profile";
51
52 private static final String MYECLIPSE_HB_DAO_SF_ID = "daoSFId";
53
54 private static final String MYECLIPSE_HB_VERSION = "version";
55
56 private static final String MYECLIPSE_HB_JNDI_PATH = "jndiPath";
57
58 private static final String MYECLIPSE_HB_DETECT_M2M = "detectM2M";
59
60 private static final String MYECLIPSE_HB_RE_STRATEGY_CLASS = "reStrategyClass";
61
62 private static final String MYECLIPSE_HB_SPRING_DAO_FILE = "springDaoFile";
63
64 private static final String MYECLIPSE_HB_USE_JAVA_TYPES = "useJavaTypes";
65
66 private static final String MYECLIPSE_HB_KEY_GENERATOR = "keyGenerator";
67
68 private static final String MYECLIPSE_HB_LIB_INSTALL_FOLDER = "libInstallFolder";
69
70 private static final String MYECLIPSE_HB_ADD_LIBS_TO_PROJECT = "addLibs2Project";
71
72 private static final String MYECLIPSE_HB_GEN_VERSION_TAG = "genVersionTag";
73
74 private static final String MYECLIPSE_HB_SESSION_FACTORY_ID = "sessionFactoryId";
75
76 private static final String MYECLIPSE_HB_BASE_PERSISTENT_CLASS = "basePersistenceClass";
77
78 private static final String MYECLIPSE_HB_RE_SETTINGS_FILE = "reSettingsFile";
79
80 private static final String MYECLIPSE_HB_CONFIG_FILE = "configFile";
81
82 private static final String MYECLIPSE_HB_CREATE_CONFIG_FILE = "createConfigFile";
83
84 private static final String MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH = "addLibs2Classpath";
85
86 private static final String MYECLIPSE_HB_BASE_DAO_CLASS = "baseDaoClass";
87
88
89
90
91 private Map hibernateConfig;
92
93
94
95
96
97
98 public MyEclipseHibernateWriter( Map hibernateConfig )
99 {
100 this.hibernateConfig = hibernateConfig;
101 }
102
103
104
105
106
107
108 public void write()
109 throws MojoExecutionException
110 {
111 FileWriter w;
112
113 try
114 {
115 w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_HIBERNATE_DATA_FILE ) );
116 }
117 catch ( IOException ex )
118 {
119 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
120 }
121
122 PrintWriter pw = new PrintWriter( w );
123
124 pw.println( "# Generated by Maven" );
125 addProperty( pw, MYECLIPSE_HB_GEN_COMP_ID, "false" );
126 addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_NAME, null );
127 addProperty( pw, MYECLIPSE_HB_PROFILE, null );
128 addProperty( pw, MYECLIPSE_HB_DAO_SF_ID, null );
129 addProperty( pw, MYECLIPSE_HB_VERSION, getHibernateVersion() );
130 addProperty( pw, MYECLIPSE_HB_JNDI_PATH, null );
131 addProperty( pw, MYECLIPSE_HB_DETECT_M2M, "false" );
132 addProperty( pw, MYECLIPSE_HB_RE_STRATEGY_CLASS, null );
133 addProperty( pw, MYECLIPSE_HB_SPRING_DAO_FILE, null );
134 addProperty( pw, MYECLIPSE_HB_USE_JAVA_TYPES, "true" );
135 addProperty( pw, MYECLIPSE_HB_KEY_GENERATOR, null );
136 addProperty( pw, MYECLIPSE_HB_LIB_INSTALL_FOLDER, null );
137 addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_PROJECT, "false" );
138 addProperty( pw, MYECLIPSE_HB_GEN_VERSION_TAG, "false" );
139 addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_ID, (String) hibernateConfig.get( "session-factory-id" ) );
140 addProperty( pw, MYECLIPSE_HB_BASE_PERSISTENT_CLASS, null );
141 addProperty( pw, MYECLIPSE_HB_RE_SETTINGS_FILE, null );
142 addProperty( pw, MYECLIPSE_HB_CONFIG_FILE,
143 makePathToHibernateConfigFile( (String) hibernateConfig.get( "config-file" ) ) );
144 addProperty( pw, MYECLIPSE_HB_CREATE_CONFIG_FILE, "false" );
145 addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH, "false" );
146 addProperty( pw, MYECLIPSE_HB_BASE_DAO_CLASS, null );
147
148 IOUtil.close( w );
149 }
150
151
152
153
154
155
156
157
158 private void addProperty( PrintWriter pw, String propName, String propValue )
159 {
160 StringBuilder sb = new StringBuilder( 64 );
161
162 sb.append( propName ).append( "=" );
163
164 if ( propValue != null )
165 {
166 sb.append( propValue );
167 }
168
169 pw.println( sb.toString() );
170 }
171
172
173
174
175
176
177 private String getHibernateVersion()
178 {
179 String version =
180 IdeUtils.getArtifactVersion( new String[] { "hibernate" }, config.getProject().getDependencies(), 3 );
181
182 return version != null ? version : "3.2";
183 }
184
185
186
187
188
189
190 private String makePathToHibernateConfigFile( String configFile )
191 {
192
193 return "/" + config.getProject().getArtifactId() + "/" + configFile;
194 }
195 }