Search in sources :

Example 6 with InsertAction

use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction in project ddf by codice.

the class TransactionRequestConverter method marshal.

@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
    if (o == null || !CswTransactionRequest.class.isAssignableFrom(o.getClass())) {
        return;
    }
    CswTransactionRequest request = (CswTransactionRequest) o;
    writer.addAttribute(CswConstants.SERVICE, request.getService());
    writer.addAttribute(CswConstants.VERSION, request.getVersion());
    writer.addAttribute(CswConstants.VERBOSE_RESPONSE, String.valueOf(request.isVerbose()));
    writer.addAttribute(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.CSW_NAMESPACE_PREFIX, CswConstants.CSW_OUTPUT_SCHEMA);
    writer.addAttribute(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.OGC_NAMESPACE_PREFIX, CswConstants.OGC_SCHEMA);
    for (InsertAction insertAction : request.getInsertActions()) {
        writer.startNode(CswConstants.CSW_TRANSACTION_INSERT_NODE);
        writer.addAttribute(CswConstants.TYPE_NAME_PARAMETER, insertAction.getTypeName());
        marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
        marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, insertAction.getTypeName());
        for (Metacard metacard : insertAction.getRecords()) {
            marshallingContext.convertAnother(metacard, delegatingTransformer);
        }
        writer.endNode();
    }
    for (UpdateAction updateAction : request.getUpdateActions()) {
        writer.startNode(CswConstants.CSW_TRANSACTION_UPDATE_NODE);
        writer.addAttribute(CswConstants.TYPE_NAME_PARAMETER, updateAction.getTypeName());
        marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
        marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, updateAction.getTypeName());
        marshallingContext.convertAnother(updateAction.getMetacard(), delegatingTransformer);
        writer.endNode();
    }
    for (DeleteAction deleteAction : request.getDeleteActions()) {
        writer.startNode(CswConstants.CSW_TRANSACTION_DELETE_NODE);
        writer.addAttribute(CswConstants.TYPE_NAME_PARAMETER, deleteAction.getTypeName());
        writer.startNode(CswConstants.CSW_CONSTRAINT);
        writer.addAttribute(CswConstants.VERSION, CswConstants.CONSTRAINT_VERSION);
        writer.startNode(CswConstants.CSW_CQL_TEXT);
        writer.setValue(deleteAction.getConstraint().getCqlText());
        writer.endNode();
        writer.endNode();
        writer.endNode();
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction)

Example 7 with InsertAction

use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction in project ddf by codice.

the class TestCswEndpoint method testIngestVerboseTransaction.

@Test
public void testIngestVerboseTransaction() throws CswException, SourceUnavailableException, FederationException, IngestException {
    CswTransactionRequest request = new CswTransactionRequest();
    request.getInsertActions().add(new InsertAction(CswConstants.CSW_TYPE, null, Arrays.asList(new MetacardImpl())));
    request.setVerbose(true);
    TransactionResponseType response = csw.transaction(request);
    assertThat(response, notNullValue());
    assertThat(response.getInsertResult().size(), is(1));
    assertThat(response.getTransactionSummary(), notNullValue());
    TransactionSummaryType summary = response.getTransactionSummary();
    assertThat(summary.getTotalDeleted().intValue(), is(0));
    assertThat(summary.getTotalUpdated().intValue(), is(0));
    assertThat(summary.getTotalInserted().intValue(), is(1));
    String contextPath = StringUtils.join(new String[] { CswConstants.OGC_CSW_PACKAGE, CswConstants.OGC_FILTER_PACKAGE, CswConstants.OGC_GML_PACKAGE, CswConstants.OGC_OWS_PACKAGE }, ":");
    verifyMarshalResponse(response, contextPath, new QName(CswConstants.CSW_OUTPUT_SCHEMA, CswConstants.TRANSACTION));
}
Also used : CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) QName(javax.xml.namespace.QName) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) Test(org.junit.Test)

Example 8 with InsertAction

use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction in project ddf by codice.

