Search in sources :

Example 1 with SimpleXMLFormat

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

the class QueryOperation method createSimpleXMLFormat.

@SuppressWarnings({ "unchecked" })
public Object createSimpleXMLFormat(XRServiceAdapter xrService, Object value) {
    XMLRoot xmlRoot = new XMLRoot();
    SimpleXMLFormat simpleXMLFormat = result.getSimpleXMLFormat();
    String tempSimpleXMLFormatTag = SimpleXMLFormat.DEFAULT_SIMPLE_XML_FORMAT_TAG;
    String simpleXMLFormatTag = simpleXMLFormat.getSimpleXMLFormatTag();
    if (simpleXMLFormatTag != null && !EMPTY_STR.equals(simpleXMLFormatTag)) {
        tempSimpleXMLFormatTag = simpleXMLFormatTag;
    }
    xmlRoot.setLocalName(tempSimpleXMLFormatTag);
    String tempXMLTag = DEFAULT_SIMPLE_XML_TAG;
    String xmlTag = simpleXMLFormat.getXMLTag();
    if (xmlTag != null && !EMPTY_STR.equals(xmlTag)) {
        tempXMLTag = xmlTag;
    }
    Vector<DatabaseRecord> records = null;
    if (value instanceof ArrayList) {
        // JPA query results in a list of raw values
        // Here we have raw values returned as opposed to DatabaseRecords - this means
        // we need to figure out the tag names based on the call's output parameters.
        // assumes JPAQuery
        JPAQuery jpaQuery = (JPAQuery) queryHandler.getDatabaseQuery();
        // to match field names with results, we need to gather the database fields from each of the Output parameters
        List<DatabaseField> paramFlds = new ArrayList<>();
        DatasourceCall dsCall = (DatasourceCall) jpaQuery.getDatabaseQuery().getDatasourceCall();
        for (Object obj : dsCall.getParameters()) {
            if (obj instanceof OutputParameterForCallableStatement) {
                paramFlds.add(((OutputParameterForCallableStatement) obj).getOutputField());
            } else if (obj instanceof Object[]) {
                Object[] objArray = (Object[]) obj;
                for (int i = 0; i < objArray.length; i++) {
                    Object o = objArray[i];
                    if (o instanceof OutputParameterForCallableStatement) {
                        paramFlds.add(((OutputParameterForCallableStatement) o).getOutputField());
                    }
                }
            }
        }
        // now create a record using DatabaseField/value pairs
        DatabaseRecord dr = new DatabaseRecord();
        if (paramFlds.size() > 0) {
            for (int i = 0; i < ((ArrayList<?>) value).size(); i++) {
                dr.add(paramFlds.get(i), ((ArrayList<?>) value).get(i));
            }
        } else {
            dr.add(new DatabaseField(RESULT_STR), ((ArrayList<?>) value).get(0));
        }
        records = new Vector<>();
        records.add(dr);
    } else if (value instanceof Vector) {
        Class<?> vectorContent = ((Vector<?>) value).firstElement().getClass();
        if (DatabaseRecord.class.isAssignableFrom(vectorContent)) {
            records = (Vector<DatabaseRecord>) value;
        } else {
            records = new Vector<>();
            DatabaseRecord dr = new DatabaseRecord();
            dr.add(new DatabaseField(RESULT_STR), ((Vector<?>) value).firstElement());
            records.add(dr);
        }
    } else {
        records = new Vector<>();
        DatabaseRecord dr = new DatabaseRecord();
        dr.add(new DatabaseField(RESULT_STR), value);
        records.add(dr);
    }
    SimpleXMLFormatModel simpleXMLFormatModel = new SimpleXMLFormatModel();
    XMLConversionManager conversionManager = (XMLConversionManager) xrService.getOXSession().getDatasourcePlatform().getConversionManager();
    SessionLog log = xrService.getOXSession().getSessionLog();
    for (DatabaseRecord dr : records) {
        Element rowElement = TEMP_DOC.createElement(tempXMLTag);
        for (DatabaseField field : dr.getFields()) {
            // handle complex types, i.e. ones we have a descriptor for
            if (field instanceof ObjectRelationalDatabaseField) {
                ObjectRelationalDatabaseField ordtField = (ObjectRelationalDatabaseField) field;
                if (xrService.getOXSession().getDescriptor(ordtField.getType()) != null) {
                    xrService.getXMLContext().createMarshaller().marshal(dr.get(field), rowElement);
                    continue;
                }
            }
            Object fieldValue = dr.get(field);
            if (fieldValue != null) {
                if (fieldValue instanceof Calendar) {
                    Calendar cValue = (Calendar) fieldValue;
                    fieldValue = conversionManager.convertObject(cValue, STRING, DATE_TIME_QNAME);
                }
                if (fieldValue instanceof Date) {
                    Date dValue = (Date) fieldValue;
                    fieldValue = conversionManager.convertObject(dValue, STRING, DATE_QNAME);
                } else if (fieldValue instanceof Time) {
                    Time tValue = (Time) fieldValue;
                    fieldValue = conversionManager.convertObject(tValue, STRING, TIME_QNAME);
                } else if (fieldValue instanceof Timestamp) {
                    Timestamp tsValue = (Timestamp) fieldValue;
                    fieldValue = conversionManager.convertObject(tsValue, STRING, DATE_TIME_QNAME);
                } else if (fieldValue instanceof Blob) {
                    fieldValue = conversionManager.convertObject(fieldValue, ClassConstants.APBYTE);
                } else if (SQLXML.class.isAssignableFrom(fieldValue.getClass())) {
                    // handle XMLType case where an oracle.jdbc.driver.OracleSQLXML instance was returned
                    SQLXML sqlXml = (SQLXML) fieldValue;
                    try {
                        String str = sqlXml.getString();
                        sqlXml.free();
                        // Oracle 12c appends a \n character to the xml string
                        fieldValue = str.endsWith("\n") ? str.substring(0, str.length() - 1) : str;
                    } catch (SQLException e) {
                        log.logThrowable(SessionLog.FINE, SessionLog.DBWS, e);
                    }
                } else if (fieldValue.getClass().getName().equalsIgnoreCase(ORACLEOPAQUE_STR)) {
                    // handle XMLType case where an oracle.sql.OPAQUE instance was returned
                    try {
                        Class<?> oracleOPAQUE;
                        Class<?> xmlTypeFactoryClass;
                        Constructor<?> xmlTypeFactoryConstructor;
                        Object xmlTypeFactory;
                        Method getStringMethod;
                        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                            oracleOPAQUE = AccessController.doPrivileged(new PrivilegedClassForName<>(IORACLEOPAQUE_STR, true, this.getClass().getClassLoader()));
                            xmlTypeFactoryClass = AccessController.doPrivileged(new PrivilegedClassForName<>(XMLTYPEFACTORY_STR, true, this.getClass().getClassLoader()));
                            xmlTypeFactoryConstructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(xmlTypeFactoryClass, new Class<?>[0], true));
                            xmlTypeFactory = AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(xmlTypeFactoryConstructor, new Object[0]));
                            getStringMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(xmlTypeFactoryClass, GETSTRING_METHOD, new Class<?>[] { oracleOPAQUE }));
                            fieldValue = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(getStringMethod, xmlTypeFactory, new Object[] { fieldValue }));
                        } else {
                            oracleOPAQUE = PrivilegedAccessHelper.getClassForName(IORACLEOPAQUE_STR, false, this.getClass().getClassLoader());
                            xmlTypeFactoryClass = PrivilegedAccessHelper.getClassForName(XMLTYPEFACTORY_STR, true, this.getClass().getClassLoader());
                            xmlTypeFactoryConstructor = PrivilegedAccessHelper.getConstructorFor(xmlTypeFactoryClass, new Class<?>[0], true);
                            xmlTypeFactory = PrivilegedAccessHelper.invokeConstructor(xmlTypeFactoryConstructor, new Object[0]);
                            getStringMethod = PrivilegedAccessHelper.getDeclaredMethod(xmlTypeFactoryClass, GETSTRING_METHOD, new Class<?>[] { oracleOPAQUE });
                            fieldValue = PrivilegedAccessHelper.invokeMethod(getStringMethod, xmlTypeFactory, new Object[] { fieldValue });
                        }
                    } catch (ReflectiveOperationException | PrivilegedActionException e) {
                        // if the required resources are not available there's nothing we can do...
                        log.logThrowable(SessionLog.FINE, SessionLog.DBWS, e);
                    }
                }
                String elementName;
                if (field.getName() == null || (elementName = sqlToXmlName(field.getName())).equals(EMPTY_STR)) {
                    // return arg from stored function has no name
                    elementName = RESULT_STR;
                }
                Element columnElement = TEMP_DOC.createElement(elementName);
                rowElement.appendChild(columnElement);
                String fieldValueString = fieldValue.toString();
                // handle binary content - attachments dealt with in invoke() above
                if (result.getType().equals(BASE_64_BINARY_QNAME)) {
                    fieldValueString = Helper.buildHexStringFromBytes(Base64.base64Encode((byte[]) fieldValue));
                    columnElement.setAttributeNS(XMLNS_URL, XSD_STR, SCHEMA_URL);
                    columnElement.setAttributeNS(XMLNS_URL, XSI_STR, SCHEMA_INSTANCE_URL);
                    columnElement.setAttributeNS(SCHEMA_INSTANCE_URL, XSITYPE_STR, BASE64_BINARY_STR);
                }
                columnElement.appendChild(TEMP_DOC.createTextNode(fieldValueString));
            }
        }
        simpleXMLFormatModel.simpleXML.add(rowElement);
    }
    xmlRoot.setObject(simpleXMLFormatModel);
    return xmlRoot;
}
Also used : SQLException(java.sql.SQLException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Time(java.sql.Time) SimpleXMLFormat(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormat) DatasourceCall(org.eclipse.persistence.internal.databaseaccess.DatasourceCall) Timestamp(java.sql.Timestamp) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) Vector(java.util.Vector) ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) Blob(java.sql.Blob) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) PrivilegedInvokeConstructor(org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor) Constructor(java.lang.reflect.Constructor) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) JPAQuery(org.eclipse.persistence.internal.jpa.JPAQuery) SimpleXMLFormatModel(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatModel) Date(java.sql.Date) SessionLog(org.eclipse.persistence.logging.SessionLog) SQLXML(java.sql.SQLXML) OutputParameterForCallableStatement(org.eclipse.persistence.internal.databaseaccess.OutputParameterForCallableStatement) ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 2 with SimpleXMLFormat

