Search in sources :

Example 1 with UpdateRequestImpl

use of ddf.catalog.operation.impl.UpdateRequestImpl 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 2 with UpdateRequestImpl

use of ddf.catalog.operation.impl.UpdateRequestImpl in project ddf by codice.

the class TestRegistryStore method testUpdateNonRegistryMetacard.

@Test(expected = IngestException.class)
public void testUpdateNonRegistryMetacard() throws Exception {
    MetacardImpl updatedMcard = getDefaultMetacard();
    updatedMcard.setAttribute(RegistryObjectMetacardType.REGISTRY_ID, null);
    UpdateRequestImpl request = new UpdateRequestImpl("testId", updatedMcard);
    registryStore.update(request);
}
Also used : UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 3 with UpdateRequestImpl

use of ddf.catalog.operation.impl.UpdateRequestImpl in project ddf by codice.

the class WorkspacePreIngestPluginTest method update.

private static UpdateRequest update(Metacard original, Metacard updated) {
    UpdateRequestImpl request = new UpdateRequestImpl(original.getId(), updated);
    OperationTransaction transaction = new OperationTransactionImpl(OperationTransaction.OperationType.UPDATE, Arrays.asList(original));
    request.setProperties(Collections.singletonMap(Constants.OPERATION_TRANSACTION_KEY, transaction));
    return request;
}
Also used : OperationTransaction(ddf.catalog.operation.OperationTransaction) OperationTransactionImpl(ddf.catalog.operation.impl.OperationTransactionImpl) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl)

Example 4 with UpdateRequestImpl

use of ddf.catalog.operation.impl.UpdateRequestImpl in project ddf by codice.

the class MetacardEditEndpoint method deleteAttribute.

@DELETE
@Path("/{id}/{attribute}")
public Response deleteAttribute(@Context HttpServletResponse response, @PathParam("id") String id, @PathParam("attribute") String attribute, String value) throws Exception {
    Metacard metacard = endpointUtil.getMetacard(id);
    Attribute metacardAttribute = metacard.getAttribute(attribute);
    if (metacardAttribute == null) {
        return Response.ok().build();
    }
    metacard.setAttribute(new AttributeImpl(attribute, (Serializable) null));
    catalogFramework.update(new UpdateRequestImpl(id, metacard));
    return Response.ok().build();
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 5 with UpdateRequestImpl

use of ddf.catalog.operation.impl.UpdateRequestImpl in project ddf by codice.

the class TestMetacardGroomerPlugin method testUpdateByAlternativeIdentifier.

@Test
public void testUpdateByAlternativeIdentifier() throws PluginExecutionException, StopProcessingException, URISyntaxException {
    Date snapshotOfNow = new Date();
    Metacard inputMetacard = getStandardMetacard(null);
    URI[] uris = new URI[1];
    uris[0] = new URI(SAMPLE_ID);
    UpdateRequestImpl inputRequest = new UpdateRequestImpl(uris, Arrays.asList(copy(inputMetacard)));
    UpdateRequest returnedRequest = plugin.process(inputRequest);
    assertNotNull(returnedRequest);
    assertThat(returnedRequest.getUpdates().size(), is(1));
    Metacard outputMetacard = returnedRequest.getUpdates().get(0).getValue();
    assertThat(inputMetacard.getId(), is(outputMetacard.getId()));
    assertEquals(DEFAULT_TITLE, outputMetacard.getTitle());
    assertEquals(DEFAULT_LOCATION, outputMetacard.getLocation());
    assertEquals(DEFAULT_TYPE, outputMetacard.getContentTypeName());
    assertEquals(DEFAULT_VERSION, outputMetacard.getContentTypeVersion());
    assertThat(outputMetacard.getMetadata(), is(DEFAULT_METADATA));
    assertThat(outputMetacard.getCreatedDate().getTime(), is(inputMetacard.getCreatedDate().getTime()));
    assertThat(((Date) outputMetacard.getAttribute(Core.METACARD_CREATED).getValue()).getTime(), equalTo(((Date) inputMetacard.getAttribute(Core.METACARD_CREATED).getValue()).getTime()));
    assertThat(outputMetacard.getModifiedDate().getTime(), is(equalTo(inputMetacard.getModifiedDate().getTime())));
    assertThat(((Date) outputMetacard.getAttribute(Core.METACARD_MODIFIED).getValue()).getTime(), is(greaterThanOrEqualTo(snapshotOfNow.getTime())));
    assertEquals(inputMetacard.getEffectiveDate(), outputMetacard.getEffectiveDate());
    assertEquals(inputMetacard.getExpirationDate(), outputMetacard.getExpirationDate());
    assertTrue(Arrays.equals(inputMetacard.getThumbnail(), outputMetacard.getThumbnail()));
    assertEquals(inputMetacard.getLocation(), outputMetacard.getLocation());
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateRequest(ddf.catalog.operation.UpdateRequest) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) URI(java.net.URI) Date(java.util.Date) Test(org.junit.Test)

Aggregations

UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)57 Metacard (ddf.catalog.data.Metacard)42 ArrayList (java.util.ArrayList)35 Test (org.junit.Test)34 UpdateRequest (ddf.catalog.operation.UpdateRequest)28 HashMap (java.util.HashMap)28 Serializable (java.io.Serializable)26 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)21 UpdateResponse (ddf.catalog.operation.UpdateResponse)19 IngestException (ddf.catalog.source.IngestException)13 AbstractMap (java.util.AbstractMap)13 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 CatalogFramework (ddf.catalog.CatalogFramework)11 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)10 List (java.util.List)10 Result (ddf.catalog.data.Result)9 URI (java.net.URI)9 Entry (java.util.Map.Entry)9