use of org.openntf.domino.helpers.Formula in project org.openntf.domino by OpenNTF.
the class Document method get.
/**
* Gets a value depending on the parameter 'key'. Key can either be a @Formula, a special value or an item name.
* <p>
* Possible values for the 'key' parameter:
* <dl>
* <dt>parentdocument</dt>
* <dd>Returns the parent document if this document is a response</dd>
* <dt>@accesseddate</dt>
* <dd>Indicates the time and date when the document was last accessed by a Notes client, whether for reading or editing as a
* java.util.Date.</dd>
* <dt>@modifieddate</dt>
* <dd>Returns a time-date value indicating when the document was modified initially as a java.util.Date</dd>
* <dt>@createddate</dt>
* <dd>Returns the time-date when the document was created as a java.util.Date</dd>
* </dl>
* </p>
*
* @param key
* @formula, item name or a special value from a list above
* @return value corresponding to the given 'key'
*/
@Override
public Object get(final Object key) {
// TODO NTF: Implement a read-only mode for Documents so we know in advance that our cache is valid
if (key == null) {
return null;
}
if (key instanceof CharSequence) {
String skey = key.toString().toLowerCase();
if ("parentdocument".equalsIgnoreCase(skey)) {
return this.getParentDocument();
}
if ("form".equalsIgnoreCase(skey)) {
return this.getFormName();
}
if (skey.indexOf("@") != -1) {
// TODO RPr: Should we REALLY detect all formulas, like "3+5" or "field[2]" ?
// TODO NTF: If so, we should change to looking for valid item names first, then trying to treat as formula
int pos = skey.indexOf('(');
if (pos != -1) {
skey = skey.substring(0, pos);
}
if ("@accessed".equals(skey)) {
return this.getLastAccessed();
}
if ("@modified".equals(skey)) {
return this.getLastModified();
}
if ("@created".equals(skey)) {
return this.getCreated();
}
if ("@accesseddate".equals(skey)) {
return this.getLastAccessedDate();
}
if ("@modifieddate".equals(skey)) {
return this.getLastModifiedDate();
}
if ("@createddate".equals(skey)) {
return this.getCreatedDate();
}
if ("@documentuniqueid".equals(skey)) {
return this.getUniversalID();
}
if ("@noteid".equals(skey)) {
return this.getNoteID();
}
if ("@doclength".equals(skey)) {
return this.getSize();
}
if ("@isresponsedoc".equals(skey)) {
return this.isResponse();
}
if ("@replicaid".equals(skey)) {
return this.getAncestorDatabase().getReplicaID();
}
if ("@responses".equals(skey)) {
DocumentCollection resp = this.getResponses();
if (resp == null) {
return 0;
}
return resp.getCount();
}
if ("@isnewdoc".equals(skey)) {
return this.isNewNote();
}
if ("@inheriteddocumentuniqueid".equals(skey)) {
org.openntf.domino.Document parent = this.getParentDocument();
if (parent == null) {
return "";
}
return parent.getUniversalID();
}
// TODO RPr: This should be replaced
// TODO NTF: Agreed when we can have an extensible switch for which formula engine to use
Formula formula = new Formula();
formula.setExpression(key.toString());
List<?> value = formula.getValue(this);
if (value.size() == 1) {
return value.get(0);
}
return value;
}
}
// TODO: What is the best way to use here without breaking everything?
// Object value = this.getItemValue(key.toString(), Object.class);
// Object value = this.getItemValue(key.toString(), Object[].class);
Object value = null;
String keyS = key.toString();
try {
value = this.getItemValue(keyS, Object.class);
} catch (OpenNTFNotesException e) {
if (e.getCause() instanceof NotesException || (e.getCause() != null && e.getCause().getCause() instanceof NotesException)) {
value = getFirstItem(keyS, true);
}
if (value == null) {
throw e;
}
}
if (value instanceof Vector) {
Vector<?> v = (Vector<?>) value;
if (v.size() == 1) {
return v.get(0);
}
}
return value;
// if (this.containsKey(key)) {
// Vector<Object> value = this.getItemValue(key.toString());
// if (value == null) {
// //TODO Throw an exception if the item data can't be read? Null implies the key doesn't exist
// return null;
// } else if (value.size() == 1) {
// return value.get(0);
// }
// return value;
// }
// return null;
}
use of org.openntf.domino.helpers.Formula in project org.openntf.domino by OpenNTF.
the class AbstractPropertyHandler method processElementProperty.
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object processElementProperty(final Object frame, final Method method, final Object[] arguments, final Annotation annotation, final FramedGraph framedGraph, final Element element) {
boolean isDerived = false;
// $NON-NLS-1$
String value = "";
@SuppressWarnings("unused") Class<?> converter = null;
String defaultValue = null;
String computation = null;
if (annotation instanceof Property) {
value = ((Property) annotation).value();
} else if (annotation instanceof TypedProperty) {
value = ((TypedProperty) annotation).value();
isDerived = ((TypedProperty) annotation).derived();
// converter = ((TypedProperty) annotation).converter();
defaultValue = ((TypedProperty) annotation).defaultValue();
// if (defaultValue != null) {
// System.out.println("TEMP DEBUG defaultValue found of " + defaultValue + " on method " + method.getName());
// }
} else if (annotation instanceof ComputedProperty) {
// System.out.println("TEMP DEBUG handling a computed property");
value = ((ComputedProperty) annotation).value();
computation = ((ComputedProperty) annotation).computation();
// isDerived = true;
}
if (ClassUtilities.isSetMethod(method) && isDerived) {
// $NON-NLS-1$
throw new DerivedPropertySetException(MessageFormat.format("Setting on a derived property {0} is not permitted.", value));
}
Class<?> type = method.getReturnType();
if (ClassUtilities.isSetMethod(method)) {
Class<?>[] paramTypes = method.getParameterTypes();
int i = 0;
for (Class paramType : paramTypes) {
if (lotus.domino.Base.class.isAssignableFrom(paramType)) {
arguments[i] = org.openntf.domino.utils.TypeUtils.convertToTarget(arguments[i], paramType, org.openntf.domino.utils.Factory.getSession(SessionType.NATIVE));
} else {
arguments[i] = org.openntf.domino.utils.TypeUtils.convertToTarget(arguments[i], paramType, null);
}
i++;
}
}
Object raw = orig_processElement(value, method, arguments, framedGraph, element, null);
if (null == raw || (raw instanceof String && ((String) raw).length() == 0)) {
if (defaultValue != null && defaultValue.length() > 0) {
Formula formula = new Formula(defaultValue);
raw = processFormula(formula, element);
// System.out.println("TEMP DEBUG defaultValue of " + String.valueOf(raw) + " calculated for " + value);
} else if (computation != null && computation.length() > 0) {
// System.out.println("TEMP DEBUG Running a computed property: " + value);
Formula formula = new Formula(computation);
raw = processFormula(formula, element);
}
}
Object result = null;
if (raw == null) {
result = null;
} else if (type.isAssignableFrom(raw.getClass())) {
result = type.cast(raw);
} else if (lotus.domino.Base.class.isAssignableFrom(type)) {
result = org.openntf.domino.utils.TypeUtils.convertToTarget(raw, type, org.openntf.domino.utils.Factory.getSession(SessionType.CURRENT));
} else {
result = org.openntf.domino.utils.TypeUtils.convertToTarget(raw, type, null);
}
if (computation != null && computation.length() > 0) {
element.setProperty(value, result);
}
return result;
}
use of org.openntf.domino.helpers.Formula in project org.openntf.domino by OpenNTF.
the class FormulaTester method run.
@Override
public void run() {
Session session = Factory.getSession(SessionType.CURRENT);
Formula formula = new Formula();
String source = "REM {Begin_Do_Not_Tag};\r\n" + "\r\n" + "SenderName := @If(SendTo = \"\";EnterSendTo;SendTo);\r\n" + "Send := @Subset(SenderName; 1);\r\n" + "CN1 := @Trim(@Name([CN]; Send));\r\n" + "CN := @If(@Contains(@Right(Send;\"@\");\">\") & CN1 = \"\";@Trim(Send); CN1);\r\n" + "G := @If(CN = \"\"; @Name([G]; @Subset(SenderName; 1)); \"\");\r\n" + "S := @If(CN = \"\"; @Name([S]; @Subset(SenderName; 1)); \"\");\r\n" + "Person := @If(CN != \"\"; CN; G + \" \" + S);\r\n" + "@If(@Left(Person;1)=\"\\\"\" & @Right(Person;1)=\"\\\"\"; @LeftBack(@RightBack(Person;1);1); Person);\r\n" + "REM {End_Do_Not_Tag};";
formula.setExpression(source);
org.openntf.domino.helpers.Formula.Parser parser = formula.getParser();
if (parser != null) {
parser.parse();
Set<String> literals = parser.getLiterals();
System.out.println("BEGIN LITERALS");
for (String literal : literals) {
System.out.print(literal + ", ");
}
System.out.println("END LITERALS");
Set<String> functions = parser.getFunctions();
System.out.println("BEGIN FUNCTIONS");
for (String function : functions) {
System.out.print(function + ", ");
}
System.out.println("END FUNCTIONS");
Set<String> localVars = parser.getLocalVars();
System.out.println("BEGIN LOCAL VARIABLES");
for (String var : localVars) {
System.out.print(var + ", ");
}
System.out.println("END LOCAL VARIABLES");
Set<String> fieldVars = parser.getFieldVars();
System.out.println("BEGIN FIELDS");
for (String var : fieldVars) {
System.out.print(var + ", ");
}
System.out.println("END FIELDS");
Set<String> envVars = parser.getEnvVars();
System.out.println("BEGIN ENVIRONMENTS");
for (String var : envVars) {
System.out.print(var + ", ");
}
System.out.println("END ENVIRONMENTS");
Set<String> keywords = parser.getKeywords();
System.out.println("BEGIN KEYWORDS");
for (String var : keywords) {
System.out.print(var + ", ");
}
System.out.println("END KEYWORDS");
Set<String> numbers = parser.getNumberLiterals();
System.out.println("BEGIN NUMBERS");
for (String var : numbers) {
System.out.print(var + ", ");
}
System.out.println("END NUMBERS");
} else {
System.out.println("Parser was null?");
}
}
use of org.openntf.domino.helpers.Formula in project org.openntf.domino by OpenNTF.
the class DominoRunnable method run.
@Override
public void run() {
Session session = Factory.getSession(SessionType.CURRENT);
Formula formula = new Formula();
String source = "REM {the quick \"brown\" fox jumped};\r\n" + "REM {over the \"lazy\" dog};\r\n" + "DEFAULT defVar := @If(isThing2; \"thing2\"; thing);\r\n" + "ENVIRONMENT envVar := @Now;\r\n" + "FIELD field1 := \"the cow jumped over the moon\";\r\n" + "FIELD field2 := \"dish... spoon... you know the score.\";\r\n" + "FIELD field3 := @Adjust([08/08/2002]; 1; 2; 3; 4; 5; 6);\r\n" + "tmpVar := field1 + \" rhyming \\\"time\\\" \" + field2[2];\r\n" + "tmpVar2 := 54938 + docField2;\r\n" + "tmpVar + tmpVar2 + docField3 + @Name([CN]; @UserName) + 8;\r\n" + "@Command([AddBookmark]; \"foo\"; docField);\r\n" + "";
formula.setExpression(source);
org.openntf.domino.helpers.Formula.Parser parser = formula.getParser();
if (parser != null) {
parser.parse();
Set<String> literals = parser.getLiterals();
System.out.println("BEGIN LITERALS");
for (String literal : literals) {
System.out.print(literal + ", ");
}
System.out.println("END LITERALS");
Set<String> functions = parser.getFunctions();
System.out.println("BEGIN FUNCTIONS");
for (String function : functions) {
System.out.print(function + ", ");
}
System.out.println("END FUNCTIONS");
Set<String> localVars = parser.getLocalVars();
System.out.println("BEGIN LOCAL VARIABLES");
for (String var : localVars) {
System.out.print(var + ", ");
}
System.out.println("END LOCAL VARIABLES");
Set<String> fieldVars = parser.getFieldVars();
System.out.println("BEGIN FIELDS");
for (String var : fieldVars) {
System.out.print(var + ", ");
}
System.out.println("END FIELDS");
Set<String> envVars = parser.getEnvVars();
System.out.println("BEGIN ENVIRONMENTS");
for (String var : envVars) {
System.out.print(var + ", ");
}
System.out.println("END ENVIRONMENTS");
Set<String> keywords = parser.getKeywords();
System.out.println("BEGIN KEYWORDS");
for (String var : keywords) {
System.out.print(var + ", ");
}
System.out.println("END KEYWORDS");
Set<String> numbers = parser.getNumberLiterals();
System.out.println("BEGIN NUMBERS");
for (String var : numbers) {
System.out.print(var + ", ");
}
System.out.println("END NUMBERS");
}
}
Aggregations