use of eu.europeana.metis.mongo.dao.RecordRedirectDao in project metis-framework by europeana.
the class RecordRedirectsUtil method introduceRedirection.
/**
* Introduce a new redirect X -> Y. If X equals Y we do nothing. Otherwise, we do the following:
* <ol>
* <li>
* We delete all mappings Y -> ?. We can do this because we are indexing record Y and it should
* therefore not be redirected to any other link.
* </li>
* <li>
* We delete all mappings X -> ? except X -> Y. We can do this because X will be redirected to
* record Y only, and it should therefore not be redirected to any other record.
* </li>
* <li>
* Introduce new redirect from X -> Y if it doesn't already exist. This new redirect will get the
* record redirect date (passed to this method).
* </li>
* <li>
* We update all existing redirects ? -> X to point to Y instead (so becoming ? -> Y).
* </li>
* </ol>
* Note that after this method is called (and if X does not equal Y), X should only occur as
* source of a redirect, and then only as part of redirect X -> Y, whereas Y should occur only as
* destination of a redirect. Neither of them can therefore be part of a redirection cycle.
*
* @param recordRedirectDao The DAO object to manage redirects.
* @param newIdentifier The new identifier (value Y).
* @param oldIdentifier The old identifier (value X).
* @param recordRedirectDate The date (timestamp) for any new redirects.
*/
private static void introduceRedirection(RecordRedirectDao recordRedirectDao, String newIdentifier, String oldIdentifier, Date recordRedirectDate) {
// Sanity check: if old and new identifier are equal, do nothing.
if (oldIdentifier.equals(newIdentifier)) {
LOGGER.info("Encountered the request to create mappping from {} to itself. This will be ignored.", oldIdentifier);
return;
}
// Remove any redirects Y -> ?.
recordRedirectDao.getRecordRedirectsByOldId(newIdentifier).forEach(recordRedirectDao::delete);
// Remove any redirects X -> ? except X -> Y.
final List<RecordRedirect> existingRedirectsFromOldIdentifier = recordRedirectDao.getRecordRedirectsByOldId(oldIdentifier);
existingRedirectsFromOldIdentifier.stream().filter(redirect -> !redirect.getNewId().equals(newIdentifier)).forEach(recordRedirectDao::delete);
// Create the new redirect X -> Y if one doesn't already exist.
final boolean mappingAlreadyExists = existingRedirectsFromOldIdentifier.stream().map(RecordRedirect::getNewId).anyMatch(newIdentifier::equals);
if (!mappingAlreadyExists) {
recordRedirectDao.createUpdate(new RecordRedirect(newIdentifier, oldIdentifier, recordRedirectDate));
}
// Update the redirects ? -> X to point to Y instead, becoming ? -> Y.
recordRedirectDao.getRecordRedirectsByNewId(oldIdentifier).forEach(redirect -> {
redirect.setNewId(newIdentifier);
recordRedirectDao.createUpdate(redirect);
});
}
use of eu.europeana.metis.mongo.dao.RecordRedirectDao in project metis-framework by europeana.
the class RecordRedirectDaoTest method setup.
@BeforeAll
static void setup() {
embeddedLocalhostMongo = new EmbeddedLocalhostMongo();
embeddedLocalhostMongo.start();
final String mongoHost = embeddedLocalhostMongo.getMongoHost();
final int mongoPort = embeddedLocalhostMongo.getMongoPort();
final MongoClient mongoClient = MongoClients.create(String.format("mongodb://%s:%s", mongoHost, mongoPort));
recordRedirectDao = new RecordRedirectDao(mongoClient, DATABASE_NAME);
}
Aggregations