Search in sources :

Example 11 with MetadataValue

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

the class CommunityCollectionGenerator method buildCollection.

public SwordCollection buildCollection(Context context, DSpaceObject dso, SwordConfigurationDSpace swordConfig) throws DSpaceSwordException {
    if (!(dso instanceof Community)) {
        log.error("buildCollection passed something other than a Community object");
        throw new DSpaceSwordException("Incorrect ATOMCollectionGenerator instantiated");
    }
    // get the things we need out of the service
    SwordUrlManager urlManager = swordConfig.getUrlManager(context, swordConfig);
    Community com = (Community) dso;
    SwordCollection scol = new SwordCollection();
    // prepare the parameters to be put in the sword collection
    String location = urlManager.getDepositLocation(com);
    if (location == null) {
        location = handleService.getCanonicalForm(com.getHandle());
    }
    scol.setLocation(location);
    // collection title is just the community name
    String title = communityService.getName(com);
    if (StringUtils.isNotBlank(title)) {
        scol.setTitle(title);
    }
    // FIXME: the community has no obvious licence
    // the collection policy is the licence to which the collection adheres
    // String collectionPolicy = col.getLicense();
    // abstract is the short description of the collection
    List<MetadataValue> abstracts = communityService.getMetadataByMetadataString(com, "short_description");
    if (abstracts != null && !abstracts.isEmpty()) {
        String firstValue = abstracts.get(0).getValue();
        if (StringUtils.isNotBlank(firstValue)) {
            scol.setAbstract(firstValue);
        }
    }
    // do we support mediated deposit
    scol.setMediation(swordConfig.isMediated());
    // NOTE: for communities, there are no MIME types that it accepts.
    // the list of mime types that we accept
    // offer up the collections from this item as deposit targets
    String subService = urlManager.constructSubServiceUrl(com);
    scol.addSubService(new IRI(subService));
    log.debug("Created ATOM Collection for DSpace Community");
    return scol;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) IRI(org.apache.abdera.i18n.iri.IRI) Community(org.dspace.content.Community) SwordCollection(org.swordapp.server.SwordCollection)

Example 12 with MetadataValue

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

the class SyndicationFeed method populate.

/**
 * Fills in the feed and entry-level metadata from DSpace objects.
 *
 * @param request request
 * @param context context
 * @param dso     the scope
 * @param items   array of objects
 * @param labels  label map
 */
