KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > functions > ExampleBuilder


1 package org.mmbase.util.functions;
2
3 import java.util.*;
4
5 import org.mmbase.bridge.*;
6 import org.mmbase.storage.search.*;
7 import org.mmbase.module.core.*;
8 import org.mmbase.storage.search.SortOrder;
9 import org.mmbase.util.logging.*;
10
11
12 /**
13  * Example builder implementation implementing functions. Lots of people are sooner or earlier
14  * trying to make their own builder implementation. Especially whith the advent the 'function' tags in
15  * 1.7 it would be nice that people could seen an example of how that could be done.
16  *
17  * To try it out, take a builder xml and add
18  * <code>&lt;classfile&gt;org.mmbase.util.functions.ExampleBuilder&lt;/classfile&gt; </code>
19  * and e.g. a jsp like this:
20  * <code>
21  * <pre>
22  * &lt;mm:listnodes type="pools" max="1"&gt;
23  * &lt; mm:import id="max"&gt;100&lt;/mm:import&gt;
24  * &lt;mm:nodelistfunction referids="max" name="latest"&gt;
25  * -- &lt;mm:field name="number" /&gt;&lt;br /&gt;
26  * &lt/mm:nodelistfunction&gt;
27  * &lt;/mm:listnodes&gt;
28  * </pre>
29  * </code>
30  *
31  * This is done in the MyNews examples (on the news builder), and example JSP's can be found on /mmexamples/taglib/functions.jsp.
32  *
33  * @author Michiel Meeuwissen
34  * @version $Id: ExampleBuilder.java,v 1.13 2006/01/13 15:37:24 pierre Exp $
35  * @see ExampleBean For examples on hot to add functions to a builder without extending it.
36  * @since MMBase-1.7
37  */

38 public final class ExampleBuilder extends MMObjectBuilder { // final to avoid that people actually use this to extend their stuff from or so.
39
private static final Logger log = Logging.getLoggerInstance(ExampleBuilder.class);
40
41
42     /**
43      * Parameter constant for use bij the 'latest' function. This constant must be protected,
44      * otherwise it is pickup up by the automatich function detection.
45      */

46     protected final static Parameter[] LISTLATEST_PARAMETERS = {
47         new Parameter("max", Integer JavaDoc.class, new Integer JavaDoc(10)), /* name, type, default value */
48         new Parameter(Parameter.CLOUD, true) /* true: required! */
49     };
50
51     protected final static Parameter[] SUMFIELDS_PARAMETERS = {
52         new Parameter("fields", List.class, Arrays.asList(new String JavaDoc[] {"otype", "number"})) /* name, type, default value */
53     };
54
55     /**
56      * Implementation of 'builder function', which can be compared with a static method in java.
57      */

58     protected final Function listLatestFunction = new AbstractFunction("latest", LISTLATEST_PARAMETERS, ReturnType.NODELIST) {
59             {
60                 setDescription("This (rather silly) function returns the latest instances of this builder.");
61             }
62             public Object JavaDoc getFunctionValue(Parameters parameters) {
63                 Integer JavaDoc max = (Integer JavaDoc) parameters.get("max");
64                 Cloud cloud = (Cloud) parameters.get(Parameter.CLOUD);
65                 NodeManager thisManager = cloud.getNodeManager(getTableName());
66                 NodeQuery q = thisManager.createQuery();
67                 q.setMaxNumber(max.intValue());
68                 q.addSortOrder(q.getStepField(thisManager.getField("number")), SortOrder.ORDER_DESCENDING);
69                 return thisManager.getList(q);
70             }
71     };
72     {
73         // functions must be registered.
74
addFunction(listLatestFunction);
75     }
76
77     /**
78      * Implementation of 'node function', which can be compared with a instance method in java.
79      */

80     protected final Function sumFieldsFunction = new NodeFunction("sumfields", SUMFIELDS_PARAMETERS, ReturnType.INTEGER) {
81             {
82                 setDescription("This (rather silly) function returns the sum of the given fields of a certain node");
83             }
84             public Object JavaDoc getFunctionValue(Node node, Parameters parameters) {
85                 List fields = (List) parameters.get("fields");
86                 int result = 0;
87                 Iterator i = fields.iterator();
88                 while (i.hasNext()) {
89                     result += node.getIntValue((String JavaDoc)i.next());
90                 }
91                 return new Integer JavaDoc(result);
92             }
93     };
94     {
95         // node-function are registered in the same way.
96
addFunction(sumFieldsFunction);
97     }
98
99
100     {
101
102         // you can of course even implement it anonymously.
103
addFunction(new AbstractFunction("showparameter",
104                                          new Parameter[] {
105                                              new Parameter("collectionparam", Collection.class),
106                                              new Parameter("mapparam", Map.class),
107                                              new Parameter("integerparam", Integer JavaDoc.class),
108                                              new Parameter("numberparam", Number JavaDoc.class)
109                                          },
110                                          ReturnType.LIST) {
111                 {
112                     setDescription("With this function one can demonstrate how to create parameters of several types, and in what excactly that results");
113                 }
114                 public Object JavaDoc getFunctionValue(Parameters parameters) {
115                     List result = new ArrayList();
116                     Parameter[] def = parameters.getDefinition();
117                     for (int i = 0 ; i < def.length; i++) {
118                         Object JavaDoc value = parameters.get(i);
119                         if(value != null) {
120                             result.add(def[i].toString() + " ->" + value.getClass().getName() + " " + value);
121                         }
122                     }
123                     return result;
124                 }
125             });
126     }
127
128     {
129         // an example of a function which is both node-function and builder-function
130
// it also demonstrate that you may use bridge to implement a node-function.
131

132         addFunction(new NodeFunction("predecessor",
133                                      new Parameter[] {
134                                          Parameter.CLOUD // makes it possible to implement by bridge.
135
},
136                                      ReturnType.NODE) {
137                 {
138                     setDescription("Returns the node older then the current node, or null if this node is the oldest (if called on node), or the newest node of this type, or null of there are no nodes of this type (if called on builder)");
139                 }
140                 public Object JavaDoc getFunctionValue(Node node, Parameters parameters) {
141                     NodeManager nm = node.getNodeManager();
142                     NodeQuery q = nm.createQuery();
143                     StepField field = q.getStepField(nm.getField("number"));
144                     q.setConstraint(q.createConstraint(field, FieldCompareConstraint.LESS, new Integer JavaDoc(node.getNumber())));
145                     q.addSortOrder(field, SortOrder.ORDER_DESCENDING);
146                     q.setMaxNumber(1);
147                     NodeIterator i = nm.getList(q).nodeIterator();
148                     return i.hasNext() ? i.nextNode() : null;
149                 }
150
151                 public Object JavaDoc getFunctionValue(Parameters parameters) {
152                     Cloud cloud = (Cloud) parameters.get(Parameter.CLOUD);
153                     NodeManager nm = cloud.getNodeManager(ExampleBuilder.this.getTableName());
154                     NodeQuery q = nm.createQuery();
155                     StepField field = q.getStepField(nm.getField("number"));
156                     q.addSortOrder(field, SortOrder.ORDER_DESCENDING);
157                     q.setMaxNumber(1);
158                     NodeIterator i = nm.getList(q).nodeIterator();
159                     return i.hasNext() ? i.nextNode() : null;
160                 }
161             });
162     }
163
164 }
165
Popular Tags