Search in sources :

Example 1 with Parameter

use of org.eclipse.persistence.internal.xr.Parameter in project eclipselink by eclipse-ee4j.

the class JDBCHelper method buildProcedureOperation.

@Override
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel) {
    String name = procedureOperationModel.getName();
    boolean isMySQL = dbwsBuilder.getDatabasePlatform().getClass().getName().contains("MySQL");
    for (ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
        StringBuilder sb = new StringBuilder();
        if (name == null || name.length() == 0) {
            if (storedProcedure.getOverload() > 0) {
                sb.append(storedProcedure.getOverload());
                sb.append('_');
            }
            if (storedProcedure.getCatalogName() != null && storedProcedure.getCatalogName().length() > 0) {
                sb.append(storedProcedure.getCatalogName());
                sb.append('_');
            }
            if (storedProcedure.getSchema() != null && storedProcedure.getSchema().length() > 0) {
                sb.append(storedProcedure.getSchema());
                sb.append('_');
            }
            sb.append(storedProcedure.getProcedureName());
        } else {
            sb.append(name);
        }
        QueryOperation qo = new QueryOperation();
        qo.setName(sb.toString());
        QueryHandler qh;
        if (storedProcedure.isFunctionType()) {
            qh = new StoredFunctionQueryHandler();
        } else {
            qh = new StoredProcedureQueryHandler();
        }
        sb = new StringBuilder();
        if (!isMySQL) {
            if (storedProcedure.getCatalogName() != null && storedProcedure.getCatalogName().length() > 0) {
                sb.append(storedProcedure.getCatalogName());
                sb.append('.');
            }
        }
        if (storedProcedure.getSchema() != null && storedProcedure.getSchema().length() > 0) {
            sb.append(storedProcedure.getSchema());
            sb.append('.');
        }
        sb.append(storedProcedure.getProcedureName());
        ((StoredProcedureQueryHandler) qh).setName(sb.toString());
        dbwsBuilder.logMessage(FINEST, "Building QueryOperation for " + sb.toString());
        // before assigning queryHandler, check for named query in OR project
        List<DatabaseQuery> queries = dbwsBuilder.getOrProject().getQueries();
        if (queries.size() > 0) {
            for (DatabaseQuery q : queries) {
                if (q.getName().equals(qo.getName())) {
                    qh = new NamedQueryHandler();
                    ((NamedQueryHandler) qh).setName(qo.getName());
                }
            }
        }
        qo.setQueryHandler(qh);
        String returnType = procedureOperationModel.getReturnType();
        boolean isCollection = procedureOperationModel.isCollection();
        boolean isSimpleXMLFormat = procedureOperationModel.isSimpleXMLFormat();
        Result result = null;
        if (storedProcedure.isFunctionType()) {
            FunctionType storedFunction = (FunctionType) storedProcedure;
            DatabaseType rarg = storedFunction.getReturnArgument();
            if (rarg.getTypeName().contains("CURSOR")) {
                result = new CollectionResult();
                result.setType(SXF_QNAME_CURSOR);
            } else {
                result = new Result();
                int rargJdbcType = Util.getJDBCTypeFromTypeName(rarg.getTypeName());
                switch(rargJdbcType) {
                    case STRUCT:
                    case ARRAY:
                    case OTHER:
                        if (returnType != null) {
                            result.setType(buildCustomQName(returnType, dbwsBuilder));
                        } else {
                            result.setType(ANY_QNAME);
                        }
                        break;
                    default:
                        result.setType(getXMLTypeFromJDBCType(rargJdbcType));
                        break;
                }
            }
        } else {
            // if user overrides returnType, assume they're right
            if (returnType != null) {
                result = new Result();
                result.setType(buildCustomQName(returnType, dbwsBuilder));
            } else {
                if (isCollection) {
                    result = new CollectionResult();
                    if (isSimpleXMLFormat) {
                        result.setType(SXF_QNAME_CURSOR);
                    }
                } else {
                    result = new Result();
                    result.setType(SXF_QNAME);
                }
            }
        }
        if (procedureOperationModel.getBinaryAttachment()) {
            Attachment attachment = new Attachment();
            attachment.setMimeType("application/octet-stream");
            result.setAttachment(attachment);
        }
        for (ArgumentType arg : storedProcedure.getArguments()) {
            String argName = arg.getArgumentName();
            if (argName != null) {
                ProcedureArgument pa = null;
                Parameter parm = null;
                ArgumentTypeDirection direction = arg.getDirection();
                QName xmlType = null;
                switch(Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
                    case STRUCT:
                    case ARRAY:
                    case OTHER:
                        String typeString = nct.generateSchemaAlias(arg.getTypeName());
                        xmlType = buildCustomQName(typeString, dbwsBuilder);
                        break;
                    default:
                        xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
                        break;
                }
                if (direction == IN) {
                    parm = new Parameter();
                    parm.setName(argName);
                    parm.setType(xmlType);
                    pa = new ProcedureArgument();
                    pa.setName(argName);
                    pa.setParameterName(argName);
                    if (qh instanceof StoredProcedureQueryHandler) {
                        ((StoredProcedureQueryHandler) qh).getInArguments().add(pa);
                    }
                } else {
                    // the first OUT/INOUT arg determines singleResult vs. collectionResult
                    pa = new ProcedureOutputArgument();
                    ProcedureOutputArgument pao = (ProcedureOutputArgument) pa;
                    pao.setName(argName);
                    pao.setParameterName(argName);
                    if (arg.getTypeName().contains("CURSOR") && returnType == null) {
                        // if user overrides returnType, assume they're right
                        pao.setResultType(SXF_QNAME_CURSOR);
                        if (result == null) {
                            result = new CollectionResult();
                            result.setType(SXF_QNAME_CURSOR);
                        }
                    } else {
                        // Hmm, multiple OUT's gonna be a problem - later!
                        if (returnType != null && !isSimpleXMLFormat) {
                            xmlType = qNameFromString("{" + dbwsBuilder.getTargetNamespace() + "}" + returnType, dbwsBuilder.getSchema());
                        }
                        pao.setResultType(xmlType);
                        if (result == null) {
                            if (isCollection) {
                                result = new CollectionResult();
                            } else {
                                result = new Result();
                            }
                            result.setType(xmlType);
                        }
                    }
                    if (direction == INOUT) {
                        parm = new Parameter();
                        parm.setName(argName);
                        parm.setType(xmlType);
                        result.setType(xmlType);
                        // use of INOUT precludes SimpleXMLFormat
                        isSimpleXMLFormat = false;
                        if (qh instanceof StoredProcedureQueryHandler) {
                            ((StoredProcedureQueryHandler) qh).getInOutArguments().add(pao);
                        }
                    } else {
                        if (qh instanceof StoredProcedureQueryHandler) {
                            ((StoredProcedureQueryHandler) qh).getOutArguments().add(pao);
                        }
                    }
                }
                if (parm != null) {
                    qo.getParameters().add(parm);
                }
            }
        }
        handleSimpleXMLFormat(isSimpleXMLFormat, result, procedureOperationModel);
        qo.setResult(result);
        dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(), qo);
    }
    finishProcedureOperation();
}
Also used : ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) StoredProcedureQueryHandler(org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler) StoredFunctionQueryHandler(org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler) QueryHandler(org.eclipse.persistence.internal.xr.QueryHandler) NamedQueryHandler(org.eclipse.persistence.internal.xr.NamedQueryHandler) CompositeDatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.CompositeDatabaseType) DatabaseType(org.eclipse.persistence.tools.oracleddl.metadata.DatabaseType) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Util.buildCustomQName(org.eclipse.persistence.tools.dbws.Util.buildCustomQName) QName(javax.xml.namespace.QName) FunctionType(org.eclipse.persistence.tools.oracleddl.metadata.FunctionType) StoredProcedureQueryHandler(org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler) Attachment(org.eclipse.persistence.internal.xr.Attachment) Util.qNameFromString(org.eclipse.persistence.tools.dbws.Util.qNameFromString) ArgumentType(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentType) ProcedureArgument(org.eclipse.persistence.internal.xr.ProcedureArgument) ProcedureOutputArgument(org.eclipse.persistence.internal.xr.ProcedureOutputArgument) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) Result(org.eclipse.persistence.internal.xr.Result) DatabaseMetaData.procedureReturnsResult(java.sql.DatabaseMetaData.procedureReturnsResult) StoredFunctionQueryHandler(org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) NamedQueryHandler(org.eclipse.persistence.internal.xr.NamedQueryHandler) Parameter(org.eclipse.persistence.internal.xr.Parameter) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation) ArgumentTypeDirection(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentTypeDirection)