public void populate(HttpServletRequest request, Context context, IndexableObject dso, List<IndexableObject> items, Map<String, String> labels) {
    String logoURL = null;
    String objectURL = null;
    String defaultTitle = null;
    boolean podcastFeed = false;
    this.request = request;
    // dso is null for the whole site, or a search without scope
    if (dso == null) {
        defaultTitle = configurationService.getProperty("dspace.name");
        feed.setDescription(localize(labels, MSG_FEED_DESCRIPTION));
        objectURL = resolveURL(request, null);
    } else {
        Bitstream logo = null;
        if (dso instanceof IndexableCollection) {
            Collection col = ((IndexableCollection) dso).getIndexedObject();
            defaultTitle = col.getName();
            feed.setDescription(collectionService.getMetadataFirstValue(col, CollectionService.MD_SHORT_DESCRIPTION, Item.ANY));
            logo = col.getLogo();
            String cols = configurationService.getProperty("webui.feed.podcast.collections");
            if (cols != null && cols.length() > 1 && cols.contains(col.getHandle())) {
                podcastFeed = true;
            }
            objectURL = resolveURL(request, col);
        } else if (dso instanceof IndexableCommunity) {
            Community comm = ((IndexableCommunity) dso).getIndexedObject();
            defaultTitle = comm.getName();
            feed.setDescription(communityService.getMetadataFirstValue(comm, CommunityService.MD_SHORT_DESCRIPTION, Item.ANY));
            logo = comm.getLogo();
            String comms = configurationService.getProperty("webui.feed.podcast.communities");
            if (comms != null && comms.length() > 1 && comms.contains(comm.getHandle())) {
                podcastFeed = true;
            }
            objectURL = resolveURL(request, comm);
        }
        if (logo != null) {
            logoURL = urlOfBitstream(request, logo);
        }
    }
    feed.setTitle(labels.containsKey(MSG_FEED_TITLE) ? localize(labels, MSG_FEED_TITLE) : defaultTitle);
    feed.setLink(objectURL);
    feed.setPublishedDate(new Date());
    feed.setUri(objectURL);
    // add logo if we found one:
    if (logoURL != null) {
        // we use the path to the logo for this, the logo itself cannot
        // be contained in the rdf. Not all RSS-viewers show this logo.
        SyndImage image = new SyndImageImpl();
        image.setLink(objectURL);
        if (StringUtils.isNotBlank(feed.getTitle())) {
            image.setTitle(feed.getTitle());
        } else {
            image.setTitle(localize(labels, MSG_LOGO_TITLE));
        }
        image.setUrl(logoURL);
        feed.setImage(image);
    }
    // add entries for items
    if (items != null) {
        List<SyndEntry> entries = new ArrayList<>();
        for (IndexableObject idxObj : items) {
            if (!(idxObj instanceof IndexableItem)) {
                continue;
            }
            Item item = ((IndexableItem) idxObj).getIndexedObject();
            boolean hasDate = false;
            SyndEntry entry = new SyndEntryImpl();
            entries.add(entry);
            String entryURL = resolveURL(request, item);
            entry.setLink(entryURL);
            entry.setUri(entryURL);
            String title = getOneDC(item, titleField);
            entry.setTitle(title == null ? localize(labels, MSG_UNTITLED) : title);
            // "published" date -- should be dc.date.issued
            String pubDate = getOneDC(item, dateField);
            if (pubDate != null) {
                entry.setPublishedDate((new DCDate(pubDate)).toDate());
                hasDate = true;
            }
            // date of last change to Item
            entry.setUpdatedDate(item.getLastModified());
            StringBuilder db = new StringBuilder();
            for (String df : descriptionFields) {
                // Special Case: "(date)" in field name means render as date
                boolean isDate = df.indexOf("(date)") > 0;
                if (isDate) {
                    df = df.replaceAll("\\(date\\)", "");
                }
                List<MetadataValue> dcv = itemService.getMetadataByMetadataString(item, df);
                if (dcv.size() > 0) {
                    String fieldLabel = labels.get(MSG_METADATA + df);
                    if (fieldLabel != null && fieldLabel.length() > 0) {
                        db.append(fieldLabel).append(": ");
                    }
                    boolean first = true;
                    for (MetadataValue v : dcv) {
                        if (first) {
                            first = false;
                        } else {
                            db.append("; ");
                        }
                        db.append(isDate ? new DCDate(v.getValue()).toString() : v.getValue());
                    }
                    db.append("\n");
                }
            }
            if (db.length() > 0) {
                SyndContent desc = new SyndContentImpl();
                desc.setType("text/plain");
                desc.setValue(db.toString());
                entry.setDescription(desc);
            }
            // This gets the authors into an ATOM feed
            List<MetadataValue> authors = itemService.getMetadataByMetadataString(item, authorField);
            if (authors.size() > 0) {
                List<SyndPerson> creators = new ArrayList<>();
                for (MetadataValue author : authors) {
                    SyndPerson sp = new SyndPersonImpl();
                    sp.setName(author.getValue());
                    creators.add(sp);
                }
                entry.setAuthors(creators);
            }
            // only add DC module if any DC fields are configured
            if (dcCreatorField != null || dcDateField != null || dcDescriptionField != null) {
                DCModule dc = new DCModuleImpl();
                if (dcCreatorField != null) {
                    List<MetadataValue> dcAuthors = itemService.getMetadataByMetadataString(item, dcCreatorField);
                    if (dcAuthors.size() > 0) {
                        List<String> creators = new ArrayList<>();
                        for (MetadataValue author : dcAuthors) {
                            creators.add(author.getValue());
                        }
                        dc.setCreators(creators);
                    }
                }
                if (dcDateField != null && !hasDate) {
                    List<MetadataValue> v = itemService.getMetadataByMetadataString(item, dcDateField);
                    if (v.size() > 0) {
                        dc.setDate((new DCDate(v.get(0).getValue())).toDate());
                    }
                }
                if (dcDescriptionField != null) {
                    List<MetadataValue> v = itemService.getMetadataByMetadataString(item, dcDescriptionField);
                    if (v.size() > 0) {
                        StringBuilder descs = new StringBuilder();
                        for (MetadataValue d : v) {
                            if (descs.length() > 0) {
                                descs.append("\n\n");
                            }
                            descs.append(d.getValue());
                        }
                        dc.setDescription(descs.toString());
                    }
                }
                entry.getModules().add(dc);
            }
            // iTunes Podcast Support - START
            if (podcastFeed) {
                // Add enclosure(s)
                List<SyndEnclosure> enclosures = new ArrayList();
                try {
                    List<Bundle> bunds = itemService.getBundles(item, "ORIGINAL");
                    if (bunds.get(0) != null) {
                        List<Bitstream> bits = bunds.get(0).getBitstreams();
                        for (Bitstream bit : bits) {
                            String mime = bit.getFormat(context).getMIMEType();
                            if (ArrayUtils.contains(podcastableMIMETypes, mime)) {
                                SyndEnclosure enc = new SyndEnclosureImpl();
                                enc.setType(bit.getFormat(context).getMIMEType());
                                enc.setLength(bit.getSizeBytes());
                                enc.setUrl(urlOfBitstream(request, bit));
                                enclosures.add(enc);
                            }
                        }
                    }
                    // Also try to add an external value from dc.identifier.other
                    // We are assuming that if this is set, then it is a media file
                    List<MetadataValue> externalMedia = itemService.getMetadataByMetadataString(item, externalSourceField);
                    if (externalMedia.size() > 0) {
                        for (MetadataValue anExternalMedia : externalMedia) {
                            SyndEnclosure enc = new SyndEnclosureImpl();
                            enc.setType(// We can't determine MIME of external file, so just
                            "audio/x-mpeg");
                            // picking one.
                            enc.setLength(1);
                            enc.setUrl(anExternalMedia.getValue());
                            enclosures.add(enc);
                        }
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
                entry.setEnclosures(enclosures);
                // Get iTunes specific fields: author, subtitle, summary, duration, keywords
                EntryInformation itunes = new EntryInformationImpl();
                String author = getOneDC(item, authorField);
                if (author != null && author.length() > 0) {
                    // <itunes:author>
                    itunes.setAuthor(author);
                }
                // <itunes:subtitle>
                itunes.setSubtitle(title == null ? localize(labels, MSG_UNTITLED) : title);
                if (db.length() > 0) {
                    // <itunes:summary>
                    itunes.setSummary(db.toString());
                }
                String extent = getOneDC(item, // assumed that user will enter this field
                "dc.format.extent");
                // with length of song in seconds
                if (extent != null && extent.length() > 0) {
                    extent = extent.split(" ")[0];
                    Integer duration = Integer.parseInt(extent);
                    // <itunes:duration>
                    itunes.setDuration(new Duration(duration));
                }
                String subject = getOneDC(item, "dc.subject");
                if (subject != null && subject.length() > 0) {
                    String[] subjects = new String[1];
                    subjects[0] = subject;
                    // <itunes:keywords>
                    itunes.setKeywords(subjects);
                }
                entry.getModules().add(itunes);
            }
        }
        feed.setEntries(entries);
    }
}
Also used : EntryInformationImpl(com.rometools.modules.itunes.EntryInformationImpl) SyndImageImpl(com.rometools.rome.feed.synd.SyndImageImpl) Bitstream(org.dspace.content.Bitstream) SQLException(java.sql.SQLException) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) ArrayList(java.util.ArrayList) DCModule(com.rometools.rome.feed.module.DCModule) SyndPerson(com.rometools.rome.feed.synd.SyndPerson) IndexableCommunity(org.dspace.discovery.indexobject.IndexableCommunity) IndexableItem(org.dspace.discovery.indexobject.IndexableItem) Item(org.dspace.content.Item) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) DCDate(org.dspace.content.DCDate) SyndEnclosure(com.rometools.rome.feed.synd.SyndEnclosure) SyndImage(com.rometools.rome.feed.synd.SyndImage) EntryInformation(com.rometools.modules.itunes.EntryInformation) SyndEnclosureImpl(com.rometools.rome.feed.synd.SyndEnclosureImpl) IndexableCollection(org.dspace.discovery.indexobject.IndexableCollection) MetadataValue(org.dspace.content.MetadataValue) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) Bundle(org.dspace.content.Bundle) Duration(com.rometools.modules.itunes.types.Duration) Date(java.util.Date) DCDate(org.dspace.content.DCDate) SyndPersonImpl(com.rometools.rome.feed.synd.SyndPersonImpl) DCModuleImpl(com.rometools.rome.feed.module.DCModuleImpl) SyndContent(com.rometools.rome.feed.synd.SyndContent) IndexableItem(org.dspace.discovery.indexobject.IndexableItem) Collection(org.dspace.content.Collection) IndexableCollection(org.dspace.discovery.indexobject.IndexableCollection) IndexableObject(org.dspace.discovery.IndexableObject) Community(org.dspace.content.Community) IndexableCommunity(org.dspace.discovery.indexobject.IndexableCommunity)

