2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
* OperationGenerator.java
45
* Created on September 6, 2006, 4:53 PM
47
* To change this template, choose Tools | Template Manager
48
* and open the template in the editor.
51
package org.netbeans.modules.xml.wsdl.ui.wizard;
53
import java.util.ArrayList;
54
import java.util.Collection;
55
import java.util.HashMap;
56
import java.util.Iterator;
57
import java.util.List;
59
import javax.xml.namespace.QName;
61
import org.netbeans.modules.xml.schema.model.GlobalElement;
62
import org.netbeans.modules.xml.schema.model.GlobalType;
63
import org.netbeans.modules.xml.schema.model.Import;
64
import org.netbeans.modules.xml.schema.model.Schema;
65
import org.netbeans.modules.xml.schema.model.SchemaModel;
66
import org.netbeans.modules.xml.wsdl.model.Definitions;
67
import org.netbeans.modules.xml.wsdl.model.Fault;
68
import org.netbeans.modules.xml.wsdl.model.Input;
69
import org.netbeans.modules.xml.wsdl.model.Message;
70
import org.netbeans.modules.xml.wsdl.model.Operation;
71
import org.netbeans.modules.xml.wsdl.model.Output;
72
import org.netbeans.modules.xml.wsdl.model.Part;
73
import org.netbeans.modules.xml.wsdl.model.PortType;
74
import org.netbeans.modules.xml.wsdl.model.Types;
75
import org.netbeans.modules.xml.wsdl.model.WSDLModel;
76
import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
77
import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
78
import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
79
import org.netbeans.modules.xml.wsdl.ui.view.ElementOrType;
80
import org.netbeans.modules.xml.wsdl.ui.view.OperationType;
81
import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel;
82
import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel.PartAndElementOrType;
83
import org.netbeans.modules.xml.wsdl.ui.wsdl.util.RelativePath;
84
import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
85
import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
86
import org.openide.filesystems.FileObject;
87
import org.openide.filesystems.FileUtil;
93
public class OperationGenerator implements Command {
95
private WSDLModel mModel;
97
private Map mConfigurationMap;
99
private PortType mParentPortType;
101
private Operation mOperation;
103
private List<Message> mNewMessageList = new ArrayList<Message>();
105
private Collection<Import> mImports = new ArrayList<Import>();
107
/** Creates a new instance of OperationGenerator */
108
public OperationGenerator(WSDLModel model, PortType parent, Map configurationMap) {
110
this.mParentPortType = parent;
111
this.mConfigurationMap = configurationMap;
114
public Operation getOperation() {
115
return this.mOperation;
118
public List<Message> getNewMessages() {
119
return this.mNewMessageList;
122
public Collection<Import> getImports() {
123
return this.mImports;
126
public void execute() {
129
String operationName = (String) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_NAME);
130
OperationType ot = (OperationType) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_TYPE);
132
this.mOperation = createOperation(ot, mModel);
133
this.mOperation.setName(operationName);
134
this.mParentPortType.addOperation(this.mOperation);
137
List<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts =
138
(List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT);
140
List<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts =
141
(List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT);
143
List<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts =
144
(List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT);
146
SchemaImportsGenerator schemaImportGenerator = new SchemaImportsGenerator(this.mModel, mConfigurationMap);
147
schemaImportGenerator.execute();
148
mImports.addAll(schemaImportGenerator.getImports());
150
String inputMessageName = (String) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT_MESSAGE);
151
String outputMessageName = (String) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT_MESSAGE);
152
String faultMessageName = (String) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT_MESSAGE);
154
processOperationType(ot,
166
private Operation createOperation(OperationType ot,
168
Operation operation = null;
169
if(ot.getOperationType().equals(OperationType.OPERATION_REQUEST_REPLY)) {
170
operation = model.getFactory().createRequestResponseOperation();
171
} else if(ot.getOperationType().equals(OperationType.OPERATION_ONE_WAY)) {
172
operation = model.getFactory().createOneWayOperation();
173
} else if (ot.getOperationType().equals(OperationType.OPERATION_SOLICIT_RESPONSE)) {
174
operation = model.getFactory().createSolicitResponseOperation();
175
} else if (ot.getOperationType().equals(OperationType.OPERATION_NOTIFICATION)) {
176
operation = model.getFactory().createNotificationOperation();
183
private void processOperationType(OperationType ot,
185
List<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts,
186
List<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts,
187
List<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts,
188
String inputMessageName,
189
String ouputMessageName,
190
String faultMessageName) {
191
Message inputMessage = null;
192
Message outputMessage = null;
193
Message faultMessage = null;
194
NamedComponentReference<Message> inMessageRef = null;
195
NamedComponentReference<Message> outMessageRef = null;
196
NamedComponentReference<Message> faultMessageRef = null;
198
//inputMessageName is provided by dialog not from wizard and this name could be name of
199
//new message which needs to be created or it could point to name of existing message.
200
if(inputMessageName != null) {
201
inputMessage = findMessage(inputMessageName);
203
//we are from wizard and inputMessageName is not provided
204
inputMessageName = NameGenerator.getInstance().generateUniqueInputMessageName(op.getName(), this.mModel);
208
//inputMessageParts are provided
209
if(inputMessage == null && inputMessageParts != null) {
210
//inputMessage is not an existing message then create and add new message
211
inputMessage = createAndAddMessage(inputMessageParts);
212
inputMessage.setName(inputMessageName);
215
if(inputMessage != null) {
216
inMessageRef = op.createReferenceTo(inputMessage, Message.class);
217
createAndAddInput(op, inMessageRef);
220
//ouputMessageName is provided by dialog not from wizard and this name could be name of
221
//new message which needs to be created or it could point to name of existing message.
222
if(ouputMessageName != null) {
223
outputMessage = findMessage(ouputMessageName);
225
//we are from wizard and ouputMessageName is not provided
226
ouputMessageName = NameGenerator.getInstance().generateUniqueOutputMessageName(op.getName(), this.mModel);
229
if(outputMessage == null && outputMessageParts != null) {
231
//ouputMessageName is not an existing message then create and add new message
232
outputMessage = createAndAddMessage(outputMessageParts);
233
outputMessage.setName(ouputMessageName);
236
if(outputMessage != null) {
237
outMessageRef = op.createReferenceTo(outputMessage, Message.class);
238
createAndAddOuput(op, outMessageRef);
241
//faultMessageName is provided by dialog not from wizard and this name could be name of
242
//new message which needs to be created or it could point to name of existing message.
243
if(faultMessageName != null) {
244
faultMessage = findMessage(faultMessageName);
246
faultMessageName = NameGenerator.getInstance().generateUniqueFaultMessageName(op.getName(), this.mModel);
249
//if faultMessage is null meaning did not find existing message so we must create a new message
250
if(faultMessage == null && faultMessageParts != null && faultMessageParts.size() > 0) {
251
//for fault we create them only if user specifies atleast one part
252
faultMessage = createAndAddMessage(faultMessageParts);
253
faultMessage.setName(faultMessageName);
257
if(faultMessage != null) {
258
faultMessageRef = op.createReferenceTo(faultMessage, Message.class);
259
createAndAddFault(op, faultMessageRef);
264
private Input createAndAddInput(Operation op, NamedComponentReference<Message> messageRef) {
265
Input in = this.mModel.getFactory().createInput();
267
String operationInputName = NameGenerator.getInstance().generateUniqueOperationInputName(op);
268
in.setName(operationInputName);
270
if(messageRef != null) {
271
in.setMessage(messageRef);
277
private Output createAndAddOuput(Operation op, NamedComponentReference<Message> messageRef) {
278
Output out = this.mModel.getFactory().createOutput();
280
String operationOutputName = NameGenerator.getInstance().generateUniqueOperationOutputName(op);
281
out.setName(operationOutputName);
283
if(messageRef != null) {
284
out.setMessage(messageRef);
290
private Fault createAndAddFault(Operation op, NamedComponentReference<Message> messageRef) {
291
Fault fault = this.mModel.getFactory().createFault();
293
String operationFaultName = NameGenerator.getInstance().generateUniqueOperationFaultName(op);
294
fault.setName(operationFaultName);
296
if(messageRef != null) {
297
fault.setMessage(messageRef);
303
private Message createAndAddMessage(List<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts) {
304
Message msg = this.mModel.getFactory().createMessage();
305
this.mModel.getDefinitions().addMessage(msg);
306
mNewMessageList.add(msg);
308
if(inputMessageParts != null) {
309
Iterator<PartAndElementOrTypeTableModel.PartAndElementOrType> it = inputMessageParts.iterator();
310
while(it.hasNext()) {
311
PartAndElementOrTypeTableModel.PartAndElementOrType partOrElement = it.next();
312
String partName = partOrElement.getPartName();
313
ElementOrType elementOrType = partOrElement.getElementOrType();
315
Part part = this.mModel.getFactory().createPart();
316
part.setName(partName);
318
if(elementOrType != null) {
319
GlobalElement element = elementOrType.getElement();
320
GlobalType type = elementOrType.getType();
321
if(element != null) {
322
NamedComponentReference<GlobalElement> elementRef = part.createSchemaReference(element, GlobalElement.class);
323
if(elementRef != null) {
324
part.setElement(elementRef);
326
} else if(type != null) {
327
NamedComponentReference<GlobalType> typeRef = part.createSchemaReference(type, GlobalType.class);
328
if(typeRef != null) {
329
part.setType(typeRef);
339
private Message findMessage(String messageName) {
340
QName qName = constructQName(messageName);
342
return this.mModel.findComponentByName(qName, Message.class);
348
private QName constructQName(String name) {
354
int prefixIndex = name.lastIndexOf(":");
356
String namespace = null;
357
String localPart = null;
358
if(prefixIndex != -1) {
359
prefix = name.substring(0, prefixIndex);
360
localPart = name.substring(prefixIndex + 1);
361
namespace = ((AbstractDocumentComponent) this.mModel.getDefinitions()).lookupNamespaceURI(prefix);
364
namespace = this.mModel.getDefinitions().getTargetNamespace();
367
qName = new QName(namespace, localPart, prefix);