Search in sources :

Example 1 with SessionDescendant

use of org.openntf.domino.types.SessionDescendant in project org.openntf.domino by OpenNTF.

the class DElement method getProperty.

@SuppressWarnings("unchecked")
@Override
public <T> T getProperty(final String propertyName, final Class<T> type) {
    // TODO NTF cached properties should be automatically reset if the base Document is known to have changed
    // if ("form".equalsIgnoreCase(propertyName)) {
    // System.out.println("Getting form value now...");
    // }
    Object result = null;
    Map<String, Object> props = getProps();
    result = props.get(propertyName);
    Map<String, Object> delegate = getDelegate();
    if (result == null || Deferred.INSTANCE.equals(result)) {
        try {
            if (delegate instanceof Document) {
                Document doc = (Document) delegate;
                doc.closeMIMEEntities(false);
                doc.setAutoMime(AutoMime.WRAP_ALL);
                if (doc.hasItem(propertyName)) {
                    Item item = doc.getFirstItem(propertyName);
                    if (item instanceof RichTextItem && Object.class.equals(type)) {
                        result = item;
                    } else {
                        try {
                            result = doc.getItemValue(propertyName, type);
                        } catch (Throwable t) {
                        // System.out.println("TEMP DEBUG didn't get property " + propertyName + " with type " + type.getSimpleName()
                        // + " because of a " + t.getClass().getSimpleName() + ": " + t.getMessage());
                        }
                    }
                }
                if (result == null || Deferred.INSTANCE.equals(result)) {
                    try {
                        Object raw = doc.get(propertyName);
                        if (raw instanceof Vector) {
                            if (((Vector<?>) raw).isEmpty()) {
                                props.put(propertyName, Null.INSTANCE);
                                return null;
                            }
                        }
                        result = TypeUtils.objectToClass(raw, type, doc.getAncestorSession());
                    } catch (Throwable t) {
                        if (log_.isLoggable(Level.FINE)) {
                            log_.log(Level.FINE, "Invalid property for document " + propertyName, t);
                        }
                    }
                }
            } else if (delegate instanceof SessionDescendant) {
                Session s = ((SessionDescendant) delegate).getAncestorSession();
                result = TypeUtils.convertToTarget(delegate.get(propertyName), type, s);
            } else if (delegate != null) {
                try {
                    result = TypeUtils.convertToTarget(delegate.get(propertyName), type, null);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
            if (result == null) {
                props.put(propertyName, Null.INSTANCE);
            } else if (result instanceof Serializable) {
                props.put(propertyName, result);
            } else {
                if (log_.isLoggable(Level.FINE)) {
                    log_.log(Level.FINE, "Got a value from the document but it's not Serializable. It's a " + result.getClass().getName());
                }
                props.put(propertyName, result);
            }
        } catch (UserAccessException uae) {
            throw uae;
        } catch (Exception e) {
            log_.log(Level.WARNING, "Exception occured attempting to get value from document for " + propertyName + " so we cannot return a value", e);
        }
    } else if (result == Null.INSTANCE) {
    } else {
        if (result != null && !type.isAssignableFrom(result.getClass())) {
            try {
                // Map<String, Object> delegate = getDelegate();
                if (delegate instanceof Document) {
                    Document doc = (Document) delegate;
                    Item item = doc.getFirstItem(propertyName);
                    if (item instanceof RichTextItem && Object.class.equals(type)) {
                        result = ((RichTextItem) item).getUnformattedText();
                    } else {
                        result = doc.getItemValue(propertyName, type);
                    }
                    ((Document) delegate).closeMIMEEntities(false);
                } else if (delegate instanceof SessionDescendant) {
                    Session s = ((SessionDescendant) delegate).getAncestorSession();
                    result = TypeUtils.convertToTarget(delegate.get(propertyName), type, s);
                } else if (delegate != null) {
                    Object chk = delegate.get(propertyName);
                    if (chk != null) {
                        result = TypeUtils.convertToTarget(delegate.get(propertyName), type, null);
                    }
                }
                if (result == null) {
                    props.put(propertyName, Null.INSTANCE);
                } else if (result instanceof Serializable) {
                    props.put(propertyName, result);
                } else {
                    log_.log(Level.FINE, "Got a value from the document but it's not Serializable. It's a " + result.getClass().getName());
                    props.put(propertyName, result);
                }
            } catch (UserAccessException uae) {
                throw uae;
            } catch (Exception e) {
                if (result != null) {
                    log_.log(Level.WARNING, "Exception occured attempting to get value from document for " + propertyName + " but we have a value in the cache of type " + result.getClass().getName() + " when we were looking for a " + type.getName(), e);
                }
            }
        }
    }
    if (result == Null.INSTANCE) {
        result = null;
    }
    if (delegate instanceof Document && delegate != null) {
        ((Document) delegate).closeMIMEEntities();
    }
    // }
    if (result == Deferred.INSTANCE) {
    // System.out.println("Returning Deferred INSTANCE for property " + propertyName);
    }
    return (T) result;
}
Also used : Serializable(java.io.Serializable) Document(org.openntf.domino.Document) UserAccessException(org.openntf.domino.exceptions.UserAccessException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) SessionDescendant(org.openntf.domino.types.SessionDescendant) RichTextItem(org.openntf.domino.RichTextItem) Item(org.openntf.domino.Item) RichTextItem(org.openntf.domino.RichTextItem) Vector(java.util.Vector) Session(org.openntf.domino.Session)

Aggregations

Serializable (java.io.Serializable)1 Vector (java.util.Vector)1 Document (org.openntf.domino.Document)1 Item (org.openntf.domino.Item)1 RichTextItem (org.openntf.domino.RichTextItem)1 Session (org.openntf.domino.Session)1 UserAccessException (org.openntf.domino.exceptions.UserAccessException)1 SessionDescendant (org.openntf.domino.types.SessionDescendant)1