KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > wizard > OperationGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * OperationGenerator.java
22  *
23  * Created on September 6, 2006, 4:53 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.wizard;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import javax.xml.namespace.QName JavaDoc;
38
39 import org.netbeans.modules.xml.schema.model.GlobalElement;
40 import org.netbeans.modules.xml.schema.model.GlobalType;
41 import org.netbeans.modules.xml.schema.model.Import;
42 import org.netbeans.modules.xml.schema.model.Schema;
43 import org.netbeans.modules.xml.schema.model.SchemaModel;
44 import org.netbeans.modules.xml.wsdl.model.Definitions;
45 import org.netbeans.modules.xml.wsdl.model.Fault;
46 import org.netbeans.modules.xml.wsdl.model.Input;
47 import org.netbeans.modules.xml.wsdl.model.Message;
48 import org.netbeans.modules.xml.wsdl.model.Operation;
49 import org.netbeans.modules.xml.wsdl.model.Output;
50 import org.netbeans.modules.xml.wsdl.model.Part;
51 import org.netbeans.modules.xml.wsdl.model.PortType;
52 import org.netbeans.modules.xml.wsdl.model.Types;
53 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
54 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
55 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
56 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
57 import org.netbeans.modules.xml.wsdl.ui.view.ElementOrType;
58 import org.netbeans.modules.xml.wsdl.ui.view.OperationType;
59 import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel;
60 import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel.PartAndElementOrType;
61 import org.netbeans.modules.xml.wsdl.ui.wsdl.util.RelativePath;
62 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
63 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
64 import org.openide.filesystems.FileObject;
65 import org.openide.filesystems.FileUtil;
66
67 /**
68  *
69  * @author radval
70  */