Example 13 with MetadataValue

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

the class Util method getControlledVocabulariesDisplayValueLocalized.

/**
 * Get a list of all the respective "displayed-value(s)" from the given
 * "stored-value(s)" for a specific metadata field of a DSpace Item, by
 * reading submission-forms.xml
 *
 * @param item      The Dspace Item
 * @param values    A Metadatum[] array of the specific "stored-value(s)"
 * @param schema    A String with the schema name of the metadata field
 * @param element   A String with the element name of the metadata field
 * @param qualifier A String with the qualifier name of the metadata field
 * @param locale    locale
 * @return A list of the respective "displayed-values"
 * @throws SQLException            if database error
 * @throws DCInputsReaderException if reader error
 */
public static List<String> getControlledVocabulariesDisplayValueLocalized(Item item, List<MetadataValue> values, String schema, String element, String qualifier, Locale locale) throws SQLException, DCInputsReaderException {
    List<String> toReturn = new ArrayList<>();
    DCInput myInputs = null;
    boolean myInputsFound = false;
    String formFileName = I18nUtil.getInputFormsFileName(locale);
    String col_handle = "";
    Collection collection = item.getOwningCollection();
    if (collection == null) {
        // set an empty handle so to get the default input set
        col_handle = "";
    } else {
        col_handle = collection.getHandle();
    }
    // Read the input form file for the specific collection
    DCInputsReader inputsReader = new DCInputsReader(formFileName);
    List<DCInputSet> inputSets = inputsReader.getInputsByCollectionHandle(col_handle);
    // Replace the values of Metadatum[] with the correct ones in case
    // of
    // controlled vocabularies
    String currentField = Utils.standardize(schema, element, qualifier, ".");
    for (DCInputSet inputSet : inputSets) {
        if (inputSet != null) {
            int fieldsNums = inputSet.getNumberFields();
            for (int p = 0; p < fieldsNums; p++) {
                DCInput[][] inputs = inputSet.getFields();
                if (inputs != null) {
                    for (int i = 0; i < inputs.length; i++) {
                        for (int j = 0; j < inputs[i].length; j++) {
                            String inputField = Utils.standardize(inputs[i][j].getSchema(), inputs[i][j].getElement(), inputs[i][j].getQualifier(), ".");
                            if (currentField.equals(inputField)) {
                                myInputs = inputs[i][j];
                                myInputsFound = true;
                                break;
                            }
                        }
                    }
                }
                if (myInputsFound) {
                    break;
                }
            }
        }
        if (myInputsFound) {
            for (MetadataValue value : values) {
                String pairsName = myInputs.getPairsType();
                String stored_value = value.getValue();
                String displayVal = myInputs.getDisplayString(pairsName, stored_value);
                if (displayVal != null && !"".equals(displayVal)) {
                    toReturn.add(displayVal);
                }
            }
        }
    }
    return toReturn;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) ArrayList(java.util.ArrayList) Collection(org.dspace.content.Collection)

