Search in sources :

Example 1 with RichTextItem

use of org.openntf.domino.RichTextItem in project org.openntf.domino by OpenNTF.

the class Document method toJson.

@Override
public String toJson(final boolean compact) {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw, compact);
    try {
        jw.startObject();
        jw.outStringProperty("@unid", getUniversalID());
        jw.outStringProperty("@noteid", getNoteID());
        jw.outStringProperty("@replicaid", getParentDatabase().getReplicaID());
        jw.outStringProperty("@metaversalid", getMetaversalID());
        try {
            jw.outStringProperty("@created", getCreated().toGMTISO());
            jw.outStringProperty("@lastmodified", getLastModified().toGMTISO());
            jw.outStringProperty("@lastaccessed", getLastAccessed().toGMTISO());
        } catch (Exception e) {
            DominoUtils.handleException(e, "Exception trying to index Dates.");
        }
        Set<String> keys = keySet();
        for (String key : keys) {
            Item currItem = getFirstItem(key);
            // A beer to anyone who can work out how this could happen, except for the person who identified it!
            if (null != currItem) {
                Type itemType = currItem.getTypeEx();
                try {
                    if (itemType == Type.ATTACHMENT) {
                        jw.outProperty(key, "ATTACHMENT");
                    } else if (itemType == Type.AUTHORS || itemType == Type.READERS || itemType == Type.NAMES || itemType == Type.TEXT || itemType == Type.NUMBERS) {
                        Vector<Object> values = currItem.getValues();
                        if (values.size() == 1) {
                            jw.outProperty(key, values.elementAt(0));
                        } else {
                            jw.outProperty(key, values);
                        }
                    } else if (itemType == Type.DATETIMES) {
                        Vector<DateTime> values = currItem.getValueDateTimeArray();
                        if (values.size() == 1) {
                            jw.outProperty(key, values.get(0).toGMTISO());
                        } else {
                            jw.outProperty(key, TypeUtils.toStrings(values));
                        }
                    } else if (itemType == Type.EMBEDDEDOBJECT) {
                        jw.outProperty(key, "EMBEDDED_OBJECT");
                    } else if (itemType == Type.RICHTEXT) {
                        RichTextItem rtItem = (RichTextItem) currItem;
                        jw.outProperty(key, rtItem.getUnformattedText());
                    } else if (itemType == Type.MIME_PART) {
                        MIMEEntity mimeEntity = currItem.getMIMEEntity();
                        if (mimeEntity != null) {
                            jw.outProperty(key, mimeEntity.getContentAsText());
                        } else {
                            jw.outProperty(key, "MIME_PART null");
                        }
                    }
                } catch (Exception e) {
                    DominoUtils.handleException(e, this);
                    // NTF - temporary
                    e.printStackTrace();
                }
            }
            // if (currItem.getMIMEEntity() == null) {
            // jw.outProperty(key, currItem.getText());
            // } else {
            // String abstractedText = currItem.abstractText(0, false, false);
            // if (null == abstractedText) {
            // jw.outProperty(key, "**MIME ITEM, VALUE CANNOT BE DECODED TO JSON**");
            // } else {
            // jw.outProperty(key, abstractedText);
            // }
            // }
            // Now output attachments
            jw.outProperty("@attachments", getAttachmentNames());
        }
        jw.endObject();
        jw.flush();
    } catch (IOException e) {
        DominoUtils.handleException(e, this);
        return null;
    } catch (JsonException e) {
        DominoUtils.handleException(e, this);
        return null;
    }
    return sw.toString();
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) IOException(java.io.IOException) JsonWriter(com.ibm.commons.util.io.json.util.JsonWriter) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) JsonException(com.ibm.commons.util.io.json.JsonException) DocumentWriteAccessException(org.openntf.domino.exceptions.DocumentWriteAccessException) DataNotCompatibleException(org.openntf.domino.exceptions.DataNotCompatibleException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) NotesException(lotus.domino.NotesException) DominoNonSummaryLimitException(org.openntf.domino.exceptions.DominoNonSummaryLimitException) IOException(java.io.IOException) ItemNotFoundException(org.openntf.domino.exceptions.ItemNotFoundException) DateTime(org.openntf.domino.DateTime) RichTextItem(org.openntf.domino.RichTextItem) Item(org.openntf.domino.Item) Type(org.openntf.domino.Item.Type) MIMEEntity(org.openntf.domino.MIMEEntity) StringWriter(java.io.StringWriter) RichTextItem(org.openntf.domino.RichTextItem) Vector(java.util.Vector) ItemVector(org.openntf.domino.iterators.ItemVector)