71 public class OperationGenerator implements Command {
72     
73     private WSDLModel mModel;
74     
75     private Map JavaDoc mConfigurationMap;
76     
77     private PortType mParentPortType;
78     
79     private Operation mOperation;
80     
81     private List JavaDoc<Message> mNewMessageList = new ArrayList JavaDoc<Message>();
82     
83     private Collection JavaDoc<Import> mImports = new ArrayList JavaDoc<Import>();
84     
85     /** Creates a new instance of OperationGenerator */
86     public OperationGenerator(WSDLModel model, PortType parent, Map JavaDoc configurationMap) {
87         this.mModel = model;
88         this.mParentPortType = parent;
89         this.mConfigurationMap = configurationMap;
90     }
91     
92     public Operation getOperation() {
93         return this.mOperation;
94     }
95     
96     public List JavaDoc<Message> getNewMessages() {
97         return this.mNewMessageList;
98     }
99     
100     public Collection JavaDoc<Import> getImports() {
101         return this.mImports;
102     }
103     
104     public void execute() {
105         if(mModel != null) {
106             //operation
107
String JavaDoc operationName = (String JavaDoc) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_NAME);
108             OperationType ot = (OperationType) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_TYPE);
109             
110             this.mOperation = createOperation(ot, mModel);
111             this.mOperation.setName(operationName);
112             this.mParentPortType.addOperation(this.mOperation);
113             
114             //opertion type
115
List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts =
116                     (List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT);
117             
118             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts =
119                     (List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT);
120             
121             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts =
122                     (List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT);
123
124             SchemaImportsGenerator schemaImportGenerator = new SchemaImportsGenerator(this.mModel, mConfigurationMap);
125             schemaImportGenerator.execute();
126             mImports.addAll(schemaImportGenerator.getImports());
127             
128             String JavaDoc inputMessageName = (String JavaDoc) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT_MESSAGE);
129             String JavaDoc outputMessageName = (String JavaDoc) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT_MESSAGE);
130             String JavaDoc faultMessageName = (String JavaDoc) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_FAULT_MESSAGE);
131                     
132             processOperationType(ot,
133                                 this.mOperation,
134                                 inputMessageParts,
135                                 outputMessageParts,
136                                 faultMessageParts,
137                                 inputMessageName,
138                                 outputMessageName,
139                                 faultMessageName);
140         }
141        
142     }
143     
144     private Operation createOperation(OperationType ot,
145                                           WSDLModel model) {
146         Operation operation = null;
147         if(ot.getOperationType().equals(OperationType.OPERATION_REQUEST_REPLY)) {
148             operation = model.getFactory().createRequestResponseOperation();
149         } else if(ot.getOperationType().equals(OperationType.OPERATION_ONE_WAY)) {
150             operation = model.getFactory().createOneWayOperation();
151         } else if (ot.getOperationType().equals(OperationType.OPERATION_SOLICIT_RESPONSE)) {
152             operation = model.getFactory().createSolicitResponseOperation();
153         } else if (ot.getOperationType().equals(OperationType.OPERATION_NOTIFICATION)) {
154             operation = model.getFactory().createNotificationOperation();
155         }
156
157         return operation;
158     }
159     
160     
161     private void processOperationType(OperationType ot,
162                                       Operation op,
163                                       List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts,
164                                       List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts,
165                                       List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts,
166                                       String JavaDoc inputMessageName,
167                                       String JavaDoc ouputMessageName,
168                                       String JavaDoc faultMessageName) {
169         Message inputMessage = null;
170         Message outputMessage = null;
171         Message faultMessage = null;
172         NamedComponentReference<Message> inMessageRef = null;
173         NamedComponentReference<Message> outMessageRef = null;
174         NamedComponentReference<Message> faultMessageRef = null;
175         
176         //inputMessageName is provided by dialog not from wizard and this name could be name of
177
//new message which needs to be created or it could point to name of existing message.
178
if(inputMessageName != null) {
179             inputMessage = findMessage(inputMessageName);
180         } else {
181             //we are from wizard and inputMessageName is not provided
182
inputMessageName = NameGenerator.getInstance().generateUniqueInputMessageName(op.getName(), this.mModel);
183         }
184
185         
186         //inputMessageParts are provided
187
if(inputMessage == null && inputMessageParts != null) {
188             //inputMessage is not an existing message then create and add new message
189
inputMessage = createAndAddMessage(inputMessageParts);
190             inputMessage.setName(inputMessageName);
191         }
192         
193         if(inputMessage != null) {
194             inMessageRef = op.createReferenceTo(inputMessage, Message.class);
195             createAndAddInput(op, inMessageRef);
196         }
197
198         //ouputMessageName 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(ouputMessageName != null) {
201            outputMessage = findMessage(ouputMessageName);
202         } else {
203             //we are from wizard and ouputMessageName is not provided
204
ouputMessageName = NameGenerator.getInstance().generateUniqueOutputMessageName(op.getName(), this.mModel);
205         }
206         
207         if(outputMessage == null && outputMessageParts != null) {
208             
209             //ouputMessageName is not an existing message then create and add new message
210
outputMessage = createAndAddMessage(outputMessageParts);
211             outputMessage.setName(ouputMessageName);
212         }
213         
214         if(outputMessage != null) {
215             outMessageRef = op.createReferenceTo(outputMessage, Message.class);
216             createAndAddOuput(op, outMessageRef);
217         }
218         
219         //faultMessageName is provided by dialog not from wizard and this name could be name of
220
//new message which needs to be created or it could point to name of existing message.
221
if(faultMessageName != null) {
222             faultMessage = findMessage(faultMessageName);
223         } else {
224             faultMessageName = NameGenerator.getInstance().generateUniqueFaultMessageName(op.getName(), this.mModel);
225         }
226         
227         //if faultMessage is null meaning did not find existing message so we must create a new message
228
if(faultMessage == null && faultMessageParts != null && faultMessageParts.size() > 0) {
229             //for fault we create them only if user specifies atleast one part
230
faultMessage = createAndAddMessage(faultMessageParts);
231             faultMessage.setName(faultMessageName);
232             
233         }
234         
235         if(faultMessage != null) {
236             faultMessageRef = op.createReferenceTo(faultMessage, Message.class);
237             createAndAddFault(op, faultMessageRef);
238         }
239         
240     }
241     
242     private Input createAndAddInput(Operation op, NamedComponentReference<Message> messageRef) {
243         Input in = this.mModel.getFactory().createInput();
244         
245         String JavaDoc operationInputName = NameGenerator.getInstance().generateUniqueOperationInputName(op);
246         in.setName(operationInputName);
247         op.setInput(in);
248         if(messageRef != null) {
249             in.setMessage(messageRef);
250         }
251         
252         return in;
253     }
254     
255     private Output createAndAddOuput(Operation op, NamedComponentReference<Message> messageRef) {
256         Output out = this.mModel.getFactory().createOutput();
257         
258         String JavaDoc operationOutputName = NameGenerator.getInstance().generateUniqueOperationOutputName(op);
259         out.setName(operationOutputName);
260         op.setOutput(out);
261         if(messageRef != null) {
262             out.setMessage(messageRef);
263         }
264         
265         return out;
266     }
267     
268     private Fault createAndAddFault(Operation op, NamedComponentReference<Message> messageRef) {
269         Fault fault = this.mModel.getFactory().createFault();
270         
271         String JavaDoc operationFaultName = NameGenerator.getInstance().generateUniqueOperationFaultName(op);
272         fault.setName(operationFaultName);
273         op.addFault(fault);
274         if(messageRef != null) {
275             fault.setMessage(messageRef);
276         }
277         
278         return fault;
279     }
280     
281     private Message createAndAddMessage(List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts) {
282         Message msg = this.mModel.getFactory().createMessage();
283         
284         if(inputMessageParts != null) {
285             Iterator JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> it = inputMessageParts.iterator();
286             while(it.hasNext()) {
287                 PartAndElementOrTypeTableModel.PartAndElementOrType partOrElement = it.next();
288                 String JavaDoc partName = partOrElement.getPartName();
289                 ElementOrType elementOrType = partOrElement.getElementOrType();
290
291                 Part part = this.mModel.getFactory().createPart();
292                 part.setName(partName);
293                 msg.addPart(part);
294                 if(elementOrType != null) {
295                     GlobalElement element = elementOrType.getElement();
296                     GlobalType type = elementOrType.getType();
297                     if(element != null) {
298                         NamedComponentReference<GlobalElement> elementRef = part.createSchemaReference(element, GlobalElement.class);
299                         if(elementRef != null) {
300                             part.setElement(elementRef);
301                         }
302                     } else if(type != null) {
303                         NamedComponentReference<GlobalType> typeRef = part.createSchemaReference(type, GlobalType.class);
304                         if(typeRef != null) {
305                             part.setType(typeRef);
306                         }
307                     }
308
309                 }
310             }
311         }
312         this.mModel.getDefinitions().addMessage(msg);
313         mNewMessageList.add(msg);
314         return msg;
315     }
316     
317     private Message findMessage(String JavaDoc messageName) {
318         QName JavaDoc qName = constructQName(messageName);
319         if(qName != null) {
320             return this.mModel.findComponentByName(qName, Message.class);
321         }
322         
323         return null;
324     }
325     
326     private QName JavaDoc constructQName(String JavaDoc name) {
327         if(name == null) {
328             return null;
329         }
330         
331         QName JavaDoc qName = null;
332         int prefixIndex = name.lastIndexOf(":");
333         String JavaDoc prefix = "";
334         String JavaDoc namespace = null;
335         String JavaDoc localPart = null;
336         if(prefixIndex != -1) {
337             prefix = name.substring(0, prefixIndex);
338             localPart = name.substring(prefixIndex + 1);
339             namespace = ((AbstractDocumentComponent) this.mModel.getDefinitions()).lookupNamespaceURI(prefix);
340         } else {
341             localPart = name;
342             namespace = this.mModel.getDefinitions().getTargetNamespace();
343         }
344         
345         qName = new QName JavaDoc(namespace, localPart, prefix);
346         
347         return qName;
348     }
349 }
350
Popular Tags