Skip to main content
summaryrefslogtreecommitdiffstats
blob: 684c41cedb3829584c6cf6d79883953ca4eba36f (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
/*******************************************************************************
 * Copyright (c) 2004 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
 *******************************************************************************/
package org.eclipse.jst.ws.internal.axis.creation.ui.widgets.bean;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jem.internal.plugin.JavaEMFNature;
import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.java.JavaRefFactory;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.ws.internal.common.ResourceUtils;
import org.eclipse.jst.ws.internal.consumption.common.JavaResourceFilter;
import org.eclipse.wst.command.internal.provisional.env.core.AbstractDataModelOperation;
import org.eclipse.wst.command.internal.provisional.env.core.common.MessageUtils;
import org.eclipse.wst.command.internal.provisional.env.core.common.StatusUtils;
import org.eclipse.wst.common.environment.Environment;


public class ValidateObjectSelectionCommand extends AbstractDataModelOperation
{
  private String JAVA_EXTENSION = ".java"; //$NON-NLS-1$
  private String CLASS_EXTENSION = ".class"; //$NON-NLS-1$
  private MessageUtils msgUtils_;
  private IStructuredSelection objectSelection;
  private String serviceProjectName;

  
  /**
   * Validates that the selected Java bean can be loaded from the service project. If it can't, an error is displayed.
   */
  public ValidateObjectSelectionCommand()
  {
    super();
    String pluginId = "org.eclipse.jst.ws.axis.creation.ui";
    msgUtils_ = new MessageUtils(pluginId + ".plugin", this);
  }
	public IStatus execute( IProgressMonitor monitor, IAdaptable adaptable ) 
	{
		Environment environment = getEnvironment();
    //Make sure a bean is selected
    String javaBeanName = null;
    if (objectSelection != null && !objectSelection.isEmpty())
    {
      Object object = objectSelection.getFirstElement();
      if (object instanceof String)
      {
        javaBeanName = ((String)object);
      }
      else
      {
      	//The ObjectSelectionWidget never appeared and so the IStucturedSelection from 
      	//ObjectSelectionWidgetOutputCommand is the initial selection. 
      	//Get the Java bean name from the selection.
      	try
		{
      	  System.out.println(object.getClass().toString());
      	  javaBeanName = getJavaBeanFromObjectSelection(objectSelection);
		} catch (CoreException ce)
		{
		  IStatus errorStatus = StatusUtils.errorStatus(msgUtils_.getMessage("MSG_ERROR_CANNOT_NO_JAVA_BEAN"));
		  environment.getStatusHandler().reportError(errorStatus);
		  return errorStatus;			
		}
      }
    }    
    
    if (javaBeanName==null || javaBeanName.length()==0)
    {
      IStatus errorStatus = StatusUtils.errorStatus(msgUtils_.getMessage("MSG_ERROR_CANNOT_NO_JAVA_BEAN"));
      environment.getStatusHandler().reportError(errorStatus);
      return errorStatus;
    }
    
    //Make sure a project is selected
    IProject serviceProject = ProjectUtilities.getProject(serviceProjectName);
    if (serviceProject==null)
    {
      IStatus errorStatus = StatusUtils.errorStatus(msgUtils_.getMessage("MSG_ERROR_NO_PROJECT"));
      environment.getStatusHandler().reportError(errorStatus);
      return errorStatus;      
    }
    
    //Make sure the selected bean can be loaded from the selected project
	if (javaBeanName.toLowerCase().endsWith(JAVA_EXTENSION)
		|| javaBeanName.toLowerCase().endsWith(CLASS_EXTENSION)) {
		javaBeanName = javaBeanName.substring(0, javaBeanName.lastIndexOf('.'));
	}
	
    try
    {
	  JavaEMFNature jMOF = (JavaEMFNature) JavaEMFNature.createRuntime(serviceProject);
	  JavaClass javaClass = (JavaClass)JavaRefFactory.eINSTANCE.reflectType(javaBeanName, jMOF.getResourceSet());
	  if (!javaClass.isExistingType()) 
	  {		
		IStatus errorStatus = StatusUtils.errorStatus( msgUtils_.getMessage("MSG_ERROR_CANNOT_LOAD_JAVA_BEAN", new String[] { javaBeanName, serviceProjectName }));
		environment.getStatusHandler().reportError(errorStatus);
		return errorStatus;
	  }
    } catch (CoreException ce)
    {
	  IStatus errorStatus = StatusUtils.errorStatus( msgUtils_.getMessage("MSG_ERROR_CANNOT_LOAD_JAVA_BEAN", new String[] { javaBeanName, serviceProjectName }));
	  environment.getStatusHandler().reportError(errorStatus);
      return errorStatus;      
    }
    
    return Status.OK_STATUS;
  }
  
  /**
   * @param serviceProjectName The serviceProjectName to set.
   */
  public void setServiceProjectName(String serviceProjectName)
  {
    this.serviceProjectName = serviceProjectName;
  }
  
  /**
   * @param objectSelection The objectSelection to set.
   */
  public void setObjectSelection(IStructuredSelection objectSelection)
  {
    this.objectSelection = objectSelection;
  }

  private String getJavaBeanFromObjectSelection(IStructuredSelection objectSelection) throws CoreException
  {
    String beanClass = "";
    JavaResourceFilter filter = new JavaResourceFilter(); 
    //IStructuredSelection selection = initialSelection_;
    //
    if (objectSelection != null)
    {
      Object obj = objectSelection.getFirstElement();
      if (obj != null)
      {
        // get the IResource represented by the selection
        IResource res = null;
        
        res = ResourceUtils.getResourceFromSelection(obj);
        
        if (filter.accepts(res))
        {
          // get the package-qualified class name without file extensions
          String beanPackage = ResourceUtils.getJavaResourcePackageName(res.getFullPath());
          if (beanPackage==null)
            beanPackage = "";
          else
            beanPackage = beanPackage + ".";

          beanClass = beanPackage + res.getName();

          if (beanClass.toLowerCase().endsWith(".java") || beanClass.toLowerCase().endsWith(".class")) {
            beanClass = beanClass.substring(0,beanClass.lastIndexOf('.'));
          }
        }
      }
    }
    
    //
    return beanClass;
  }    
}

Back to the top