Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9368188aa14ac48be724759c9bb562af4d4f2f0f (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 (c) 2005, 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 org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jpt.core.internal.IJpaCoreConstants;
import org.eclipse.jpt.core.internal.JpaPlatformRegistry;
import org.eclipse.jpt.core.internal.JpaProject;
import org.eclipse.jpt.core.internal.JptCorePlugin;
import org.eclipse.jpt.core.internal.platform.generic.GenericPlatform;
import org.eclipse.jpt.db.internal.ConnectionProfileRepository;
import org.osgi.service.prefs.BackingStoreException;

public class JpaFacetUtils
{
	private static QualifiedName CONNECTION_KEY = 
			new QualifiedName(JptCorePlugin.PLUGIN_ID, IJpaCoreConstants.DATA_SOURCE_CONNECTION_NAME);
	
	
	/**
	 * Private constructor - static class only
	 */
	private JpaFacetUtils() {}
	
	
	public static String getPlatform(final IProject project) {
		final IScopeContext context = new ProjectScope(project);
		final IEclipsePreferences prefs = context.getNode(JptCorePlugin.PLUGIN_ID);
		
		String platformId = prefs.get(IJpaCoreConstants.JPA_PLATFORM, null);
		
		if (platformId == null) {
			try {
				setPlatform(project, GenericPlatform.ID);
			}
			catch (CoreException ce) {
				// do nothing.  not sure what can be done here.
			}
			return GenericPlatform.ID;
		}
		
		return platformId;
	}
	
	public static void setPlatform(final IProject project, String jpaPlatformId) 
		throws CoreException
	{
		final IScopeContext context = new ProjectScope(project);
		final IEclipsePreferences prefs = context.getNode(JptCorePlugin.PLUGIN_ID);
		
		JpaProject jpaProject = (JpaProject) JptCorePlugin.getJpaProject(project);
		
		if (jpaProject == null) {
			throw new IllegalArgumentException(project.getName());
		}
		
		if (JpaPlatformRegistry.INSTANCE.getJpaPlatform(jpaPlatformId) == null) {
			throw new IllegalArgumentException(jpaPlatformId);
		}
		
		jpaProject.setPlatform(jpaPlatformId);
		prefs.put(IJpaCoreConstants.JPA_PLATFORM, jpaPlatformId);
		
		try {
			prefs.flush();
		}
		catch( BackingStoreException e ) {
			JptCorePlugin.log(e);
		}
	}
	
	public static String getConnectionName(final IProject project) {
		try {
			return project.getPersistentProperty(CONNECTION_KEY);
		}
		catch (CoreException ce) {
			return null;
		}
	}
	
	public static void setConnectionName(final IProject project, String connectionName)
		throws CoreException
	{
		JpaProject jpaProject = (JpaProject) JptCorePlugin.getJpaProject(project);
		
		if (jpaProject == null) {
			throw new IllegalArgumentException(project.getName());
		}
		
		if (ConnectionProfileRepository.instance().getConnectionWithProfileNamed(connectionName) == null) {
			throw new IllegalArgumentException(connectionName);
		}
		
		jpaProject.setDataSource(connectionName);
		project.setPersistentProperty(CONNECTION_KEY, connectionName);
	}
	
	public static boolean getDiscoverAnnotatedClasses(final IProject project) {
		final IScopeContext context = new ProjectScope(project);
		final IEclipsePreferences prefs = context.getNode(JptCorePlugin.PLUGIN_ID);
		
		return prefs.getBoolean(IJpaCoreConstants.DISCOVER_ANNOTATED_CLASSES, false);
	}
	
	public static void setDiscoverAnnotatedClasses(final IProject project, boolean discoverAnnotatedClasses) 
		throws CoreException
	{
		final IScopeContext context = new ProjectScope(project);
		final IEclipsePreferences prefs = context.getNode(JptCorePlugin.PLUGIN_ID);
		
		JpaProject jpaProject = (JpaProject) JptCorePlugin.getJpaProject(project);
		
		if (jpaProject == null) {
			throw new IllegalArgumentException(project.getName());
		}
		
		jpaProject.setDiscoverAnnotatedClasses(discoverAnnotatedClasses);
		prefs.putBoolean(IJpaCoreConstants.DISCOVER_ANNOTATED_CLASSES, discoverAnnotatedClasses);
		
		try {
			prefs.flush();
		}
		catch(BackingStoreException e) {
			JptCorePlugin.log(e);
		}
	}
}

Back to the top