Search in sources :

Example 1 with UpdateImpl

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

the class MockMemoryProvider method update.

@Override
public UpdateResponse update(UpdateRequest request) {
    String methodName = "update";
    LOGGER.debug("Entering: {}", methodName);
    hasReceivedUpdate = true;
    hasReceivedUpdateByIdentifier = true;
    List<Entry<Serializable, Metacard>> updatedCards = request.getUpdates();
    Map<String, Serializable> properties = new HashMap<>();
    List<Update> returnedMetacards = new ArrayList<>(updatedCards.size());
    for (Entry<Serializable, Metacard> curCard : updatedCards) {
        if (store.containsKey(curCard.getValue().getId())) {
            LOGGER.debug("Store contains the key");
            Metacard oldMetacard = store.get(curCard.getValue().getId());
            store.put(curCard.getValue().getId(), curCard.getValue());
            properties.put(curCard.getValue().getId(), curCard.getValue());
            LOGGER.debug("adding returnedMetacard");
            returnedMetacards.add(new UpdateImpl(curCard.getValue(), oldMetacard));
        } else {
            LOGGER.debug("Key not contained in the store");
        }
    }
    UpdateResponse response = new UpdateResponseImpl(request, properties, returnedMetacards);
    LOGGER.debug("Exiting:{}", methodName);
    return response;
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) UpdateResponse(ddf.catalog.operation.UpdateResponse) Entry(java.util.Map.Entry) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl)

Example 2 with UpdateImpl

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

the class CatalogComponentFrameworkTest method testUpdateWithIngestException.

@Test
public /**
     * Operation: UPDATE
     * Body contains:  Metacard
     */
void testUpdateWithIngestException() throws Exception {
    resetMocks();
    // Setup expectations to verify
    final MockEndpoint mockVerifierEndpoint = getMockEndpoint("mock:result");
    mockVerifierEndpoint.expectedMessageCount(1);
    final List<Metacard> metacards = new ArrayList<Metacard>();
    metacards.add(metacard1);
    // setup mock catalog framework
    final Update update = new UpdateImpl(metacard1, metacard2);
    List<Update> updates = new ArrayList<Update>();
    updates.add(update);
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    UpdateRequest updateRequest = new UpdateRequestImpl(metacardIds, metacards);
    UpdateResponse updateResponse = new UpdateResponseImpl(updateRequest, new HashMap(), updates);
    when(catalogFramework.update(any(UpdateRequest.class))).thenThrow(new IngestException());
    // Exercise the route with a UPDATE operation
    template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    // Verify that the number of metacards in the exchange after the records
    // is identical to the input
    assertListSize(mockVerifierEndpoint.getExchanges(), 1);
    final Exchange exchange = mockVerifierEndpoint.getExchanges().get(0);
    final List<Update> cardsUpdated = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsUpdated, 0);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UpdateRequest(ddf.catalog.operation.UpdateRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) ArrayList(java.util.ArrayList) List(java.util.List) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 3 with UpdateImpl

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

the class CatalogComponentFrameworkTest method testUpdateWithListOfMetacards.

@Test
public /**
     * Operation: UPDATE
     * Body contains:  List<Metacard>
     */
void testUpdateWithListOfMetacards() throws Exception {
    resetMocks();
    // Setup expectations to verify
    final MockEndpoint mockVerifierEndpoint = getMockEndpoint("mock:result");
    mockVerifierEndpoint.expectedMessageCount(1);
    final List<Metacard> metacards = new ArrayList<Metacard>();
    metacards.add(metacard1);
    // setup mock catalog framework
    final Update update = new UpdateImpl(metacard1, metacard2);
    List<Update> updates = new ArrayList<Update>();
    updates.add(update);
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    UpdateRequest updateRequest = new UpdateRequestImpl(metacardIds, metacards);
    UpdateResponse updateResponse = new UpdateResponseImpl(updateRequest, new HashMap(), updates);
    when(catalogFramework.update(any(UpdateRequest.class))).thenReturn(updateResponse);
    // Exercise the route with a UPDATE operation
    template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    // Verify that the number of metacards in the exchange after the records
    // is identical to the input
    assertListSize(mockVerifierEndpoint.getExchanges(), 1);
    final Exchange exchange = mockVerifierEndpoint.getExchanges().get(0);
    final List<Update> cardsUpdated = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsUpdated, 1);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UpdateRequest(ddf.catalog.operation.UpdateRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) ArrayList(java.util.ArrayList) List(java.util.List) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 4 with UpdateImpl

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