the class TransactionRequestConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    CswTransactionRequest cswTransactionRequest = new CswTransactionRequest();
    cswTransactionRequest.setVersion(reader.getAttribute(CswConstants.VERSION));
    cswTransactionRequest.setService(reader.getAttribute(CswConstants.SERVICE));
    cswTransactionRequest.setVerbose(Boolean.valueOf(reader.getAttribute(CswConstants.VERBOSE_RESPONSE)));
    XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        if (reader.getNodeName().contains("Insert")) {
            String typeName = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.TYPE_NAME_PARAMETER), CswConstants.CSW_RECORD);
            String handle = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.HANDLE_PARAMETER), "");
            context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
            context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, typeName);
            List<Metacard> metacards = new ArrayList<>();
            // Loop through the individual records to be inserted, converting each into a Metacard
            while (reader.hasMoreChildren()) {
                // move down to the record's tag
                reader.moveDown();
                Metacard metacard = (Metacard) context.convertAnother(null, MetacardImpl.class, delegatingTransformer);
                if (metacard != null) {
                    metacards.add(metacard);
                }
                // move back up to the <SearchResults> parent of the <csw:Record> tags
                reader.moveUp();
            }
            cswTransactionRequest.getInsertActions().add(new InsertAction(typeName, handle, metacards));
        } else if (reader.getNodeName().contains("Delete")) {
            XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
            Map<String, String> xmlnsAttributeToUriMappings = getXmlnsAttributeToUriMappingsFromContext(context);
            Map<String, String> prefixToUriMappings = getPrefixToUriMappingsFromXmlnsAttributes(xmlnsAttributeToUriMappings);
            StringWriter writer = new StringWriter();
            XStreamAttributeCopier.copyXml(reader, writer, xmlnsAttributeToUriMappings);
            DeleteType deleteType = getElementFromXml(writer.toString(), DeleteType.class);
            cswTransactionRequest.getDeleteActions().add(new DeleteAction(deleteType, prefixToUriMappings));
        } else if (reader.getNodeName().contains("Update")) {
            XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
            UpdateAction updateAction = parseUpdateAction(reader, context);
            cswTransactionRequest.getUpdateActions().add(updateAction);
        }
        reader.moveUp();
    }
    return cswTransactionRequest;
}
Also used : Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) ArrayList(java.util.ArrayList) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DefaultCswRecordMap(org.codice.ddf.spatial.ogc.csw.catalog.common.converter.DefaultCswRecordMap) HashMap(java.util.HashMap) Map(java.util.Map) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 9 with InsertAction

use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction in project ddf by codice.

the class TestTransactionRequestConverter method testValidInsertMarshal.

@Test
public void testValidInsertMarshal() throws SAXException, IOException, XpathException {
    CswTransactionRequest transactionRequest = new CswTransactionRequest();
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId(METACARD_ID);
    InsertAction insertAction = new InsertAction(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
    transactionRequest.getInsertActions().add(insertAction);
    transactionRequest.setService(CswConstants.CSW);
    transactionRequest.setVerbose(true);
    transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
    String xml = xStream.toXML(transactionRequest);
    System.out.println("xml = " + xml);
    System.out.println("EXPECTED_INSERT_XML = " + EXPECTED_INSERT_XML);
    Diff diff = XMLUnit.compareXML(xml, EXPECTED_INSERT_XML);
    assertThat(diff.similar(), is(true));
}
Also used : Diff(org.custommonkey.xmlunit.Diff) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 10 with InsertAction

use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction in project ddf by codice.

the class TestTransactionMessageBodyReader method testReadInsertAndDeleteFrom.

@Test
public void testReadInsertAndDeleteFrom() throws IOException {
    Converter mockConverter = mock(Converter.class);
    when(mockConverter.canConvert(any(Metacard.class.getClass()))).thenReturn(true);
    when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
    TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
    CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(INSERT_AND_DELETE_REQUEST_XML));
    assertThat(request, notNullValue());
    assertThat(request.getDeleteActions().size(), is(1));
    assertThat(request.getInsertActions().size(), is(1));
    assertThat(request.getUpdateActions().size(), is(0));
    DeleteAction deleteAction = request.getDeleteActions().get(0);
    assertThat(deleteAction, notNullValue());
    assertThat(deleteAction.getTypeName(), is(CswConstants.CSW_RECORD));
    assertThat(deleteAction.getHandle(), is("something"));
    assertThat(deleteAction.getConstraint(), notNullValue());
    assertThat(deleteAction.getConstraint().getCqlText().trim(), is("title = 'foo'"));
    InsertAction insertAction = request.getInsertActions().get(0);
    assertThat(insertAction, notNullValue());
    assertThat(insertAction.getRecords().size(), is(1));
    assertThat(request.getService(), is(CswConstants.CSW));
    assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
    assertThat(request.isVerbose(), is(true));
}
Also used : Metacard(ddf.catalog.data.Metacard) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) Converter(com.thoughtworks.xstream.converters.Converter) CswRecordConverter(org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Aggregations

InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction)11 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)10 Test (org.junit.Test)7 DeleteAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction)6 Metacard (ddf.catalog.data.Metacard)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)5 UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction)5 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)4 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)3 Converter (com.thoughtworks.xstream.converters.Converter)2 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)2 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)2 IngestException (ddf.catalog.source.IngestException)2 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DeleteType (net.opengis.cat.csw.v_2_0_2.DeleteType)2 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)2 CswRecordConverter (org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter)2 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)2