1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.internal.ws.wsdl.document;
27
28import com.sun.codemodel.internal.JClass;
29import com.sun.tools.internal.ws.api.wsdl.TWSDLExtensible;
30import com.sun.tools.internal.ws.api.wsdl.TWSDLExtension;
31import com.sun.tools.internal.ws.api.wsdl.TWSDLOperation;
32import com.sun.tools.internal.ws.wsdl.framework.Entity;
33import com.sun.tools.internal.ws.wsdl.framework.EntityAction;
34import com.sun.tools.internal.ws.wsdl.framework.ExtensibilityHelper;
35import org.xml.sax.Locator;
36
37import javax.xml.namespace.QName;
38import java.util.*;
39
40/**
41 * Entity corresponding to the "operation" child element of a "portType" WSDL element.
42 *
43 * @author WS Development Team
44 */
45public class Operation extends Entity implements TWSDLOperation {
46
47    public Operation(Locator locator) {
48        super(locator);
49        _faults = new ArrayList<Fault>();
50        _helper = new ExtensibilityHelper();
51    }
52
53    public String getName() {
54        return _name;
55    }
56
57    public void setName(String name) {
58        _name = name;
59    }
60
61    public String getUniqueKey() {
62        if (_uniqueKey == null) {
63            StringBuffer sb = new StringBuffer();
64            sb.append(_name);
65            sb.append(' ');
66            if (_input != null) {
67                sb.append(_input.getName());
68            } else {
69                sb.append(_name);
70                if (_style == OperationStyle.REQUEST_RESPONSE) {
71                    sb.append("Request");
72                } else if (_style == OperationStyle.SOLICIT_RESPONSE) {
73                    sb.append("Response");
74                }
75            }
76            sb.append(' ');
77            if (_output != null) {
78                sb.append(_output.getName());
79            } else {
80                sb.append(_name);
81                if (_style == OperationStyle.SOLICIT_RESPONSE) {
82                    sb.append("Solicit");
83                } else if (_style == OperationStyle.REQUEST_RESPONSE) {
84                    sb.append("Response");
85                }
86            }
87            _uniqueKey = sb.toString();
88        }
89
90        return _uniqueKey;
91    }
92
93    public OperationStyle getStyle() {
94        return _style;
95    }
96
97    public void setStyle(OperationStyle s) {
98        _style = s;
99    }
100
101    public Input getInput() {
102        return _input;
103    }
104
105    public void setInput(Input i) {
106        _input = i;
107    }
108
109    public Output getOutput() {
110        return _output;
111    }
112
113    public void setOutput(Output o) {
114        _output = o;
115    }
116
117    public void addFault(Fault f) {
118        _faults.add(f);
119    }
120
121    public Iterable<Fault> faults() {
122        return _faults;
123    }
124
125    public String getParameterOrder() {
126        return _parameterOrder;
127    }
128
129    public void setParameterOrder(String s) {
130        _parameterOrder = s;
131    }
132
133    public QName getElementName() {
134        return WSDLConstants.QNAME_OPERATION;
135    }
136
137    public Documentation getDocumentation() {
138        return _documentation;
139    }
140
141    public void setDocumentation(Documentation d) {
142        _documentation = d;
143    }
144
145    public void withAllSubEntitiesDo(EntityAction action) {
146        super.withAllSubEntitiesDo(action);
147
148        if (_input != null) {
149            action.perform(_input);
150        }
151        if (_output != null) {
152            action.perform(_output);
153        }
154        for (Fault _fault : _faults) {
155            action.perform(_fault);
156        }
157        _helper.withAllSubEntitiesDo(action);
158    }
159
160    public void accept(WSDLDocumentVisitor visitor) throws Exception {
161        visitor.preVisit(this);
162        if (_input != null) {
163            _input.accept(visitor);
164        }
165        if (_output != null) {
166            _output.accept(visitor);
167        }
168        for (Fault _fault : _faults) {
169            _fault.accept(visitor);
170        }
171        visitor.postVisit(this);
172    }
173
174    public void validateThis() {
175        if (_name == null) {
176            failValidation("validation.missingRequiredAttribute", "name");
177        }
178        if (_style == null) {
179            failValidation("validation.missingRequiredProperty", "style");
180        }
181
182        // verify operation style
183        if (_style == OperationStyle.ONE_WAY) {
184            if (_input == null) {
185                failValidation("validation.missingRequiredSubEntity", "input");
186            }
187            if (_output != null) {
188                failValidation("validation.invalidSubEntity", "output");
189            }
190            if (_faults != null && _faults.size() != 0) {
191                failValidation("validation.invalidSubEntity", "fault");
192            }
193        } else if (_style == OperationStyle.NOTIFICATION) {
194            if (_parameterOrder != null) {
195                failValidation("validation.invalidAttribute", "parameterOrder");
196            }
197        }
198    }
199
200    public String getNameValue() {
201        return getName();
202    }
203
204    public String getNamespaceURI() {
205        return parent.getNamespaceURI();
206    }
207
208    public QName getWSDLElementName() {
209        return getElementName();
210    }
211
212    /* (non-Javadoc)
213    * @see TWSDLExtensible#addExtension(ExtensionImpl)
214    */
215    public void addExtension(TWSDLExtension e) {
216        _helper.addExtension(e);
217
218    }
219
220    /* (non-Javadoc)
221     * @see TWSDLExtensible#extensions()
222     */
223    public Iterable<? extends TWSDLExtension> extensions() {
224        return _helper.extensions();
225    }
226
227    public TWSDLExtensible getParent() {
228        return parent;
229    }
230
231    public void setParent(TWSDLExtensible parent) {
232        this.parent = parent;
233    }
234
235    public Map<String, JClass> getFaults() {
236        return unmodifiableFaultClassMap;
237    }
238
239    public void putFault(String faultName, JClass exception){
240        faultClassMap.put(faultName, exception);
241    }
242
243    private TWSDLExtensible parent;
244    private Documentation _documentation;
245    private String _name;
246    private Input _input;
247    private Output _output;
248    private List<Fault> _faults;
249    private OperationStyle _style;
250    private String _parameterOrder;
251    private String _uniqueKey;
252    private ExtensibilityHelper _helper;
253    private final Map<String, JClass> faultClassMap = new HashMap<String, JClass>();
254    private final Map<String, JClass> unmodifiableFaultClassMap = Collections.unmodifiableMap(faultClassMap);
255}
256