Skip to content

Commit

Permalink
Make MarcxmlEntityBuilder concrete
Browse files Browse the repository at this point in the history
Now contains a generalized build() method where a more specific builder is not needed. Organize imports.
  • Loading branch information
rjyounes committed Sep 1, 2017
1 parent 1e7546a commit 16c1510
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 45 deletions.
28 changes: 25 additions & 3 deletions src/main/java/org/ld4l/bib2lod/entitybuilders/BuildParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@
import java.util.List;

import org.ld4l.bib2lod.entity.Entity;
import org.ld4l.bib2lod.ontology.DatatypeProp;
import org.ld4l.bib2lod.ontology.ObjectProp;
import org.ld4l.bib2lod.ontology.Type;
import org.ld4l.bib2lod.records.Record;
import org.ld4l.bib2lod.records.RecordField;

public class BuildParams {

private RecordField field;

// Parent of the parent; e.g., the bib resource of an Activity of an
// Agent
private Entity grandparent;

// The "parent" of the Entity being built: e.g., the Instance of a Title
// or PublicationActivity
private Entity parent;

// Datatype property to add to the Entity. Uses either subfield text
// value or the value String as the object.
private DatatypeProp property;

private Record record;

// PROBABLY TEMPORARY
// Relationship between parent and the Entity being built
// MAYBE TEMPORARY?
// Relationship from parent to the Entity being built
private ObjectProp relationship;

private List<RecordField> subfields;

private Type type;
private String value;

Expand Down Expand Up @@ -80,6 +89,19 @@ public BuildParams setParent(Entity parent) {
// Return this for method chaining
return this;
}

/**
* Returns null if no datatype property has been set.
*/
public DatatypeProp getProperty() {
return property;
}

public BuildParams setProperty(DatatypeProp property) {
this.property = property;
// Return this for method chaining
return this;
}

/**
* Returns null if no record has been set.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.ld4l.bib2lod.entitybuilders.marcxml;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ld4l.bib2lod.entity.Entity;
import org.ld4l.bib2lod.entitybuilders.BaseEntityBuilder;
import org.ld4l.bib2lod.entitybuilders.BuildParams;
import org.ld4l.bib2lod.entitybuilders.EntityBuilder;
import org.ld4l.bib2lod.ontology.DatatypeProp;
import org.ld4l.bib2lod.ontology.ObjectProp;
import org.ld4l.bib2lod.ontology.Type;
import org.ld4l.bib2lod.ontology.ld4l.Ld4lIdentifierType;
import org.ld4l.bib2lod.records.xml.marcxml.MarcxmlControlField;
import org.ld4l.bib2lod.records.xml.marcxml.MarcxmlDataField;
import org.ld4l.bib2lod.records.xml.marcxml.MarcxmlRecord;
import org.ld4l.bib2lod.records.xml.marcxml.MarcxmlSubfield;

public abstract class MarcxmlEntityBuilder extends BaseEntityBuilder {
public class MarcxmlEntityBuilder extends BaseEntityBuilder {

/*
* TODO
Expand All @@ -27,7 +25,86 @@ public abstract class MarcxmlEntityBuilder extends BaseEntityBuilder {
*/

private static final Logger LOGGER = LogManager.getLogger();

private Entity parent;
private DatatypeProp property;
private ObjectProp relationship;
private MarcxmlSubfield subfield;
private Type type;
private String value;

@Override
/**
* Builds a resource with a single datatype property. Used to generalize
* standard builds without requiring a more specific builder. Called
* from a subclass using super.build() to build a child entity.
*
* TODO Could we extend this to also be able to add an object property
* assertion? One way is to set a build param for child params, which
* would include type and other information. Could get too complicated.
*/
public Entity build(BuildParams params) throws EntityBuilderException {

reset();
parseBuildParams(params);

Entity entity = new Entity(type);

if (value == null) {
value = subfield.getTrimmedTextValue();
}

entity.addAttribute(property, value);

parent.addRelationship(relationship, entity);

return entity;
}

private void reset() {
this.parent = null;
this.property = null;
this.relationship = null;
this.subfield = null;
this.type = null;
this.value = null;
}

private void parseBuildParams(BuildParams params)
throws EntityBuilderException {

this.type = params.getType();
if (type == null) {
throw new EntityBuilderException(
"A type is required to build this entity.");
}

this.parent = params.getParent();
if (parent == null) {
throw new EntityBuilderException("A parent entity is " +
"required to build this entity.");
}

this.property = params.getProperty();
if (property == null) {
throw new EntityBuilderException("A datatype property is " +
"required to build this entity.");
}

this.subfield = (MarcxmlSubfield) params.getSubfield();
this.value = params.getValue();

if (subfield == null && value == null) {
throw new EntityBuilderException("A subfield or string value " +
"is required to build an agent.");
}

this.relationship = params.getRelationship();
if (relationship == null)
throw new EntityBuilderException("A relationship to the " +
"parent entity is required to build this entity.");
}

protected void buildEntityFromRecord(Type type, Entity parent,
MarcxmlRecord record) throws EntityBuilderException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ld4l.bib2lod.entitybuilders.marcxml.ld4l;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -97,9 +96,8 @@ private void convert_040() throws EntityBuilderException {
private void addSources(MarcxmlDataField field)
throws EntityBuilderException {

// $a non-repeating, $c non-repeating
List<MarcxmlSubfield> subfields =
field.getSubfields(Arrays.asList('a', 'c'));
// 040 $a non-repeating, $c non-repeating
List<MarcxmlSubfield> subfields = field.getSubfields('a', 'c');

if (subfields.isEmpty()) {
return;
Expand All @@ -120,7 +118,7 @@ private void addSources(MarcxmlDataField field)

private void addDescriptionLanguage(MarcxmlDataField field) {

// $b non-repeating
// 040 $b non-repeating
MarcxmlSubfield subfield = field.getSubfield('b');
if (subfield == null) {
return;
Expand All @@ -133,30 +131,52 @@ private void addDescriptionLanguage(MarcxmlDataField field) {
}


private void addDescriptionModifiers(MarcxmlDataField field) {
private void addDescriptionModifiers(MarcxmlDataField field)
throws EntityBuilderException {

// $d repeating
// 040 $d repeating
List<MarcxmlSubfield> subfields = field.getSubfields('d');

// TODO *** Use agent builder
if (subfields.isEmpty()) {
return;
}

for (MarcxmlSubfield subfield : field.getSubfields('d')) {
Entity agent = new Entity(Ld4lAgentType.superClass());
agent.addAttribute(Ld4lDatatypeProp.NAME,
subfield.getTextValue());
adminMetadata.addRelationship(
Ld4lObjectProp.HAS_DESCRIPTION_MODIFIER, agent);
}
EntityBuilder builder = getBuilder(Ld4lAgentType.superClass());
BuildParams params = new BuildParams()
.setParent(adminMetadata)
.setRelationship(Ld4lObjectProp.HAS_DESCRIPTION_MODIFIER);

for (MarcxmlSubfield subfield : subfields) {
if (subfield != null) {
params.setSubfield(subfield);
builder.build(params);
}
}
}

private void addDescriptionConventions(MarcxmlDataField field) {
private void addDescriptionConventions(MarcxmlDataField field)
throws EntityBuilderException {

for (MarcxmlSubfield subfield : field.getSubfields('e')) {
Entity conventions =
new Entity(Ld4lDescriptionConventionsType.superClass());
conventions.addAttribute(Ld4lDatatypeProp.LABEL,
subfield.getTextValue());
adminMetadata.addRelationship(
Ld4lObjectProp.HAS_DESCRIPTION_CONVENTIONS, conventions);

/* Add a generic method to MarcxmlEntityBuilder - pass in
type, subfield, property to add subfield text value to, and
relationship.*/

BuildParams params = new BuildParams()
.setType(Ld4lDescriptionConventionsType.superClass())
.setProperty(Ld4lDatatypeProp.LABEL)
.setSubfield(subfield)
.setParent(adminMetadata)
.setRelationship(
Ld4lObjectProp.HAS_DESCRIPTION_CONVENTIONS);
super.build(params);
// Entity conventions =
// new Entity(Ld4lDescriptionConventionsType.superClass());
// conventions.addAttribute(Ld4lDatatypeProp.LABEL,
// subfield.getTextValue());
// adminMetadata.addRelationship(
// Ld4lObjectProp.HAS_DESCRIPTION_CONVENTIONS, conventions);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public List<MarcxmlSubfield> getSubfields(List<Character> codes) {
return list;
}

public List<MarcxmlSubfield> getSubfields(Character...codes) {
return getSubfields(Arrays.asList(codes));
}

/**
* Returns the subfield of the datafield with the specified code. Use
* for non-repeating subfields. If sent a repeating subfield, returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.ld4l.bib2lod.datatypes.XsdDatatype;
import org.ld4l.bib2lod.entity.Entity;
Expand Down Expand Up @@ -130,80 +129,80 @@ public void testLanguage() throws Exception {
}

@Test
public void testTwoSources() throws Exception {
public void testTwoSources_040$a$c() throws Exception {
Entity adminMetadata = buildAdminMetadata(SOURCE_040$a$c);
Assert.assertEquals(2, adminMetadata.getChildren(
Ld4lObjectProp.HAS_SOURCE).size());
}

@Test
public void testSourceIsAgent() throws Exception {
public void testSourceIsAgent_040() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Entity agent = adminMetadata.getChild(Ld4lObjectProp.HAS_SOURCE);
Assert.assertTrue(agent.hasType(Ld4lAgentType.superClass()));
}

@Test
public void testSourceHasName() throws Exception {
public void testSourceHasName_040() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Entity agent = adminMetadata.getChild(Ld4lObjectProp.HAS_SOURCE);
Assert.assertEquals("NIC", agent.getValue(Ld4lDatatypeProp.NAME));
}

@Test
public void testDescriptionModifiers() throws Exception {
public void testDescriptionModifiers_040$d() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Assert.assertEquals(2, (adminMetadata.getChildren(
Ld4lObjectProp.HAS_DESCRIPTION_MODIFIER)).size());
}

@Test
public void testDescriptionModifierIsAgent() throws Exception {
public void testDescriptionModifierIsAgent_040$d() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Entity agent = (adminMetadata.getChildren(
Ld4lObjectProp.HAS_DESCRIPTION_MODIFIER)).get(0);
Assert.assertTrue(agent.hasType(Ld4lAgentType.superClass()));
}

@Test
public void testDescriptionModifierHasName() throws Exception {
public void testDescriptionModifierHasName_040$d() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Entity agent = (adminMetadata.getChildren(
Ld4lObjectProp.HAS_DESCRIPTION_MODIFIER)).get(0);
Assert.assertEquals("NIC", agent.getValue(Ld4lDatatypeProp.NAME));
}

@Test
public void testDescriptionConventions() throws Exception {
public void testDescriptionConventions_040$e() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Assert.assertEquals(2, (adminMetadata.getChildren(
Ld4lObjectProp.HAS_DESCRIPTION_CONVENTIONS)).size());
}

@Test
public void testDescriptionConventionsValue() throws Exception {
public void testDescriptionConventionsValue_040$e() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Entity conventions = (adminMetadata.getChildren(
Ld4lObjectProp.HAS_DESCRIPTION_CONVENTIONS)).get(0);
Assert.assertEquals("rda", conventions.getValue(Ld4lDatatypeProp.LABEL));
}

@Test
public void test005DateTimeValue() throws Exception {
public void testDateTimeValue_005() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Assert.assertEquals("2013-03-30T14:56:47", adminMetadata.getValue(
Ld4lDatatypeProp.CHANGE_DATE));
}

@Test
public void test005DateTimeDatatype() throws Exception {
public void testDateTimeDatatype_005() throws Exception {
Entity adminMetadata = buildAdminMetadata(TEST_RECORD);
Assert.assertSame(XsdDatatype.DATETIME, adminMetadata.getAttribute(
Ld4lDatatypeProp.CHANGE_DATE).getDatatype());
}

@Test
public void invalid005DateTimeValue_ThrowsException() throws Exception {
public void invalidDateTimeValue_005_ThrowsException() throws Exception {
buildAndExpectException(INVALID_005_VALUE,
"Invalid value for control field 005");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.ld4l.bib2lod.entity.InstanceEntity;
import org.ld4l.bib2lod.entitybuilders.BuildParams;
import org.ld4l.bib2lod.entitybuilders.EntityBuilder.EntityBuilderException;
import org.ld4l.bib2lod.entitybuilders.marcxml.ld4l.ItemBuilder;
import org.ld4l.bib2lod.ontology.ld4l.Ld4lItemType;
import org.ld4l.bib2lod.ontology.ld4l.Ld4lObjectProp;
import org.ld4l.bib2lod.testing.AbstractTestClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.ld4l.bib2lod.entity.Entity;
import org.ld4l.bib2lod.entitybuilders.BuildParams;
import org.ld4l.bib2lod.entitybuilders.EntityBuilder.EntityBuilderException;
import org.ld4l.bib2lod.entitybuilders.marcxml.ld4l.LegacySourceDataEntityBuilder;
import org.ld4l.bib2lod.ontology.Type;
import org.ld4l.bib2lod.ontology.ld4l.Ld4lActivityType;
import org.ld4l.bib2lod.ontology.ld4l.Ld4lDatatypeProp;
Expand Down

0 comments on commit 16c1510

Please sign in to comment.