~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdlui/src/org/netbeans/modules/xml/wsdl/ui/wizard/OperationGenerator.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
/*
 
43
 * OperationGenerator.java
 
44
 *
 
45
 * Created on September 6, 2006, 4:53 PM
 
46
 *
 
47
 * To change this template, choose Tools | Template Manager
 
48
 * and open the template in the editor.
 
49
 */
 
50
 
 
51
package org.netbeans.modules.xml.wsdl.ui.wizard;
 
52
 
 
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;
 
58
import java.util.Map;
 
59
import javax.xml.namespace.QName;
 
60
 
 
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;
 
88
 
 
89
/**
 
90
 *
 
91
 * @author radval
 
92
 */
 
93
public class OperationGenerator implements Command {
 
94
    
 
95
    private WSDLModel mModel;
 
96
    
 
97
    private Map mConfigurationMap;
 
98
    
 
99
    private PortType mParentPortType;
 
100
    
 
101
    private Operation mOperation;
 
102
    
 
103
    private List<Message> mNewMessageList = new ArrayList<Message>();
 
104
    
 
105
    private Collection<Import> mImports = new ArrayList<Import>();
 
106
    
 
107
    /** Creates a new instance of OperationGenerator */
 
108
    public OperationGenerator(WSDLModel model, PortType parent, Map configurationMap) {
 
109
        this.mModel = model;
 
110
        this.mParentPortType = parent;
 
111
        this.mConfigurationMap = configurationMap;        
 
112
    }
 
113
    
 
114
    public Operation getOperation() {
 
115
        return this.mOperation;
 
116
    }
 
117
    
 
118
    public List<Message> getNewMessages() {
 
119
        return this.mNewMessageList;
 
120
    }
 
121
    
 
122
    public Collection<Import> getImports() {
 
123
        return this.mImports;
 
124
    }
 
125
    
 
126
    public void execute() {
 
127
        if(mModel != null) {
 
128
            //operation
 
129
            String operationName = (String) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_NAME);
 
130
            OperationType ot = (OperationType) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_TYPE);
 
131
            
 
132
            this.mOperation = createOperation(ot, mModel);
 
133
            this.mOperation.setName(operationName);
 
134
            this.mParentPortType.addOperation(this.mOperation);
 
135
            
 
136
            //opertion type
 
137
            List<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = 
 
138
                    (List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT);
 
139
            
 
140
            List<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = 
 
141
                    (List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT);
 
142
            
 
143
            List<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts = 
 
144
                    (List<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT);
 
145
 
 
146
            SchemaImportsGenerator schemaImportGenerator = new SchemaImportsGenerator(this.mModel, mConfigurationMap);
 
147
            schemaImportGenerator.execute();
 
148
            mImports.addAll(schemaImportGenerator.getImports());
 
149
            
 
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);
 
153
                    
 
154
            processOperationType(ot, 
 
155
                                this.mOperation, 
 
156
                                inputMessageParts, 
 
157
                                outputMessageParts, 
 
158
                                faultMessageParts,
 
159
                                inputMessageName,
 
160
                                outputMessageName,
 
161
                                faultMessageName);
 
162
        }
 
163
       
 
164
    }
 
165
    
 
166
    private Operation createOperation(OperationType ot, 
 
167
                                          WSDLModel model) {
 
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();
 
177
        }
 
178
 
 
179
        return operation;
 
180
    }
 
181
    
 
182
    
 
183
    private void processOperationType(OperationType ot, 
 
184
                                      Operation op,
 
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;
 
197
        
 
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);
 
202
        } else {
 
203
            //we are from wizard and inputMessageName is not provided
 
204
            inputMessageName = NameGenerator.getInstance().generateUniqueInputMessageName(op.getName(), this.mModel);
 
205
        }
 
206
 
 
207
        
 
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);
 
213
        }
 
214
        
 
215
        if(inputMessage != null) {
 
216
            inMessageRef = op.createReferenceTo(inputMessage, Message.class);
 
217
            createAndAddInput(op, inMessageRef); 
 
218
        }
 
219
 
 
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);
 
224
        } else {
 
225
            //we are from wizard and ouputMessageName is not provided
 
226
            ouputMessageName = NameGenerator.getInstance().generateUniqueOutputMessageName(op.getName(), this.mModel);
 
227
        }
 
