1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui.wizard; 30 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import javax.xml.namespace.QName ; 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 71 public class OperationGenerator implements Command { 72 73 private WSDLModel mModel; 74 75 private Map mConfigurationMap; 76 77 private PortType mParentPortType; 78 79 private Operation mOperation; 80 81 private List <Message> mNewMessageList = new ArrayList <Message>(); 82 83 private Collection <Import> mImports = new ArrayList <Import>(); 84 85 86 public OperationGenerator(WSDLModel model, PortType parent, Map 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 <Message> getNewMessages() { 97 return this.mNewMessageList; 98 } 99 100 public Collection <Import> getImports() { 101 return this.mImports; 102 } 103 104 public void execute() { 105 if(mModel != null) { 106 String operationName = (String ) 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 List <PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = 116 (List <PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT); 117 118 List <PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = 119 (List <PartAndElementOrTypeTableModel.PartAndElementOrType>) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT); 120 121 List <PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts = 122 (List <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 inputMessageName = (String ) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_INPUT_MESSAGE); 129 String outputMessageName = (String ) this.mConfigurationMap.get(WizardPortTypeConfigurationStep.OPERATION_OUTPUT_MESSAGE); 130 String faultMessageName = (String ) 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 <PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts, 164 List <PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts, 165 List <PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts, 166 String inputMessageName, 167 String ouputMessageName, 168 String 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 if(inputMessageName != null) { 179 inputMessage = findMessage(inputMessageName); 180 } else { 181 inputMessageName = NameGenerator.getInstance().generateUniqueInputMessageName(op.getName(), this.mModel); 183 } 184 185 186 if(inputMessage == null && inputMessageParts != null) { 188 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 if(ouputMessageName != null) { 201 outputMessage = findMessage(ouputMessageName); 202 } else { 203 ouputMessageName = NameGenerator.getInstance().generateUniqueOutputMessageName(op.getName(), this.mModel); 205 } 206 207 if(outputMessage == null && outputMessageParts != null) { 208 209 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 if(faultMessageName != null) { 222 faultMessage = findMessage(faultMessageName); 223 } else { 224 faultMessageName = NameGenerator.getInstance().generateUniqueFaultMessageName(op.getName(), this.mModel); 225 } 226 227 if(faultMessage == null && faultMessageParts != null && faultMessageParts.size() > 0) { 229 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 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 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 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 <PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts) { 282 Message msg = this.mModel.getFactory().createMessage(); 283 284 if(inputMessageParts != null) { 285 Iterator <PartAndElementOrTypeTableModel.PartAndElementOrType> it = inputMessageParts.iterator(); 286 while(it.hasNext()) { 287 PartAndElementOrTypeTableModel.PartAndElementOrType partOrElement = it.next(); 288 String 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 messageName) { 318 QName 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 constructQName(String name) { 327 if(name == null) { 328 return null; 329 } 330 331 QName qName = null; 332 int prefixIndex = name.lastIndexOf(":"); 333 String prefix = ""; 334 String namespace = null; 335 String 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 (namespace, localPart, prefix); 346 347 return qName; 348 } 349 } 350 | Popular Tags |