OpenNTF / org.openntf.domino

Open replacement for lotus.domino package in HCL Domino
Apache License 2.0
66 stars 34 forks source link

DataNotCompatibleException thrown when the original value correspondes to the casting class #141

Closed shillem closed 8 years ago

shillem commented 8 years ago

I get a DataNotCompatibleException when I'm retrieving a column value that is already an instance of java.util.Date (because of the setPreferJavaDates). I don't believe it's correct behavior.

Example:

#!java
DominoViewEntry e = nav.getFirstDocument();
entry.setPreferJavaDates(true);

entry.getColumnValue("myDate", java.util.Date);

I think this method should foresee a possible "instance of Date" value and if so, return it without modifications.

#!java
    public static Date toDate(Object value) throws DataNotCompatibleException {
        if (value == null)
            return null;
        if (value instanceof Vector && (((Vector<?>) value).isEmpty()))
            return null;
        if (value instanceof Vector) {
            value = ((Vector<?>) value).get(0);
        }
        if (value instanceof Long) {
            return new Date(((Long) value).longValue());
        } else if (value instanceof String) {
            // TODO finish
            DateFormat df = new SimpleDateFormat();
            String str = (String) value;
            if (str.length() < 1)
                return null;
            try {
                return df.parse(str);
            } catch (ParseException e) {
                throw new DataNotCompatibleException("Cannot create a Date from String value " + (String) value);
            }
        } else if (value instanceof lotus.domino.DateTime) {
            return DominoUtils.toJavaDateSafe((lotus.domino.DateTime) value);
        } else {
            throw new DataNotCompatibleException("Cannot create a Date from a " + value.getClass().getName());
        }
    }
jesse-gallagher commented 8 years ago

Agreed - fixed.

shillem commented 8 years ago

What the... thanks, that was fast, really fast!