Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 Oracle. |
3 |
* All rights reserved. This program and the accompanying materials are |
4 |
* made available under the terms of the Eclipse Public License v1.0 which |
5 |
* accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Oracle - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jpt.core.internal.operations; |
12 |
|
13 |
import java.util.Set; |
14 |
import org.eclipse.core.resources.IFile; |
15 |
import org.eclipse.core.resources.IFolder; |
16 |
import org.eclipse.core.resources.IProject; |
17 |
import org.eclipse.core.runtime.IStatus; |
18 |
import org.eclipse.core.runtime.Path; |
19 |
import org.eclipse.core.runtime.Status; |
20 |
import org.eclipse.jdt.core.IJavaProject; |
21 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
22 |
import org.eclipse.jdt.core.JavaCore; |
23 |
import org.eclipse.jem.util.emf.workbench.ProjectUtilities; |
24 |
import org.eclipse.jpt.core.JpaProject; |
25 |
import org.eclipse.jpt.core.JptCorePlugin; |
26 |
import org.eclipse.jpt.core.context.persistence.Persistence; |
27 |
import org.eclipse.jpt.core.context.persistence.PersistenceUnit; |
28 |
import org.eclipse.jpt.core.context.persistence.PersistenceXml; |
29 |
import org.eclipse.jpt.core.internal.JptCoreMessages; |
30 |
import org.eclipse.jpt.core.resource.orm.AccessType; |
31 |
import org.eclipse.jpt.utility.internal.StringTools; |
32 |
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities; |
33 |
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider; |
34 |
import org.eclipse.wst.common.frameworks.datamodel.DataModelPropertyDescriptor; |
35 |
import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
36 |
import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation; |
37 |
|
38 |
public class OrmFileCreationDataModelProvider extends AbstractDataModelProvider |
39 |
implements OrmFileCreationDataModelProperties |
40 |
{ |
41 |
/** |
42 |
* required default constructor |
43 |
*/ |
44 |
public OrmFileCreationDataModelProvider() { |
45 |
super(); |
46 |
} |
47 |
|
48 |
|
49 |
@Override |
50 |
public IDataModelOperation getDefaultOperation() { |
51 |
return new OrmFileCreationOperation(getDataModel()); |
52 |
} |
53 |
|
54 |
@Override |
55 |
public Set<String> getPropertyNames() { |
56 |
@SuppressWarnings("unchecked") |
57 |
Set<String> propertyNames = super.getPropertyNames(); |
58 |
propertyNames.add(PROJECT_NAME); |
59 |
propertyNames.add(SOURCE_FOLDER); |
60 |
propertyNames.add(FILE_PATH); |
61 |
propertyNames.add(DEFAULT_ACCESS); |
62 |
propertyNames.add(ADD_TO_PERSISTENCE_UNIT); |
63 |
propertyNames.add(PERSISTENCE_UNIT); |
64 |
return propertyNames; |
65 |
} |
66 |
|
67 |
@Override |
68 |
public Object getDefaultProperty(String propertyName) { |
69 |
if (propertyName.equals(SOURCE_FOLDER)) { |
70 |
IFolder sourceFolder = getDefaultSourceFolder(); |
71 |
if (sourceFolder != null && sourceFolder.exists()) { |
72 |
return sourceFolder.getFullPath().toOSString(); |
73 |
} |
74 |
} |
75 |
else if (propertyName.equals(FILE_PATH)) { |
76 |
return new Path(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH).toOSString(); |
77 |
} |
78 |
else if (propertyName.equals(DEFAULT_ACCESS)) { |
79 |
return null; |
80 |
} |
81 |
else if (propertyName.equals(ADD_TO_PERSISTENCE_UNIT)) { |
82 |
return Boolean.FALSE; |
83 |
} |
84 |
else if (propertyName.equals(PERSISTENCE_UNIT)) { |
85 |
PersistenceUnit pUnit = getDefaultPersistenceUnit(); |
86 |
if (pUnit != null) { |
87 |
return pUnit.getName(); |
88 |
} |
89 |
} |
90 |
return super.getDefaultProperty(propertyName); |
91 |
} |
92 |
|
93 |
@Override |
94 |
public boolean propertySet(String propertyName, Object propertyValue) { |
95 |
boolean ok = super.propertySet(propertyName, propertyValue); |
96 |
if (propertyName.equals(PROJECT_NAME)) { |
97 |
this.model.notifyPropertyChange(SOURCE_FOLDER, IDataModel.DEFAULT_CHG); |
98 |
} |
99 |
return ok; |
100 |
} |
101 |
|
102 |
@Override |
103 |
public DataModelPropertyDescriptor[] getValidPropertyDescriptors(String propertyName) { |
104 |
if (propertyName.equals(DEFAULT_ACCESS)) { |
105 |
DataModelPropertyDescriptor[] accessTypes = new DataModelPropertyDescriptor[3]; |
106 |
accessTypes[0] = new DataModelPropertyDescriptor(null, JptCoreMessages.NONE); |
107 |
accessTypes[1] = new DataModelPropertyDescriptor(AccessType.FIELD, AccessType.FIELD.getName()); |
108 |
accessTypes[2] = new DataModelPropertyDescriptor(AccessType.PROPERTY, AccessType.PROPERTY.getName()); |
109 |
return accessTypes; |
110 |
} |
111 |
else { |
112 |
return super.getValidPropertyDescriptors(propertyName); |
113 |
} |
114 |
} |
115 |
|
116 |
|
117 |
// **************** validation ********************************************* |
118 |
|
119 |
@Override |
120 |
public IStatus validate(String propertyName) { |
121 |
if (propertyName.equals(PROJECT_NAME) |
122 |
|| propertyName.equals(SOURCE_FOLDER) |
123 |
|| propertyName.equals(FILE_PATH)) { |
124 |
return validateProjectSourceFolderAndFilePath(); |
125 |
} |
126 |
else if (propertyName.equals(ADD_TO_PERSISTENCE_UNIT) |
127 |
|| propertyName.equals(PERSISTENCE_UNIT)) { |
128 |
return validatePersistenceUnit(); |
129 |
} |
130 |
return super.validate(propertyName); |
131 |
} |
132 |
|
133 |
private IStatus validateProjectSourceFolderAndFilePath() { |
134 |
String projectName = (String) getProperty(PROJECT_NAME); |
135 |
if (StringTools.stringIsEmpty(projectName)) { |
136 |
return new Status( |
137 |
IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
138 |
JptCoreMessages.VALIDATE_PROJECT_NOT_SPECIFIED); |
139 |
} |
140 |
String sourceFolderPath = getStringProperty(SOURCE_FOLDER); |
141 |
if (StringTools.stringIsEmpty(sourceFolderPath)) { |
142 |
return new Status( |
143 |
IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
144 |
JptCoreMessages.VALIDATE_SOURCE_FOLDER_NOT_SPECIFIED); |
145 |
} |
146 |
if (getVerifiedSourceFolder() == null) { |
147 |
return new Status( |
148 |
IStatus.WARNING, JptCorePlugin.PLUGIN_ID, |
149 |
JptCoreMessages.bind(JptCoreMessages.VALIDATE_SOURCE_FOLDER_DOES_NOT_EXIST, sourceFolderPath)); |
150 |
} |
151 |
if (getVerifiedSourceFolder().getProject() != getProject()) { |
152 |
return new Status( |
153 |
IStatus.WARNING, JptCorePlugin.PLUGIN_ID, |
154 |
JptCoreMessages.bind( |
155 |
JptCoreMessages.VALIDATE_SOURCE_FOLDER_NOT_IN_PROJECT, |
156 |
sourceFolderPath, projectName)); |
157 |
} |
158 |
if (getVerifiedJavaSourceFolder() == null) { |
159 |
return new Status( |
160 |
IStatus.WARNING, JptCorePlugin.PLUGIN_ID, |
161 |
JptCoreMessages.bind(JptCoreMessages.VALIDATE_SOURCE_FOLDER_NOT_SOURCE_FOLDER, sourceFolderPath)); |
162 |
} |
163 |
if (getExistingOrmFile() != null) { |
164 |
return new Status( |
165 |
IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
166 |
JptCoreMessages.VALIDATE_ORM_FILE_ALREADY_EXISTS); |
167 |
} |
168 |
return Status.OK_STATUS; |
169 |
} |
170 |
|
171 |
private IStatus validatePersistenceUnit() { |
172 |
boolean addToPUnit = getBooleanProperty(ADD_TO_PERSISTENCE_UNIT); |
173 |
String pUnitName = getStringProperty(PERSISTENCE_UNIT); |
174 |
if (addToPUnit) { |
175 |
if (getPersistenceUnit() == null) { |
176 |
return new Status( |
177 |
IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
178 |
JptCoreMessages.bind(JptCoreMessages.VALIDATE_PERSISTENCE_UNIT_DOES_NOT_EXIST, pUnitName)); |
179 |
} |
180 |
} |
181 |
return Status.OK_STATUS; |
182 |
} |
183 |
|
184 |
|
185 |
// **************** helper methods ***************************************** |
186 |
|
187 |
// Copied from ArtifactEditOperationDataModelProvider |
188 |
private IProject getProject() { |
189 |
String projectName = (String) model.getProperty(PROJECT_NAME); |
190 |
if (StringTools.stringIsEmpty(projectName)) { |
191 |
return null; |
192 |
} |
193 |
return ProjectUtilities.getProject(projectName); |
194 |
} |
195 |
|
196 |
private JpaProject getJpaProject() { |
197 |
IProject project = getProject(); |
198 |
if (project == null) { |
199 |
return null; |
200 |
} |
201 |
return JptCorePlugin.getJpaProject(project); |
202 |
} |
203 |
|
204 |
/** |
205 |
* Return a best guess java source folder for the specified project |
206 |
*/ |
207 |
// Copied from NewJavaClassDataModelProvider |
208 |
private IFolder getDefaultSourceFolder() { |
209 |
IProject project = getProject(); |
210 |
if (project == null) { |
211 |
return null; |
212 |
} |
213 |
IPackageFragmentRoot[] sources = J2EEProjectUtilities.getSourceContainers(project); |
214 |
// Try and return the first source folder |
215 |
if (sources.length > 0) { |
216 |
try { |
217 |
return (IFolder) sources[0].getCorrespondingResource(); |
218 |
} catch (Exception e) { |
219 |
return null; |
220 |
} |
221 |
} |
222 |
return null; |
223 |
} |
224 |
|
225 |
/** |
226 |
* Return an IFolder represented by the SOURCE_FOLDER property, verified |
227 |
* to exist |
228 |
*/ |
229 |
private IFolder getVerifiedSourceFolder() { |
230 |
String folderPath = getStringProperty(SOURCE_FOLDER); |
231 |
IProject project = getProject(); |
232 |
if (project == null) { |
233 |
return null; |
234 |
} |
235 |
IFolder folder; |
236 |
try { |
237 |
folder = project.getWorkspace().getRoot().getFolder(new Path(folderPath)); |
238 |
} |
239 |
catch (IllegalArgumentException e) { |
240 |
return null; |
241 |
} |
242 |
if (folder == null || ! folder.exists()) { |
243 |
return null; |
244 |
} |
245 |
return folder; |
246 |
} |
247 |
|
248 |
/** |
249 |
* Return the source folder, provided it is verified to be an actual java |
250 |
* source folder |
251 |
*/ |
252 |
private IFolder getVerifiedJavaSourceFolder() { |
253 |
IFolder folder = getVerifiedSourceFolder(); |
254 |
if (folder == null) { |
255 |
return null; |
256 |
} |
257 |
IJavaProject jProject = JavaCore.create(getProject()); |
258 |
if (jProject == null) { |
259 |
return null; |
260 |
} |
261 |
IPackageFragmentRoot packageFragmentRoot = jProject.getPackageFragmentRoot(folder); |
262 |
if (packageFragmentRoot == null) { |
263 |
return null; |
264 |
} |
265 |
return folder; |
266 |
} |
267 |
|
268 |
private IFile getExistingOrmFile() { |
269 |
IFolder folder = getVerifiedSourceFolder(); |
270 |
if (folder == null) { |
271 |
return null; |
272 |
} |
273 |
String filePath = getStringProperty(FILE_PATH); |
274 |
IFile existingFile = folder.getFile(new Path(filePath)); |
275 |
if (! existingFile.exists()) { |
276 |
return null; |
277 |
} |
278 |
return existingFile; |
279 |
} |
280 |
|
281 |
private PersistenceUnit getDefaultPersistenceUnit() { |
282 |
JpaProject jpaProject = getJpaProject(); |
283 |
if (jpaProject == null) { |
284 |
return null; |
285 |
} |
286 |
PersistenceXml persistenceXml = jpaProject.getRootContext().getPersistenceXml(); |
287 |
if (persistenceXml == null) { |
288 |
return null; |
289 |
} |
290 |
Persistence persistence = persistenceXml.getPersistence(); |
291 |
if (persistence == null) { |
292 |
return null; |
293 |
} |
294 |
if (persistence.persistenceUnitsSize() == 0) { |
295 |
return null; |
296 |
} |
297 |
return persistence.persistenceUnits().next(); |
298 |
} |
299 |
|
300 |
private PersistenceUnit getPersistenceUnit() { |
301 |
String pUnitName = getStringProperty(PERSISTENCE_UNIT); |
302 |
if (StringTools.stringIsEmpty(pUnitName)) { |
303 |
return null; |
304 |
} |
305 |
JpaProject jpaProject = getJpaProject(); |
306 |
if (jpaProject == null) { |
307 |
return null; |
308 |
} |
309 |
PersistenceXml persistenceXml = jpaProject.getRootContext().getPersistenceXml(); |
310 |
if (persistenceXml == null) { |
311 |
return null; |
312 |
} |
313 |
PersistenceUnit pUnit = persistenceXml.getPersistenceUnit(); |
314 |
if (! pUnitName.equals(pUnit.getName())) { |
315 |
return null; |
316 |
} |
317 |
return pUnit; |
318 |
} |
319 |
} |