the class DeliveryProcessor method process.

public void process(Event event) {
    String methodName = "process";
    LOGGER.debug("ENTERING: {}", methodName);
    Metacard entry = (Metacard) event.getProperty(PubSubConstants.HEADER_ENTRY_KEY);
    String operation = event.getProperty(PubSubConstants.HEADER_OPERATION_KEY).toString();
    LOGGER.debug("Delivering catalog entry.");
    if (subscription != null) {
        if (entry != null) {
            if (operation.equalsIgnoreCase(PubSubConstants.CREATE)) {
                try {
                    for (PreDeliveryPlugin plugin : preDelivery) {
                        LOGGER.debug("Processing 'created' entry with preDelivery plugin");
                        entry = plugin.processCreate(entry);
                    }
                    subscription.getDeliveryMethod().created(entry);
                } catch (PluginExecutionException e) {
                    LOGGER.debug("Plugin had exception during execution - still delivering the entry", e);
                    subscription.getDeliveryMethod().created(entry);
                } catch (StopProcessingException e) {
                    LOGGER.info("Pre-delivery plugin determined entry cannot be delivered", e);
                }
            } else if (operation.equalsIgnoreCase(PubSubConstants.UPDATE)) {
                // TODO: Handle hit or miss
                try {
                    for (PreDeliveryPlugin plugin : preDelivery) {
                        LOGGER.debug("Processing 'updated' entry with preDelivery plugin");
                        Update updatedEntry = plugin.processUpdateHit(new UpdateImpl(entry, null));
                        entry = updatedEntry.getNewMetacard();
                    }
                    subscription.getDeliveryMethod().updatedHit(entry, entry);
                } catch (PluginExecutionException e) {
                    LOGGER.debug("Plugin had exception during execution - still delivering the entry", e);
                    subscription.getDeliveryMethod().updatedHit(entry, entry);
                } catch (StopProcessingException e) {
                    LOGGER.info("Pre-delivery plugin determined entry cannot be delivered", e);
                }
            } else if (operation.equalsIgnoreCase(PubSubConstants.DELETE)) {
                try {
                    for (PreDeliveryPlugin plugin : preDelivery) {
                        LOGGER.debug("Processing 'deleted' entry with preDelivery plugin");
                        entry = plugin.processCreate(entry);
                    }
                    subscription.getDeliveryMethod().deleted(entry);
                } catch (PluginExecutionException e) {
                    LOGGER.debug("Plugin had exception during execution - still delivering the entry", e);
                    subscription.getDeliveryMethod().deleted(entry);
                } catch (StopProcessingException e) {
                    LOGGER.info("Pre-delivery plugin determined entry cannot be delivered", e);
                }
            } else {
                LOGGER.debug("Could not deliver hit for subscription.");
            }
        } else {
            LOGGER.debug("Could not deliver hit for subscription. Catalog entry is null.");
        }
    } else {
        LOGGER.debug("Could not deliver hit for subscription. Subscription is null.");
    }
    LOGGER.debug("EXITING: {}", methodName);
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Metacard(ddf.catalog.data.Metacard) PreDeliveryPlugin(ddf.catalog.plugin.PreDeliveryPlugin) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Update(ddf.catalog.operation.Update) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException)

Example 5 with UpdateImpl

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

