Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.mindoo.domino.jna.formula;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import com.mindoo.domino.jna.IAdaptable;
import com.mindoo.domino.jna.NotesItem;
import com.mindoo.domino.jna.NotesNote;
import com.mindoo.domino.jna.NotesTimeDate;
import com.mindoo.domino.jna.errors.FormulaCompilationError;
import com.mindoo.domino.jna.errors.INotesErrorConstants;
import com.mindoo.domino.jna.errors.NotesError;
import com.mindoo.domino.jna.errors.NotesErrorUtils;
import com.mindoo.domino.jna.errors.UnsupportedItemValueError;
import com.mindoo.domino.jna.gc.IRecyclableNotesObject;
import com.mindoo.domino.jna.gc.NotesGC;
import com.mindoo.domino.jna.internal.ItemDecoder;
import com.mindoo.domino.jna.internal.Mem32;
import com.mindoo.domino.jna.internal.Mem64;
import com.mindoo.domino.jna.internal.NotesNativeAPI32;
import com.mindoo.domino.jna.internal.NotesNativeAPI64;
import com.mindoo.domino.jna.utils.DumpUtil;
import com.mindoo.domino.jna.utils.NotesStringUtils;
import com.mindoo.domino.jna.utils.PlatformUtils;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.ShortByReference;
/**
* Utility class to execute a Domino formula on one or more {@link NotesNote} objects.
*
* @author Karsten Lehmann
*/
public class FormulaExecution implements IRecyclableNotesObject, IAdaptable {
private String m_formula;
private long m_hFormula64;
private long m_hCompute64;
private int m_hFormula32;
private int m_hCompute32;
private int m_compiledFormulaLength;
private Pointer m_ptrCompiledFormula;
private boolean m_preferNotesTimeDates;
/**
* Creates a new instance. The constructure compiles the formula and throws a {@link FormulaCompilationError},
* if there are any compilation errors
*
* @param formula formula
* @throws FormulaCompilationError if formula has wrong syntax
*/
public FormulaExecution(String formula) throws FormulaCompilationError {
m_formula = formula;
Memory formulaName = null;
short formulaNameLength = 0;
Memory formulaText = NotesStringUtils.toLMBCS(formula, false, false);
short formulaTextLength = (short) formulaText.size();
short computeFlags = 0;
if (PlatformUtils.is64Bit()) {
m_hFormula64 = 0;
LongByReference rethFormula = new LongByReference();
ShortByReference retFormulaLength = new ShortByReference();
ShortByReference retCompileError = new ShortByReference();
ShortByReference retCompileErrorLine = new ShortByReference();
ShortByReference retCompileErrorColumn = new ShortByReference();
ShortByReference retCompileErrorOffset = new ShortByReference();
ShortByReference retCompileErrorLength = new ShortByReference();
short result = NotesNativeAPI64.get().NSFFormulaCompile(formulaName, formulaNameLength, formulaText,
formulaTextLength, rethFormula, retFormulaLength, retCompileError, retCompileErrorLine,
retCompileErrorColumn, retCompileErrorOffset, retCompileErrorLength);
if (result == INotesErrorConstants.ERR_FORMULA_COMPILATION) {
String errMsg = NotesErrorUtils.errToString(result);
throw new FormulaCompilationError(result, errMsg, formula,
retCompileError.getValue(),
retCompileErrorLine.getValue(),
retCompileErrorColumn.getValue(),
retCompileErrorOffset.getValue(),
retCompileErrorLength.getValue());
}
NotesErrorUtils.checkResult(result);
m_hFormula64 = rethFormula.getValue();
m_compiledFormulaLength = (int) (retFormulaLength.getValue() & 0xffff);
LongByReference rethCompute = new LongByReference();
m_ptrCompiledFormula = Mem64.OSLockObject(m_hFormula64);
result = NotesNativeAPI64.get().NSFComputeStart(computeFlags, m_ptrCompiledFormula, rethCompute);
NotesErrorUtils.checkResult(result);
m_hCompute64 = rethCompute.getValue();
NotesGC.__objectCreated(FormulaExecution.class, this);
}
else {
m_hFormula32 = 0;
IntByReference rethFormula = new IntByReference();
ShortByReference retFormulaLength = new ShortByReference();
ShortByReference retCompileError = new ShortByReference();
ShortByReference retCompileErrorLine = new ShortByReference();
ShortByReference retCompileErrorColumn = new ShortByReference();
ShortByReference retCompileErrorOffset = new ShortByReference();
ShortByReference retCompileErrorLength = new ShortByReference();
short result = NotesNativeAPI32.get().NSFFormulaCompile(formulaName, formulaNameLength, formulaText,
formulaTextLength, rethFormula, retFormulaLength, retCompileError, retCompileErrorLine,
retCompileErrorColumn, retCompileErrorOffset, retCompileErrorLength);
if (result == INotesErrorConstants.ERR_FORMULA_COMPILATION) {
String errMsg = NotesErrorUtils.errToString(result);
throw new FormulaCompilationError(result, errMsg, formula,
retCompileError.getValue(),
retCompileErrorLine.getValue(),
retCompileErrorColumn.getValue(),
retCompileErrorOffset.getValue(),
retCompileErrorLength.getValue());
}
NotesErrorUtils.checkResult(result);
m_hFormula32 = rethFormula.getValue();
m_compiledFormulaLength = (int) (retFormulaLength.getValue() & 0xffff);
IntByReference rethCompute = new IntByReference();
m_ptrCompiledFormula = Mem32.OSLockObject(m_hFormula32);
result = NotesNativeAPI32.get().NSFComputeStart(computeFlags, m_ptrCompiledFormula, rethCompute);
NotesErrorUtils.checkResult(result);
m_hCompute32 = rethCompute.getValue();
NotesGC.__objectCreated(FormulaExecution.class, this);
}
}
@SuppressWarnings("unchecked")
@Override
public T getAdapter(Class clazz) {
checkHandle();
if (clazz == byte[].class) {
//return compiled formula as byte array
byte[] compiledFormula = m_ptrCompiledFormula.getByteArray(0, m_compiledFormulaLength);
return (T) compiledFormula;
}
return null;
}
public String getFormula() {
return m_formula;
}
/**
* Sets whether date/time values returned by the executed formulas should be
* returned as {@link NotesTimeDate} instead of being converted to {@link Calendar}.
*
* @param b true to prefer NotesTimeDate (false by default)
*/
public void setPreferNotesTimeDates(boolean b) {
m_preferNotesTimeDates = b;
}
/**
* Returns whether date/time values returned by the executed formulas should be
* returned as {@link NotesTimeDate} instead of being converted to {@link Calendar}.
*
* @return true to prefer NotesTimeDate
*/
public boolean isPreferNotesTimeDates() {
return m_preferNotesTimeDates;
}
public String toString() {
if (isRecycled()) {
return "Compiled formula [recycled, formula="+m_formula+"]";
}
else {
return "Compiled formula [formula="+m_formula+"]";
}
}
/**
* Convenience method to execute a formula on a single note and return the result as a string.
*
* Please note:
* If the same formula should be run
* on multiple notes, you should consider to create a shared instance of {@link FormulaExecution}
* and run its {@link #evaluate(NotesNote)} method. Then the formula is parsed and compiled only
* once, which results in better performance and optimized memory usage.
*
* @param formula formula
* @param note note
* @return computation result as string; if the formula returns a list, we pick the first value
* @throws FormulaCompilationError if formula has wrong syntax
*/
public static String evaluateAsString(String formula, NotesNote note) throws FormulaCompilationError {
List