Search in sources :

Example 1 with InsertActionImpl

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

the class AbstractCswStore method create.

@Override
public CreateResponse create(CreateRequest createRequest) throws IngestException {
    Map<String, Serializable> properties = new HashMap<>();
    validateOperation();
    Subject subject = (Subject) createRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
    Csw csw = factory.getClientForSubject(subject);
    CswTransactionRequest transactionRequest = getTransactionRequest();
    List<Metacard> metacards = createRequest.getMetacards();
    List<String> metacardIds = metacards.stream().map(Metacard::getId).collect(Collectors.toList());
    List<Metacard> createdMetacards = new ArrayList<>();
    List<Filter> createdMetacardFilters;
    HashSet<ProcessingDetails> errors = new HashSet<>();
    String insertTypeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
    if (insertTypeName == null) {
        throw new IngestException("Could not find transformer for output schema " + cswSourceConfiguration.getOutputSchema());
    }
    transactionRequest.getInsertActions().add(new InsertActionImpl(insertTypeName, null, metacards));
    try {
        TransactionResponseType response = csw.transaction(transactionRequest);
        Set<String> processedIds = new HashSet<>();
        // dive down into the response to get the created ID's. We need these so we can query
        // the source again to get the created metacards and put them in the result
        createdMetacardFilters = response.getInsertResult().stream().map(InsertResultType::getBriefRecord).flatMap(Collection::stream).map(BriefRecordType::getIdentifier).flatMap(Collection::stream).map(JAXBElement::getValue).map(SimpleLiteral::getContent).flatMap(Collection::stream).map(id -> {
            processedIds.add(id);
            return filterBuilder.attribute(Core.ID).is().equalTo().text(id);
        }).collect(Collectors.toList());
        metacardIds.removeAll(processedIds);
        errors.addAll(metacardIds.stream().map(id -> new ProcessingDetailsImpl(id, null, "Failed to create metacard")).collect(Collectors.toList()));
    } catch (CswException e) {
        throw new IngestException("Csw Transaction Failed : ", e);
    }
    try {
        createdMetacards = transactionQuery(createdMetacardFilters, subject);
    } catch (UnsupportedQueryException e) {
        errors.add(new ProcessingDetailsImpl(this.getId(), e, "Failed to retrieve newly created metacards"));
    }
    return new CreateResponseImpl(createRequest, properties, createdMetacards, errors);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) ProcessingDetails(ddf.catalog.operation.ProcessingDetails) IngestException(ddf.catalog.source.IngestException) SimpleLiteral(net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral) HashSet(java.util.HashSet) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Subject(ddf.security.Subject) Metacard(ddf.catalog.data.Metacard) InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) Filter(org.opengis.filter.Filter) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) Collection(java.util.Collection) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl)

Example 2 with InsertActionImpl

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

the class CswEndpointTest method testIngestTransaction.

@Test
public void testIngestTransaction() throws CswException, SourceUnavailableException, FederationException, IngestException {
    CswTransactionRequest request = new CswTransactionRequest();
    request.getInsertActions().add(new InsertActionImpl(CswConstants.CSW_TYPE, null, Arrays.asList(new MetacardImpl())));
    TransactionResponseType response = csw.transaction(request);
    assertThat(response, notNullValue());
    assertThat(response.getInsertResult().isEmpty(), is(true));
    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));
    verifyMarshalResponse(response, "net.opengis.cat.csw.v_2_0_2:net.opengis.filter.v_1_1_0:net.opengis.gml.v_3_1_1", cswQnameOutPutSchema);
}
Also used : InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) 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 3 with InsertActionImpl

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

the class TransactionRequestConverterTest 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 InsertActionImpl(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 : InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) Diff(org.custommonkey.xmlunit.Diff) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 4 with InsertActionImpl

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

the class TransactionRequestConverterTest method testMultipleOperations.

@Test
public void testMultipleOperations() throws Exception {
    CswTransactionRequest transactionRequest = new CswTransactionRequest();
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId(METACARD_ID);
    transactionRequest.setService(CswConstants.CSW);
    transactionRequest.setVerbose(true);
    transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
    InsertAction insertAction = new InsertActionImpl(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
    transactionRequest.getInsertActions().add(insertAction);
    UpdateAction updateAction = new UpdateActionImpl(metacard, CswConstants.CSW_METACARD_TYPE_NAME, null);
    transactionRequest.getUpdateActions().add(updateAction);
    DeleteType deleteType = new DeleteType();
    QueryConstraintType queryConstraintType = new QueryConstraintType();
    queryConstraintType.setCqlText("identifier = " + METACARD_ID);
    deleteType.setConstraint(queryConstraintType);
    DeleteAction deleteAction = new DeleteActionImpl(deleteType, null);
    transactionRequest.getDeleteActions().add(deleteAction);
    String xml = xStream.toXML(transactionRequest);
    Diff diff = XMLUnit.compareXML(xml, EXPECTED_MULTI_OP_XML);
    assertThat(diff.similar(), is(true));
}
Also used : InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) UpdateActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateActionImpl) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) Diff(org.custommonkey.xmlunit.Diff) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DeleteActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Test(org.junit.Test)

Example 5 with InsertActionImpl

use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl 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 InsertActionImpl(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 DeleteActionImpl(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) InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) StringWriter(java.io.StringWriter) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) ArrayList(java.util.ArrayList) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DeleteActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl) 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)

Aggregations

CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)6 InsertActionImpl (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl)6 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)5 Test (org.junit.Test)4 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)3 Metacard (ddf.catalog.data.Metacard)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DeleteType (net.opengis.cat.csw.v_2_0_2.DeleteType)2 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)2 InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction)2 UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction)2 DeleteActionImpl (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl)2 Diff (org.custommonkey.xmlunit.Diff)2 ProcessingDetails (ddf.catalog.operation.ProcessingDetails)1 CreateResponseImpl (ddf.catalog.operation.impl.CreateResponseImpl)1 ProcessingDetailsImpl (ddf.catalog.operation.impl.ProcessingDetailsImpl)1 IngestException (ddf.catalog.source.IngestException)1 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)1 Subject (ddf.security.Subject)1