Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6492234a8168a99602ccf6bf1fa9fadeb4e93237 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * <copyright>
 *
 * Copyright (c) 2009 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: GenerateORM.java,v 1.1 2009/03/23 19:01:40 mtaal Exp $
 */

package org.eclipse.emf.teneo.jpa;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Properties;

import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.TeneoException;
import org.eclipse.emf.teneo.annotations.mapper.PersistenceMappingBuilder;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedModel;
import org.eclipse.emf.teneo.extension.ExtensionManager;
import org.eclipse.emf.teneo.extension.ExtensionManagerFactory;
import org.eclipse.emf.teneo.jpa.convert.ORMGenerator;

/**
 * Class is responsible for generating the hbm file. Is run through a launcher
 * therefore the main methods.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.1 $
 */

public class GenerateORM {

	/** Parses the args and generates a hbm file */
	public static void main(String[] args) {
		// decode the args
		Properties options = new Properties();
		ArrayList<String> ecores = new ArrayList<String>();
		String targetFileName = null;
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				targetFileName = args[0];
			} else if (args[i].startsWith("+")) { // option
				final String[] keyValue = args[i].substring(1).split(",");
				options.put(keyValue[0], keyValue[1]);
			} else if (args[i].startsWith("-")) { // epackage classnames
				final String[] epacks = args[i].substring(1).split(",");

				// the epackages load them all, hope for the best
				// when loading the epackage should be loaded
				for (String element : epacks) {
					try {
						Class.forName(element);
					} catch (Throwable t) { // ignore everything but show
						// something
						t.printStackTrace(System.err);
					}
				}
			} else {
				ecores.add(args[i]);
			}
		}

		createORMapperFile(targetFileName, ecores.toArray(new String[ecores
				.size()]), options);
	}

	/** Creates the mapping file */
	private static void createORMapperFile(String targetFileName,
			String[] ecores, Properties options) {
		try {
			// get the first ecore file
			File firstEcore = new File(ecores[0]);
			File file = new File(firstEcore.getParentFile(), targetFileName);
			final File archiveFile = new File(firstEcore.getParentFile(),
					targetFileName + "_old");

			if (file.exists()) {
				if (archiveFile.exists()) {
					archiveFile.delete();
				}
				copyFile(file, archiveFile);
				file.delete();
			}
			file.createNewFile();

			final ExtensionManager extensionManager = ExtensionManagerFactory
					.getInstance().create();

			final ORMGenerator converter = new ORMGenerator();
			final PersistenceOptions po = converter
					.getDefaultPersistenceOptions(options);
			final PAnnotatedModel paModel = extensionManager.getExtension(
					PersistenceMappingBuilder.class).buildMapping(ecores, po,
					extensionManager);
			final String orm = converter.generateORM(paModel.getPaEPackages(),
					po);
			FileWriter writer = new FileWriter(file);
			writer.write(orm);
			writer.flush();
		} catch (IOException e) {
			throw new TeneoException(
					"IOException when creating or mapping file", e);
		}
	}

	/** Copies a file */
	public static void copyFile(File src, File dst) throws IOException {
		if (!dst.exists()) {
			dst.createNewFile();
		}

		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dst);

		// Transfer bytes from in to out
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}
}

Back to the top