Example 2 with Parameter

use of org.eclipse.persistence.internal.xr.Parameter in project eclipselink by eclipse-ee4j.

the class ProviderHelper method init.

@SuppressWarnings("unchecked")
public void init(ClassLoader parentClassLoader, ServletContext sc, boolean mtomEnabled) {
    this.parentClassLoader = parentClassLoader;
    this.mtomEnabled = mtomEnabled;
    InputStream xrServiceStream = initXRServiceStream(parentClassLoader, sc);
    DBWSModelProject xrServiceModelProject = new DBWSModelProject();
    XMLContext xmlContext = new XMLContext(xrServiceModelProject);
    XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller();
    XRServiceModel xrServiceModel;
    try {
        xrServiceModel = (XRServiceModel) unmarshaller.unmarshal(xrServiceStream);
    } catch (XMLMarshalException e) {
        // something went wrong parsing the eclipselink-dbws.xml - can't recover from that
        throw new WebServiceException(DBWSException.couldNotParseDBWSFile());
    } finally {
        try {
            xrServiceStream.close();
        } catch (IOException e) {
        // ignore
        }
    }
    xrSchemaStream = initXRSchemaStream(parentClassLoader, sc);
    try {
        // inherit xrService processing from XRServiceFactory
        buildService(xrServiceModel);
    } catch (Exception e) {
        // something went wrong building the service
        throw new WebServiceException(e);
    }
    // the xrService built by 'buildService' above is overridden to produce an
    // instance of DBWSAdapter (a sub-class of XRService)
    DBWSAdapter dbwsAdapter = (DBWSAdapter) xrService;
    // get inline schema from WSDL - has additional types for the operations
    try (InputStream wsdlInputStream = initWSDLInputStream(parentClassLoader, sc)) {
        StringWriter sw = new StringWriter();
        StreamSource wsdlStreamSource = new StreamSource(wsdlInputStream);
        Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(MATCH_SCHEMA)));
        StreamResult streamResult = new StreamResult(sw);
        t.transform(wsdlStreamSource, streamResult);
        SchemaModelProject schemaProject = new SchemaModelProject();
        XMLContext xmlContext2 = new XMLContext(schemaProject);
        unmarshaller = xmlContext2.createUnmarshaller();
        Schema extendedSchema = (Schema) unmarshaller.unmarshal(new StringReader(sw.toString()));
        dbwsAdapter.setExtendedSchema(extendedSchema);
    } catch (IOException | TransformerException e) {
        // that's Ok, WSDL may not contain inline schema
        xmlContext.getSession().getSessionLog().log(SessionLog.FINE, SessionLog.DBWS, "dbws_no_wsdl_inline_schema", e.getLocalizedMessage());
    }
    // an Invocation needs a mapping for its parameters - use XMLAnyCollectionMapping +
    // custom AttributeAccessor
    // NB - this code is NOt in it own initNNN method, cannot be overridden
    String tns = dbwsAdapter.getExtendedSchema().getTargetNamespace();
    Project oxProject = dbwsAdapter.getOXSession().getProject();
    XMLDescriptor invocationDescriptor = new XMLDescriptor();
    invocationDescriptor.setJavaClass(Invocation.class);
    NamespaceResolver nr = new NamespaceResolver();
    invocationDescriptor.setNamespaceResolver(nr);
    nr.put(SERVICE_NAMESPACE_PREFIX, tns);
    nr.setDefaultNamespaceURI(tns);
    XMLAnyCollectionMapping parametersMapping = new XMLAnyCollectionMapping();
    parametersMapping.setAttributeName("parameters");
    parametersMapping.setAttributeAccessor(new AttributeAccessor() {

        Project oxProject;

        DBWSAdapter dbwsAdapter;

        @Override
        public Object getAttributeValueFromObject(Object object) {
            return ((Invocation) object).getParameters();
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            Invocation invocation = (Invocation) object;
            Vector<Object> values = (Vector<Object>) value;
            for (Iterator<Object> i = values.iterator(); i.hasNext(); ) {
                /* scan through values:
                   *  if XML conforms to something mapped, it an object; else it is a DOM Element
                   *  (probably a scalar). Walk through operations for the types, converting
                   *   as required. The 'key' is the local name of the element - for mapped objects,
                   *   have to get the element name from the schema context for the object
                   */
                Object o = i.next();
                if (o instanceof Element) {
                    Element e = (Element) o;
                    String key = e.getLocalName();
                    if ("theInstance".equals(key)) {
                        NodeList nl = e.getChildNodes();
                        for (int j = 0; j < nl.getLength(); j++) {
                            Node n = nl.item(j);
                            if (n.getNodeType() == Node.ELEMENT_NODE) {
                                try {
                                    Object theInstance = dbwsAdapter.getXMLContext().createUnmarshaller().unmarshal(n);
                                    if (theInstance instanceof XMLRoot) {
                                        theInstance = ((XMLRoot) theInstance).getObject();
                                    }
                                    invocation.setParameter(key, theInstance);
                                    break;
                                } catch (XMLMarshalException xmlMarshallException) {
                                    throw new WebServiceException(xmlMarshallException);
                                }
                            }
                        }
                    } else {
                        ClassDescriptor desc = null;
                        for (ClassDescriptor xdesc : oxProject.getOrderedDescriptors()) {
                            XMLSchemaReference schemaReference = xdesc instanceof XMLDescriptor ? ((XMLDescriptor) xdesc).getSchemaReference() : null;
                            if (schemaReference != null && schemaReference.getSchemaContext().equalsIgnoreCase(key)) {
                                desc = xdesc;
                                break;
                            }
                        }
                        if (desc != null) {
                            try {
                                Object theObject = dbwsAdapter.getXMLContext().createUnmarshaller().unmarshal(e, desc.getJavaClass());
                                if (theObject instanceof XMLRoot) {
                                    theObject = ((XMLRoot) theObject).getObject();
                                }
                                invocation.setParameter(key, theObject);
                            } catch (XMLMarshalException xmlMarshallException) {
                                throw new WebServiceException(xmlMarshallException);
                            }
                        } else {
                            String serviceName = e.getParentNode().getLocalName();
                            boolean found = false;
                            for (Operation op : dbwsAdapter.getOperationsList()) {
                                if (op.getName().equals(serviceName)) {
                                    for (Parameter p : op.getParameters()) {
                                        if (p.getName().equals(key)) {
                                            desc = dbwsAdapter.getDescriptorsByQName().get(p.getType());
                                            if (desc != null) {
                                                found = true;
                                            }
                                            break;
                                        }
                                    }
                                }
                                if (found) {
                                    break;
                                }
                            }
                            if (found) {
                                Object theObject = dbwsAdapter.getXMLContext().createUnmarshaller().unmarshal(e, desc.getJavaClass());
                                if (theObject instanceof XMLRoot) {
                                    theObject = ((XMLRoot) theObject).getObject();
                                }
                                invocation.setParameter(key, theObject);
                            } else {
                                // cant use e.getTextContent() - some DOM impls dont support it :-(
                                // String val = e.getTextContent();
                                StringBuilder sb = new StringBuilder();
                                NodeList childNodes = e.getChildNodes();
                                for (int idx = 0; idx < childNodes.getLength(); idx++) {
                                    if (childNodes.item(idx).getNodeType() == Node.TEXT_NODE) {
                                        sb.append(childNodes.item(idx).getNodeValue());
                                    }
                                }
                                invocation.setParameter(key, sb.toString());
                            }
                        }
                    }
                } else {
                    XMLDescriptor descriptor = (XMLDescriptor) oxProject.getDescriptor(o.getClass());
                    String key = descriptor.getDefaultRootElement();
                    int idx = key.indexOf(':');
                    if (idx != -1) {
                        key = key.substring(idx + 1);
                    }
                    invocation.setParameter(key, o);
                }
            }
        }

        public AttributeAccessor setProjectAndAdapter(Project oxProject, DBWSAdapter dbwsAdapter) {
            this.oxProject = oxProject;
            this.dbwsAdapter = dbwsAdapter;
            return this;
        }
    }.setProjectAndAdapter(oxProject, dbwsAdapter));
    parametersMapping.setKeepAsElementPolicy(KEEP_UNKNOWN_AS_ELEMENT);
    invocationDescriptor.addMapping(parametersMapping);
    oxProject.addDescriptor(invocationDescriptor);
    ((DatabaseSessionImpl) dbwsAdapter.getOXSession()).initializeDescriptorIfSessionAlive(invocationDescriptor);
    dbwsAdapter.getXMLContext().storeXMLDescriptorByQName(invocationDescriptor);
    // create SOAP message response handler of appropriate version
    responseWriter = new SOAPResponseWriter(dbwsAdapter);
    responseWriter.initialize();
}
Also used : Transformer(javax.xml.transform.Transformer) Invocation(org.eclipse.persistence.internal.xr.Invocation) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SOAPElement(jakarta.xml.soap.SOAPElement) Element(org.w3c.dom.Element) SOAPBodyElement(jakarta.xml.soap.SOAPBodyElement) Node(org.w3c.dom.Node) Operation(org.eclipse.persistence.internal.xr.Operation) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Vector(java.util.Vector) TransformerException(javax.xml.transform.TransformerException) WebServiceException(jakarta.xml.ws.WebServiceException) StreamResult(javax.xml.transform.stream.StreamResult) XMLContext(org.eclipse.persistence.oxm.XMLContext) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) InputStream(java.io.InputStream) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) SOAPException(jakarta.xml.soap.SOAPException) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) SOAPFaultException(jakarta.xml.ws.soap.SOAPFaultException) WebServiceException(jakarta.xml.ws.WebServiceException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) DBWSException(org.eclipse.persistence.exceptions.DBWSException) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) XRServiceModel(org.eclipse.persistence.internal.xr.XRServiceModel) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) Parameter(org.eclipse.persistence.internal.xr.Parameter) ValueObject(org.eclipse.persistence.internal.xr.ValueObject) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor)

