Search in sources :

Example 31 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class MetadataValuesMatchCondition method getResult.

/**
 * Return true if any value for a specified field in the item matches any of the specified regex patterns
 * Return false if not
 * @param context   DSpace context
 * @param item      Item to evaluate
 * @return boolean result of evaluation
 * @throws LogicalStatementException
 */
@Override
public boolean getResult(Context context, Item item) throws LogicalStatementException {
    String field = (String) getParameters().get("field");
    if (field == null) {
        return false;
    }
    String[] fieldParts = field.split("\\.");
    String schema = (fieldParts.length > 0 ? fieldParts[0] : null);
    String element = (fieldParts.length > 1 ? fieldParts[1] : null);
    String qualifier = (fieldParts.length > 2 ? fieldParts[2] : null);
    List<MetadataValue> values = itemService.getMetadata(item, schema, element, qualifier, Item.ANY);
    for (MetadataValue value : values) {
        if (getParameters().get("patterns") instanceof List) {
            List<String> patternList = (List<String>) getParameters().get("patterns");
            // If the list is empty, just return true and log error?
            log.error("No patterns were passed for metadata value matching, defaulting to 'true'");
            if (patternList == null) {
                return true;
            }
            for (String pattern : patternList) {
                log.debug("logic for " + item.getHandle() + ": pattern passed is " + pattern + ", checking value " + value.getValue());
                Pattern p = Pattern.compile(pattern);
                Matcher m = p.matcher(value.getValue());
                if (m.find()) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) List(java.util.List)

Example 32 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class TemplateItemConverter method convert.

@Override
public TemplateItemRest convert(TemplateItem templateItem, Projection projection) {
    TemplateItemRest templateItemRest = new TemplateItemRest();
    templateItemRest.setProjection(projection);
    if (templateItem.getID() != null) {
        templateItemRest.setId(templateItem.getID());
        templateItemRest.setUuid(templateItem.getID());
    }
    templateItemRest.setLastModified(templateItem.getLastModified());
    Collection templateItemOf = templateItem.getTemplateItemOf();
    if (templateItemOf != null) {
        templateItemRest.setTemplateItemOf(converter.toRest(templateItemOf, projection));
    }
    List<MetadataValue> fullList = itemService.getMetadata(templateItem.getItem(), Item.ANY, Item.ANY, Item.ANY, Item.ANY, true);
    MetadataValueList metadataValues = new MetadataValueList(fullList);
    templateItemRest.setMetadata(converter.toRest(metadataValues, projection));
    return templateItemRest;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) MetadataValueList(org.dspace.app.rest.model.MetadataValueList) TemplateItemRest(org.dspace.app.rest.model.TemplateItemRest) Collection(org.dspace.content.Collection)

Example 33 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class ItemConverter method convert.

@Override
public ItemRest convert(Item obj, Projection projection) {
    ItemRest item = super.convert(obj, projection);
    item.setInArchive(obj.isArchived());
    item.setDiscoverable(obj.isDiscoverable());
    item.setWithdrawn(obj.isWithdrawn());
    item.setLastModified(obj.getLastModified());
    List<MetadataValue> entityTypes = itemService.getMetadata(obj, "dspace", "entity", "type", Item.ANY, false);
    if (CollectionUtils.isNotEmpty(entityTypes) && StringUtils.isNotBlank(entityTypes.get(0).getValue())) {
        item.setEntityType(entityTypes.get(0).getValue());
    }
    return item;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) ItemRest(org.dspace.app.rest.model.ItemRest)

Example 34 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class DIMDisseminationCrosswalk method disseminateElement.

@Override
public Element disseminateElement(Context context, DSpaceObject dso) throws CrosswalkException, IOException, SQLException, AuthorizeException {
    if (dso.getType() != Constants.ITEM) {
        throw new CrosswalkObjectNotSupported("DIMDisseminationCrosswalk can only crosswalk an Item.");
    }
    Item item = (Item) dso;
    List<MetadataValue> dc = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
    Element dim = new Element("dim", DIM_NS);
    for (MetadataValue aDc : dc) {
        MetadataField metadataField = aDc.getMetadataField();
        MetadataSchema metadataSchema = metadataField.getMetadataSchema();
        Element field = new Element("field", DIM_NS);
        field.setAttribute("mdschema", metadataSchema.getName());
        field.setAttribute("element", metadataField.getElement());
        if (metadataField.getQualifier() != null) {
            field.setAttribute("qualifier", metadataField.getQualifier());
        }
        if (aDc.getLanguage() != null) {
            field.setAttribute("lang", aDc.getLanguage());
        }
        if (aDc.getValue() != null) {
            field.setText(aDc.getValue());
        }
        dim.addContent(field);
    }
    return dim;
}
Also used : Item(org.dspace.content.Item) MetadataValue(org.dspace.content.MetadataValue) Element(org.jdom2.Element) MetadataSchema(org.dspace.content.MetadataSchema) MetadataField(org.dspace.content.MetadataField)

Example 35 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class GeneratePubmedQueryService method generateQueryForItem.

/**
 * Create a Query object based on a given item.
 * If the item has at least 1 value for dc.identifier.doi, the first one will be used.
 * If no DOI is found, the title will be used.
 * When no DOI or title is found, an null object is returned instead.
 *
 * @param item the Item to create a Query from
 */
@Override
public Query generateQueryForItem(Item item) throws MetadataSourceException {
    Query query = new Query();
    // Retrieve an instance of the ItemService to access business calls on an item.
    ItemService itemService = ContentServiceFactory.getInstance().getItemService();
    List<MetadataValue> doi = itemService.getMetadata(item, "dc", "identifier", "doi", Item.ANY);
    if (doi.size() > 0) {
        query.addParameter("term", doi.get(0).getValue());
        query.addParameter("field", "ELocationID");
        return query;
    }
    List<MetadataValue> title = itemService.getMetadata(item, "dc", "title", null, Item.ANY);
    if (title.size() > 0) {
        query.addParameter("term", title.get(0).getValue());
        query.addParameter("field", "title");
        return query;
    }
    return null;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) Query(org.dspace.importer.external.datamodel.Query) ItemService(org.dspace.content.service.ItemService)

Aggregations

MetadataValue (org.dspace.content.MetadataValue)140 Item (org.dspace.content.Item)45 ArrayList (java.util.ArrayList)39 SQLException (java.sql.SQLException)29 MetadataField (org.dspace.content.MetadataField)22 Test (org.junit.Test)21 Date (java.util.Date)16 Collection (org.dspace.content.Collection)16 IOException (java.io.IOException)15 AuthorizeException (org.dspace.authorize.AuthorizeException)13 List (java.util.List)11 Community (org.dspace.content.Community)11 WorkspaceItem (org.dspace.content.WorkspaceItem)11 MetadataValueRest (org.dspace.app.rest.model.MetadataValueRest)10 DCDate (org.dspace.content.DCDate)9 MetadataSchema (org.dspace.content.MetadataSchema)9 EPerson (org.dspace.eperson.EPerson)9 WorkflowItem (org.dspace.workflow.WorkflowItem)9 AbstractUnitTest (org.dspace.AbstractUnitTest)8 AbstractEntityIntegrationTest (org.dspace.app.rest.test.AbstractEntityIntegrationTest)8