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;
}
}
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;
}
}
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;
}
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();
}
}
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");
}
Aggregations