Example 3 with Parameter

use of org.eclipse.persistence.internal.xr.Parameter in project eclipselink by eclipse-ee4j.

the class OracleHelper method buildProcedureOperation.

/**
 * Builds query operations for a given ProcedureOperationModel.
 */
@Override
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel) {
    for (ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
        boolean hasComplexArgs = hasComplexArgs(storedProcedure);
        QueryOperation qo = new QueryOperation();
        qo.setName(getNameForQueryOperation(procedureOperationModel, storedProcedure));
        String qualifiedProcName = getQualifiedProcedureName(procedureOperationModel, storedProcedure);
        dbwsBuilder.logMessage(FINEST, BUILDING_QUERYOP_FOR + qualifiedProcName);
        QueryHandler qh = null;
        // before assigning queryHandler, check for named query in OR project
        List<DatabaseQuery> queries = dbwsBuilder.getOrProject().getQueries();
        if (queries.size() > 0) {
            for (DatabaseQuery q : queries) {
                if (q.getName().equals(qo.getName())) {
                    qh = new NamedQueryHandler();
                    ((NamedQueryHandler) qh).setName(qo.getName());
                }
            }
        }
        if (qh == null) {
            if (storedProcedure.isFunctionType()) {
                qh = new StoredFunctionQueryHandler();
            } else {
                qh = new StoredProcedureQueryHandler();
            }
            ((StoredProcedureQueryHandler) qh).setName(qualifiedProcName);
        }
        qo.setQueryHandler(qh);
        String returnType = procedureOperationModel.getReturnType();
        boolean isCollection = procedureOperationModel.isCollection();
        boolean isSimpleXMLFormat = procedureOperationModel.isSimpleXMLFormat();
        Result result = null;
        /**
         * For multiple OUT args as well as a stored function with OUT args, we want
         * the result to be a collection and the type to be "xsd:any".  We will
         * force SimpleXMLFormat for now as well.
         */
        int outArgCount = 0;
        for (ArgumentType argument : storedProcedure.getArguments()) {
            ArgumentTypeDirection argDirection = argument.getDirection();
            if (argDirection == OUT) {
                outArgCount++;
            }
        }
        if (outArgCount > 1 || (outArgCount > 0 && storedProcedure.isFunctionType())) {
            isCollection = true;
            isSimpleXMLFormat = true;
            result = new CollectionResult();
            result.setType(ANY_QNAME);
        } else {
            if (storedProcedure.isFunctionType()) {
                ArgumentType returnArg = ((FunctionType) storedProcedure).getReturnArgument();
                result = buildResultForStoredFunction(returnArg, returnType);
                // tags to better represent the PL/SQL record/table/column type
                if (returnArg.getEnclosedType().isPLSQLCursorType()) {
                    customizeSimpleXMLTagNames((PLSQLCursorType) returnArg.getEnclosedType(), procedureOperationModel);
                }
            } else if (hasComplexArgs) {
                if (Util.noOutArguments(storedProcedure)) {
                    result = new Result();
                    // rowcount
                    result.setType(new QName(SCHEMA_URL, INT, SCHEMA_PREFIX));
                }
            } else {
                // if user overrides returnType, assume they're right
                if (returnType != null) {
                    result = new Result();
                    result.setType(buildCustomQName(returnType, dbwsBuilder));
                } else {
                    if (isCollection) {
                        result = new CollectionResult();
                        if (isSimpleXMLFormat) {
                            result.setType(SXF_QNAME_CURSOR);
                        }
                    } else {
                        result = new Result();
                        result.setType(SXF_QNAME);
                    }
                }
            }
        }
        for (ArgumentType arg : storedProcedure.getArguments()) {
            String argName = arg.getArgumentName();
            if (argName != null) {
                QName xmlType = null;
                ProcedureArgument pa = null;
                // for INOUT's
                ProcedureArgument paShadow = null;
                Parameter parm = null;
                ArgumentTypeDirection direction = arg.getDirection();
                if (!hasComplexArgs) {
                    if (arg.getEnclosedType().isPLSQLCursorType()) {
                        PLSQLCursorType cursorType = (PLSQLCursorType) arg.getEnclosedType();
                        if (cursorType.isWeaklyTyped()) {
                            xmlType = buildCustomQName("SYS_REFCURSOR", dbwsBuilder);
                        }
                    } else {
                        xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
                    }
                } else {
                    // handle PL/SQL records and collections
                    if (arg.getEnclosedType().isPLSQLType()) {
                        String packageName = ((PLSQLType) arg.getEnclosedType()).getParentType().getPackageName();
                        // may need to prepend package name
                        String typeString = (packageName != null && packageName.length() > 0) ? packageName + UNDERSCORE + arg.getTypeName() : arg.getTypeName();
                        // may need to strip off %
                        typeString = typeString.contains(PERCENT) ? typeString.replace(PERCENT, UNDERSCORE) : typeString;
                        xmlType = buildCustomQName(nct.generateSchemaAlias(typeString), dbwsBuilder);
                    } else if (arg.getEnclosedType().isVArrayType() || arg.getEnclosedType().isObjectType() || arg.getEnclosedType().isObjectTableType()) {
                        // handle advanced JDBC types
                        xmlType = buildCustomQName(nct.generateSchemaAlias(arg.getTypeName()), dbwsBuilder);
                    } else {
                        switch(Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
                            case STRUCT:
                            case ARRAY:
                                String typeString = nct.generateSchemaAlias(arg.getTypeName());
                                xmlType = buildCustomQName(typeString, dbwsBuilder);
                                break;
                            default:
                                xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
                                break;
                        }
                    }
                }
                if (direction == null || direction == IN) {
                    parm = new Parameter();
                    parm.setName(argName);
                    parm.setType(xmlType);
                    // handle optional arg
                    parm.setOptional(arg.optional());
                    pa = new ProcedureArgument();
                    pa.setName(argName);
                    pa.setParameterName(argName);
                    if (qh instanceof StoredProcedureQueryHandler) {
                        ((StoredProcedureQueryHandler) qh).getInArguments().add(pa);
                    }
                } else {
                    // the first OUT/INOUT arg determines singleResult vs. collectionResult
                    pa = new ProcedureOutputArgument();
                    ProcedureOutputArgument pao = (ProcedureOutputArgument) pa;
                    pao.setName(argName);
                    pao.setParameterName(argName);
                    boolean isCursor = arg.isPLSQLCursorType() || arg.getTypeName().contains(CURSOR_STR);
                    // tags to better represent the PL/SQL record/table/column type
                    if (arg.isPLSQLCursorType()) {
                        customizeSimpleXMLTagNames((PLSQLCursorType) arg.getEnclosedType(), procedureOperationModel);
                    }
                    if (isCursor && returnType == null) {
                        // if user overrides returnType, assume they're right
                        pao.setResultType(SXF_QNAME_CURSOR);
                        if (result == null) {
                            result = new CollectionResult();
                            result.setType(SXF_QNAME_CURSOR);
                        }
                    } else {
                        // Hmm, multiple OUT's gonna be a problem - later!
                        if (returnType != null && !isSimpleXMLFormat) {
                            xmlType = qNameFromString(OPEN_PAREN + dbwsBuilder.getTargetNamespace() + CLOSE_PAREN + returnType, dbwsBuilder.getSchema());
                        }
                        if (isCursor) {
                            pao.setResultType(new QName(EMPTY_STRING, CURSOR_OF_STR + returnType));
                            Result newResult = new CollectionResult();
                            newResult.setType(result.getType());
                            result = newResult;
                        } else {
                            pao.setResultType(xmlType);
                        }
                        if (result == null) {
                            if (isCollection) {
                                result = new CollectionResult();
                            } else {
                                result = new Result();
                            }
                            result.setType(xmlType);
                        }
                    }
                    if (direction == INOUT) {
                        parm = new Parameter();
                        parm.setName(argName);
                        parm.setType(xmlType);
                        result.setType(xmlType);
                        if (qh instanceof StoredProcedureQueryHandler) {
                            ((StoredProcedureQueryHandler) qh).getInOutArguments().add(pao);
                        }
                        paShadow = new ProcedureArgument();
                        paShadow.setName(argName);
                        paShadow.setParameterName(argName);
                    } else {
                        // OUT arg
                        if (qh instanceof StoredProcedureQueryHandler) {
                            ((StoredProcedureQueryHandler) qh).getOutArguments().add(pao);
                        }
                    }
                }
                // for XMLType, we want the type code to be 'OPAQUE' (2007)
                if (arg.getEnclosedType() == ScalarDatabaseTypeEnum.XMLTYPE_TYPE) {
                    pa.setJdbcType(getJDBCTypeForTypeName(XMLTYPE_STR));
                }
                if (hasComplexArgs && arg.getEnclosedType().isPLSQLType()) {
                    pa.setComplexTypeName(storedProcedure.getCatalogName() + UNDERSCORE + arg.getTypeName());
                    if (paShadow != null) {
                        paShadow.setComplexTypeName(pa.getComplexTypeName());
                    }
                }
                if (parm != null) {
                    qo.getParameters().add(parm);
                }
            }
        }
        if (procedureOperationModel.getBinaryAttachment()) {
            Attachment attachment = new Attachment();
            attachment.setMimeType(APP_OCTET_STREAM);
            result.setAttachment(attachment);
        }
        // the user may want simpleXMLFormat
        handleSimpleXMLFormat(isSimpleXMLFormat, result, procedureOperationModel);
        qo.setResult(result);
        dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(), qo);
    }
    finishProcedureOperation();
}
Also used : ProcedureType(org.eclipse.persistence.tools.oracleddl.metadata.ProcedureType) StoredProcedureQueryHandler(org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler) StoredFunctionQueryHandler(org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler) QueryHandler(org.eclipse.persistence.internal.xr.QueryHandler) NamedQueryHandler(org.eclipse.persistence.internal.xr.NamedQueryHandler) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Util.buildCustomQName(org.eclipse.persistence.tools.dbws.Util.buildCustomQName) QName(javax.xml.namespace.QName) FunctionType(org.eclipse.persistence.tools.oracleddl.metadata.FunctionType) StoredProcedureQueryHandler(org.eclipse.persistence.internal.xr.StoredProcedureQueryHandler) PLSQLCursorType(org.eclipse.persistence.tools.oracleddl.metadata.PLSQLCursorType) Attachment(org.eclipse.persistence.internal.xr.Attachment) Util.qNameFromString(org.eclipse.persistence.tools.dbws.Util.qNameFromString) ArgumentType(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentType) ProcedureArgument(org.eclipse.persistence.internal.xr.ProcedureArgument) ProcedureOutputArgument(org.eclipse.persistence.internal.xr.ProcedureOutputArgument) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) Result(org.eclipse.persistence.internal.xr.Result) StoredFunctionQueryHandler(org.eclipse.persistence.internal.xr.StoredFunctionQueryHandler) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) NamedQueryHandler(org.eclipse.persistence.internal.xr.NamedQueryHandler) Parameter(org.eclipse.persistence.internal.xr.Parameter) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation) ArgumentTypeDirection(org.eclipse.persistence.tools.oracleddl.metadata.ArgumentTypeDirection)

