Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4bb41e97a561bc0478ba0a492fcc012fdbaad7af (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Copyright (c) 2000, 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.consumption.sampleapp.command;

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.java.JavaClass;
import org.eclipse.jst.ws.internal.consumption.codegen.javamofvisitoractions.JavaMofBeanVisitorAction;
import org.eclipse.jst.ws.internal.consumption.codegen.javamofvisitors.JavaMofBeanVisitor;
import org.eclipse.jst.ws.internal.consumption.command.common.JavaMofReflectionCommand;
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.command.internal.provisional.env.core.selection.BooleanSelection;
import org.eclipse.wst.common.environment.Choice;
import org.eclipse.wst.common.environment.Environment;
import org.eclipse.wst.ws.internal.datamodel.Element;
import org.eclipse.wst.ws.internal.datamodel.Model;

/**
* This is the base class for commands that need to report progress
* and status during and after their execution. This class extends
* {@link org.eclipse.emf.common.command.AbstractCommand AbstractCommand}

  // Copyright
  public static final String copyright = "(c) Copyright IBM Corporation 2000, 2002.";

* with methods to {@link #setProgressMonitor set}
* and {@link #getProgressMonitor get} a progress monitor,
* and to get a status object {@link #getReadyStatus before}
* or {@link #getResultStatus after} execution.
* <p>
* Note that the responsibility of providing an
* {@link org.eclipse.core.runtime.IProgressMonitor IProgressMonitor}
* rests with frameworks that construct or run ProgressCommand objects,
* whereas the responsibility of providing an
* {@link org.eclipse.core.runtime.IStatus IStatus}
* object lies with the subclasses of ProgressCommand.
* Subclasses must follow the rules described for
* {@link org.eclipse.emf.common.command.AbstractCommand AbstractCommand}.
*/
public class JavaToModelCommand extends AbstractDataModelOperation
{
  private MessageUtils msgUtils;
  private String clientProject;
  private BooleanSelection[] methods;
  private String proxyBean;
  private JavaClass javaClass;
  private Model model;
  private Element parentElement;
  
  public JavaToModelCommand ()
  {
	String pluginId = "org.eclipse.jst.ws.consumption";
	msgUtils = new MessageUtils(pluginId + ".plugin", this);  	
  }

  
  private IStatus createJavaReflection(Environment env, IProgressMonitor monitor )
  {
  	IStatus status = Status.OK_STATUS;
    JavaMofReflectionCommand javaMofReflectionCommand = new JavaMofReflectionCommand();
    javaMofReflectionCommand.setClientProject(clientProject);
    javaMofReflectionCommand.setProxyBean(proxyBean);
    javaMofReflectionCommand.setEnvironment( env );
    //javaMofReflectionCommand.setStatusMonitor(getStatusMonitor());
    status = javaMofReflectionCommand.execute( monitor, null);
    javaClass = (JavaClass)javaMofReflectionCommand.getJavaClass();
    return status;
  }

  /**
  * The Model that was created from this javamof
  * @return Model The data model that was created
  **/
  public Model getJavaDataModel()
  {
    return model;
  }

  /**
  * Build the datamodel from the mof
  */
  public IStatus buildModelFromMof (Environment env) throws CoreException
  {
  	
  	Choice OKChoice = new Choice('O', msgUtils.getMessage("LABEL_OK"), msgUtils.getMessage("DESCRIPTION_OK"));
  	Choice CancelChoice = new Choice('C', msgUtils.getMessage("LABEL_CANCEL"), msgUtils.getMessage("DESCRIPTION_CANCEL"));
  	
    // we could have one of three cases:
    //1. The model is null meaning we want and the parent element is null, meaning we want to
    //   create a brand new model and make this bean its root
    //2. The model is null but the parentElement is not, meaning we want to add the Bean to the
    //   given parent element
    //3. The model is not null however the parentElement is, meaning we want to add this Bean to
    //   This model but dont attach it to anything
      IStatus status = Status.OK_STATUS;

      if(model == null && parentElement == null){
         JavaMofBeanVisitorAction beanVisitorAction = new JavaMofBeanVisitorAction(clientProject,methods, env);
         //beanVisitorAction.setStatusMonitor(getStatusMonitor());
	     JavaMofBeanVisitor beanVisitor = new JavaMofBeanVisitor();
         status = beanVisitor.run(javaClass,beanVisitorAction);
         //
         int severity = status.getSeverity(); 
         if (severity==Status.ERROR)
         	return status;
         
         if (severity==Status.WARNING)
         {
           Choice result = env.getStatusHandler().report(status, new Choice[]{OKChoice, CancelChoice});
           if (result.getLabel().equals(CancelChoice.getLabel()))
           {
           	 //return an error status since the user canceled
           	  return StatusUtils.errorStatus( msgUtils.getMessage("MSG_ERROR_SAMPLE_CREATION_CANCELED") );
           }
           	
         }
         //
         model = beanVisitorAction.getModel();

      }
      else if (model == null && parentElement != null){
         JavaMofBeanVisitorAction beanVisitorAction = new JavaMofBeanVisitorAction(parentElement,clientProject, env);
         //beanVisitorAction.setStatusMonitor(getStatusMonitor());
	     JavaMofBeanVisitor beanVisitor = new JavaMofBeanVisitor();
         status = beanVisitor.run(javaClass,beanVisitorAction);
         //
         int severity = status.getSeverity(); 
         if (severity==Status.ERROR)
         	return status;
         
         if (severity==Status.WARNING)
         {
           Choice result = env.getStatusHandler().report(status, new Choice[]{OKChoice, CancelChoice});
           if (result.getLabel().equals(CancelChoice.getLabel()))
           {
           	 //return an error status since the user canceled
           	  return StatusUtils.errorStatus( msgUtils.getMessage("MSG_ERROR_SAMPLE_CREATION_CANCELED") );
           }
           	
         }
         //         
         model = beanVisitorAction.getModel();
      }
      else {
         JavaMofBeanVisitorAction beanVisitorAction = new JavaMofBeanVisitorAction(model,clientProject, env);
         //beanVisitorAction.setStatusMonitor(getStatusMonitor());
	     JavaMofBeanVisitor beanVisitor = new JavaMofBeanVisitor();
         status = beanVisitor.run(javaClass,beanVisitorAction);
         //
         int severity = status.getSeverity(); 
         if (severity==Status.ERROR)
         	return status;
         
         if (severity==Status.WARNING)
         {
           Choice result = env.getStatusHandler().report(status, new Choice[]{OKChoice, CancelChoice});
           if (result.getLabel().equals(CancelChoice.getLabel()))
           {
           	 //return an error status since the user canceled
           	  return StatusUtils.errorStatus( msgUtils.getMessage("MSG_ERROR_SAMPLE_CREATION_CANCELED") );
           }
           	
         }
         //         
         model = beanVisitorAction.getModel();
      }

      return status;
  }

  /**
  * Get the java model from the resource then
  * build the model from the mof
  */
  public IStatus execute( IProgressMonitor monitor, IAdaptable adaptable )
  {    
    Environment env = getEnvironment();
  	IStatus status = Status.OK_STATUS;
    if(clientProject == null) return status;
    
  	status = createJavaReflection(env, monitor);
    if (status.getSeverity()==Status.ERROR) return status;
    try{
      status = buildModelFromMof(env);
      return status;
    }catch(CoreException exc){
      IStatus embeddedStatus = exc.getStatus();
      status = embeddedStatus;
      return status;
    }

  }
    
  public void setMethods(BooleanSelection[] methods)
  {
  	this.methods =  methods;
  }
  
  public void setClientProject(String clientProject)
  {
  	this.clientProject = clientProject;
  }
  
  public void setParentElement(Element parentElement)
  {
  	this.parentElement = parentElement;
  }
     
  public void setProxyBean(String proxyBean)
  {
  	this.proxyBean = proxyBean;
  }

 }

Back to the top