Search in sources :

Example 6 with EdmDataServices

use of org.odata4j.edm.EdmDataServices in project teiid by teiid.

the class TestODataUpdateExecution method testArrayInsert.

@Test
public void testArrayInsert() throws Exception {
    ModelMetaData model = new ModelMetaData();
    model.setName("nw");
    model.setModelType(Type.PHYSICAL);
    MetadataFactory mf = new MetadataFactory("northwind", 1, SystemMetadata.getInstance().getRuntimeTypeMap(), model);
    EdmDataServices edm = new EdmxFormatParser().parseMetadata(StaxUtil.newXMLEventReader(new FileReader(UnitTestUtil.getTestDataFile("arraytest.xml"))));
    ODataMetadataProcessor metadataProcessor = new ODataMetadataProcessor();
    // $NON-NLS-1$
    PropertiesUtils.setBeanProperties(metadataProcessor, mf.getModelProperties(), "importer");
    metadataProcessor.getMetadata(mf, edm);
    Column c = mf.getSchema().getTable("G2").getColumnByName("e3");
    assertEquals("integer[]", c.getRuntimeType());
    String ddl = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    TransformationMetadata metadata = RealMetadataFactory.fromDDL(ddl, "northwind", "nw");
    String query = "insert into G2 (e1, e3) values(1, (1,2,3))";
    String expectedURL = "G2";
    String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" xml:base=\"http://localhost:8080/odata/loopy/\">\n" + "   <title type=\"text\">VM1.x</title>\n" + "   <id>http://localhost:8080/odata/loopy/VM1.x</id>\n" + "   <updated>2015-10-14T19:36:58Z</updated>\n" + "   <link rel=\"self\" title=\"VM1.x\" href=\"VM1.x\" />\n" + "   <entry>\n" + "      <id>http://localhost:8080/odata/loopy/VM1.x('x')</id>\n" + "      <title type=\"text\" />\n" + "      <updated>2015-10-14T19:36:58Z</updated>\n" + "      <author>\n" + "         <name />\n" + "      </author>\n" + "      <link rel=\"edit\" title=\"x\" href=\"VM1.x('x')\" />\n" + "      <category term=\"PM1.G2\" scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" />\n" + "      <content type=\"application/xml\">\n" + "         <m:properties>\n" + "            <d:e1>32</d:e1>\n" + "            <d:e3 m:type=\"Collection(Edm.Int32)\">\n" + "               <d:element>1</d:element>\n" + "               <d:element>2</d:element>\n" + "               <d:element>3</d:element>\n" + "            </d:e3>\n" + "         </m:properties>\n" + "      </content>\n" + "   </entry>\n" + "</feed>";
    String payload = helpExecute(query, result, expectedURL, new int[] { 201, 201 }, metadata, 1);
    String expected = "<category term=\"PM1.G2\" " + "scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\">" + "</category>" + "<content type=\"application/xml\">" + "<m:properties>" + "<d:e1 m:type=\"Edm.Int32\">1</d:e1>" + "<d:e3 m:type=\"Collection(Edm.Int32)\">" + "<d:element>1</d:element>" + "<d:element>2</d:element>" + "<d:element>3</d:element>" + "</d:e3>" + "</m:properties>" + "</content>" + "</entry>";
    assertTrue(expected, payload.endsWith(expected));
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) Column(org.teiid.metadata.Column) EdmxFormatParser(org.odata4j.format.xml.EdmxFormatParser) EdmDataServices(org.odata4j.edm.EdmDataServices) FileReader(java.io.FileReader) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 7 with EdmDataServices

use of org.odata4j.edm.EdmDataServices in project teiid by teiid.

the class ODataUpdateExecution method execute.

@Override
public void execute() throws TranslatorException {
    if (this.visitor.getMethod().equals("DELETE")) {
        // $NON-NLS-1$
        // DELETE
        BinaryWSProcedureExecution execution = executeDirect(this.visitor.getMethod(), this.visitor.buildURL(), null, getDefaultHeaders());
        if (execution.getResponseCode() != Status.OK.getStatusCode() && (execution.getResponseCode() != Status.NO_CONTENT.getStatusCode())) {
            throw buildError(execution);
        }
    } else if (this.visitor.getMethod().equals("PUT")) {
        // $NON-NLS-1$
        // UPDATE
        Schema schema = visitor.getTable().getParent();
        EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
        // $NON-NLS-1$
        BinaryWSProcedureExecution execution = executeDirect("GET", this.visitor.buildURL(), null, getDefaultHeaders());
        if (execution.getResponseCode() == Status.OK.getStatusCode()) {
            // $NON-NLS-1$
            String etag = getHeader(execution, "ETag");
            String payload = buildPayload(this.visitor.getTable().getName(), this.visitor.getPayload(), edm);
            this.response = executeWithReturnEntity(this.visitor.getMethod(), this.visitor.buildURL(), payload, this.visitor.getTable().getName(), edm, etag, Status.OK, Status.NO_CONTENT);
            if (this.response != null) {
                if (this.response.hasError()) {
                    throw this.response.getError();
                }
            }
        }
    } else if (this.visitor.getMethod().equals("POST")) {
        // $NON-NLS-1$
        // INSERT
        Schema schema = visitor.getTable().getParent();
        EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
        String payload = buildPayload(this.visitor.getTable().getName(), this.visitor.getPayload(), edm);
        this.response = executeWithReturnEntity(this.visitor.getMethod(), this.visitor.buildURL(), payload, this.visitor.getTable().getName(), edm, null, Status.CREATED);
        if (this.response != null) {
            if (this.response.hasError()) {
                throw this.response.getError();
            }
        }
    }
}
Also used : BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) Schema(org.teiid.metadata.Schema) EdmDataServices(org.odata4j.edm.EdmDataServices)

Aggregations

EdmDataServices (org.odata4j.edm.EdmDataServices)7 EdmxFormatParser (org.odata4j.format.xml.EdmxFormatParser)4 MetadataFactory (org.teiid.metadata.MetadataFactory)4 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)4 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)4 FileReader (java.io.FileReader)3 Test (org.junit.Test)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 Column (org.teiid.metadata.Column)3 Schema (org.teiid.metadata.Schema)3 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)3 InputStreamReader (java.io.InputStreamReader)2 Blob (java.sql.Blob)2 SQLException (java.sql.SQLException)2 TranslatorException (org.teiid.translator.TranslatorException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 List (java.util.List)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1