Search in sources :

Example 1 with SimpleLiteral

use of net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral 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 InsertAction(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) Filter(org.opengis.filter.Filter) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) Collection(java.util.Collection) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl)

Example 2 with SimpleLiteral

use of net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral in project ddf by codice.

the class TestRegistryStore method testCreateNoExistingMetacard.

@Test
public void testCreateNoExistingMetacard() throws Exception {
    Metacard mcard = getDefaultMetacard();
    Csw csw = mock(Csw.class);
    TransactionResponseType responseType = mock(TransactionResponseType.class);
    InsertResultType insertResultType = mock(InsertResultType.class);
    BriefRecordType briefRecord = mock(BriefRecordType.class);
    JAXBElement identifier = mock(JAXBElement.class);
    SimpleLiteral literal = mock(SimpleLiteral.class);
    when(literal.getContent()).thenReturn(Collections.singletonList(mcard.getId()));
    when(identifier.getValue()).thenReturn(literal);
    when(briefRecord.getIdentifier()).thenReturn(Collections.singletonList(identifier));
    when(insertResultType.getBriefRecord()).thenReturn(Collections.singletonList(briefRecord));
    when(responseType.getInsertResult()).thenReturn(Collections.singletonList(insertResultType));
    when(factory.getClientForSubject(any())).thenReturn(csw);
    when(csw.transaction(any())).thenReturn(responseType);
    when(transformer.getTransformerIdForSchema(any())).thenReturn("myInsertType");
    queryResults.add(new ResultImpl(mcard));
    CreateRequest request = new CreateRequestImpl(mcard);
    CreateResponse response = registryStore.create(request);
    assertThat(response.getCreatedMetacards().get(0), is(mcard));
}
Also used : Metacard(ddf.catalog.data.Metacard) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) SimpleLiteral(net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral) ResultImpl(ddf.catalog.data.impl.ResultImpl) JAXBElement(javax.xml.bind.JAXBElement) InsertResultType(net.opengis.cat.csw.v_2_0_2.InsertResultType) BriefRecordType(net.opengis.cat.csw.v_2_0_2.BriefRecordType) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) Test(org.junit.Test)

Example 3 with SimpleLiteral

use of net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral in project ddf by codice.

the class TestCswSourceBase method generateCswCollection.

protected CswRecordCollection generateCswCollection(String file) {
    InputStream stream = getClass().getResourceAsStream(file);
    GetRecordsResponseType recordsResponse = parseXml(stream);
    GetRecordsResponseType records = new GetRecordsResponseType();
    recordsResponse.copyTo(records);
    List<Metacard> cswRecords = new LinkedList<>();
    for (JAXBElement<? extends AbstractRecordType> rec : records.getSearchResults().getAbstractRecord()) {
        MetacardImpl metacard = new MetacardImpl();
        cswRecords.add(metacard);
        if (rec.getValue() instanceof BriefRecordType) {
            BriefRecordType record = (BriefRecordType) rec.getValue();
            metacard.setId(record.getIdentifier().get(0).getValue().getContent().get(0));
            if (!CollectionUtils.isEmpty(record.getType().getContent())) {
                metacard.setContentTypeName(record.getType().getContent().get(0));
            }
        } else if (rec.getValue() instanceof SummaryRecordType) {
            SummaryRecordType record = (SummaryRecordType) rec.getValue();
            metacard.setId(record.getIdentifier().get(0).getValue().getContent().get(0));
            if (!CollectionUtils.isEmpty(record.getType().getContent())) {
                metacard.setContentTypeName(record.getType().getContent().get(0));
            }
        } else if (rec.getValue() instanceof RecordType) {
            RecordType record = (RecordType) rec.getValue();
            for (JAXBElement<SimpleLiteral> jb : record.getDCElement()) {
                if ("identifier".equals(jb.getName().getLocalPart())) {
                    metacard.setId(jb.getValue().getContent().get(0));
                }
                if ("type".equals(jb.getName().getLocalPart()) && !CollectionUtils.isEmpty(jb.getValue().getContent())) {
                    metacard.setContentTypeName(jb.getValue().getContent().get(0));
                }
            }
        }
    }
    CswRecordCollection collection = new CswRecordCollection();
    collection.setCswRecords(cswRecords);
    collection.setNumberOfRecordsMatched(records.getSearchResults().getNumberOfRecordsMatched().intValue());
    collection.setNumberOfRecordsReturned(records.getSearchResults().getNumberOfRecordsReturned().intValue());
    return collection;
}
Also used : SummaryRecordType(net.opengis.cat.csw.v_2_0_2.SummaryRecordType) InputStream(java.io.InputStream) BriefRecordType(net.opengis.cat.csw.v_2_0_2.BriefRecordType) LinkedList(java.util.LinkedList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) AbstractRecordType(net.opengis.cat.csw.v_2_0_2.AbstractRecordType) RecordType(net.opengis.cat.csw.v_2_0_2.RecordType) SummaryRecordType(net.opengis.cat.csw.v_2_0_2.SummaryRecordType) BriefRecordType(net.opengis.cat.csw.v_2_0_2.BriefRecordType) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) GetRecordsResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType) SimpleLiteral(net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral)

