Skip to main content
summaryrefslogtreecommitdiffstats
blob: 922308d027dede53a6c0a25d79278778f075e67c (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2009 Oracle. 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:
 *     Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.core.internal.operations;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jpt.core.JpaProject;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.core.resource.AbstractXmlResourceProvider;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

public abstract class AbstractJpaFileCreationOperation
	extends AbstractDataModelOperation
	implements JpaFileCreationDataModelProperties
{
	/**
	 * Will be null until folder is created
	 */
	protected IContainer createdSourceFolder;
	
	/**
	 * Will be null until file is created
	 */
	protected IFile createdFile;
	
	
	protected AbstractJpaFileCreationOperation(IDataModel dataModel) {
		super(dataModel);
	}
	
	
	@Override
	public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		// Create source folder if it does not exist
		createSourceFolder();
		// Create file
		createFile();
		return OK_STATUS;
	}
	
	protected IProject getProject() throws ExecutionException {
		String projectName = (String) getDataModel().getProperty(PROJECT_NAME);
		IProject project = ProjectUtilities.getProject(projectName);
		if (project == null) {
			throw new ExecutionException("No project name specified"); //$NON-NLS-1$
		}
		return project;
	}
	
	protected JpaProject getJpaProject() throws ExecutionException {
		IProject project = getProject();
		JpaProject jpaProject = JptCorePlugin.getJpaProject(project);
		if (jpaProject == null) {
			throw new ExecutionException("Project does not have JPA content"); //$NON-NLS-1$
		}
		return jpaProject;
	}
	
	/**
	 * This method will return the source folder as specified in the data model. 
	 * It will create the source folder if it does not exist. It will not add
	 * it as a source folder to the project build path if it is not already.
	 * This method may return null.
	 */
	// copied from NewJavaClassOperation
	protected void createSourceFolder() throws ExecutionException {
		// Get the source folder name from the data model
		String folderPath = model.getStringProperty(SOURCE_FOLDER);
		IProject project = getProject();
		IWorkspaceRoot root = project.getWorkspace().getRoot();
		IContainer container = root.getContainerForLocation(root.getLocation().append(new Path(folderPath)));
		boolean createVirtualReference = ComponentCore.createFolder(project, new Path("")).exists();
		if (container instanceof IFolder) {
			IFolder folder = (IFolder) container;
			// If folder does not exist, create the folder with the specified path
			if (! folder.exists()) {
				try {
					folder.create(true, true, null);
				} catch (CoreException e) {
					throw new ExecutionException("Could not create folder", e); //$NON-NLS-1$
				}
			}
			if (createVirtualReference) {
				IVirtualFolder rootFolder = ComponentCore.createComponent(project).getRootFolder();
				try {
					rootFolder.getFolder(new Path("/")).createLink(container.getProjectRelativePath(), 0, null);
				}
				catch (CoreException ce) {
					JptCorePlugin.log(ce);
				}
			}
		}
		else if (container instanceof IProject) {
			if (createVirtualReference) {
				IVirtualFolder rootFolder = ComponentCore.createComponent(project).getRootFolder();
				try {
					rootFolder.getFolder(new Path("/META-INF")).createLink(container.getProjectRelativePath().append("META-INF"), 0, null);
				}
				catch (CoreException ce) {
					JptCorePlugin.log(ce);
				}
			}
		}
		// Return the source folder
		this.createdSourceFolder = container;
	}
	
	protected void createFile() {
		String filePath = getDataModel().getStringProperty(FILE_PATH);
		IFile newFile = this.createdSourceFolder.getFile(new Path(filePath));
		AbstractXmlResourceProvider resourceProvider = getXmlResourceProvider(newFile);
		try {
			resourceProvider.createFileAndResource(getDataModel());
		}
		catch (CoreException e) {
			JptCorePlugin.log(e);
			newFile = null;
		}
		this.createdFile = newFile;
	}
	
	@Override
	public ISchedulingRule getSchedulingRule() {
		try {
			return this.getProject();
		} catch (ExecutionException ex) {
			throw new RuntimeException(ex);
		}
	}
	
	protected abstract AbstractXmlResourceProvider getXmlResourceProvider(IFile file);
}

Back to the top