Example 4 with Parameter

use of org.eclipse.persistence.internal.xr.Parameter in project eclipselink by eclipse-ee4j.

the class WSDLGenerator method createInlineSchema.

/**
 * Build the inline schema that will make up the content of the
 * {@code <wsdl:types>} section of the WSDL.
 */
@SuppressWarnings("unchecked")
private org.w3c.dom.Element createInlineSchema() {
    SchemaModelProject project = new SchemaModelProject();
    XMLContext context = new XMLContext(project);
    XMLMarshaller marshaller = context.createMarshaller();
    XMLDescriptor descriptor = (XMLDescriptor) project.getClassDescriptor(Schema.class);
    if (descriptor.getNamespaceResolver() == null) {
        descriptor.setNamespaceResolver(new NamespaceResolver());
    }
    descriptor.getNamespaceResolver().put(NS_TNS_PREFIX, serviceNameSpace);
    descriptor.getNamespaceResolver().put(NS_SCHEMA_PREFIX, W3C_XML_SCHEMA_NS_URI);
    if (hasAttachments) {
        descriptor.getNamespaceResolver().put(WSI_SWAREF_PREFIX, WSI_SWAREF_URI);
    }
    Schema schema = new Schema();
    schema.setTargetNamespace(serviceNameSpace);
    schema.setElementFormDefault(true);
    Import parent = new Import();
    parent.setNamespace(importedSchemaNameSpace);
    parent.setSchemaLocation(DBWS_SCHEMA_XML);
    schema.getImports().add(parent);
    if (hasAttachments) {
        Import ref = new Import();
        ref.setNamespace(WSI_SWAREF_URI);
        // ref.setSchemaLocation("http://ws-i.org/profiles/basic/1.1/swaref.xsd"); // later version
        ref.setSchemaLocation(WSI_SWAREF_XSD_FILE);
        schema.getImports().add(ref);
    }
    boolean requireFaultTypeEmptyResponse = false;
    for (Operation op : xrServiceModel.getOperationsList()) {
        String opName = op.getName();
        ComplexType requestType = new ComplexType();
        // may need to extract tableNameAlias from operation name - everything after first
        // underscore '_' character (for cases where a QueryOperation is not user defined)
        // i.e. String tableNameAlias = opName.substring(opName.indexOf('_') + 1);
        requestType.setName(opName + REQUEST_SUFFIX + TYPE_SUFFIX);
        Sequence requestSequence = null;
        if (op.getParameters().size() > 0) {
            requestSequence = new Sequence();
            for (Parameter p : op.getParameters()) {
                Element arg = new Element();
                if (p.isOptional()) {
                    arg.setMinOccurs("0");
                }
                arg.setName(p.getName());
                if (THE_INSTANCE_NAME.equals(p.getName())) {
                    ComplexType nestedComplexType = new ComplexType();
                    Sequence nestedSequence = new Sequence();
                    nestedComplexType.setSequence(nestedSequence);
                    Element nestedElement = new Element();
                    nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + p.getType().getLocalPart());
                    // here we may need to use 'tableNameAlias' instead, i.e.
                    // nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + tableNameAlias);
                    nestedSequence.addElement(nestedElement);
                    arg.setComplexType(nestedComplexType);
                } else {
                    arg.setName(p.getName());
                    if (p.getType().getNamespaceURI().equals(W3C_XML_SCHEMA_NS_URI)) {
                        arg.setType(NS_SCHEMA_PREFIX + ":" + p.getType().getLocalPart());
                    } else if (p.getType().getNamespaceURI().equals(importedSchemaNameSpace)) {
                        arg.setType(TARGET_NAMESPACE_PREFIX + ":" + p.getType().getLocalPart());
                    } else {
                        arg.setType(p.getType().getLocalPart());
                    }
                }
                requestSequence.addElement(arg);
            }
            requestType.setSequence(requestSequence);
        }
        schema.addTopLevelComplexTypes(requestType);
        Element requestElement = new Element();
        requestElement.setName(op.getName());
        requestElement.setType(NS_TNS_PREFIX + ":" + requestType.getName());
        schema.addTopLevelElement(requestElement);
        // build response message based on operation type
        if (op instanceof QueryOperation) {
            QueryOperation q = (QueryOperation) op;
            ComplexType responseType = new ComplexType();
            responseType.setName(op.getName() + RESPONSE_SUFFIX + TYPE_SUFFIX);
            Sequence responseSequence = new Sequence();
            Element result = new Element();
            result.setName("result");
            if (q.isAttachment()) {
                result.setType(WSI_SWAREF_PREFIX + ":" + WSI_SWAREF);
            } else if (q.isSimpleXMLFormat() || q.getResultType().equals(new QName(W3C_XML_SCHEMA_NS_URI, "any"))) {
                ComplexType anyType = new ComplexType();
                Sequence anySequence = new Sequence();
                anySequence.addAny(new Any());
                anyType.setSequence(anySequence);
                result.setComplexType(anyType);
            } else {
                if (q.getResultType().getNamespaceURI().equals(W3C_XML_SCHEMA_NS_URI)) {
                    result.setType(NS_SCHEMA_PREFIX + ":" + q.getResultType().getLocalPart());
                } else {
                    ComplexType nestedComplexType = new ComplexType();
                    Sequence nestedSequence = new Sequence();
                    nestedComplexType.setSequence(nestedSequence);
                    Element nestedElement = new Element();
                    // may need if/else based on if the operation is/isn't user defined, i.e.
                    // if (!q.isUserDefined()) {
                    // nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + tableNameAlias);
                    // } else {
                    // nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + q.getResultType().getLocalPart());
                    // }
                    nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + q.getResultType().getLocalPart());
                    nestedElement.setMinOccurs("0");
                    if (q.isCollection()) {
                        nestedElement.setMaxOccurs("unbounded");
                    }
                    nestedSequence.addElement(nestedElement);
                    result.setComplexType(nestedComplexType);
                }
            }
            responseSequence.addElement(result);
            responseType.setSequence(responseSequence);
            schema.addTopLevelComplexTypes(responseType);
            Element responseElement = new Element();
            responseElement.setName(op.getName() + RESPONSE_SUFFIX);
            responseElement.setType(NS_TNS_PREFIX + ":" + responseType.getName());
            schema.addTopLevelElement(responseElement);
        } else {
            requireFaultTypeEmptyResponse = true;
        }
    }
    if (requireFaultTypeEmptyResponse) {
        // <element name="EmptyResponse">
        // <xsd:complexType/>
        // </element>
        Element emptyResponseElement = new Element();
        emptyResponseElement.setName(EMPTY_RESPONSE);
        ComplexType emptyResponseComplexType = new ComplexType();
        emptyResponseElement.setComplexType(emptyResponseComplexType);
        schema.addTopLevelElement(emptyResponseElement);
        // <xsd:element name="FaultType">
        // <xsd:complexType>
        // <xsd:sequence>
        // <xsd:element name="faultCode" type="xsd:string"/>
        // <xsd:element name="faultString" type="xsd:string"/>
        // </xsd:sequence>
        // </xsd:complexType>
        // </element>
        Element elementFaultType = new Element();
        elementFaultType.setName(FAULT_SUFFIX + TYPE_SUFFIX);
        ComplexType faultComplexType = new ComplexType();
        elementFaultType.setComplexType(faultComplexType);
        Sequence nestedSequence = new Sequence();
        faultComplexType.setSequence(nestedSequence);
        Element faultCodeElement = new Element();
        faultCodeElement.setName("faultCode");
        faultCodeElement.setMinOccurs("1");
        faultCodeElement.setType(NS_SCHEMA_PREFIX + ":string");
        nestedSequence.addElement(faultCodeElement);
        Element faultStringElement = new Element();
        faultStringElement.setMinOccurs("1");
        faultStringElement.setName("faultString");
        faultStringElement.setType(NS_SCHEMA_PREFIX + ":string");
        nestedSequence.addElement(faultStringElement);
        schema.addTopLevelElement(elementFaultType);
    }
    return marshaller.objectToXML(schema).getDocumentElement();
}
Also used : Import(org.eclipse.persistence.internal.oxm.schema.model.Import) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) QName(javax.xml.namespace.QName) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation) SOAP12Operation(javax.wsdl.extensions.soap12.SOAP12Operation) Operation(org.eclipse.persistence.internal.xr.Operation) BindingOperation(javax.wsdl.BindingOperation) SOAPOperation(javax.wsdl.extensions.soap.SOAPOperation) Sequence(org.eclipse.persistence.internal.oxm.schema.model.Sequence) Any(org.eclipse.persistence.internal.oxm.schema.model.Any) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) Parameter(org.eclipse.persistence.internal.xr.Parameter) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation)

