Skip to main content
summaryrefslogtreecommitdiffstats
blob: ec3795d4bc1c21c82d1e1bcc969dfc082df98400 (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
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation 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:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060427   138058 joan@ca.ibm.com - Joan Haggarty
 *******************************************************************************/
package org.eclipse.jst.ws.internal.consumption.ui.common;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jst.ws.internal.common.J2EEUtils;
import org.eclipse.jst.ws.internal.common.ResourceUtils;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;

public class DefaultingUtils {
	
	public DefaultingUtils()
	{
			
	}
	
	/**
	 * 
	 * Uses the following steps to determine a valid default EAR project name:
	 *  
	 * 1. If project does exist:
	 * 		a - look for an existing, referencing EAR component, if one exists return that EAR name
	 * 		b - look for an existing EAR component with the same J2EE version as the project
	 * 2. If can't find an appropriate existing EAR component or project does not exist in the 
	 *     workspace, return a new EAR name of form projectNameEAR
	 * 3. If project name is null or an empty string return the default EAR name
	 * 
	 * @param projectName a string representing the name of the project for the service
	 * @return a string to be used as the default EAR project name for the project name provided
	 */
	public static String getDefaultEARProjectName(String projectName)
	{

		if (projectName != null && projectName.length() > 0) {
			
			IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

			if (proj.exists()) {
				
				//Step 1a - return a referencing EAR project
				IVirtualComponent[] ears = J2EEUtils.getReferencingEARComponents(proj);
				if (ears != null && ears.length > 0) {
					return ears[0].getName();
				}
				
				//Step 1b - return an appropriate existing EAR project
			    IVirtualComponent[] allEarComps = J2EEUtils.getAllEARComponents();			      

			    if (allEarComps.length > 0)
			    {			      
			        for (int i=0; i < allEarComps.length; i++)
			        {
			          IProject earProject = allEarComps[i].getProject();
			          IStatus associationStatus = J2EEUtils.canAssociateProjectToEAR(proj, earProject);
			          if (associationStatus.getSeverity()==IStatus.OK)
			          {
			            return allEarComps[i].getName(); 
			          }
			        }			      			      
			    }
			}
			
			//Step 2 - return project name with EAR on the end
			return projectName + ResourceUtils.getDefaultEARExtension();			
		}
		
		//Step 3 - return the default EAR project name
		return ResourceUtils.getDefaultServiceEARProjectName();		
	}
}

Back to the top