Search in sources :

Example 16 with DocumentCollection

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

the class Document method replaceItemValueCustomData.

/**
 * serialize the Object value and stores it in the item. if <code>dataTypeName</code>="mime-bean" the item will be a MIME-bean,
 * otherwise, data is serialized by lotus.domino.Docmuemt.replaceItemValueCustomData
 */
public Item replaceItemValueCustomData(final String itemName, final String dataTypeName, final Object value, final boolean returnItem) {
    checkMimeOpen(itemName);
    lotus.domino.Item result = null;
    try {
        if (!"mime-bean".equalsIgnoreCase(dataTypeName)) {
            // if data-type is != "mime-bean" the object is written in native mode.
            beginEdit();
            result = getDelegate().replaceItemValueCustomData(itemName, dataTypeName, value);
            markDirty(itemName, true);
        } else if (value instanceof Serializable) {
            Documents.saveState((Serializable) value, this, itemName);
        // TODO RPr: Discuss if the other strategies make sense here.
        // In my opinion NoteCollection does work UNTIL the next compact task runs.
        // So it makes NO sense to serialize NoteIDs!
        } else if (value instanceof DocumentCollection) {
            // NoteIDs would be faster for this and, particularly, NoteCollection, but it should be replica-friendly
            DocumentCollection docs = (DocumentCollection) value;
            String[] unids = new String[docs.getCount()];
            int index = 0;
            for (org.openntf.domino.Document doc : docs) {
                unids[index++] = doc.getUniversalID();
            }
            Map<String, String> headers = new HashMap<String, String>(1);
            headers.put("X-Original-Java-Class", "org.openntf.domino.DocumentCollection");
            Documents.saveState(unids, this, itemName, true, headers);
        } else if (value instanceof NoteCollection) {
            // Maybe it'd be faster to use .getNoteIDs - I'm not sure how the performance compares
            // NTF .getNoteIDs() *IS* faster. By about an order of magnitude.
            NoteCollection notes = (NoteCollection) value;
            String[] unids = new String[notes.getCount()];
            String noteid = notes.getFirstNoteID();
            int index = 0;
            while (noteid != null && !noteid.isEmpty()) {
                unids[index++] = notes.getUNID(noteid);
                noteid = notes.getNextNoteID(noteid);
            }
            Map<String, String> headers = new HashMap<String, String>(1);
            headers.put("X-Original-Java-Class", "org.openntf.domino.NoteCollection");
            Documents.saveState(unids, this, itemName, true, headers);
        } else {
            // TODO RPr: Is this really needed or only a theoretical approach? See above...
            try {
                Class<?> stateHolderClass = Class.forName("javax.faces.component.StateHolder", true, Factory.getClassLoader());
                if (stateHolderClass.isInstance(value)) {
                    Class<?> facesContextClass = Class.forName("javax.faces.context.FacesContext", true, Factory.getClassLoader());
                    Method getCurrentInstance = facesContextClass.getMethod("getCurrentInstance");
                    Method saveState = stateHolderClass.getMethod("saveState", facesContextClass);
                    Serializable state = (Serializable) saveState.invoke(value, getCurrentInstance.invoke(null));
                    Map<String, String> headers = new HashMap<String, String>();
                    headers.put("X-Storage-Scheme", "StateHolder");
                    headers.put("X-Original-Java-Class", value.getClass().getName());
                    Documents.saveState(state, this, itemName, true, headers);
                } else {
                    throw new IllegalArgumentException(value.getClass() + " is not of type Serializable, DocumentCollection, NoteCollection or StateHolder");
                }
            } catch (ClassNotFoundException cnfe) {
                throw new IllegalArgumentException(value.getClass() + " is not of type Serializable, DocumentCollection or NoteCollection");
            }
        }
        if (returnItem) {
            if (result == null) {
                // MSt: This is safe now. (Was tested.)
                return getFirstItem(itemName, true);
            }
            // If we didn't write a MIME attachment, then result is already assigned, and therefore we don't need to get it again.
            return fromLotus(result, Item.SCHEMA, this);
        } else {
            return null;
        }
    } catch (Exception e) {
        DominoUtils.handleException(e, this, "Item=" + itemName);
        return null;
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) DocumentCollection(org.openntf.domino.DocumentCollection) 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) NoteCollection(org.openntf.domino.NoteCollection) NoteClass(org.openntf.domino.ext.NoteClass) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 17 with DocumentCollection

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

the class Database method search.

/*
	 * (non-Javadoc)
	 *
	 * @see org.openntf.domino.Database#search(java.lang.String, lotus.domino.DateTime, int)
	 */
@Override
public DocumentCollection search(final String formula, final lotus.domino.DateTime startDate, final int maxDocs) {
    try {
        DocumentCollection result;
        lotus.domino.DateTime dt = toLotus(startDate);
        result = fromLotus(getDelegate().search(formula, dt, maxDocs), DocumentCollection.SCHEMA, this);
        if (startDate instanceof Encapsulated) {
            dt.recycle();
        }
        return result;
    } catch (NotesException e) {
        DominoUtils.handleException(e, this);
        return null;
    }
}
Also used : Encapsulated(org.openntf.domino.types.Encapsulated) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) NotesException(lotus.domino.NotesException) DocumentCollection(org.openntf.domino.DocumentCollection)