Example 5 with Parameter

use of org.eclipse.persistence.internal.xr.Parameter in project eclipselink by eclipse-ee4j.

the class SQLOperationModel method buildOperation.

@Override
public void buildOperation(DBWSBuilder builder) {
    super.buildOperation(builder);
    QueryOperation qo = new QueryOperation();
    qo.setName(name);
    SQLQueryHandler sqlqh = new SQLQueryHandler();
    Result result;
    if (isCollection) {
        result = new CollectionResult();
    } else {
        result = new Result();
    }
    SimpleXMLFormat sxf = null;
    if (isSimpleXMLFormat() || getReturnType() == null) {
        sxf = new SimpleXMLFormat();
        if (simpleXMLFormatTag != null && simpleXMLFormatTag.length() > 0) {
            sxf.setSimpleXMLFormatTag(simpleXMLFormatTag);
        }
        result.setType(SXF_QNAME);
    }
    if (xmlTag != null && xmlTag.length() > 0) {
        if (sxf == null) {
            sxf = new SimpleXMLFormat();
            result.setType(SXF_QNAME);
        }
        sxf.setXMLTag(xmlTag);
    }
    if (sxf != null) {
        result.setSimpleXMLFormat(sxf);
        // check to see if the O-X project needs descriptor for SimpleXMLFormat
        if (builder.oxProject.getDescriptorForAlias(DEFAULT_SIMPLE_XML_FORMAT_TAG) == null) {
            SimpleXMLFormatProject sxfProject = new SimpleXMLFormatProject();
            builder.oxProject.addDescriptor(sxfProject.buildXRRowSetModelDescriptor());
        }
    }
    if (binaryAttachment) {
        Attachment attachment = new Attachment();
        attachment.setMimeType(DEFAULT_ATTACHMENT_MIMETYPE);
        result.setAttachment(attachment);
    }
    if (returnType != null && returnType.length() > 0) {
        result.setType(qNameFromString("{" + builder.getTargetNamespace() + "}" + returnType, builder.schema));
    } else {
        result.setType(SXF_QNAME);
        result.setSimpleXMLFormat(sxf == null ? new SimpleXMLFormat() : sxf);
    }
    qo.setResult(result);
    sqlqh.setSqlString(convertJDBCParameterBindingMarkers(sql, bindings));
    if (!bindings.isEmpty()) {
        for (BindingModel param : bindings) {
            Parameter p = new Parameter();
            p.setName(param.name);
            p.setType(qNameFromString(param.type, builder.schema));
            qo.getParameters().add(p);
        }
    }
    qo.setQueryHandler(sqlqh);
    builder.xrServiceModel.getOperations().put(qo.getName(), qo);
    // check to see if the schema requires sxfType to be added
    if (requiresSimpleXMLFormat(builder.xrServiceModel) && builder.schema.getTopLevelElements().get("simple-xml-format") == null) {
        addSimpleXMLFormat(builder.schema);
    }
}
Also used : CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) SimpleXMLFormatProject(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject) SQLQueryHandler(org.eclipse.persistence.internal.xr.SQLQueryHandler) Parameter(org.eclipse.persistence.internal.xr.Parameter) Attachment(org.eclipse.persistence.internal.xr.Attachment) Util.addSimpleXMLFormat(org.eclipse.persistence.tools.dbws.Util.addSimpleXMLFormat) SimpleXMLFormat(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormat) Util.requiresSimpleXMLFormat(org.eclipse.persistence.tools.dbws.Util.requiresSimpleXMLFormat) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation) Result(org.eclipse.persistence.internal.xr.Result) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult)

Aggregations

Parameter (org.eclipse.persistence.internal.xr.Parameter)6 QueryOperation (org.eclipse.persistence.internal.xr.QueryOperation)5 QName (javax.xml.namespace.QName)4 CollectionResult (org.eclipse.persistence.internal.xr.CollectionResult)4 Result (org.eclipse.persistence.internal.xr.Result)4 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)3 Attachment (org.eclipse.persistence.internal.xr.Attachment)3 NamedQueryHandler (org.eclipse.persistence.internal.xr.NamedQueryHandler)3 XMLContext (org.eclipse.persistence.oxm.XMLContext)3 DBWSModelProject (org.eclipse.persistence.dbws.DBWSModelProject)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)2 Operation (org.eclipse.persistence.internal.xr.Operation)2 SimpleXMLFormatProject (org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject)2 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)2 Project (org.eclipse.persistence.sessions.Project)2 SOAPBodyElement (jakarta.xml.soap.SOAPBodyElement)1 SOAPElement (jakarta.xml.soap.SOAPElement)1