Example 2 with RichTextItem

use of org.openntf.domino.RichTextItem in project org.openntf.domino by OpenNTF.

the class BaseOpenLogItem method writeToLog.

/* (non-Javadoc)
	 * @see org.openntf.domino.logging.OpenLogItem#writeToLog()
	 */
@Override
public boolean writeToLog() {
    Database currDb = Factory.getSession(SessionType.CURRENT).getCurrentDatabase();
    // Current database may be null from Xots
    if (null == currDb) {
        if (!StringUtil.equals(getCurrentDatabasePath(), "")) {
            reinitialiseSettings();
        }
    } else {
        if (!StringUtil.equals(getCurrentDatabasePath(), Factory.getSession(SessionType.CURRENT).getCurrentDatabase().getFilePath())) {
            reinitialiseSettings();
        }
    }
    // exit early if there is no database
    Database db = getLogDb();
    if (db == null) {
        return false;
    }
    boolean retval = false;
    Document logDoc = null;
    RichTextItem rtitem = null;
    Database docDb = null;
    try {
        logDoc = db.createDocument();
        logDoc.appendItemValue("Form", _logFormName);
        Throwable ee = getBase();
        if (ee != null) {
            StackTraceElement ste = ee.getStackTrace()[0];
            if (ee instanceof NotesException) {
                logDoc.replaceItemValue("LogErrorNumber", ((NotesException) ee).id);
                logDoc.replaceItemValue("LogErrorMessage", ((NotesException) ee).text);
            } else {
                // Fixed next line
                logDoc.replaceItemValue("LogErrorMessage", getMessage());
            }
            logDoc.replaceItemValue("LogErrorLine", ste.getLineNumber());
            logDoc.replaceItemValue("LogFromMethod", ste.getClassName() + "." + ste.getMethodName());
        }
        if (LogType.TYPE_EVENT.getValue().equals(getEventType())) {
            if (!getSuppressEventStack()) {
                logDoc.replaceItemValue("LogStackTrace", getStackTrace(ee));
            }
        } else {
            logDoc.replaceItemValue("LogStackTrace", getStackTrace(ee));
        }
        logDoc.replaceItemValue("LogSeverity", getSeverity().getName());
        logDoc.replaceItemValue("LogEventTime", getEventTime());
        logDoc.replaceItemValue("LogEventType", getEventType());
        logDoc.replaceItemValue("LogMessage", getMessage());
        logDoc.replaceItemValue("LogFromDatabase", getCurrentDatabasePath());
        logDoc.replaceItemValue("LogFromServer", getThisServer());
        logDoc.replaceItemValue("LogFromAgent", getThisAgent());
        // Fixed next line
        logDoc.replaceItemValue("LogAgentLanguage", "Java");
        logDoc.replaceItemValue("LogUserName", Factory.getSession(SessionType.CURRENT).getUserName());
        logDoc.replaceItemValue("LogEffectiveName", Factory.getSession(SessionType.CURRENT).getEffectiveUserName());
        logDoc.replaceItemValue("LogAccessLevel", getAccessLevel());
        logDoc.replaceItemValue("LogUserRoles", getUserRoles());
        logDoc.replaceItemValue("LogClientVersion", getClientVersion());
        logDoc.replaceItemValue("LogAgentStartTime", getStartTime());
        if (getErrDoc() != null) {
            docDb = getErrDoc().getParentDatabase();
            rtitem = logDoc.createRichTextItem("LogDocInfo");
            rtitem.appendText("The document associated with this event is:");
            rtitem.addNewLine(1);
            rtitem.appendText("Server: " + docDb.getServer());
            rtitem.addNewLine(1);
            rtitem.appendText("Database: " + docDb.getFilePath());
            rtitem.addNewLine(1);
            rtitem.appendText("UNID: " + getErrDoc().getUniversalID());
            rtitem.addNewLine(1);
            rtitem.appendText("Note ID: " + getErrDoc().getNoteID());
            rtitem.addNewLine(1);
            rtitem.appendText("DocLink: ");
            rtitem.appendDocLink(_errDoc, getErrDoc().getUniversalID());
        }
        // make sure Depositor-level users can add documents too
        logDoc.appendItemValue("$PublicAccess", "1");
        logDoc.save(true);
        retval = true;
    } catch (Exception e) {
        debugPrint(e);
        retval = false;
    } finally {
        _errDoc = null;
        _errDocUnid = null;
    }
    return retval;
}
Also used : NotesException(lotus.domino.NotesException) RichTextItem(org.openntf.domino.RichTextItem) Database(org.openntf.domino.Database) Document(org.openntf.domino.Document) NotesException(lotus.domino.NotesException)

