Skip to content

Commit

Permalink
Additional tweaks to bundle to facilitate merge importing
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesname committed Apr 4, 2014
1 parent fe1c458 commit 76e132e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,23 @@ public Multimap<String,Bundle> getDependentRelations() {
* @param relations A full set of relations
* @return The new bundle
*/
public Bundle withRelations(Multimap<String, Bundle> relations) {
public Bundle replaceRelations(Multimap<String, Bundle> relations) {
return new Bundle(id, type, data, relations, meta, temp);
}

/**
* Add a map of relations.
*
* @param relations A full set of relations
* @return The new bundle
*/
public Bundle withRelations(Multimap<String, Bundle> relations) {
Multimap<String, Bundle> tmp = LinkedListMultimap
.create(relations);
tmp.putAll(relations);
return new Bundle(id, type, data, tmp, meta, temp);
}

/**
* Get a set of relations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public final class Serializer {

private static final Logger logger = LoggerFactory.getLogger(Serializer.class);

private static final int DEFAULT_CACHE_SIZE = 100;

private static class LruCache<A, B> extends LinkedHashMap<A, B> {
private final int maxEntries;

Expand Down Expand Up @@ -113,7 +115,11 @@ public Builder withLiteMode(boolean lite) {
}

public Builder withCache() {
this.cache = new LruCache<String, Bundle>(100);
return withCache(DEFAULT_CACHE_SIZE);
}

public Builder withCache(int size) {
this.cache = new LruCache<String, Bundle>(size);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private static Bundle mutateAttribute(Bundle bundle, BundlePath path,
relations.set(section.getIndex(),
mutateAttribute(subject, path.next(), op));
allRelations.putAll(section.getPath(), relations);
return bundle.withRelations(allRelations);
return bundle.replaceRelations(allRelations);
} catch (IndexOutOfBoundsException e) {
throw new BundleIndexError(String.format(
"Relation index '%s[%s]' not found", section.getPath(),
Expand Down Expand Up @@ -309,7 +309,7 @@ private static Bundle setNode(Bundle bundle, BundlePath path, Bundle newNode) {
setNode(subject, next, newNode));
}
allRelations.putAll(section.getPath(), relations);
return bundle.withRelations(allRelations);
return bundle.replaceRelations(allRelations);
} catch (IndexOutOfBoundsException e) {
throw new BundleIndexError(String.format(
"Relation index '%s[%s]' not found", next.current().getPath(),
Expand Down Expand Up @@ -351,7 +351,7 @@ private static Bundle deleteNode(Bundle bundle, BundlePath path) {
}
if (!relations.isEmpty())
allRelations.putAll(section.getPath(), relations);
return bundle.withRelations(allRelations);
return bundle.replaceRelations(allRelations);
} catch (IndexOutOfBoundsException e) {
throw new BundleIndexError(String.format(
"Relation index '%s[%s]' not found", section.getPath(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package eu.ehri.project.persistence;

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import eu.ehri.project.definitions.Ontology;
import eu.ehri.project.models.EntityClass;
import eu.ehri.project.persistence.utils.BundleUtils;
Expand Down Expand Up @@ -174,6 +176,30 @@ public void testGetRelations() throws Exception {
assertEquals(1L, relations.size());
}

@Test
public void testReplaceRelations() throws Exception {
Bundle newDesc = new Bundle(EntityClass.DOCUMENT_DESCRIPTION)
.withDataValue(Ontology.NAME_KEY, "Foobar")
.withDataValue(Ontology.LANGUAGE, "en");
Multimap<String,Bundle> rels = ImmutableListMultimap
.of(Ontology.DESCRIPTION_FOR_ENTITY, newDesc);
Bundle bundle2 = bundle.replaceRelations(rels);
assertEquals(1L, bundle2.getRelations(
Ontology.DESCRIPTION_FOR_ENTITY).size());
}

@Test
public void testWithRelationsMap() throws Exception {
Bundle newDesc = new Bundle(EntityClass.DOCUMENT_DESCRIPTION)
.withDataValue(Ontology.NAME_KEY, "Foobar")
.withDataValue(Ontology.LANGUAGE, "en");
Multimap<String,Bundle> rels = ImmutableListMultimap
.of(Ontology.DESCRIPTION_FOR_ENTITY, newDesc);
Bundle bundle2 = bundle.withRelations(rels);
assertEquals(2L, bundle2.getRelations(
Ontology.DESCRIPTION_FOR_ENTITY).size());
}

@Test
public void testWithRelations() throws Exception {
Bundle newDesc = new Bundle(EntityClass.DOCUMENT_DESCRIPTION)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.ehri.project.importers;

import com.google.common.collect.Multimap;
import com.tinkerpop.frames.FramedGraph;
import eu.ehri.project.definitions.Ontology;
import eu.ehri.project.exceptions.ItemNotFound;
Expand All @@ -9,14 +8,14 @@
import eu.ehri.project.models.*;
import eu.ehri.project.models.base.*;
import eu.ehri.project.persistence.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eu.ehri.project.persistence.BundleDAO;
import eu.ehri.project.persistence.Mutation;
import java.util.logging.Level;

import eu.ehri.project.persistence.Serializer;
import org.slf4j.Logger;
Expand All @@ -27,11 +26,10 @@
* procedure. An EAD a single entity at the highest level of description or multiple top-level entities, with or without
* a hierarchical structure describing their child items. This means that we need to recursively descend through the
* archdesc and c,c01-12 levels.
*
* <p/>
* TODO: Extensive cleanups, optimisation, and rationalisation.
*
* @author lindar
*
*/
public class IcaAtomEadImporter extends EaImporter {

Expand Down Expand Up @@ -68,7 +66,7 @@ public DocumentaryUnit importItem(Map<String, Object> itemData, List<String> idP
BundleDAO persister = getPersister(idPath);

Bundle unit = new Bundle(EntityClass.DOCUMENTARY_UNIT, extractDocumentaryUnit(itemData));

// Check for missing identifier, throw an exception when there is no ID.
if (unit.getDataValue(Ontology.IDENTIFIER_KEY) == null) {
throw new ValidationError(unit, Ontology.IDENTIFIER_KEY,
Expand All @@ -91,7 +89,8 @@ public DocumentaryUnit importItem(Map<String, Object> itemData, List<String> idP
descBundle = descBundle.withRelation(Ontology.HAS_UNKNOWN_PROPERTY, new Bundle(EntityClass.UNKNOWN_PROPERTY, unknowns));
}

Mutation<DocumentaryUnit> mutation = mergeWithPreviousAndSave(unit, persister, descBundle);
Mutation<DocumentaryUnit> mutation = persister.createOrUpdate(mergeWithPreviousAndSave(unit, descBundle),
DocumentaryUnit.class);
// persister.createOrUpdate(unit, DocumentaryUnit.class);
DocumentaryUnit frame = mutation.getNode();

Expand Down Expand Up @@ -121,15 +120,15 @@ public DocumentaryUnit importItem(Map<String, Object> itemData, List<String> idP


/**
* finds any bundle in the graph with the same ObjectIdentifier.
* finds any bundle in the graph with the same ObjectIdentifier.
* if it exists it replaces the Description in the given language, else it just saves it
* @param unit - the DocumentaryUnit to be saved
* @param persister
*
* @param unit - the DocumentaryUnit to be saved
* @param descBundle - the documentsDescription to replace any previous ones with this language
* @return
* @throws ValidationError
* @return A bundle with description relationships merged.
* @throws ValidationError
*/
protected Mutation<DocumentaryUnit> mergeWithPreviousAndSave(Bundle unit, BundleDAO persister, Bundle descBundle) throws ValidationError {
protected Bundle mergeWithPreviousAndSave(Bundle unit, Bundle descBundle) throws ValidationError {
final String languageOfDesc = descBundle.getDataValue(Ontology.LANGUAGE_OF_DESCRIPTION);
Bundle withIds = unit.generateIds(getPermissionScope().idPath());
if (manager.exists(withIds.getId())) {
Expand All @@ -150,25 +149,21 @@ public boolean remove(String relationLabel, Bundle bundle) {
};
Bundle filtered = oldBundle.filterRelations(filter);

Bundle newBundle = withIds.withRelations(filtered.getRelations())
return withIds.withRelations(filtered.getRelations())
.withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);

//save it
return persister.createOrUpdate(newBundle, DocumentaryUnit.class);

} catch (SerializationError ex) {
throw new ValidationError(unit, "serialization error", ex.getMessage());
} catch (ItemNotFound ex) {
throw new ValidationError(unit, "item not found exception", ex.getMessage());
}
} else {
unit = unit.withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
return persister.createOrUpdate(unit, DocumentaryUnit.class);
return unit.withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
}
}

@SuppressWarnings("unchecked")
@Override
@Override
protected Iterable<Map<String, Object>> extractRelations(Map<String, Object> data) {
final String REL = "Access";
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Expand All @@ -195,7 +190,7 @@ protected Iterable<Map<String, Object>> extractRelations(Map<String, Object> dat
}
return list;
}

protected Map<String, Object> extractDocumentaryUnit(Map<String, Object> itemData, int depth) throws ValidationError {
Map<String, Object> unit = new HashMap<String, Object>();
if (itemData.get(OBJECT_ID) != null) {
Expand Down

0 comments on commit 76e132e

Please sign in to comment.