Search in sources :

Example 1 with DCDate

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

the class DayTableEmbargoSetter method parseTerms.

/**
 * Parse the terms into a definite date. Only terms expressions processed
 * are those defined in 'embargo.terms.days' configuration property.
 *
 * @param context the DSpace context
 * @param item    the item to embargo
 * @param terms   the embargo terms
 * @return parsed date in DCDate format
 */
@Override
public DCDate parseTerms(Context context, Item item, String terms) throws SQLException, AuthorizeException {
    String termsOpen = DSpaceServicesFactory.getInstance().getConfigurationService().getProperty("embargo.terms.open");
    Properties termProps = getTermProperties();
    if (terms != null) {
        if (termsOpen.equals(terms)) {
            return EmbargoServiceImpl.FOREVER;
        }
        String days = termProps.getProperty(terms);
        if (days != null && days.length() > 0) {
            long lift = System.currentTimeMillis() + (Long.parseLong(days) * 24 * 60 * 60 * 1000);
            return new DCDate(new Date(lift));
        }
    }
    return null;
}
Also used : DCDate(org.dspace.content.DCDate) Properties(java.util.Properties) Date(java.util.Date) DCDate(org.dspace.content.DCDate)

Example 2 with DCDate

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

the class EmbargoServiceImpl method getEmbargoTermsAsDate.

@Override
public DCDate getEmbargoTermsAsDate(Context context, Item item) throws SQLException, AuthorizeException {
    List<MetadataValue> terms = itemService.getMetadata(item, terms_schema, terms_element, terms_qualifier, Item.ANY);
    DCDate result = null;
    // Its poor form to blindly use an object that could be null...
    if (terms == null) {
        return null;
    }
    result = setter.parseTerms(context, item, terms.size() > 0 ? terms.get(0).getValue() : null);
    if (result == null) {
        return null;
    }
    // new DCDate(non-date String) means toDate() will return null
    Date liftDate = result.toDate();
    if (liftDate == null) {
        throw new IllegalArgumentException("Embargo lift date is uninterpretable:  " + result.toString());
    }
    /*
         * NOTE: We do not check here for past dates as it can result in errors during AIP restoration.
         * Therefore, UIs should perform any such date validation on input. See DS-3348
         */
    return result;
}
Also used : MetadataValue(org.dspace.content.MetadataValue) DCDate(org.dspace.content.DCDate) Date(java.util.Date) DCDate(org.dspace.content.DCDate)

Example 3 with DCDate

use of org.dspace.content.DCDate 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 4 with DCDate

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

the class SolrLoggerServiceImpl method shardSolrIndex.

