Skip to main content
summaryrefslogtreecommitdiffstats
blob: d595de05c250ac7e0942d97262e9b9f6eb0ce8e5 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060222   128094 joan@ca.ibm.com - Joan Haggarty
 * 20070305   175194 makandre@ca.ibm.com - Andrew Mak, Ant task InitialSelection only allows wsdl within the workspace
 *******************************************************************************/
package org.eclipse.jst.ws.internal.common;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jem.java.JavaClass;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jst.j2ee.ejb.EJBJar;
import org.eclipse.jst.j2ee.ejb.EJBResource;
import org.eclipse.jst.j2ee.ejb.EnterpriseBean;
import org.eclipse.jst.j2ee.ejb.componentcore.util.EJBArtifactEdit;
import org.eclipse.jst.ws.internal.WSPluginMessages;
import org.eclipse.wst.command.internal.env.common.FileResourceUtils;
import org.eclipse.wst.command.internal.env.core.data.Transformer;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;

/**
 * Transforms a string value representing a workspace resource path into a StructuredSelection object
 * 
 * @author joan
 *
 */

public class String2SelectionTransformer implements Transformer {

	public Object transform(Object value) {
		
		StructuredSelection selection = null;
		IResource resource = FileResourceUtils.getWorkspaceRoot().findMember(new Path(value.toString()));
		if (resource != null)
		{
			IProject resProject = resource.getProject();			
			// check if resource is part of an EJB
			if (J2EEUtils.isEJBComponent(resProject))
			{
				// if resource is part of EJB need to get the EnterpriseBean as the selection
				IVirtualComponent[] ejbComponents = J2EEUtils.getEJBComponents(resProject);
				EJBArtifactEdit  ejbEdit = null;			
				try{				
					ejbEdit = EJBArtifactEdit.getEJBArtifactEditForRead(ejbComponents[0]);				
		            EJBResource ejbRes = ejbEdit.getEJBJarXmiResource();
		            EJBJar ejbJar = ejbRes.getEJBJar();	            
		            IPath path = resource.getFullPath();
		            String resourcePath = ResourceUtils.getJavaResourcePackageName(path);
		            String javaClassName = resource.getName();
		            // strip off file extension if necessary
		            if (javaClassName.lastIndexOf('.')>-1)
		            {	
		              javaClassName = javaClassName.substring(0, javaClassName.lastIndexOf('.'));	              
		            } 
		            JavaClass javaClass = JavaMOFUtils.getJavaClass(resourcePath, javaClassName, resProject);
		            EnterpriseBean ejb = ejbJar.getEnterpriseBeanWithReference(javaClass);
		            if (ejb != null)
		            {
		            	selection = new StructuredSelection(ejb.getName());	
		            }
				}
				catch (Exception exc)
				{
					
				}
				finally {
		              if (ejbEdit!=null)
		                ejbEdit.dispose();
		        }
			}
			// if selection not already set - create it from the resource object
			if (selection == null)
			{
			  selection = new StructuredSelection(resource);
			}
		}
		else if (value instanceof String && value.toString().indexOf(":") != -1) {
			// remote file
			selection = new StructuredSelection(value);
		}
		else
		{
			// string doesn't return resource - error condition
			throw new IllegalArgumentException(WSPluginMessages.ERROR_SELECTION_TRANSFORM);
		}
		return selection;
	}

}

Back to the top