use of org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormat 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)

Example 3 with SimpleXMLFormat

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

the class BaseDBWSBuilderHelper method handleSimpleXMLFormat.

/**
 * Apply SimpleXMLFormat if 'isSimpleXMLFormat' is true.  The SimpleXMLFormat is
 * configured based on the given ProcedureOperationModel's simpleXMLFormatTag
 * and xmlTag (if set) and set on the given Result.  A descriptor is also added
 * to the OXProject if none exists.
 */
protected void handleSimpleXMLFormat(boolean isSimpleXMLFormat, Result result, ProcedureOperationModel procedureOperationModel) {
    if (isSimpleXMLFormat || result.getType() == Util.SXF_QNAME) {
        SimpleXMLFormat sxf = new SimpleXMLFormat();
        String simpleXMLFormatTag = procedureOperationModel.getSimpleXMLFormatTag();
        if (simpleXMLFormatTag != null && simpleXMLFormatTag.length() > 0) {
            sxf.setSimpleXMLFormatTag(simpleXMLFormatTag);
        }
        String xmlTag = procedureOperationModel.getXmlTag();
        if (xmlTag != null && xmlTag.length() > 0) {
            sxf.setXMLTag(xmlTag);
        }
        result.setSimpleXMLFormat(sxf);
        // check to see if the O-X project needs descriptor for SimpleXMLFormat
        if (dbwsBuilder.getOxProject().getDescriptorForAlias(DEFAULT_SIMPLE_XML_FORMAT_TAG) == null) {
            SimpleXMLFormatProject sxfProject = new SimpleXMLFormatProject();
            dbwsBuilder.getOxProject().addDescriptor(sxfProject.buildXRRowSetModelDescriptor());
        }
    }
}
Also used : SimpleXMLFormatProject(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject) SimpleXMLFormat(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormat) Util.addSimpleXMLFormat(org.eclipse.persistence.tools.dbws.Util.addSimpleXMLFormat) Util.requiresSimpleXMLFormat(org.eclipse.persistence.tools.dbws.Util.requiresSimpleXMLFormat)

Aggregations

SimpleXMLFormat (org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormat)3 SimpleXMLFormatProject (org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject)2 Util.addSimpleXMLFormat (org.eclipse.persistence.tools.dbws.Util.addSimpleXMLFormat)2 Util.requiresSimpleXMLFormat (org.eclipse.persistence.tools.dbws.Util.requiresSimpleXMLFormat)2 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Blob (java.sql.Blob)1 Date (java.sql.Date)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Vector (java.util.Vector)1 DatasourceCall (org.eclipse.persistence.internal.databaseaccess.DatasourceCall)1 OutputParameterForCallableStatement (org.eclipse.persistence.internal.databaseaccess.OutputParameterForCallableStatement)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 JPAQuery (org.eclipse.persistence.internal.jpa.JPAQuery)1 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)1