Example 14 with MetadataValue

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

the class GoogleMetadata method parseWildcard.

/**
 * Expand any wildcard characters to an array of all matching fields for
 * this item. No order consistency is implied.
 *
 * @param field The field identifier containing a wildcard character.
 * @return Expanded field list.
 */
protected ArrayList<String> parseWildcard(String field) {
    if (!field.contains("*")) {
        return null;
    } else {
        String[] components = parseComponents(field);
        for (int i = 0; i < components.length; i++) {
            if (components[i].trim().equals("*")) {
                components[i] = Item.ANY;
            }
        }
        List<MetadataValue> allMD = itemService.getMetadata(item, components[0], components[1], components[2], Item.ANY);
        ArrayList<String> expandedDC = new ArrayList<>();
        for (MetadataValue v : allMD) {
            // De-dup multiple occurrences of field names in item
            if (!expandedDC.contains(buildFieldName(v))) {
                expandedDC.add(buildFieldName(v));
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Field Names From Expanded Wildcard \"" + field + "\"");
            for (String v : expandedDC) {
                log.debug("    " + v);
            }
        }
        return expandedDC;
    }
}
Also used : MetadataValue(org.dspace.content.MetadataValue) ArrayList(java.util.ArrayList)

Example 15 with MetadataValue

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

