Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3d5631119bd30ec38d768b928c6c393f8b0ac186 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
 * Copyright (c) 2006, 2007 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.facet;

import java.util.Arrays;
import java.util.Set;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jpt.core.internal.JptCoreMessages;
import org.eclipse.jpt.core.internal.JptCorePlugin;
import org.eclipse.jpt.core.internal.platform.generic.GenericPlatform;
import org.eclipse.jpt.core.internal.prefs.JpaPreferenceConstants;
import org.eclipse.jpt.db.internal.ConnectionProfileRepository;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.wst.common.componentcore.datamodel.FacetInstallDataModelProvider;
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
import org.eclipse.wst.common.frameworks.datamodel.DataModelPropertyDescriptor;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonMessages;
import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonPlugin;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;

public class JpaFacetDataModelProvider
	extends FacetInstallDataModelProvider
	implements IJpaFacetDataModelProperties
{
	public JpaFacetDataModelProvider() {
		super();
	}
	
	
	@Override
	public Set getPropertyNames() {
		Set propertyNames = super.getPropertyNames();
		propertyNames.add(PLATFORM_ID);
		propertyNames.add(CONNECTION);
		propertyNames.add(RUNTIME);
		propertyNames.add(USE_SERVER_JPA_IMPLEMENTATION);
		propertyNames.add(JPA_LIBRARY);
		propertyNames.add(DISCOVER_ANNOTATED_CLASSES);
		propertyNames.add(CREATE_ORM_XML);
		return propertyNames;
	}
	
	@Override
	public Object getDefaultProperty(String propertyName) {
		if (FACET_ID.equals(propertyName)) {
			return JptCorePlugin.FACET_ID;
		}
		else if (PLATFORM_ID.equals(propertyName)) {
			return GenericPlatform.ID;
		}
		else if (CONNECTION.equals(propertyName)) {
			return "";
		}
		else if (RUNTIME.equals(propertyName)) {
			return null;
		}
		else if (USE_SERVER_JPA_IMPLEMENTATION.equals(propertyName)) {
			return runtimeSupportsEjb30(getRuntime());
		}
		else if (JPA_LIBRARY.equals(propertyName)) {
			return JptCorePlugin.getPlugin().getPluginPreferences()
					.getString(JpaPreferenceConstants.PREF_DEFAULT_JPA_LIB);
		}
		else if (DISCOVER_ANNOTATED_CLASSES.equals(propertyName)) {
			return runtimeSupportsEjb30(getRuntime());
		}
		else if (CREATE_ORM_XML.equals(propertyName)) {
			return true;
		}
		else {
			return super.getDefaultProperty(propertyName);
		}
	}
	
	@Override
	public boolean propertySet(String propertyName, Object propertyValue) {
		boolean ok = super.propertySet(propertyName, propertyValue);
		if (RUNTIME.equals(propertyName)) {
			model.notifyPropertyChange(USE_SERVER_JPA_IMPLEMENTATION, IDataModel.DEFAULT_CHG);
			model.notifyPropertyChange(DISCOVER_ANNOTATED_CLASSES, IDataModel.DEFAULT_CHG);
		}
		return ok;
	}
	
	@Override
	public DataModelPropertyDescriptor[] getValidPropertyDescriptors(String propertyName) {
		if (JPA_LIBRARY.equals(propertyName)) {
			String[] libraries = JavaCore.getUserLibraryNames();
			Arrays.sort(libraries);
			DataModelPropertyDescriptor[] descriptors = new DataModelPropertyDescriptor[libraries.length + 1];
			
			descriptors[0] = new DataModelPropertyDescriptor("", WTPCommonPlugin.getResourceString(WTPCommonMessages.RUNTIME_NONE, null));
			
			int i = 1;
			for (String library : libraries) {
				descriptors[i++] = new DataModelPropertyDescriptor(library, library);
			}	
			return descriptors;
		}
		
		return super.getValidPropertyDescriptors(propertyName);
	}
	
	@Override
	public IStatus validate(String name) {
		if (PLATFORM_ID.equals(name)) {
			return validatePlatform(getStringProperty(name));
		}
		else if (CONNECTION.equals(name)) {
			return validateConnection(getStringProperty(name));
		}
		else if (USE_SERVER_JPA_IMPLEMENTATION.equals(name) || JPA_LIBRARY.equals(name)) {
			return validateJpaLibrary();
		}
		else if (DISCOVER_ANNOTATED_CLASSES.equals(name)) {
			return validatePersistentClassManagement();
		}
		else {
			return super.validate(name);
		}
	}
	
	private IRuntime getRuntime() {
		return (IRuntime) getProperty(RUNTIME);
	}
	
	private boolean runtimeSupportsEjb30(IRuntime runtime) {
		IProjectFacetVersion ejb30 = ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_EJB_MODULE).getVersion("3.0");
		return (runtime == null) ? false : runtime.supports(ejb30);
	}
	
	private IStatus validatePlatform(String platformId) {
		if (platformId == null || platformId.equals("")) {
			return new Status(IStatus.ERROR, JptCorePlugin.PLUGIN_ID, JptCoreMessages.VALIDATE_PLATFORM_NOT_SPECIFIED);
		}
		else {
			return OK_STATUS;
		}
	}
	
	private IStatus validateConnection(String connectionName) {
		if (connectionName == null || connectionName.equals("") || ! ConnectionProfileRepository.instance().getConnectionWithProfileNamed(connectionName).isConnected()) {
			return new Status(IStatus.INFO, JptCorePlugin.PLUGIN_ID, JptCoreMessages.VALIDATE_CONNECTION_NOT_CONNECTED);
		}
		else {
			return OK_STATUS;
		}
	}
	
	private IStatus validateJpaLibrary() {
		if (getBooleanProperty(USE_SERVER_JPA_IMPLEMENTATION)) {
			IRuntime runtime = getRuntime();
			if (runtime == null) {
				return new Status(IStatus.WARNING, JptCorePlugin.PLUGIN_ID, JptCoreMessages.VALIDATE_RUNTIME_NOT_SPECIFIED);
			}
			if (! runtimeSupportsEjb30(runtime)) {
				return new Status(IStatus.WARNING, JptCorePlugin.PLUGIN_ID, JptCoreMessages.VALIDATE_RUNTIME_DOES_NOT_SUPPORT_EJB_30);
			}
		}
		else {
			if (StringTools.stringIsEmpty(getStringProperty(JPA_LIBRARY))) {
				return new Status(IStatus.WARNING, JptCorePlugin.PLUGIN_ID, JptCoreMessages.VALIDATE_LIBRARY_NOT_SPECIFIED);
			}
		}
		return OK_STATUS;
	}
	
	private IStatus validatePersistentClassManagement() {
		// warning if "discovery" is used, but no runtime specified ??
		return OK_STATUS;
	}
}

Back to the top