Search in sources :

Example 6 with QueryConstraintType

use of net.opengis.cat.csw.v_2_0_2.QueryConstraintType in project ddf by codice.

the class TestTransactionMessageBodyReader method testReadUpdateByConstraintFrom.

@Test
public void testReadUpdateByConstraintFrom() throws IOException, ParseException {
    TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mock(Converter.class), CswQueryFactoryTest.getCswMetacardType(), registry);
    CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(UPDATE_REQUEST_BY_CONSTRAINT_XML));
    assertThat(request, notNullValue());
    assertThat(request.getInsertActions().size(), is(0));
    assertThat(request.getDeleteActions().size(), is(0));
    assertThat(request.getUpdateActions().size(), is(1));
    UpdateAction updateAction = request.getUpdateActions().get(0);
    assertThat(updateAction, notNullValue());
    assertThat(updateAction.getMetacard(), nullValue());
    Map<String, Serializable> recordProperties = updateAction.getRecordProperties();
    assertThat(recordProperties, notNullValue());
    assertThat(recordProperties.size(), is(4));
    Serializable newSubjectValue = recordProperties.get(Topic.CATEGORY);
    assertThat(newSubjectValue, notNullValue());
    assertThat(newSubjectValue, is("Foo"));
    Serializable newDateValue = recordProperties.get("modified");
    assertThat(newDateValue, notNullValue());
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = simpleDateFormat.parse("2015-07-21");
    assertThat(newDateValue, is(date));
    Serializable newLocationValue = recordProperties.get("location");
    assertThat(newLocationValue, notNullValue());
    assertThat(newLocationValue, is("POLYGON ((1.0 2.0, 3.0 2.0, 3.0 4.0, 1.0 4.0, 1.0 2.0))"));
    Serializable newFormatValue = recordProperties.get("media.format");
    // No <Value> was specified in the request.
    assertThat(newFormatValue, nullValue());
    QueryConstraintType constraint = updateAction.getConstraint();
    assertThat(constraint, notNullValue());
    FilterType filter = constraint.getFilter();
    assertThat(filter, notNullValue());
    assertThat(filter.isSetComparisonOps(), is(true));
    assertThat(filter.isSetLogicOps(), is(false));
    assertThat(filter.isSetSpatialOps(), is(false));
    assertThat(request.getService(), is(CswConstants.CSW));
    assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
    assertThat(request.isVerbose(), is(false));
}
Also used : Serializable(java.io.Serializable) FilterType(net.opengis.filter.v_1_1_0.FilterType) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) Converter(com.thoughtworks.xstream.converters.Converter) CswRecordConverter(org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 7 with QueryConstraintType

use of net.opengis.cat.csw.v_2_0_2.QueryConstraintType in project ddf by codice.

the class TestTransactionRequestConverter 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 InsertAction(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
    transactionRequest.getInsertActions().add(insertAction);
    UpdateAction updateAction = new UpdateAction(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 DeleteAction(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 : UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.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.common.transaction.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Test(org.junit.Test)

Example 8 with QueryConstraintType

use of net.opengis.cat.csw.v_2_0_2.QueryConstraintType in project ddf by codice.

the class CswEndpoint method updateRecords.

private int updateRecords(UpdateAction updateAction) throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
    if (updateAction.getMetacard() != null) {
        Metacard newRecord = updateAction.getMetacard();
        if (newRecord.getId() != null) {
            UpdateRequest updateRequest = new UpdateRequestImpl(newRecord.getId(), newRecord);
            LOGGER.debug("Attempting to update {} ", newRecord.getId());
            UpdateResponse updateResponse = framework.update(updateRequest);
            return updateResponse.getUpdatedMetacards().size();
        } else {
            throw new CswException("Unable to update record.  No ID was specified in the request.", CswConstants.MISSING_PARAMETER_VALUE, updateAction.getHandle());
        }
    } else if (updateAction.getConstraint() != null) {
        QueryConstraintType constraint = updateAction.getConstraint();
        QueryRequest queryRequest = queryFactory.getQuery(constraint);
        queryRequest = queryFactory.updateQueryRequestTags(queryRequest, schemaTransformerManager.getTransformerSchemaForId(updateAction.getTypeName()));
        QueryResponse response = framework.query(queryRequest);
        if (response.getHits() > 0) {
            Map<String, Serializable> recordProperties = updateAction.getRecordProperties();
            List<String> updatedMetacardIdsList = new ArrayList<>();
            List<Metacard> updatedMetacards = new ArrayList<>();
            for (Result result : response.getResults()) {
                Metacard metacard = result.getMetacard();
                if (metacard != null) {
                    for (Entry<String, Serializable> recordProperty : recordProperties.entrySet()) {
                        Attribute attribute = new AttributeImpl(recordProperty.getKey(), recordProperty.getValue());
                        metacard.setAttribute(attribute);
                    }
                    updatedMetacardIdsList.add(metacard.getId());
                    updatedMetacards.add(metacard);
                }
            }
            if (updatedMetacardIdsList.size() > 0) {
                String[] updatedMetacardIds = updatedMetacardIdsList.toArray(new String[updatedMetacardIdsList.size()]);
                UpdateRequest updateRequest = new UpdateRequestImpl(updatedMetacardIds, updatedMetacards);
                LOGGER.debug("Attempting to update {} metacards.", updatedMetacardIdsList.size());
                UpdateResponse updateResponse = framework.update(updateRequest);
                return updateResponse.getUpdatedMetacards().size();
            }
        }
    }
    return 0;
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) UpdateRequest(ddf.catalog.operation.UpdateRequest) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Result(ddf.catalog.data.Result) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) Entry(java.util.Map.Entry) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with QueryConstraintType

use of net.opengis.cat.csw.v_2_0_2.QueryConstraintType in project ddf by codice.

the class TestCswEndpoint method testDeleteTransaction.

@Test
public void testDeleteTransaction() throws CswException, UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
    DeleteType deleteType = mock(DeleteType.class);
    doReturn(CswConstants.CSW_RECORD).when(deleteType).getTypeName();
    doReturn("").when(deleteType).getHandle();
    QueryConstraintType queryConstraintType = new QueryConstraintType();
    queryConstraintType.setCqlText("title = \"foo\"");
    doReturn(queryConstraintType).when(deleteType).getConstraint();
    List<Result> results = new ArrayList<>();
    results.add(new ResultImpl(new MetacardImpl()));
    results.add(new ResultImpl(new MetacardImpl()));
    QueryResponse queryResponse = new QueryResponseImpl(null, results, results.size());
    doReturn(queryResponse).when(catalogFramework).query(any(QueryRequest.class));
    List<Metacard> deletedMetacards = new ArrayList<>();
    deletedMetacards.add(new MetacardImpl());
    deletedMetacards.add(new MetacardImpl());
    DeleteResponse deleteResponse = new DeleteResponseImpl(null, null, deletedMetacards);
    doReturn(deleteResponse).when(catalogFramework).delete(any(DeleteRequest.class));
    DeleteAction deleteAction = new DeleteAction(deleteType, DefaultCswRecordMap.getDefaultCswRecordMap().getPrefixToUriMapping());
    CswTransactionRequest deleteRequest = new CswTransactionRequest();
    deleteRequest.getDeleteActions().add(deleteAction);
    deleteRequest.setVersion(CswConstants.VERSION_2_0_2);
    deleteRequest.setService(CswConstants.CSW);
    deleteRequest.setVerbose(false);
    TransactionResponseType response = csw.transaction(deleteRequest);
    assertThat(response, notNullValue());
    TransactionSummaryType summary = response.getTransactionSummary();
    assertThat(summary, notNullValue());
    assertThat(summary.getTotalDeleted().intValue(), is(2));
    assertThat(summary.getTotalInserted().intValue(), is(0));
    assertThat(summary.getTotalUpdated().intValue(), is(0));
    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 : QueryRequest(ddf.catalog.operation.QueryRequest) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) QueryResponse(ddf.catalog.operation.QueryResponse) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Example 10 with QueryConstraintType

use of net.opengis.cat.csw.v_2_0_2.QueryConstraintType in project ddf by codice.

the class CswQueryFactoryTest method cqlSpatialRelativeQuery.

/**
     * Runs a relative spatial CQL Query, verifying that the right filter class is generated based on CQL
     *
     * @param clz Class of filter to generate
     * @param cql CQL Query String
     * @throws UnsupportedQueryException
     * @throws SourceUnavailableException
     * @throws FederationException
     * @throws CswException
     */
private <N extends DistanceBufferOperator> void cqlSpatialRelativeQuery(Class<N> clz, String cql) throws UnsupportedQueryException, SourceUnavailableException, FederationException, CswException {
    GetRecordsType grr = createDefaultPostRecordsRequest();
    QueryType query = new QueryType();
    List<QName> typeNames = new ArrayList<>();
    typeNames.add(new QName(CswConstants.CSW_OUTPUT_SCHEMA, VALID_TYPE, VALID_PREFIX));
    query.setTypeNames(typeNames);
    QueryConstraintType constraint = new QueryConstraintType();
    constraint.setCqlText(cql);
    query.setConstraint(constraint);
    JAXBElement<QueryType> jaxbQuery = new JAXBElement<>(cswQnameOutPutSchema, QueryType.class, query);
    grr.setAbstractQuery(jaxbQuery);
    QueryImpl frameworkQuery = (QueryImpl) queryFactory.getQuery(grr).getQuery();
    assertThat(frameworkQuery.getFilter(), instanceOf(clz));
    @SuppressWarnings("unchecked") N spatial = (N) frameworkQuery.getFilter();
    assertThat(((LiteralExpressionImpl) spatial.getExpression2()).getValue(), is(polygon));
    assertThat(((AttributeExpressionImpl) spatial.getExpression1()).getPropertyName(), is(SPATIAL_TEST_ATTRIBUTE));
    assertThat(spatial.getDistanceUnits(), is(UomOgcMapping.METRE.name()));
    assertThat(spatial.getDistance(), is(EXPECTED_GEO_DISTANCE));
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) JAXBElement(javax.xml.bind.JAXBElement) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType)

Aggregations

QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)24 ArrayList (java.util.ArrayList)17 QName (javax.xml.namespace.QName)14 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)14 JAXBElement (javax.xml.bind.JAXBElement)13 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)13 Test (org.junit.Test)12 QueryImpl (ddf.catalog.operation.impl.QueryImpl)7 FilterType (net.opengis.filter.v_1_1_0.FilterType)7 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)7 Metacard (ddf.catalog.data.Metacard)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)5 QueryRequest (ddf.catalog.operation.QueryRequest)5 Serializable (java.io.Serializable)5 UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction)5 HashMap (java.util.HashMap)4 DeleteType (net.opengis.cat.csw.v_2_0_2.DeleteType)4 DeleteAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction)4 Result (ddf.catalog.data.Result)3 QueryResponse (ddf.catalog.operation.QueryResponse)3