mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-15 14:03:17 +00:00
[DS-317] Add Support for Embargo function
git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@4277 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
@@ -18,7 +18,8 @@
|
|||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* - Neither the name of the DSpace Foundation nor the names of its
|
* - Neither the name of the Hewlett-Packard Company nor the name of the
|
||||||
|
* Massachusetts Institute of Technology nor the names of their
|
||||||
* contributors may be used to endorse or promote products derived from
|
* contributors may be used to endorse or promote products derived from
|
||||||
* this software without specific prior written permission.
|
* this software without specific prior written permission.
|
||||||
*
|
*
|
||||||
@@ -38,17 +39,17 @@
|
|||||||
package org.dspace.content;
|
package org.dspace.content;
|
||||||
|
|
||||||
import java.text.DateFormatSymbols;
|
import java.text.DateFormatSymbols;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
// FIXME: No tests
|
|
||||||
// FIXME: Not very robust - assumes dates will always be valid
|
// FIXME: Not very robust - assumes dates will always be valid
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,126 +61,77 @@ import org.apache.log4j.Logger;
|
|||||||
* <code>YYYY-MM-DDThh:mm:ss</code>
|
* <code>YYYY-MM-DDThh:mm:ss</code>
|
||||||
* <P>
|
* <P>
|
||||||
* There are four levels of granularity, depending on how much date information
|
* There are four levels of granularity, depending on how much date information
|
||||||
* is available.
|
* is available: year, month, day, time.
|
||||||
* <P>
|
* <P>
|
||||||
* Examples: <code>1994-05-03T15:30:24</code>,<code>1995-10-04</code>,
|
* Examples: <code>1994-05-03T15:30:24</code>,<code>1995-10-04</code>,
|
||||||
* <code>2001-10</code>,<code>1975</code>
|
* <code>2001-10</code>,<code>1975</code>
|
||||||
*
|
*
|
||||||
|
* The main() method is a simple test program: run it with an optional
|
||||||
|
* first argument that is a date string to decode, and it prints the
|
||||||
|
* results of all the accessor methods.
|
||||||
|
*
|
||||||
* @author Robert Tansley
|
* @author Robert Tansley
|
||||||
|
* @author Larry Stone
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
public class DCDate
|
public class DCDate
|
||||||
{
|
{
|
||||||
/** Logger */
|
/** Logger */
|
||||||
private static Logger cat = Logger.getLogger(DCDate.class);
|
private static Logger log = Logger.getLogger(DCDate.class);
|
||||||
|
|
||||||
/** The year, or -1 if none */
|
// UTC timezone
|
||||||
private int year;
|
private static final TimeZone utcZone = TimeZone.getTimeZone("UTC");
|
||||||
|
|
||||||
/** The month, or -1 if none */
|
// Full ISO 8601 is e.g. "2009-07-16T13:59:21Z"
|
||||||
private int month;
|
private static final SimpleDateFormat fullIso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||||
|
static { fullIso.setTimeZone(utcZone); }
|
||||||
|
|
||||||
/** The day, or -1 if none */
|
// without Z
|
||||||
private int day;
|
private static final SimpleDateFormat fullIso2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||||
|
static { fullIso2.setTimeZone(utcZone); }
|
||||||
|
|
||||||
/** Hours, -1 if none */
|
// without seconds
|
||||||
private int hours;
|
private static final SimpleDateFormat fullIso3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
|
||||||
|
static { fullIso3.setTimeZone(utcZone); }
|
||||||
|
|
||||||
/** Minutes, -1 if none */
|
// Date-only ISO 8601 is e.g. "2009-07-16"
|
||||||
private int minutes;
|
private static final SimpleDateFormat dateIso = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
static { dateIso.setTimeZone(utcZone); }
|
||||||
|
|
||||||
/** seconds, -1 if none */
|
// Year-Month-only ISO 8601 is e.g. "2009-07"
|
||||||
private int seconds;
|
private static final SimpleDateFormat yearMonthIso = new SimpleDateFormat("yyyy-MM");
|
||||||
|
static { yearMonthIso.setTimeZone(utcZone); }
|
||||||
|
|
||||||
/**
|
// just year, "2009"
|
||||||
* Calendar object for timezone conversion. Only used if the date has a time
|
private static final SimpleDateFormat yearIso = new SimpleDateFormat("yyyy");
|
||||||
* component.
|
static { yearIso.setTimeZone(utcZone); }
|
||||||
*/
|
|
||||||
private GregorianCalendar localGC;
|
// components of time in UTC
|
||||||
|
private GregorianCalendar calendar = null;
|
||||||
|
|
||||||
|
// components of time in local zone, if needed
|
||||||
|
private GregorianCalendar localCalendar = null;
|
||||||
|
|
||||||
|
private enum DateGran { YEAR, MONTH, DAY, TIME };
|
||||||
|
DateGran granularity = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DateFormatSymbols for locale monthsname
|
* DateFormatSymbols for locale monthsname
|
||||||
*/
|
*/
|
||||||
private static DateFormatSymbols dfs = null;
|
private static DateFormatSymbols dfs = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* note the session locale
|
* note the session locale
|
||||||
*/
|
*/
|
||||||
private static Locale langMonth = null;
|
private static Locale langMonth = null;
|
||||||
|
|
||||||
|
|
||||||
private final Pattern fullDateTimePattern = Pattern.compile("(\\d*)-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})Z");
|
|
||||||
private final Pattern datePattern = Pattern.compile("(\\d*)-(\\d{2})-(\\d{2})");
|
|
||||||
private final Pattern yearMonthPattern = Pattern.compile("(\\d*)-(\\d{2})");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a clean date
|
* Construct a clean date
|
||||||
*/
|
*/
|
||||||
public DCDate()
|
public DCDate()
|
||||||
{
|
{
|
||||||
// Set all fields to unknown
|
super();
|
||||||
year = month = day = hours = minutes = seconds = -1;
|
|
||||||
localGC = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a date from a Dublin Core value
|
|
||||||
*
|
|
||||||
* @param fromDC
|
|
||||||
* the date string, in ISO 8601 (no timezone, always use UTC/GMT)
|
|
||||||
*/
|
|
||||||
public DCDate(String fromDC)
|
|
||||||
{
|
|
||||||
// Set all fields to unknown
|
|
||||||
year = month = day = hours = minutes = seconds = -1;
|
|
||||||
localGC = null;
|
|
||||||
|
|
||||||
// An empty date is OK
|
|
||||||
if ((fromDC == null) || fromDC.equals(""))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
Matcher fullDateTimeMatcher = fullDateTimePattern.matcher(fromDC);
|
|
||||||
Matcher dateMatcher = datePattern.matcher(fromDC);
|
|
||||||
Matcher yearMonthMatcher = yearMonthPattern.matcher(fromDC);
|
|
||||||
|
|
||||||
if (fullDateTimeMatcher.matches())
|
|
||||||
{
|
|
||||||
year = Integer.parseInt(fullDateTimeMatcher.group(1));
|
|
||||||
month = Integer.parseInt(fullDateTimeMatcher.group(2));
|
|
||||||
day = Integer.parseInt(fullDateTimeMatcher.group(3));
|
|
||||||
|
|
||||||
hours = Integer.parseInt(fullDateTimeMatcher.group(4));
|
|
||||||
minutes = Integer.parseInt(fullDateTimeMatcher.group(5));
|
|
||||||
seconds = Integer.parseInt(fullDateTimeMatcher.group(6));
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (dateMatcher.matches())
|
|
||||||
{
|
|
||||||
year = Integer.parseInt(dateMatcher.group(1));
|
|
||||||
month = Integer.parseInt(dateMatcher.group(2));
|
|
||||||
day = Integer.parseInt(dateMatcher.group(3));
|
|
||||||
}
|
|
||||||
else if (yearMonthMatcher.matches())
|
|
||||||
{
|
|
||||||
year = Integer.parseInt(yearMonthMatcher.group(1));
|
|
||||||
month = Integer.parseInt(yearMonthMatcher.group(2));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ // only the year
|
|
||||||
year = Integer.parseInt(fromDC);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
// Mangled date
|
|
||||||
cat.warn("Mangled date: " + fromDC + " Exception: " + e);
|
|
||||||
year = month = day = hours = minutes = seconds = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -190,19 +142,89 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public DCDate(Date date)
|
public DCDate(Date date)
|
||||||
{
|
{
|
||||||
Calendar calendar = Calendar.getInstance();
|
super();
|
||||||
|
setTime(date);
|
||||||
|
if (!(calendar.get(Calendar.HOUR_OF_DAY) == 0 &&
|
||||||
|
calendar.get(Calendar.MINUTE) == 0 &&
|
||||||
|
calendar.get(Calendar.SECOND) == 0))
|
||||||
|
granularity = DateGran.TIME;
|
||||||
|
|
||||||
|
// if date is 1-jan, assume it's because this was set for year
|
||||||
|
else if (calendar.get(Calendar.DAY_OF_MONTH) == 1 && calendar.get(Calendar.MONTH) == 0)
|
||||||
|
granularity = DateGran.YEAR;
|
||||||
|
|
||||||
|
// otherwise day
|
||||||
|
else
|
||||||
|
granularity = DateGran.DAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a date from a Dublin Core value
|
||||||
|
*
|
||||||
|
* @param fromDC
|
||||||
|
* the date string, in ISO 8601 (no timezone, always use UTC/GMT)
|
||||||
|
*/
|
||||||
|
public DCDate(String fromDC)
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
|
||||||
|
// An empty date is OK
|
||||||
|
if ((fromDC == null) || fromDC.equals(""))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// default granularity
|
||||||
|
granularity = DateGran.TIME;
|
||||||
|
Date date = tryParse(fullIso, fromDC);
|
||||||
|
if (date == null)
|
||||||
|
date = tryParse(fullIso2, fromDC);
|
||||||
|
if (date == null)
|
||||||
|
date = tryParse(fullIso3, fromDC);
|
||||||
|
if (date == null)
|
||||||
|
{
|
||||||
|
date = tryParse(dateIso, fromDC);
|
||||||
|
granularity = DateGran.DAY;
|
||||||
|
}
|
||||||
|
if (date == null)
|
||||||
|
{
|
||||||
|
date = tryParse(yearMonthIso, fromDC);
|
||||||
|
granularity = DateGran.MONTH;
|
||||||
|
}
|
||||||
|
if (date == null)
|
||||||
|
{
|
||||||
|
date = tryParse(yearIso, fromDC);
|
||||||
|
granularity = DateGran.YEAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (date == null)
|
||||||
|
log.warn("Mangled date: " + fromDC + " ..failed all attempts to parse as date.");
|
||||||
|
else
|
||||||
|
setTime(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to parse, swallowing errors; return null for failure.
|
||||||
|
private synchronized Date tryParse(SimpleDateFormat sdf, String source)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return sdf.parse(source);
|
||||||
|
}
|
||||||
|
catch (ParseException pe)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the time components to reflect the absolute time in this Date.
|
||||||
|
*
|
||||||
|
* @param date
|
||||||
|
* the Java <code>Date</code> object.
|
||||||
|
*/
|
||||||
|
private void setTime(Date date)
|
||||||
|
{
|
||||||
|
calendar = new GregorianCalendar(utcZone);
|
||||||
calendar.setTime(date);
|
calendar.setTime(date);
|
||||||
|
|
||||||
// Set all fields
|
|
||||||
setDateLocal(calendar.get(Calendar.YEAR),
|
|
||||||
|
|
||||||
// Uses 1 to 12 implementation below instead of Java's
|
|
||||||
// 0 to 11 convention
|
|
||||||
calendar.get(Calendar.MONTH) + 1, calendar
|
|
||||||
.get(Calendar.DAY_OF_MONTH), calendar
|
|
||||||
.get(Calendar.HOUR_OF_DAY), calendar
|
|
||||||
.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -222,31 +244,21 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
StringBuffer sb = new StringBuffer();
|
if (calendar == null)
|
||||||
|
return "null";
|
||||||
|
return toStringInternal();
|
||||||
|
}
|
||||||
|
|
||||||
if (year > 0)
|
private synchronized String toStringInternal()
|
||||||
{
|
{
|
||||||
sb.append(year);
|
if (granularity == DateGran.YEAR)
|
||||||
}
|
return yearIso.format(calendar.getTime());
|
||||||
|
else if (granularity == DateGran.MONTH)
|
||||||
if (month > 0)
|
return yearMonthIso.format(calendar.getTime());
|
||||||
{
|
else if (granularity == DateGran.DAY)
|
||||||
sb.append('-').append(fleshOut(month));
|
return dateIso.format(calendar.getTime());
|
||||||
}
|
else
|
||||||
|
return fullIso.format(calendar.getTime());
|
||||||
if (day > 0)
|
|
||||||
{
|
|
||||||
sb.append('-').append(fleshOut(day));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hours >= 0)
|
|
||||||
{
|
|
||||||
sb.append("T").append(fleshOut(hours)).append(':').append(
|
|
||||||
fleshOut(minutes)).append(':').append(fleshOut(seconds))
|
|
||||||
.append("Z");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (sb.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -256,59 +268,10 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public Date toDate()
|
public Date toDate()
|
||||||
{
|
{
|
||||||
int tmpmonth;
|
if (calendar == null)
|
||||||
int tmpday;
|
return null;
|
||||||
int tmphours;
|
else
|
||||||
int tmpmin;
|
return calendar.getTime();
|
||||||
int tmpsec;
|
|
||||||
|
|
||||||
if (month < 0) {
|
|
||||||
// Month is unknown and set to -1
|
|
||||||
// but GregorianCalendar will interpret this as a rollback
|
|
||||||
// to December of the previous year
|
|
||||||
tmpmonth = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Month is known, but GC calendar is 0 - 11, not 1 - 12
|
|
||||||
// so we'll do subtraction here
|
|
||||||
tmpmonth = month - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (day < 0) {
|
|
||||||
tmpday = 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tmpday = day;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hours < 0) {
|
|
||||||
tmphours = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tmphours = hours;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (minutes < 0) {
|
|
||||||
tmpmin = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tmpmin = minutes;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seconds < 0) {
|
|
||||||
tmpsec = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tmpsec = seconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
GregorianCalendar utcGC = new GregorianCalendar(TimeZone
|
|
||||||
.getTimeZone("UTC"));
|
|
||||||
|
|
||||||
utcGC.set(year, tmpmonth, tmpday, tmphours, tmpmin, tmpsec);
|
|
||||||
|
|
||||||
return utcGC.getTime();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -331,89 +294,62 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public void setDateLocal(int yyyy, int mm, int dd, int hh, int mn, int ss)
|
public void setDateLocal(int yyyy, int mm, int dd, int hh, int mn, int ss)
|
||||||
{
|
{
|
||||||
year = month = day = hours = minutes = seconds = -1;
|
// default values
|
||||||
|
int lyear = 0;
|
||||||
|
int lhours = 0;
|
||||||
|
int lminutes = 0;
|
||||||
|
int lseconds = 0;
|
||||||
|
int lmonth = 1;
|
||||||
|
int lday = 1;
|
||||||
|
|
||||||
if (yyyy > 0)
|
if (yyyy > 0)
|
||||||
{
|
{
|
||||||
year = yyyy;
|
lyear = yyyy;
|
||||||
|
granularity = DateGran.YEAR;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mm > 0)
|
if (mm > 0)
|
||||||
{
|
{
|
||||||
month = mm;
|
lmonth = mm;
|
||||||
|
granularity = DateGran.MONTH;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dd > 0)
|
if (dd > 0)
|
||||||
{
|
{
|
||||||
day = dd;
|
lday = dd;
|
||||||
|
granularity = DateGran.DAY;
|
||||||
}
|
}
|
||||||
else
|
if (hh >= 0)
|
||||||
{
|
{
|
||||||
return;
|
lhours = hh;
|
||||||
|
granularity = DateGran.TIME;
|
||||||
}
|
}
|
||||||
|
if (mn >= 0)
|
||||||
if (hh == -1)
|
|
||||||
{
|
{
|
||||||
return;
|
lminutes = mn;
|
||||||
|
granularity = DateGran.TIME;
|
||||||
|
}
|
||||||
|
if (ss >= 0)
|
||||||
|
{
|
||||||
|
lseconds = ss;
|
||||||
|
granularity = DateGran.TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We have a time, so we need to do a timezone adjustment
|
// do the timezone adjustment: get Date and put it in UTC zone.
|
||||||
localGC = new GregorianCalendar(year, month - 1, day, hh, mn, ss);
|
GregorianCalendar localGC = new GregorianCalendar(lyear, lmonth - 1, lday,
|
||||||
|
lhours, lminutes, lseconds);
|
||||||
// Adjust to UTC
|
setTime(localGC.getTime());
|
||||||
GregorianCalendar utcGC = new GregorianCalendar(TimeZone
|
|
||||||
.getTimeZone("UTC"));
|
|
||||||
|
|
||||||
utcGC.setTime(localGC.getTime());
|
|
||||||
|
|
||||||
year = utcGC.get(Calendar.YEAR);
|
|
||||||
|
|
||||||
// Notation
|
|
||||||
month = utcGC.get(Calendar.MONTH) + 1;
|
|
||||||
day = utcGC.get(Calendar.DAY_OF_MONTH);
|
|
||||||
hours = utcGC.get(Calendar.HOUR_OF_DAY);
|
|
||||||
minutes = utcGC.get(Calendar.MINUTE);
|
|
||||||
seconds = utcGC.get(Calendar.SECOND);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// get cached calendar in local timezone
|
||||||
* Get the date as an array of ints, adjusted for the current timezone
|
private GregorianCalendar getLocalCalendar()
|
||||||
*
|
|
||||||
* @return the date an an array: ( year, month, day, hour, minute, seconds) -
|
|
||||||
* unset fields are given a value of -1.
|
|
||||||
*/
|
|
||||||
private int[] getDateLocal()
|
|
||||||
{
|
{
|
||||||
// Handle simple date cases first - no timezone adjustment
|
if (localCalendar == null)
|
||||||
if (hours == -1)
|
|
||||||
{
|
{
|
||||||
return new int[] { year, month, day, -1, -1, -1 };
|
if (calendar == null)
|
||||||
|
return null;
|
||||||
|
localCalendar = new GregorianCalendar();
|
||||||
|
localCalendar.setTime(calendar.getTime());
|
||||||
}
|
}
|
||||||
|
return localCalendar;
|
||||||
// We have a full time, adjust to current timezone
|
|
||||||
if (localGC == null)
|
|
||||||
{
|
|
||||||
GregorianCalendar utcGC = new GregorianCalendar(TimeZone
|
|
||||||
.getTimeZone("UTC"));
|
|
||||||
|
|
||||||
utcGC.set(year, month - 1, day, hours, minutes, seconds);
|
|
||||||
localGC = new GregorianCalendar();
|
|
||||||
localGC.setTime(utcGC.getTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
return new int[] { localGC.get(Calendar.YEAR),
|
|
||||||
localGC.get(Calendar.MONTH) + 1,
|
|
||||||
localGC.get(Calendar.DAY_OF_MONTH),
|
|
||||||
localGC.get(Calendar.HOUR_OF_DAY),
|
|
||||||
localGC.get(Calendar.MINUTE), localGC.get(Calendar.SECOND) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -423,7 +359,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getYear()
|
public int getYear()
|
||||||
{
|
{
|
||||||
return (getDateLocal())[0];
|
return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.YEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -433,7 +369,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getMonth()
|
public int getMonth()
|
||||||
{
|
{
|
||||||
return (getDateLocal())[1];
|
return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.MONTH) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -443,7 +379,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getDay()
|
public int getDay()
|
||||||
{
|
{
|
||||||
return (getDateLocal())[2];
|
return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.DAY_OF_MONTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -453,7 +389,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getHour()
|
public int getHour()
|
||||||
{
|
{
|
||||||
return (getDateLocal())[3];
|
return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.HOUR_OF_DAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -463,7 +399,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getMinute()
|
public int getMinute()
|
||||||
{
|
{
|
||||||
return (getDateLocal())[4];
|
return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.MINUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -473,18 +409,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getSecond()
|
public int getSecond()
|
||||||
{
|
{
|
||||||
return (getDateLocal())[5];
|
return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.SECOND);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the date as an array of ints in GMT
|
|
||||||
*
|
|
||||||
* @return the date an an array: ( year, month, day, hour, minute, seconds) -
|
|
||||||
* unset fields are given a value of -1.
|
|
||||||
*/
|
|
||||||
private int[] getDateGMT()
|
|
||||||
{
|
|
||||||
return new int[] { year, month, day, hours, minutes, seconds };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -494,7 +419,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getYearGMT()
|
public int getYearGMT()
|
||||||
{
|
{
|
||||||
return (getDateGMT())[0];
|
return calendar == null ? -1 : calendar.get(Calendar.YEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -504,7 +429,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getMonthGMT()
|
public int getMonthGMT()
|
||||||
{
|
{
|
||||||
return (getDateGMT())[1];
|
return calendar == null ? -1 : calendar.get(Calendar.MONTH) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -514,7 +439,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getDayGMT()
|
public int getDayGMT()
|
||||||
{
|
{
|
||||||
return (getDateGMT())[2];
|
return calendar == null ? -1 : calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -524,7 +449,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getHourGMT()
|
public int getHourGMT()
|
||||||
{
|
{
|
||||||
return (getDateGMT())[3];
|
return calendar == null ? -1 : calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -534,7 +459,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getMinuteGMT()
|
public int getMinuteGMT()
|
||||||
{
|
{
|
||||||
return (getDateGMT())[4];
|
return calendar == null ? -1 : calendar.get(Calendar.MINUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -544,26 +469,7 @@ public class DCDate
|
|||||||
*/
|
*/
|
||||||
public int getSecondGMT()
|
public int getSecondGMT()
|
||||||
{
|
{
|
||||||
return (getDateGMT())[5];
|
return calendar == null ? -1 : calendar.get(Calendar.SECOND);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flesh out a number to two digits
|
|
||||||
*
|
|
||||||
* @param n
|
|
||||||
* the number
|
|
||||||
* @return the number as a two-digit string
|
|
||||||
*/
|
|
||||||
private String fleshOut(int n)
|
|
||||||
{
|
|
||||||
if (n < 10)
|
|
||||||
{
|
|
||||||
return "0" + n;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return String.valueOf(n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -580,10 +486,10 @@ public class DCDate
|
|||||||
if ((m > 0) && (m < 13))
|
if ((m > 0) && (m < 13))
|
||||||
{
|
{
|
||||||
if (dfs == null || !langMonth.equals(locale))
|
if (dfs == null || !langMonth.equals(locale))
|
||||||
{
|
{
|
||||||
dfs = new DateFormatSymbols(locale);
|
dfs = new DateFormatSymbols(locale);
|
||||||
langMonth = locale;
|
langMonth = locale;
|
||||||
}
|
}
|
||||||
return dfs.getMonths()[m-1];
|
return dfs.getMonths()[m-1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -592,4 +498,62 @@ public class DCDate
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple test program
|
||||||
|
* Usage: java org.dspace.content.DCdate [DCDate | -l yyyy [mm [dd ..]]] ]
|
||||||
|
* (-l form tests local time parsing)
|
||||||
|
* Default is to use current time.
|
||||||
|
*/
|
||||||
|
public static void main(String args[])
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
DCDate d;
|
||||||
|
|
||||||
|
// if there's an arg, parse it for the date, otherwise use now
|
||||||
|
if (args.length > 0)
|
||||||
|
{
|
||||||
|
if (args[0].equals("-l"))
|
||||||
|
{
|
||||||
|
int val[] = { -1, -1, -1, -1, -1, -1 };
|
||||||
|
for (int i = 1; i < 7 && i < args.length; ++i)
|
||||||
|
val[i-1] = Integer.parseInt(args[i]);
|
||||||
|
d = new DCDate();
|
||||||
|
d.setDateLocal(val[0], val[1], val[2], val[3], val[4], val[5]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
d = new DCDate(args[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
d = DCDate.getCurrent();
|
||||||
|
|
||||||
|
// display results:
|
||||||
|
System.out.println("toString() = \""+d.toString()+"\"");
|
||||||
|
System.out.println("toDate().toString() = \""+d.toDate().toString()+"\"");
|
||||||
|
|
||||||
|
System.out.println("By component:");
|
||||||
|
System.out.println("granularity = "+d.granularity);
|
||||||
|
System.out.println("getYear(), = "+d.getYear());
|
||||||
|
System.out.println("getMonth(), = "+d.getMonth());
|
||||||
|
System.out.println("getDay(), = "+d.getDay());
|
||||||
|
System.out.println("getHour(), = "+d.getHour());
|
||||||
|
System.out.println("getMinute(), = "+d.getMinute());
|
||||||
|
System.out.println("getSecond()); = "+d.getSecond());
|
||||||
|
System.out.println("By GMT component:");
|
||||||
|
System.out.println("getYearGMT(), = "+d.getYearGMT());
|
||||||
|
System.out.println("getMonthGMT(), = "+d.getMonthGMT());
|
||||||
|
System.out.println("getDayGMT(), = "+d.getDayGMT());
|
||||||
|
System.out.println("getHourGMT(), = "+d.getHourGMT());
|
||||||
|
System.out.println("getMinuteGMT(), = "+d.getMinuteGMT());
|
||||||
|
System.out.println("getSecondGMT()); = "+d.getSecondGMT());
|
||||||
|
|
||||||
|
// convert it the hard way:
|
||||||
|
DCDate hw = new DCDate();
|
||||||
|
hw.setDateLocal(d.getYear(),d.getMonth(),d.getDay(),
|
||||||
|
d.getHour(),d.getMinute(),d.getSecond());
|
||||||
|
System.out.println("hardway.toString() = \""+hw.toString()+"\"");
|
||||||
|
|
||||||
|
// month str
|
||||||
|
System.out.println("Month Name = \""+DCDate.getMonthName(d.getMonth(), Locale.getDefault())+"\"");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -43,6 +43,7 @@ import java.sql.SQLException;
|
|||||||
import org.dspace.authorize.AuthorizeException;
|
import org.dspace.authorize.AuthorizeException;
|
||||||
import org.dspace.core.ConfigurationManager;
|
import org.dspace.core.ConfigurationManager;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.embargo.EmbargoManager;
|
||||||
import org.dspace.handle.HandleManager;
|
import org.dspace.handle.HandleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,17 +89,27 @@ public class InstallItem
|
|||||||
Item item = is.getItem();
|
Item item = is.getItem();
|
||||||
String handle;
|
String handle;
|
||||||
|
|
||||||
|
// this is really just to flush out fatal embargo metadata
|
||||||
|
// problems before we set inArchive.
|
||||||
|
DCDate liftDate = EmbargoManager.getEmbargoDate(c, item);
|
||||||
|
|
||||||
// create accession date
|
// create accession date
|
||||||
DCDate now = DCDate.getCurrent();
|
DCDate now = DCDate.getCurrent();
|
||||||
item.addDC("date", "accessioned", null, now.toString());
|
item.addDC("date", "accessioned", null, now.toString());
|
||||||
item.addDC("date", "available", null, now.toString());
|
|
||||||
|
// add date available if not under embargo, otherwise it will
|
||||||
|
// be set when the embargo is lifted.
|
||||||
|
if (liftDate == null)
|
||||||
|
item.addDC("date", "available", null, now.toString());
|
||||||
|
|
||||||
// create issue date if not present
|
// create issue date if not present
|
||||||
DCValue[] currentDateIssued = item.getDC("date", "issued", Item.ANY);
|
DCValue[] currentDateIssued = item.getDC("date", "issued", Item.ANY);
|
||||||
|
|
||||||
if (currentDateIssued.length == 0)
|
if (currentDateIssued.length == 0)
|
||||||
{
|
{
|
||||||
item.addDC("date", "issued", null, now.toString());
|
DCDate issued = new DCDate();
|
||||||
|
issued.setDateLocal(now.getYear(),now.getMonth(),now.getDay(),-1,-1,-1);
|
||||||
|
item.addDC("date", "issued", null, issued.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no previous handle supplied, create one
|
// if no previous handle supplied, create one
|
||||||
@@ -155,6 +166,10 @@ public class InstallItem
|
|||||||
// the defaults from the collection
|
// the defaults from the collection
|
||||||
item.inheritCollectionDefaultPolicies(is.getCollection());
|
item.inheritCollectionDefaultPolicies(is.getCollection());
|
||||||
|
|
||||||
|
// set embargo lift date and take away read access if indicated.
|
||||||
|
if (liftDate != null)
|
||||||
|
EmbargoManager.setEmbargo(c, item, liftDate);
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2269,4 +2269,45 @@ public class Item extends DSpaceObject
|
|||||||
DCValue t[] = getMetadata("dc", "title", null, Item.ANY);
|
DCValue t[] = getMetadata("dc", "title", null, Item.ANY);
|
||||||
return (t.length >= 1) ? t[0].value : null;
|
return (t.length >= 1) ? t[0].value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an iterator of Items possessing the passed metadata field, or only
|
||||||
|
* those matching the passed value, if value is not Item.ANY
|
||||||
|
*
|
||||||
|
* @param context DSpace context object
|
||||||
|
* @param schema metdata field schema
|
||||||
|
* @param element metdata field element
|
||||||
|
* @param qualifier metdata field qualifier
|
||||||
|
* @param value field value or Item.ANY to match any value
|
||||||
|
* @return an iterator over the items matching that authority value
|
||||||
|
* @throws SQLException, AuthorizeException, IOException
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static ItemIterator findByMetadataField(Context context,
|
||||||
|
String schema, String element, String qualifier, String value)
|
||||||
|
throws SQLException, AuthorizeException, IOException
|
||||||
|
{
|
||||||
|
MetadataSchema mds = MetadataSchema.find(context, schema);
|
||||||
|
if (mds == null)
|
||||||
|
throw new IllegalArgumentException("No such metadata schema: "+schema);
|
||||||
|
MetadataField mdf = MetadataField.findByElement(context, mds.getSchemaID(), element, qualifier);
|
||||||
|
if (mdf == null)
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"No such metadata field: schema="+schema+", element="+element+", qualifier="+qualifier);
|
||||||
|
|
||||||
|
String query = "SELECT item.* FROM metadatavalue,item WHERE item.in_archive='1' "+
|
||||||
|
"AND item.item_id = metadatavalue.item_id AND metadata_field_id = ?";
|
||||||
|
TableRowIterator rows = null;
|
||||||
|
if (Item.ANY.equals(value))
|
||||||
|
{
|
||||||
|
rows = DatabaseManager.queryTable(context, "item", query, mdf.getFieldID());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
query += " AND metadatavalue.text_value = ?";
|
||||||
|
rows = DatabaseManager.queryTable(context, "item", query, mdf.getFieldID(), value);
|
||||||
|
}
|
||||||
|
return new ItemIterator(context, rows);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user