001: package org.mmbase.util.functions;
002:
003: import org.mmbase.module.core.*;
004: import org.mmbase.storage.search.*;
005: import org.mmbase.storage.search.implementation.*;
006: import org.mmbase.bridge.*;
007: import java.util.*;
008: import org.mmbase.util.logging.Logger;
009: import org.mmbase.util.logging.Logging;
010:
011: /**
012: * A bean can be accessed through the function framework.
013: *
014: * @author Michiel Meeuwissen
015: * @version $Id: ExampleBean.java,v 1.11 2007/11/25 18:25:49 nklasens Exp $
016: * @since MMBase-1.8
017: */
018: public final class ExampleBean {
019:
020: private static final Logger log = Logging
021: .getLoggerInstance(ExampleBean.class);
022: private String parameter1;
023: private Integer parameter2 = 0;
024: private String parameter3 = "default";
025: private Node node;
026: private Cloud cloud;
027:
028: public void setParameter1(String hoi) {
029: parameter1 = hoi;
030: }
031:
032: public void setParameter2(Integer j) {
033: parameter2 = j;
034: }
035:
036: public Integer getParameter2() {
037: return parameter2;
038: }
039:
040: public void setAnotherParameter(String a) {
041: parameter3 = a;
042: }
043:
044: public String getAnotherParameter() {
045: return parameter3;
046: }
047:
048: /**
049: * Makes this bean useable as a Node function.
050: */
051: public void setNode(Node node) {
052: this .node = node;
053: }
054:
055: /**
056: * Makes the functions useable in bridge (so with security). See also {@link
057: * Parameter#CLOUD}. This is an example of a parameter which is automaticly filled by function tags.
058: */
059: public void setCloud(Cloud c) {
060: cloud = c;
061: }
062:
063: /**
064: * A function defined by this class
065: */
066: public String stringFunction() {
067: return "[[" + parameter1 + "/" + parameter3 + "]]";
068: }
069:
070: public Integer integerFunction() {
071: return parameter2 * 3;
072: }
073:
074: /**
075: * A function returning a Map
076: */
077: public Map<String, String> mapFunction() {
078: Map<String, String> map = new HashMap<String, String>();
079: map.put("bloe", parameter1);
080: return map;
081: }
082:
083: /**
084: * A function returning a Node as a core object (deprecated).
085: */
086: public MMObjectNode nodeFunction1() {
087: VirtualBuilder builder = new VirtualBuilder(MMBase.getMMBase());
088: MMObjectNode virtual = builder.getNewNode("admin");
089: virtual.storeValue("bloe", parameter1);
090: return virtual;
091: }
092:
093: /**
094: * A function returning a Node as a bridge object, but based on a Map of values.
095: */
096: public Node nodeFunction2() {
097: Map<String, String> map = new HashMap<String, String>();
098: map.put("bloe", parameter1);
099: return new org.mmbase.bridge.util.MapNode(map);
100: }
101:
102: public Collection<Object> nodeListFunction() {
103: List<Object> result = new ArrayList<Object>();
104: result.add(nodeFunction1());
105: result.add(nodeFunction2());
106: return result;
107: }
108:
109: public NodeList nodeListFunction1() {
110: Collection<Object> col = nodeListFunction();
111: col.add(mapFunction());
112: return new org.mmbase.bridge.util.CollectionNodeList(col);
113: }
114:
115: /**
116: * A real node-function (using the node argument). Returns the next newer node of same type.
117: * Also a nice example on the difference between core and bridge.
118: */
119: public Object successor() {
120: if (node == null)
121: throw new IllegalArgumentException(
122: "successor is a node-function");
123: if (cloud != null) {
124: log
125: .debug("Using bridge (security restrictions will be honoured)");
126: NodeManager nm = node.getNodeManager();
127: NodeQuery q = nm.createQuery();
128: StepField field = q.getStepField(nm.getField("number"));
129: q.setConstraint(q.createConstraint(field,
130: FieldCompareConstraint.GREATER, Integer
131: .valueOf(node.getNumber())));
132: q.addSortOrder(field, SortOrder.ORDER_ASCENDING);
133: q.setMaxNumber(1);
134: NodeIterator i = nm.getList(q).nodeIterator();
135: return i.hasNext() ? i.nextNode() : null;
136: } else {
137: log.debug("Using core.");
138: MMObjectBuilder builder = MMBase.getMMBase().getBuilder(
139: node.getNodeManager().getName());
140: NodeSearchQuery query = new NodeSearchQuery(builder);
141: StepField field = query
142: .getField(builder.getField("number"));
143: BasicFieldValueConstraint cons = new BasicFieldValueConstraint(
144: field, node.getNumber());
145: cons.setOperator(FieldCompareConstraint.GREATER);
146: query.setConstraint(cons);
147: query.addSortOrder(field);
148: query.setMaxNumber(1);
149: try {
150: java.util.Iterator<MMObjectNode> i = builder.getNodes(
151: query).iterator();
152: return i.hasNext() ? i.next() : null;
153: } catch (Exception e) {
154: return null;
155: }
156: }
157: }
158:
159: }
|