Search in sources :

Example 1 with UpdateResponse

use of ddf.catalog.operation.UpdateResponse 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 UpdateResponse

use of ddf.catalog.operation.UpdateResponse in project ddf by codice.

the class SolrProviderTest method testUpdatePendingNrtIndex.

@Test
public void testUpdatePendingNrtIndex() throws Exception {
    deleteAllIn(provider);
    ConfigurationStore.getInstance().setForceAutoCommit(false);
    try {
        MockMetacard metacard = new MockMetacard(Library.getFlagstaffRecord());
        CreateResponse createResponse = create(metacard);
        String id = createResponse.getCreatedMetacards().get(0).getId();
        MockMetacard updatedMetacard = new MockMetacard(Library.getFlagstaffRecord());
        updatedMetacard.setContentTypeName("first");
        UpdateResponse firstUpdateResponse = update(id, updatedMetacard);
        updatedMetacard = new MockMetacard(Library.getFlagstaffRecord());
        updatedMetacard.setContentTypeName("second");
        UpdateResponse secondUpdateResponse = update(id, updatedMetacard);
        verifyContentTypeUpdate(firstUpdateResponse, MockMetacard.DEFAULT_TYPE, "first");
        verifyContentTypeUpdate(secondUpdateResponse, "first", "second");
    } finally {
        ConfigurationStore.getInstance().setForceAutoCommit(true);
    }
}
Also used : UpdateResponse(ddf.catalog.operation.UpdateResponse) CreateResponse(ddf.catalog.operation.CreateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with UpdateResponse

use of ddf.catalog.operation.UpdateResponse in project ddf by codice.

the class SolrProviderTest method testUpdateByMetacardId.

@Test
public void testUpdateByMetacardId() throws Exception {
    deleteAllIn(provider);
    MockMetacard metacard1 = new MockMetacard(Library.getFlagstaffRecord());
    MockMetacard metacard2 = new MockMetacard(Library.getShowLowRecord());
    String uri1 = "http://youwillfindme.com/here";
    String uri2 = "http://youwillfindme.com/there";
    metacard1.setResourceURI(new URI(uri1));
    metacard1.setContentTypeName("oldNitf");
    metacard2.setResourceURI(new URI(uri2));
    metacard2.setContentTypeName("oldNitf2");
    metacard2.setResourceSize("25L");
    List<Metacard> list = Arrays.asList((Metacard) metacard1, metacard2);
    CreateResponse createResponse = create(list);
    List<String> responseStrings = MockMetacard.toStringList(createResponse.getCreatedMetacards());
    assertEquals(2, responseStrings.size());
    /** UPDATE **/
    MockMetacard updatedMetacard1 = new MockMetacard(Library.getTampaRecord());
    MockMetacard updatedMetacard2 = new MockMetacard(Library.getFlagstaffRecord());
    updatedMetacard1.setId(metacard1.getId());
    updatedMetacard1.setContentTypeName("nitf");
    updatedMetacard2.setId(metacard2.getId());
    updatedMetacard2.setResourceURI(new URI(uri2));
    updatedMetacard2.setContentTypeName("nitf2");
    updatedMetacard2.setResourceSize("50L");
    list = Arrays.asList((Metacard) updatedMetacard1, updatedMetacard2);
    String[] ids = { metacard1.getId(), metacard2.getId() };
    UpdateResponse updateResponse = update(ids, list);
    assertEquals("Testing Update operation: ", 2, updateResponse.getUpdatedMetacards().size());
    List<Update> updatedMetacards = updateResponse.getUpdatedMetacards();
    for (Update up : updatedMetacards) {
        Metacard newCard = up.getNewMetacard();
        Metacard oldCard = up.getOldMetacard();
        assertNotNull(oldCard.getResourceURI());
        assertEquals(provider.getId(), oldCard.getSourceId());
        assertEquals(provider.getId(), newCard.getSourceId());
        if (oldCard.getContentTypeName().equals("oldNitf")) {
            assertEquals("nitf", newCard.getContentTypeName());
            // TPA is unique to the document
            assertTrue(newCard.getMetadata().indexOf("TPA") != ALL_RESULTS);
            assertThat(newCard.getResourceURI(), is(nullValue()));
            assertThat(oldCard.getResourceURI().toString(), equalTo(uri1));
            assertEquals(oldCard.getId(), newCard.getId());
            // Title
            assertEquals(MockMetacard.DEFAULT_TITLE, oldCard.getTitle());
            assertEquals(MockMetacard.DEFAULT_TITLE, newCard.getTitle());
            // Location (decimal points make them not exact Strings POINT(1
            // 0) as opposed to POINT( 1.0 0.0) )
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), oldCard.getLocation().substring(0, 8));
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), newCard.getLocation().substring(0, 8));
            // Metadata
            assertNotNull(oldCard.getMetadata());
            assertNotNull(newCard.getMetadata());
            assertTrue(!oldCard.getMetadata().isEmpty());
            assertTrue(!newCard.getMetadata().isEmpty());
            // Created Date
            assertFalse(oldCard.getCreatedDate().after(new Date()));
            assertFalse(newCard.getCreatedDate().after(new Date()));
            assertTrue(newCard.getCreatedDate().after(oldCard.getCreatedDate()));
            // Modified Date
            assertTrue(newCard.getModifiedDate().after(oldCard.getModifiedDate()));
            // Effective Date
            assertTrue(newCard.getEffectiveDate().after(oldCard.getEffectiveDate()));
            // Expiration Date
            assertTrue(newCard.getExpirationDate().after(oldCard.getExpirationDate()));
            // Thumbnail
            assertTrue(Arrays.equals(newCard.getThumbnail(), oldCard.getThumbnail()));
        } else if (oldCard.getContentTypeName().equals("oldNitf2")) {
            assertEquals("nitf2", newCard.getContentTypeName());
            // Cardinals is unique to the document
            assertTrue(newCard.getMetadata().indexOf("Cardinals") != ALL_RESULTS);
            assertTrue("50L".equals(newCard.getResourceSize()));
            assertEquals(uri2, newCard.getResourceURI().toString());
            assertEquals(oldCard.getId(), newCard.getId());
            // Title
            assertEquals(MockMetacard.DEFAULT_TITLE, oldCard.getTitle());
            assertEquals(MockMetacard.DEFAULT_TITLE, newCard.getTitle());
            // Location (decimal points make them not exact in Strings
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), oldCard.getLocation().substring(0, 8));
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), newCard.getLocation().substring(0, 8));
            // Metadata
            assertNotNull(oldCard.getMetadata());
            assertNotNull(newCard.getMetadata());
            assertTrue(!oldCard.getMetadata().isEmpty());
            assertTrue(!newCard.getMetadata().isEmpty());
            // Created Date
            assertFalse(oldCard.getCreatedDate().after(new Date()));
            assertFalse(newCard.getCreatedDate().after(new Date()));
            assertTrue(newCard.getCreatedDate().after(oldCard.getCreatedDate()));
            // Modified Date
            assertTrue(newCard.getModifiedDate().after(oldCard.getModifiedDate()));
            // Effective Date
            assertTrue(newCard.getEffectiveDate().after(oldCard.getEffectiveDate()));
            // Expiration Date
            assertTrue(newCard.getExpirationDate().after(oldCard.getExpirationDate()));
            // Thumbnail
            assertTrue(Arrays.equals(newCard.getThumbnail(), oldCard.getThumbnail()));
        } else {
            Assert.fail("Expecting one or the other of the updated records.");
        }
    }
    /** READ **/
    CommonQueryBuilder builder = new CommonQueryBuilder();
    QueryImpl query = builder.queryByProperty(Metacard.RESOURCE_URI, uri2);
    QueryRequestImpl queryRequest = new QueryRequestImpl(query);
    SourceResponse sourceResponse = provider.query(queryRequest);
    assertEquals(1, sourceResponse.getResults().size());
    for (Result r : sourceResponse.getResults()) {
        assertTrue(r.getMetacard().getMetadata().indexOf("Cardinals") != ALL_RESULTS);
        assertEquals(uri2, r.getMetacard().getResourceURI().toString());
    }
    /** UPDATE with null thumbnail **/
    updatedMetacard1.setThumbnail(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", 1, updateResponse.getUpdatedMetacards().size());
    Metacard newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    Metacard oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getThumbnail());
    assertEquals(null, newCard.getThumbnail());
    /** UPDATE with null WKT **/
    // updatedMetacard1.setLocation(null);
    // updateResponse = provider.update(new
    // UpdateRequestImpl(updatedMetacard1.getId(), updatedMetacard1));
    //
    //
    //
    // assertEquals("Testing Update operation: ", 1,
    // updateResponse.getUpdatedMetacards().size());
    //
    // newCard =
    // updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    // oldCard =
    // updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    //
    // assertNotNull(oldCard.getResourceURI());
    // assertNotNull(newCard.getResourceURI());
    // assertEquals(oldCard.getResourceURI().toString(),
    // newCard.getResourceURI().toString());
    // assertEquals(provider.getId(), oldCard.getSourceId());
    // assertEquals(provider.getId(), newCard.getSourceId());
    // LOGGER.info("New Metacard location: {}", newCard.getLocation());
    // LOGGER.info("Old Metacard location: {}", oldCard.getLocation());
    // assertTrue(oldCard.getLocation().contains("POINT"));
    // assertEquals(null, newCard.getLocation());
    /** UPDATE with null expiration date **/
    updatedMetacard1.setExpirationDate(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getExpirationDate());
    assertEquals(null, newCard.getExpirationDate());
    /** UPDATE with null content type **/
    updatedMetacard1.setContentTypeName(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getContentTypeName());
    assertThat(newCard.getContentTypeName(), nullValue());
    /** UPDATE with empty content type **/
    updatedMetacard1.setContentTypeName("");
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertThat(oldCard.getContentTypeName(), nullValue());
    assertThat(newCard.getContentTypeName(), is(""));
    /** UPDATE with null content type version **/
    updatedMetacard1.setContentTypeVersion(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getContentTypeVersion());
    assertThat(newCard.getContentTypeVersion(), nullValue());
    /** UPDATE with empty content type version **/
    updatedMetacard1.setContentTypeVersion("");
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertThat(oldCard.getContentTypeVersion(), nullValue());
    assertThat(newCard.getContentTypeVersion(), is(""));
    /** UPDATE with new resource uri **/
    updatedMetacard1.setResourceURI(new URI(uri1 + "Now"));
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertThat(oldCard.getResourceURI(), is(nullValue()));
    assertEquals(uri1 + "Now", newCard.getResourceURI().toString());
    /** TEST NULL UPDATE **/
    updateResponse = provider.update(new UpdateRequest() {

        @Override
        public boolean hasProperties() {
            return false;
        }

        @Override
        public Serializable getPropertyValue(String name) {
            return null;
        }

        @Override
        public Set<String> getPropertyNames() {
            return null;
        }

        @Override
        public Map<String, Serializable> getProperties() {
            return null;
        }

        @Override
        public boolean containsPropertyName(String name) {
            return false;
        }

        @Override
        public List<Entry<Serializable, Metacard>> getUpdates() {
            return null;
        }

        @Override
        public String getAttributeName() {
            return UpdateRequest.UPDATE_BY_ID;
        }
    });
    assertTrue(updateResponse.getUpdatedMetacards().isEmpty());
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) UpdateRequest(ddf.catalog.operation.UpdateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Update(ddf.catalog.operation.Update) URI(java.net.URI) Date(java.util.Date) Result(ddf.catalog.data.Result) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 4 with UpdateResponse

use of ddf.catalog.operation.UpdateResponse in project ddf by codice.

the class SolrProviderTest method testUpdateOperationWithNoResults.

/**
     * Testing that if no records are found to update, the provider returns an empty list.
     *
     * @throws IngestException
     * @throws UnsupportedQueryException
     */
@Test
public void testUpdateOperationWithNoResults() throws IngestException, UnsupportedQueryException {
    deleteAllIn(provider);
    MockMetacard metacard = new MockMetacard(Library.getFlagstaffRecord());
    UpdateResponse response = update("BAD_ID", metacard);
    assertEquals(0, response.getUpdatedMetacards().size());
}
Also used : UpdateResponse(ddf.catalog.operation.UpdateResponse) Test(org.junit.Test)

Example 5 with UpdateResponse

use of ddf.catalog.operation.UpdateResponse in project ddf by codice.

the class SolrProviderTest method testUpdateUnknownAttribute.

/**
     * Testing update operation of unknown attribute. Should return no results.
     *
     * @throws IngestException
     * @throws UnsupportedQueryException
     */
public void testUpdateUnknownAttribute() throws IngestException, UnsupportedQueryException {
    deleteAllIn(provider);
    UpdateResponse response = provider.update(new UpdateRequest() {

        @Override
        public boolean hasProperties() {
            return false;
        }

        @Override
        public Serializable getPropertyValue(String name) {
            return null;
        }

        @Override
        public Set<String> getPropertyNames() {
            return null;
        }

        @Override
        public Map<String, Serializable> getProperties() {
            return null;
        }

        @Override
        public boolean containsPropertyName(String name) {
            return false;
        }

        @Override
        public List<Entry<Serializable, Metacard>> getUpdates() {
            MockMetacard newMetacard = new MockMetacard(Library.getShowLowRecord());
            List<Entry<Serializable, Metacard>> updateList = new ArrayList<Entry<Serializable, Metacard>>();
            updateList.add(new SimpleEntry<Serializable, Metacard>(MockMetacard.DEFAULT_TITLE, newMetacard));
            return updateList;
        }

        @Override
        public String getAttributeName() {
            return "dataAccess";
        }
    });
    assertEquals(0, response.getUpdatedMetacards().size());
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) UpdateRequest(ddf.catalog.operation.UpdateRequest) SimpleEntry(java.util.AbstractMap.SimpleEntry) Matchers.containsString(org.hamcrest.Matchers.containsString) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map)

Aggregations

UpdateResponse (ddf.catalog.operation.UpdateResponse)79 Test (org.junit.Test)56 Metacard (ddf.catalog.data.Metacard)47 Update (ddf.catalog.operation.Update)32 UpdateRequest (ddf.catalog.operation.UpdateRequest)31 ArrayList (java.util.ArrayList)31 Serializable (java.io.Serializable)27 HashMap (java.util.HashMap)26 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)20 CreateResponse (ddf.catalog.operation.CreateResponse)19 List (java.util.List)18 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)15 Result (ddf.catalog.data.Result)14 Map (java.util.Map)14 UpdateResponseImpl (ddf.catalog.operation.impl.UpdateResponseImpl)13 IngestException (ddf.catalog.source.IngestException)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 DeletedMetacard (ddf.catalog.core.versioning.DeletedMetacard)11 DeleteResponse (ddf.catalog.operation.DeleteResponse)11 QueryImpl (ddf.catalog.operation.impl.QueryImpl)11