the class BitstreamStorageServiceImpl method clone.

/**
 * @param context   The relevant DSpace Context.
 * @param bitstream the bitstream to be cloned
 * @return id of the clone bitstream.
 * A general class of exceptions produced by failed or interrupted I/O operations.
 * @throws SQLException       An exception that provides information on a database access error or other errors.
 * @throws AuthorizeException Exception indicating the current user of the context does not have permission
 *                            to perform a particular action.
 */
@Override
public Bitstream clone(Context context, Bitstream bitstream) throws SQLException, IOException, AuthorizeException {
    Bitstream clonedBitstream = null;
    try {
        // Update our bitstream but turn off the authorization system since permissions
        // haven't been set at this point in time.
        context.turnOffAuthorisationSystem();
        clonedBitstream = bitstreamService.clone(context, bitstream);
        clonedBitstream.setStoreNumber(bitstream.getStoreNumber());
        List<MetadataValue> metadataValues = bitstreamService.getMetadata(bitstream, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
        for (MetadataValue metadataValue : metadataValues) {
            bitstreamService.addMetadata(context, clonedBitstream, metadataValue.getMetadataField(), metadataValue.getLanguage(), metadataValue.getValue(), metadataValue.getAuthority(), metadataValue.getConfidence());
        }
        bitstreamService.update(context, clonedBitstream);
    } catch (AuthorizeException e) {
        log.error(e);
    // Can never happen since we turn off authorization before we update
    } finally {
        context.restoreAuthSystemState();
    }
    return clonedBitstream;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) Bitstream(org.dspace.content.Bitstream) AuthorizeException(org.dspace.authorize.AuthorizeException)

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