@Override
public void shardSolrIndex() throws IOException, SolrServerException {
    if (!(solr instanceof HttpSolrClient)) {
        return;
    }
    /*
        Start by faceting by year so we can include each year in a separate core !
         */
    SolrQuery yearRangeQuery = new SolrQuery();
    yearRangeQuery.setQuery("*:*");
    yearRangeQuery.setRows(0);
    yearRangeQuery.setFacet(true);
    yearRangeQuery.add(FacetParams.FACET_RANGE, "time");
    // We go back to 2000 the year 2000, this is a bit overkill but this way we ensure we have everything
    // The alternative would be to sort but that isn't recommended since it would be a very costly query !
    yearRangeQuery.add(FacetParams.FACET_RANGE_START, "NOW/YEAR-" + (Calendar.getInstance().get(Calendar.YEAR) - 2000) + "YEARS");
    // Add the +0year to ensure that we DO NOT include the current year
    yearRangeQuery.add(FacetParams.FACET_RANGE_END, "NOW/YEAR+0YEARS");
    yearRangeQuery.add(FacetParams.FACET_RANGE_GAP, "+1YEAR");
    yearRangeQuery.add(FacetParams.FACET_MINCOUNT, String.valueOf(1));
    // Create a temp directory to store our files in !
    File tempDirectory = new File(configurationService.getProperty("dspace.dir") + File.separator + "temp" + File.separator);
    tempDirectory.mkdirs();
    QueryResponse queryResponse = solr.query(yearRangeQuery);
    // We only have one range query !
    List<RangeFacet.Count> yearResults = queryResponse.getFacetRanges().get(0).getCounts();
    for (RangeFacet.Count count : yearResults) {
        long totalRecords = count.getCount();
        // Create a range query from this !
        // We start with out current year
        DCDate dcStart = new DCDate(count.getValue());
        Calendar endDate = Calendar.getInstance();
        // Advance one year for the start of the next one !
        endDate.setTime(dcStart.toDate());
        endDate.add(Calendar.YEAR, 1);
        DCDate dcEndDate = new DCDate(endDate.getTime());
        StringBuilder filterQuery = new StringBuilder();
        filterQuery.append("time:([");
        filterQuery.append(ClientUtils.escapeQueryChars(dcStart.toString()));
        filterQuery.append(" TO ");
        filterQuery.append(ClientUtils.escapeQueryChars(dcEndDate.toString()));
        filterQuery.append("]");
        // The next part of the filter query excludes the content from midnight of the next year !
        filterQuery.append(" NOT ").append(ClientUtils.escapeQueryChars(dcEndDate.toString()));
        filterQuery.append(")");
        Map<String, String> yearQueryParams = new HashMap<>();
        yearQueryParams.put(CommonParams.Q, "*:*");
        yearQueryParams.put(CommonParams.ROWS, String.valueOf(10000));
        yearQueryParams.put(CommonParams.FQ, filterQuery.toString());
        yearQueryParams.put(CommonParams.WT, "csv");
        // Tell SOLR how to escape and separate the values of multi-valued fields
        yearQueryParams.put("csv.escape", "\\");
        yearQueryParams.put("csv.mv.separator", MULTIPLE_VALUES_SPLITTER);
        // Start by creating a new core
        String coreName = statisticsCoreBase + "-" + dcStart.getYearUTC();
        HttpSolrClient statisticsYearServer = createCore((HttpSolrClient) solr, coreName);
        System.out.println("Moving: " + totalRecords + " into core " + coreName);
        log.info("Moving: " + totalRecords + " records into core " + coreName);
        List<File> filesToUpload = new ArrayList<>();
        for (int i = 0; i < totalRecords; i += 10000) {
            String solrRequestUrl = ((HttpSolrClient) solr).getBaseURL() + "/select";
            solrRequestUrl = generateURL(solrRequestUrl, yearQueryParams);
            HttpGet get = new HttpGet(solrRequestUrl);
            InputStream csvInputstream;
            File csvFile = new File(tempDirectory.getPath() + File.separatorChar + "temp." + dcStart.getYearUTC() + "." + i + ".csv");
            try (CloseableHttpClient hc = HttpClientBuilder.create().build()) {
                HttpResponse response = hc.execute(get);
                csvInputstream = response.getEntity().getContent();
                // Write the csv ouput to a file !
                FileUtils.copyInputStreamToFile(csvInputstream, csvFile);
            }
            filesToUpload.add(csvFile);
            // Add 10000 & start over again
            yearQueryParams.put(CommonParams.START, String.valueOf((i + 10000)));
        }
        Set<String> multivaluedFields = getMultivaluedFieldNames();
        for (File tempCsv : filesToUpload) {
            // Upload the data in the csv files to our new solr core
            ContentStreamUpdateRequest contentStreamUpdateRequest = new ContentStreamUpdateRequest("/update");
            contentStreamUpdateRequest.setParam("stream.contentType", "text/csv;charset=utf-8");
            contentStreamUpdateRequest.setParam("escape", "\\");
            contentStreamUpdateRequest.setParam("skip", "_version_");
            contentStreamUpdateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
            contentStreamUpdateRequest.addFile(tempCsv, "text/csv;charset=utf-8");
            // instead of one value
            for (String multivaluedField : multivaluedFields) {
                contentStreamUpdateRequest.setParam("f." + multivaluedField + ".split", Boolean.TRUE.toString());
                contentStreamUpdateRequest.setParam("f." + multivaluedField + ".separator", MULTIPLE_VALUES_SPLITTER);
            }
            statisticsYearServer.request(contentStreamUpdateRequest);
        }
        statisticsYearServer.commit(true, true);
        // Delete contents of this year from our year query !
        solr.deleteByQuery(filterQuery.toString());
        solr.commit(true, true);
        log.info("Moved {} records into core: {}", totalRecords, coreName);
    }
    FileUtils.deleteDirectory(tempDirectory);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RangeFacet(org.apache.solr.client.solrj.response.RangeFacet) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Calendar(java.util.Calendar) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) SolrQuery(org.apache.solr.client.solrj.SolrQuery) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) DCDate(org.dspace.content.DCDate) File(java.io.File)

Example 5 with DCDate

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

the class XmlWorkflowServiceImpl method recordStart.

// Create workflow start provenance message
protected void recordStart(Context context, Item myitem, Action action) throws SQLException, IOException, AuthorizeException {
    // get date
    DCDate now = DCDate.getCurrent();
    // Create provenance description
    String provmessage = "";
    if (myitem.getSubmitter() != null) {
        provmessage = "Submitted by " + myitem.getSubmitter().getFullName() + " (" + myitem.getSubmitter().getEmail() + ") on " + now.toString() + " workflow start=" + action.getProvenanceStartId() + "\n";
    } else {
        // else, null submitter
        provmessage = "Submitted by unknown (probably automated) on" + now.toString() + " workflow start=" + action.getProvenanceStartId() + "\n";
    }
    // add sizes and checksums of bitstreams
    provmessage += installItemService.getBitstreamProvenanceMessage(context, myitem);
    // Add message to the DC
    itemService.addMetadata(context, myitem, MetadataSchemaEnum.DC.getName(), "description", "provenance", "en", provmessage);
    itemService.update(context, myitem);
}
Also used : DCDate(org.dspace.content.DCDate)

Aggregations

DCDate (org.dspace.content.DCDate)21 Date (java.util.Date)13 SQLException (java.sql.SQLException)8 MetadataValue (org.dspace.content.MetadataValue)7 Item (org.dspace.content.Item)4 IOException (java.io.IOException)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3 Collection (org.dspace.content.Collection)3 SimpleDateFormat (java.text.SimpleDateFormat)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 AuthorizeException (org.dspace.authorize.AuthorizeException)2 Bitstream (org.dspace.content.Bitstream)2 Bundle (org.dspace.content.Bundle)2 EntryInformation (com.rometools.modules.itunes.EntryInformation)1 EntryInformationImpl (com.rometools.modules.itunes.EntryInformationImpl)1 Duration (com.rometools.modules.itunes.types.Duration)1 DCModule (com.rometools.rome.feed.module.DCModule)1 DCModuleImpl (com.rometools.rome.feed.module.DCModuleImpl)1