Search in sources :

Example 26 with MetadataValue

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

the class VersioningServiceImpl method createNewVersion.

@Override
public Version createNewVersion(Context c, Item item, String summary) {
    try {
        VersionHistory vh = versionHistoryService.findByItem(c, item);
        if (vh == null) {
            // first time: create 2 versions: old and new one
            vh = versionHistoryService.create(c);
            // get dc:date.accessioned to be set as first version date...
            List<MetadataValue> values = itemService.getMetadata(item, "dc", "date", "accessioned", Item.ANY);
            Date versionDate = new Date();
            if (values != null && values.size() > 0) {
                String date = values.get(0).getValue();
                versionDate = new DCDate(date).toDate();
            }
            createVersion(c, vh, item, "", versionDate);
        }
        // Create new Item
        Item itemNew = provider.createNewItemAndAddItInWorkspace(c, item);
        // create new version
        Version version = createVersion(c, vh, itemNew, summary, new Date());
        // Complete any update of the Item and new Identifier generation that needs to happen
        provider.updateItemState(c, itemNew, item);
        return version;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : MetadataValue(org.dspace.content.MetadataValue) Item(org.dspace.content.Item) WorkflowItem(org.dspace.workflow.WorkflowItem) WorkspaceItem(org.dspace.content.WorkspaceItem) DCDate(org.dspace.content.DCDate) Date(java.util.Date) DCDate(org.dspace.content.DCDate) SQLException(java.sql.SQLException)

Example 27 with MetadataValue

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

the class XHTMLHeadDisseminationCrosswalk method disseminateList.

/**
 * Return &lt;meta&gt; elements that can be put in the &lt;head&gt; element
 * of an XHTML document.
 *
 * @param context context
 * @throws CrosswalkException crosswalk error
 * @throws IOException        if IO error
 * @throws SQLException       if database error
 * @throws AuthorizeException if authorization error
 * @return List of Elements
 */
@Override
public List<Element> disseminateList(Context context, DSpaceObject dso) throws CrosswalkException, IOException, SQLException, AuthorizeException {
    if (dso.getType() != Constants.ITEM) {
        String h = dso.getHandle();
        throw new CrosswalkObjectNotSupported("Can only support items; object passed in with DB ID " + dso.getID() + ", type " + Constants.typeText[dso.getType()] + ", handle " + (h == null ? "null" : h));
    }
    Item item = (Item) dso;
    String handle = item.getHandle();
    List<Element> metas = new ArrayList<>();
    List<MetadataValue> values = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
    // Add in schema URLs e.g. <link rel="schema.DC" href="...." />
    Iterator<String> schemaIterator = schemaURLs.keySet().iterator();
    while (schemaIterator.hasNext()) {
        String s = schemaIterator.next();
        Element e = new Element("link", XHTML_NAMESPACE);
        e.setAttribute("rel", s);
        e.setAttribute("href", schemaURLs.get(s));
        metas.add(e);
    }
    for (int i = 0; i < values.size(); i++) {
        MetadataValue v = values.get(i);
        MetadataField metadataField = v.getMetadataField();
        MetadataSchema metadataSchema = metadataField.getMetadataSchema();
        // Work out the key for the Maps that will tell us which metadata
        // name + scheme to use
        String key = metadataSchema.getName() + "." + metadataField.getElement() + (metadataField.getQualifier() != null ? "." + metadataField.getQualifier() : "");
        // For later error msg
        String originalKey = key;
        // Find appropriate metadata field name to put in element
        String name = names.get(key);
        // If we don't have a field, try removing qualifier
        if (name == null && metadataField.getQualifier() != null) {
            key = metadataSchema.getName() + "." + metadataField.getElement();
            name = names.get(key);
        }
        // Do not include description.provenance
        boolean provenance = "description".equals(metadataField.getElement()) && "provenance".equals(metadataField.getQualifier());
        if (name == null) {
            // element is OK, so just report at DEBUG level
            if (log.isDebugEnabled()) {
                log.debug("No <meta> field for item " + (handle == null ? String.valueOf(dso.getID()) : handle) + " field " + originalKey);
            }
        } else if (!provenance) {
            Element e = new Element("meta", XHTML_NAMESPACE);
            e.setAttribute("name", name);
            if (v.getValue() == null) {
                e.setAttribute("content", "");
            } else {
                // Check that we can output the content
                String reason = Verifier.checkCharacterData(v.getValue());
                if (reason == null) {
                    // TODO: Check valid encoding?  We assume UTF-8
                    // TODO: Check escaping "<>&
                    e.setAttribute("content", v.getValue());
                } else {
                    // Warn that we found invalid characters
                    log.warn("Invalid attribute characters in Metadata: " + reason);
                    // Strip any characters that we can, and if the result is valid, output it
                    String simpleText = v.getValue().replaceAll("\\p{Cntrl}", "");
                    if (Verifier.checkCharacterData(simpleText) == null) {
                        e.setAttribute("content", simpleText);
                    }
                }
            }
            if (v.getLanguage() != null && !v.getLanguage().equals("")) {
                e.setAttribute("lang", v.getLanguage(), Namespace.XML_NAMESPACE);
            }
            String schemeAttr = schemes.get(key);
            if (schemeAttr != null) {
                e.setAttribute("scheme", schemeAttr);
            }
            metas.add(e);
        }
    }
    return metas;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) Element(org.jdom2.Element) MetadataSchema(org.dspace.content.MetadataSchema) ArrayList(java.util.ArrayList) MetadataField(org.dspace.content.MetadataField) Item(org.dspace.content.Item)

Example 28 with MetadataValue

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

the class XSLTDisseminationCrosswalk method item2Metadata.

protected static List<MetadataValueDTO> item2Metadata(Item item) {
    List<MetadataValue> dcvs = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
    List<MetadataValueDTO> result = new ArrayList<>();
    for (MetadataValue metadataValue : dcvs) {
        result.add(new MetadataValueDTO(metadataValue));
    }
    return result;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) ArrayList(java.util.ArrayList) MetadataValueDTO(org.dspace.content.dto.MetadataValueDTO)

Example 29 with MetadataValue

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

the class MetadataValueDAOImpl method findByField.

@Override
public List<MetadataValue> findByField(Context context, MetadataField metadataField) throws SQLException {
    CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
    CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, MetadataValue.class);
    Root<MetadataValue> metadataValueRoot = criteriaQuery.from(MetadataValue.class);
    Join<MetadataValue, MetadataField> join = metadataValueRoot.join("metadataField");
    criteriaQuery.select(metadataValueRoot);
    criteriaQuery.where(criteriaBuilder.equal(join.get(MetadataField_.id), metadataField.getID()));
    return list(context, criteriaQuery, false, MetadataValue.class, -1, -1);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MetadataValue(org.dspace.content.MetadataValue) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) MetadataField(org.dspace.content.MetadataField)

Example 30 with MetadataValue

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

the class MetadataValueDAOImpl method getMinimum.

@Override
public MetadataValue getMinimum(Context context, int metadataFieldId) throws SQLException {
    String queryString = "SELECT m FROM MetadataValue m JOIN FETCH m.metadataField WHERE m.metadataField.id = " + ":metadata_field_id ORDER BY text_value";
    Query query = createQuery(context, queryString);
    query.setParameter("metadata_field_id", metadataFieldId);
    query.setMaxResults(1);
    return (MetadataValue) query.getSingleResult();
}
Also used : MetadataValue(org.dspace.content.MetadataValue) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query)

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