Coverage Report - org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseHibernateWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
MyEclipseHibernateWriter
0%
0/43
0%
0/4
1.6
 
 1  
 package org.apache.maven.plugin.eclipse.writers.myeclipse;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileWriter;
 5  
 import java.io.IOException;
 6  
 import java.io.PrintWriter;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.apache.maven.plugin.MojoExecutionException;
 10  
 import org.apache.maven.plugin.eclipse.Messages;
 11  
 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
 12  
 import org.apache.maven.plugin.ide.IdeUtils;
 13  
 import org.codehaus.plexus.util.IOUtil;
 14  
 
 15  
 /**
 16  
  * Writer for MyEclipse Hibernate Capability
 17  
  * 
 18  
  * @author <a href="mailto:olivier.jacob@gmail.com">Olivier Jacob</a>
 19  
  */
 20  
 public class MyEclipseHibernateWriter
 21  
     extends AbstractEclipseWriter
 22  
 {
 23  
 
 24  
     private static final String MYECLIPSE_HIBERNATE_DATA_FILE = ".myhibernatedata";
 25  
 
 26  
     private static final String MYECLIPSE_HB_GEN_COMP_ID = "genBasicCompId";
 27  
 
 28  
     private static final String MYECLIPSE_HB_SESSION_FACTORY_NAME = "sessionFactoryName";
 29  
 
 30  
     private static final String MYECLIPSE_HB_PROFILE = "profile";
 31  
 
 32  
     private static final String MYECLIPSE_HB_DAO_SF_ID = "daoSFId";
 33  
 
 34  
     private static final String MYECLIPSE_HB_VERSION = "version";
 35  
 
 36  
     private static final String MYECLIPSE_HB_JNDI_PATH = "jndiPath";
 37  
 
 38  
     private static final String MYECLIPSE_HB_DETECT_M2M = "detectM2M";
 39  
 
 40  
     private static final String MYECLIPSE_HB_RE_STRATEGY_CLASS = "reStrategyClass";
 41  
 
 42  
     private static final String MYECLIPSE_HB_SPRING_DAO_FILE = "springDaoFile";
 43  
 
 44  
     private static final String MYECLIPSE_HB_USE_JAVA_TYPES = "useJavaTypes";
 45  
 
 46  
     private static final String MYECLIPSE_HB_KEY_GENERATOR = "keyGenerator";
 47  
 
 48  
     private static final String MYECLIPSE_HB_LIB_INSTALL_FOLDER = "libInstallFolder";
 49  
 
 50  
     private static final String MYECLIPSE_HB_ADD_LIBS_TO_PROJECT = "addLibs2Project";
 51  
 
 52  
     private static final String MYECLIPSE_HB_GEN_VERSION_TAG = "genVersionTag";
 53  
 
 54  
     private static final String MYECLIPSE_HB_SESSION_FACTORY_ID = "sessionFactoryId";
 55  
 
 56  
     private static final String MYECLIPSE_HB_BASE_PERSISTENT_CLASS = "basePersistenceClass";
 57  
 
 58  
     private static final String MYECLIPSE_HB_RE_SETTINGS_FILE = "reSettingsFile";
 59  
 
 60  
     private static final String MYECLIPSE_HB_CONFIG_FILE = "configFile";
 61  
 
 62  
     private static final String MYECLIPSE_HB_CREATE_CONFIG_FILE = "createConfigFile";
 63  
 
 64  
     private static final String MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH = "addLibs2Classpath";
 65  
 
 66  
     private static final String MYECLIPSE_HB_BASE_DAO_CLASS = "baseDaoClass";
 67  
 
 68  
     /**
 69  
      * Relative path to the Hibernate configuration file to use in MyEclipse
 70  
      */
 71  
     private Map hibernateConfig;
 72  
 
 73  
     /**
 74  
      * Constructor
 75  
      * 
 76  
      * @param hibernateConfig path to the configuration file to use
 77  
      */
 78  
     public MyEclipseHibernateWriter( Map hibernateConfig )
 79  0
     {
 80  0
         this.hibernateConfig = hibernateConfig;
 81  0
     }
 82  
 
 83  
     /**
 84  
      * Write MyEclipse Hibernate configuration
 85  
      * 
 86  
      * @throws MojoExecutionException if an error occurs
 87  
      */
 88  
     public void write()
 89  
         throws MojoExecutionException
 90  
     {
 91  
         FileWriter w;
 92  
 
 93  
         try
 94  
         {
 95  0
             w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_HIBERNATE_DATA_FILE ) );
 96  
         }
 97  0
         catch ( IOException ex )
 98  
         {
 99  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 100  0
         }
 101  
 
 102  0
         PrintWriter pw = new PrintWriter( w );
 103  
 
 104  0
         pw.println( "# Generated by Maven" );
 105  0
         addProperty( pw, MYECLIPSE_HB_GEN_COMP_ID, "false" );
 106  0
         addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_NAME, null );
 107  0
         addProperty( pw, MYECLIPSE_HB_PROFILE, null );
 108  0
         addProperty( pw, MYECLIPSE_HB_DAO_SF_ID, null );
 109  0
         addProperty( pw, MYECLIPSE_HB_VERSION, getHibernateVersion() );
 110  0
         addProperty( pw, MYECLIPSE_HB_JNDI_PATH, null );
 111  0
         addProperty( pw, MYECLIPSE_HB_DETECT_M2M, "false" );
 112  0
         addProperty( pw, MYECLIPSE_HB_RE_STRATEGY_CLASS, null );
 113  0
         addProperty( pw, MYECLIPSE_HB_SPRING_DAO_FILE, null );
 114  0
         addProperty( pw, MYECLIPSE_HB_USE_JAVA_TYPES, "true" );
 115  0
         addProperty( pw, MYECLIPSE_HB_KEY_GENERATOR, null );
 116  0
         addProperty( pw, MYECLIPSE_HB_LIB_INSTALL_FOLDER, null );
 117  0
         addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_PROJECT, "false" );
 118  0
         addProperty( pw, MYECLIPSE_HB_GEN_VERSION_TAG, "false" );
 119  0
         addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_ID, (String) hibernateConfig.get( "session-factory-id" ) );
 120  0
         addProperty( pw, MYECLIPSE_HB_BASE_PERSISTENT_CLASS, null );
 121  0
         addProperty( pw, MYECLIPSE_HB_RE_SETTINGS_FILE, null );
 122  0
         addProperty( pw, MYECLIPSE_HB_CONFIG_FILE,
 123  
                      makePathToHibernateConfigFile( (String) hibernateConfig.get( "config-file" ) ) );
 124  0
         addProperty( pw, MYECLIPSE_HB_CREATE_CONFIG_FILE, "false" );
 125  0
         addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH, "false" );
 126  0
         addProperty( pw, MYECLIPSE_HB_BASE_DAO_CLASS, null );
 127  
 
 128  0
         IOUtil.close( w );
 129  0
     }
 130  
 
 131  
     /**
 132  
      * Writes a configuration property to the PrintWriter given in parameter
 133  
      * 
 134  
      * @param pw the PrintWriter to write to
 135  
      * @param propName the property name
 136  
      * @param propValue the property value (writes empty String if null)
 137  
      */
 138  
     private void addProperty( PrintWriter pw, String propName, String propValue )
 139  
     {
 140  0
         StringBuffer sb = new StringBuffer( 64 );
 141  
 
 142  0
         sb.append( propName ).append( "=" );
 143  
 
 144  0
         if ( propValue != null )
 145  
         {
 146  0
             sb.append( propValue );
 147  
         }
 148  
 
 149  0
         pw.println( sb.toString() );
 150  0
     }
 151  
 
 152  
     /**
 153  
      * Find Hibernate version in project dependencies
 154  
      * 
 155  
      * @return the version of the hibernate artifact if found in the dependencies or 3.2 (default value)
 156  
      */
 157  
     private String getHibernateVersion()
 158  
     {
 159  0
         String version =
 160  
             IdeUtils.getArtifactVersion( new String[] { "hibernate" }, config.getProject().getDependencies(), 3 );
 161  
 
 162  0
         return version != null ? version : "3.2";
 163  
     }
 164  
 
 165  
     /**
 166  
      * Prepend the project artifactId to the path given in the plugin configuration
 167  
      * 
 168  
      * @return the path to the file relative to the root of the Eclipse Workspace
 169  
      */
 170  
     private String makePathToHibernateConfigFile( String configFile )
 171  
     {
 172  0
         StringBuffer sb = new StringBuffer( 64 );
 173  
 
 174  0
         sb.append( "/" ).append( config.getProject().getArtifactId() ).append( "/" ).append( configFile );
 175  
 
 176  0
         return sb.toString();
 177  
     }
 178  
 }