the class SolrCatalogProvider method update.

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    if (updateRequest == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }
    List<Entry<Serializable, Metacard>> updates = updateRequest.getUpdates();
    // the list of updates, both new and old metacards
    ArrayList<Update> updateList = new ArrayList<>();
    String attributeName = updateRequest.getAttributeName();
    // need an attribute name in order to do query
    if (attributeName == null) {
        throw new IngestException("Attribute name cannot be null. " + "Please provide the name of the attribute.");
    }
    List<String> identifiers = new ArrayList<>();
    // if we have nothing to update, send the empty list
    if (updates == null || updates.size() == 0) {
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    // Loop to get all identifiers
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        identifiers.add(updateEntry.getKey().toString());
    }
    /* 1a. Create the old Metacard Query */
    String attributeQuery = getQuery(attributeName, identifiers);
    SolrQuery query = new SolrQuery(attributeQuery);
    // Set number of rows to the result size + 1.  The default row size in Solr is 10, so this
    // needs to be set in situations where the number of metacards to update is > 10.  Since there
    // could be more results in the query response than the number of metacards in the update request,
    // 1 is added to the row size, so we can still determine whether we found more metacards than
    // updated metacards provided
    query.setRows(updates.size() + 1);
    QueryResponse idResults = null;
    /* 1b. Execute Query */
    try {
        idResults = solr.query(query, METHOD.POST);
    } catch (SolrServerException | IOException e) {
        LOGGER.info("Failed to query for metacard(s) before update.", e);
    }
    // map of old metacards to be populated
    Map<Serializable, Metacard> idToMetacardMap = new HashMap<>();
    /* 1c. Populate list of old metacards */
    if (idResults != null && idResults.getResults() != null && idResults.getResults().size() != 0) {
        LOGGER.debug("Found {} current metacard(s).", idResults.getResults().size());
        // CHECK updates size assertion
        if (idResults.getResults().size() > updates.size()) {
            throw new IngestException("Found more metacards than updated metacards provided. Please ensure your attribute values match unique records.");
        }
        for (SolrDocument doc : idResults.getResults()) {
            Metacard old;
            try {
                old = client.createMetacard(doc);
            } catch (MetacardCreationException e) {
                LOGGER.info("Unable to create metacard(s) from Solr responses during update.", e);
                throw new IngestException("Could not create metacard(s).");
            }
            if (!idToMetacardMap.containsKey(old.getAttribute(attributeName).getValue())) {
                idToMetacardMap.put(old.getAttribute(attributeName).getValue(), old);
            } else {
                throw new IngestException("The attribute value given [" + old.getAttribute(attributeName).getValue() + "] matched multiple records. Attribute values must at most match only one unique Metacard.");
            }
        }
    }
    if (Metacard.ID.equals(attributeName)) {
        idToMetacardMap.putAll(pendingNrtIndex.getAllPresent(identifiers));
    }
    if (idToMetacardMap.size() == 0) {
        LOGGER.debug("No results found for given attribute values.");
        // return an empty list
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    /* 2. Update the cards */
    List<Metacard> newMetacards = new ArrayList<>();
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        String localKey = updateEntry.getKey().toString();
        /* 2a. Prepare new Metacard */
        MetacardImpl newMetacard = new MetacardImpl(updateEntry.getValue());
        // Find the exact oldMetacard that corresponds with this newMetacard
        Metacard oldMetacard = idToMetacardMap.get(localKey);
        // matched but another did not
        if (oldMetacard != null) {
            // overwrite the id, in case it has not been done properly/already
            newMetacard.setId(oldMetacard.getId());
            newMetacard.setSourceId(getId());
            newMetacards.add(newMetacard);
            updateList.add(new UpdateImpl(newMetacard, oldMetacard));
        }
    }
    try {
        client.add(newMetacards, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        LOGGER.info("Failed to update metacard(s) with Solr.", e);
        throw new IngestException("Failed to update metacard(s).");
    }
    pendingNrtIndex.putAll(updateList.stream().collect(Collectors.toMap(u -> u.getNewMetacard().getId(), u -> copyMetacard(u.getNewMetacard()))));
    return new UpdateResponseImpl(updateRequest, updateRequest.getProperties(), updateList);
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Entry(java.util.Map.Entry) SolrDocument(org.apache.solr.common.SolrDocument) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) SolrException(org.apache.solr.common.SolrException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Aggregations

Update (ddf.catalog.operation.Update)11 UpdateImpl (ddf.catalog.operation.impl.UpdateImpl)11 Metacard (ddf.catalog.data.Metacard)9 ArrayList (java.util.ArrayList)8 UpdateResponse (ddf.catalog.operation.UpdateResponse)7 UpdateResponseImpl (ddf.catalog.operation.impl.UpdateResponseImpl)7 HashMap (java.util.HashMap)7 UpdateRequest (ddf.catalog.operation.UpdateRequest)6 Test (org.junit.Test)6 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)4 Serializable (java.io.Serializable)4 List (java.util.List)4 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)3 Exchange (org.apache.camel.Exchange)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 IngestException (ddf.catalog.source.IngestException)2 Entry (java.util.Map.Entry)2 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)2 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)2 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)2