228
        
 
229
        if(outputMessage == null && outputMessageParts != null) {
 
230
            
 
231
            //ouputMessageName is not an existing message then create and add new message
 
232
            outputMessage = createAndAddMessage(outputMessageParts);
 
233
            outputMessage.setName(ouputMessageName);
 
234
        }
 
235
        
 
236
        if(outputMessage != null) {
 
237
            outMessageRef = op.createReferenceTo(outputMessage, Message.class);
 
238
            createAndAddOuput(op, outMessageRef);
 
239
        }
 
240
        
 
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);
 
245
        } else {
 
246
            faultMessageName = NameGenerator.getInstance().generateUniqueFaultMessageName(op.getName(), this.mModel);
 
247
        } 
 
248
        
 
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);
 
254
            
 
255
        }
 
256
        
 
257
        if(faultMessage != null) {
 
258
            faultMessageRef = op.createReferenceTo(faultMessage, Message.class);
 
259
            createAndAddFault(op, faultMessageRef);    
 
260
        }
 
261
        
 
262
    }
 
263
    
 
264
    private Input createAndAddInput(Operation op, NamedComponentReference<Message> messageRef) {
 
265
        Input in = this.mModel.getFactory().createInput();
 
266
        
 
267
        String operationInputName = NameGenerator.getInstance().generateUniqueOperationInputName(op);
 
268
        in.setName(operationInputName);
 
269
        op.setInput(in);
 
270
        if(messageRef != null) {
 
271
            in.setMessage(messageRef);
 
272
        }
 
273
        
 
274
        return in;
 
275
    }
 
276
    
 
277
    private Output createAndAddOuput(Operation op, NamedComponentReference<Message> messageRef) {
 
278
        Output out = this.mModel.getFactory().createOutput();
 
279
        
 
280
        String operationOutputName = NameGenerator.getInstance().generateUniqueOperationOutputName(op);
 
281
        out.setName(operationOutputName);
 
282
        op.setOutput(out);
 
283
        if(messageRef != null) {
 
284
            out.setMessage(messageRef);
 
285
        }
 
286
        
 
287
        return out;
 
288
    }
 
289
    
 
290
    private Fault createAndAddFault(Operation op, NamedComponentReference<Message> messageRef) {
 
291
        Fault fault = this.mModel.getFactory().createFault();
 
292
        
 
293
        String operationFaultName = NameGenerator.getInstance().generateUniqueOperationFaultName(op);
 
294
        fault.setName(operationFaultName);
 
295
        op.addFault(fault);
 
296
        if(messageRef != null) {
 
297
            fault.setMessage(messageRef);
 
298
        }
 
299
        
 
300
        return fault;
 
301
    }
 
302
    
 
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);
 
307
        
 
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();
 
314
 
 
315
                Part part = this.mModel.getFactory().createPart();
 
316
                part.setName(partName);
 
317
                msg.addPart(part);
 
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);
 
325
                        }
 
326
                    } else if(type != null) {
 
327
                        NamedComponentReference<GlobalType> typeRef =  part.createSchemaReference(type, GlobalType.class);
 
328
                        if(typeRef != null) {
 
329
                            part.setType(typeRef);
 
330
                        }
 
331
                    }
 
332
 
 
333
                }
 
334
            }
 
335
        }
 
336
        return msg;
 
337
    }
 
338
    
 
339
    private Message findMessage(String messageName) {
 
340
        QName qName = constructQName(messageName);
 
341
        if(qName != null) {
 
342
            return this.mModel.findComponentByName(qName, Message.class);
 
343
        }
 
344
        
 
345
        return null;
 
346
    }
 
347
    
 
348
    private QName constructQName(String name) {
 
349
        if(name == null) {
 
350
            return null;
 
351
        }
 
352
        
 
353
        QName qName = null;
 
354
        int prefixIndex = name.lastIndexOf(":");
 
355
        String prefix = "";
 
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);
 
362
        } else {
 
363
            localPart = name;
 
364
            namespace = this.mModel.getDefinitions().getTargetNamespace();
 
365
        }
 
366
        
 
367
        qName = new QName(namespace, localPart, prefix);
 
368
        
 
369
        return qName;        
 
370
    }
 
371
}