Example 4 with SimpleLiteral

use of net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral in project ddf by codice.

the class CswEndpoint method getInsertResultFromResponse.

private InsertResultType getInsertResultFromResponse(CreateResponse createResponse) throws CswException {
    InsertResultType result = new InsertResultType();
    WKTReader reader = new WKTReader();
    for (Metacard metacard : createResponse.getCreatedMetacards()) {
        BoundingBoxType boundingBox = new BoundingBoxType();
        Geometry geometry = null;
        String bbox = null;
        try {
            if (metacard.getAttribute(CswConstants.BBOX_PROP) != null) {
                bbox = metacard.getAttribute(CswConstants.BBOX_PROP).getValue().toString();
                geometry = reader.read(bbox);
            } else if (StringUtils.isNotBlank(metacard.getLocation())) {
                bbox = metacard.getLocation();
                geometry = reader.read(bbox);
            }
        } catch (ParseException e) {
            LOGGER.debug("Unable to parse BoundingBox : {}", bbox, e);
        }
        BriefRecordType briefRecordType = new BriefRecordType();
        if (geometry != null) {
            Envelope bounds = geometry.getEnvelopeInternal();
            if (bounds != null) {
                boundingBox.setCrs(CswConstants.SRS_NAME);
                boundingBox.setLowerCorner(Arrays.asList(bounds.getMinX(), bounds.getMinY()));
                boundingBox.setUpperCorner(Arrays.asList(bounds.getMaxX(), bounds.getMaxY()));
                briefRecordType.getBoundingBox().add(new net.opengis.ows.v_1_0_0.ObjectFactory().createBoundingBox(boundingBox));
            }
        }
        SimpleLiteral identifier = new SimpleLiteral();
        identifier.getContent().add(metacard.getId());
        briefRecordType.getIdentifier().add(new JAXBElement<>(CswConstants.DC_IDENTIFIER_QNAME, SimpleLiteral.class, identifier));
        SimpleLiteral title = new SimpleLiteral();
        title.getContent().add(metacard.getTitle());
        briefRecordType.getTitle().add(new JAXBElement<>(CswConstants.DC_TITLE_QNAME, SimpleLiteral.class, title));
        SimpleLiteral type = new SimpleLiteral();
        type.getContent().add(metacard.getContentTypeName());
        briefRecordType.setType(type);
        result.getBriefRecord().add(briefRecordType);
    }
    return result;
}
Also used : WKTReader(com.vividsolutions.jts.io.WKTReader) Envelope(com.vividsolutions.jts.geom.Envelope) BriefRecordType(net.opengis.cat.csw.v_2_0_2.BriefRecordType) BoundingBoxType(net.opengis.ows.v_1_0_0.BoundingBoxType) Geometry(com.vividsolutions.jts.geom.Geometry) Metacard(ddf.catalog.data.Metacard) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) SimpleLiteral(net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(com.vividsolutions.jts.io.ParseException) InsertResultType(net.opengis.cat.csw.v_2_0_2.InsertResultType)

Aggregations

Metacard (ddf.catalog.data.Metacard)4 SimpleLiteral (net.opengis.cat.csw.v_2_0_2.dc.elements.SimpleLiteral)4 BriefRecordType (net.opengis.cat.csw.v_2_0_2.BriefRecordType)3 InsertResultType (net.opengis.cat.csw.v_2_0_2.InsertResultType)2 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)2 Csw (org.codice.ddf.spatial.ogc.csw.catalog.common.Csw)2 Envelope (com.vividsolutions.jts.geom.Envelope)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 ResultImpl (ddf.catalog.data.impl.ResultImpl)1 CreateRequest (ddf.catalog.operation.CreateRequest)1 CreateResponse (ddf.catalog.operation.CreateResponse)1 ProcessingDetails (ddf.catalog.operation.ProcessingDetails)1 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)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