Example 3 with RichTextItem

use of org.openntf.domino.RichTextItem in project org.openntf.domino by OpenNTF.

the class RichTextItemReplaceAttachmentText method run.

@Override
public void run() {
    Session session = Factory.getSession(SessionType.NATIVE);
    Database db = session.getDatabase("", "sarmy/USSBulletins.nsf");
    Document doc = db.getDocumentByUNID("5F96214CFE92013D86258027007687FA");
    RichTextItem rtItem = (RichTextItem) doc.getFirstItem("BulletinText");
    rtItem.replaceAttachment("Feature Requests 2008.ods", "c:/data/Feature Requests 2008.ods");
    rtItem.compact();
    doc.save(true, true);
}
Also used : RichTextItem(org.openntf.domino.RichTextItem) Database(org.openntf.domino.Database) Document(org.openntf.domino.Document) Session(org.openntf.domino.Session)

Example 4 with RichTextItem

use of org.openntf.domino.RichTextItem in project org.openntf.domino by OpenNTF.

the class JsonGraphWriter method outLiteral.

@Override
protected void outLiteral(Object paramObject, final boolean paramBoolean) throws IOException, JsonException {
    // paramObject.getClass().getName()));
    if (paramObject != null) {
        Class<?> objClass = paramObject.getClass();
        IJsonWriterAdapter adapter = factory_.getJsonWriterAdapter(objClass);
        if (adapter != null) {
            paramObject = adapter.toJson(paramObject);
        }
    }
    if (this.getFactory().isNull(paramObject)) {
        outNull();
    // } else if (paramObject instanceof Term) {
    // JsonSearchAdapter adapter = new JsonSearchAdapter(graph_, (Term) paramObject, parameters_,
    // isCollectionRoute_);
    // super.outObject(adapter);
    // } else if (paramObject instanceof Value) {
    // JsonSearchAdapter adapter = new JsonSearchAdapter(graph_, (Value) paramObject, parameters_,
    // isCollectionRoute_);
    // super.outObject(adapter);
    // } else if (paramObject instanceof RichTextReference) {
    // JsonSearchAdapter adapter = new JsonSearchAdapter(graph_, (RichTextReference) paramObject, parameters_,
    // isCollectionRoute_);
    // super.outObject(adapter);
    } else if (paramObject instanceof VertexFrame) {
        JsonFrameAdapter adapter = new JsonFrameAdapter(graph_, (VertexFrame) paramObject, parameters_, isCollectionRoute_);
        outObject(adapter);
    } else if (paramObject instanceof EdgeFrame) {
        JsonFrameAdapter adapter = new JsonFrameAdapter(graph_, (EdgeFrame) paramObject, parameters_, isCollectionRoute_);
        outObject(adapter);
    } else if (paramObject instanceof Class<?>) {
        String className = ((Class) paramObject).getName();
        outStringLiteral(className);
    } else if (paramObject instanceof RichTextItem) {
        outRichTextItem((RichTextItem) paramObject);
    } else if (paramObject instanceof Enum) {
        String className = ((Enum) paramObject).getClass().getName();
        String enumName = ((Enum) paramObject).name();
        outStringLiteral(className + " " + enumName);
    } else if (paramObject instanceof CharSequence) {
        outStringLiteral(paramObject.toString());
    } else if (paramObject instanceof Set) {
        // System.out.println("TEMP DEBUG Got a set!");
        outArrayLiteral(((Set) paramObject).toArray());
    } else if (this.getFactory().isString(paramObject)) {
        outStringLiteral(this.getFactory().getString(paramObject));
    } else if (this.getFactory().isNumber(paramObject)) {
        outNumberLiteral(this.getFactory().getNumber(paramObject));
    } else if (this.getFactory().isBoolean(paramObject)) {
        outBooleanLiteral(this.getFactory().getBoolean(paramObject));
    } else if (this.getFactory().isObject(paramObject)) {
        outObject(paramObject, paramBoolean);
    } else if (this.getFactory().isArray(paramObject)) {
        outArrayLiteral(paramObject, paramBoolean);
    } else if (paramObject instanceof JsonReference) {
        outReference((JsonReference) paramObject);
    } else if (paramObject instanceof DateTime) {
        DateTime dt = (DateTime) paramObject;
        outDateLiteral_(dt.toJavaDate());
    } else if (paramObject instanceof DateRange) {
        DateRange dt = (DateRange) paramObject;
        outDateRangeLiteral(dt);
    } else if (paramObject instanceof NoteCoordinate) {
        outNoteCoordinate((NoteCoordinate) paramObject);
    } else if (paramObject instanceof Date) {
        outDateLiteral_((Date) paramObject);
    } else {
        outStringLiteral("JsonGenerator cannot process unknown type of " + ((paramObject != null) ? paramObject.getClass().getName() : "<null>"));
    }
}
Also used : NoteCoordinate(org.openntf.domino.big.impl.NoteCoordinate) EdgeFrame(com.tinkerpop.frames.EdgeFrame) Set(java.util.Set) JsonFrameAdapter(org.openntf.domino.rest.resources.frames.JsonFrameAdapter) IJsonWriterAdapter(org.openntf.domino.rest.json.JsonGraphFactory.IJsonWriterAdapter) DateTime(org.openntf.domino.DateTime) Date(java.util.Date) JsonReference(com.ibm.commons.util.io.json.JsonReference) DateRange(org.openntf.domino.DateRange) VertexFrame(com.tinkerpop.frames.VertexFrame) RichTextItem(org.openntf.domino.RichTextItem)