Example 18 with DocumentCollection

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

the class DocumentSyncHelper method processDocument.

/**
 * Process a specific Document
 *
 * @param targetDb
 *            Database to retrieve documents to sync to
 * @param targetView
 *            View to retrieve documents to sync to
 * @param strategy
 *            Strategy to sync Items
 * @param txn
 *            DatabaseTransaction to run under
 * @param source
 *            Document to sync from
 * @return DatabaseTransaction to run under
 */
private DatabaseTransaction processDocument(final Database targetDb, final View targetView, final Strategy strategy, DatabaseTransaction txn, final Document source) {
    if (getTransactionRule() == TransactionRule.COMMIT_EVERY_SOURCE) {
        txn = targetDb.startTransaction();
    }
    DateTime sourceLastMod = source.getLastModified();
    // Object lookupKey = Factory.wrappedEvaluate(session, getSourceKeyFormula(), source);
    Object lookupKey = getSourceKeyFormula().getValue(source);
    DocumentCollection targetColl = targetView.getAllDocumentsByKey(lookupKey, true);
    for (Document target : targetColl) {
        // boolean targetDirty = false;
        for (Map.Entry<Formula, String> entry : getSyncMap().entrySet()) {
            String targetItemName = entry.getValue();
            java.util.Vector<?> sourceValue = entry.getKey().getValue(source);
            // Factory.wrappedEvaluate(session, entry.getKey(), source);
            if (strategy == Strategy.CREATE_AND_REPLACE) {
                target.replaceItemValue(targetItemName, sourceValue);
            // targetDirty = true;
            } else {
                Item targetItem = target.getFirstItem(targetItemName);
                if (strategy == Strategy.REPLACE_IF_NEWER) {
                    DateTime itemLastMod = targetItem.getLastModified();
                    if (sourceLastMod.isAfter(itemLastMod)) {
                        targetItem.setValues(sourceValue);
                    // targetDirty = true;
                    }
                } else if (strategy == Strategy.REPLACE_ONLY) {
                    if (targetItem != null) {
                        targetItem.setValues(sourceValue);
                    // targetDirty = true;
                    }
                }
            }
        }
        if (getTransactionRule() == TransactionRule.NO_TRANSACTION || txn == null) {
            target.save();
        }
    }
    if (getTransactionRule() == TransactionRule.COMMIT_EVERY_SOURCE && txn != null) {
        txn.commit();
        txn = null;
    }
    return txn;
}
Also used : Item(org.openntf.domino.Item) DocumentCollection(org.openntf.domino.DocumentCollection) Document(org.openntf.domino.Document) HashMap(java.util.HashMap) Map(java.util.Map) DateTime(org.openntf.domino.DateTime)

Example 19 with DocumentCollection

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

the class DocumentCollectionIteratorTest method run.

@Override
public void run() {
    long testStartTime = System.nanoTime();
    try {
        Session session = Factory.getSession(SessionType.CURRENT);
        Database db = session.getDatabase("", "events4.nsf");
        DocumentCollection coll = db.getAllDocuments();
        for (Document doc : coll) {
            System.out.println("nid: " + doc.getNoteID());
        }
        long endTime = System.nanoTime();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : Database(org.openntf.domino.Database) DocumentCollection(org.openntf.domino.DocumentCollection) Document(org.openntf.domino.Document) Session(org.openntf.domino.Session)

Example 20 with DocumentCollection

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

the class MassDCRemoveAllScratchTest method run.

@Test
public void run() {
    Session s = Factory.getSession(SessionType.CURRENT);
    Database source = s.getDatabase("", SOURCE, true);
    System.out.println("-- START --");
    long start = System.nanoTime();
    DocumentCollection dc = source.getAllDocuments();
    dc.removeAll(true);
    long elapsed = System.nanoTime() - start;
    System.out.println("-- STOP --");
    System.out.println("Thread " + Thread.currentThread().getName() + " elapsed time: " + elapsed / 1000000 + "ms");
}
Also used : Database(org.openntf.domino.Database) DocumentCollection(org.openntf.domino.DocumentCollection) Session(org.openntf.domino.Session) Test(org.junit.Test)

Aggregations

DocumentCollection (org.openntf.domino.DocumentCollection)23 Database (org.openntf.domino.Database)9 NotesException (lotus.domino.NotesException)8 Document (org.openntf.domino.Document)8 Session (org.openntf.domino.Session)7 OpenNTFNotesException (org.openntf.domino.exceptions.OpenNTFNotesException)6 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 DateTime (org.openntf.domino.DateTime)4 HashMap (java.util.HashMap)3 Vector (java.util.Vector)3 View (org.openntf.domino.View)3 DocumentSorter (org.openntf.domino.helpers.DocumentSorter)3 Encapsulated (org.openntf.domino.types.Encapsulated)3 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Map (java.util.Map)2 Test (org.junit.Test)2 Item (org.openntf.domino.Item)2 NoteCollection (org.openntf.domino.NoteCollection)2