Example 5 with RichTextItem

use of org.openntf.domino.RichTextItem 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

RichTextItem (org.openntf.domino.RichTextItem)10 NotesException (lotus.domino.NotesException)4 Database (org.openntf.domino.Database)4 Document (org.openntf.domino.Document)4 DateTime (org.openntf.domino.DateTime)3 Item (org.openntf.domino.Item)3 UserAccessException (org.openntf.domino.exceptions.UserAccessException)3 EdgeFrame (com.tinkerpop.frames.EdgeFrame)2 VertexFrame (com.tinkerpop.frames.VertexFrame)2 Set (java.util.Set)2 Vector (java.util.Vector)2 DateRange (org.openntf.domino.DateRange)2 Session (org.openntf.domino.Session)2 NoteCoordinate (org.openntf.domino.big.impl.NoteCoordinate)2 OpenNTFNotesException (org.openntf.domino.exceptions.OpenNTFNotesException)2 IJsonWriterAdapter (org.openntf.domino.rest.json.JsonGraphFactory.IJsonWriterAdapter)2 JsonFrameAdapter (org.openntf.domino.rest.resources.frames.JsonFrameAdapter)2 JsonException (com.ibm.commons.util.io.json.JsonException)1 JsonReference (com.ibm.commons.util.io.json.JsonReference)1 JsonWriter (com.ibm.commons.util.io.json.util.JsonWriter)1