KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.mmbase.util.functions;
2
3 import org.mmbase.module.core.*;
4 import org.mmbase.storage.search.*;
5 import org.mmbase.storage.search.implementation.*;
6 import org.mmbase.bridge.*;
7 import java.util.*;
8 import org.mmbase.util.logging.Logger;
9 import org.mmbase.util.logging.Logging;
10
11 /**
12  * A bean can be accessed through the function framework.
13  *
14  * @author Michiel Meeuwissen
15  * @version $Id: ExampleBean.java,v 1.9 2006/06/20 20:51:49 michiel Exp $
16  * @since MMBase-1.8
17  */

18 public final class ExampleBean {
19
20     private static final Logger log = Logging.getLoggerInstance(ExampleBean.class);
21     private String JavaDoc parameter1;
22     private Integer JavaDoc parameter2 = new Integer JavaDoc(0);
23     private String JavaDoc parameter3 = "default";
24     private Node node;
25     private Cloud cloud;
26
27     public void setParameter1(String JavaDoc hoi) {
28         parameter1 = hoi;
29     }
30
31     public void setParameter2(Integer JavaDoc j) {
32         parameter2 = j;
33     }
34     public Integer JavaDoc getParameter2() {
35         return parameter2;
36     }
37     public void setAnotherParameter(String JavaDoc a) {
38         parameter3 = a;
39     }
40     public String JavaDoc getAnotherParameter() {
41         return parameter3;
42     }
43
44     /**
45      * Makes this bean useable as a Node function.
46      */

47     public void setNode(Node node) {
48         this.node = node;
49     }
50
51     /**
52      * Makes the functions useable in bridge (so with security). See also {@link
53      * Parameter#CLOUD}. This is an example of a parameter which is automaticly filled by function tags.
54      */

55     public void setCloud(Cloud c) {
56         cloud = c;
57     }
58
59
60     /**
61      * A function defined by this class
62      */

63     public String JavaDoc stringFunction() {
64         return "[[" + parameter1 + "/" + parameter3 + "]]";
65     }
66
67     public Integer JavaDoc integerFunction() {
68         return new Integer JavaDoc(parameter2.intValue() * 3);
69     }
70
71     /**
72      * A function returning a Map
73      */

74     public Map mapFunction() {
75         Map map = new HashMap();
76         map.put("bloe", parameter1);
77         return map;
78     }
79
80     /**
81      * A function returning a Node as a core object (deprecated).
82      */

83     public MMObjectNode nodeFunction1() {
84         VirtualBuilder builder = new VirtualBuilder(MMBase.getMMBase());
85         MMObjectNode virtual = builder.getNewNode("admin");
86         virtual.storeValue("bloe", parameter1);
87         return virtual;
88     }
89
90     /**
91      * A function returning a Node as a bridge object, but based on a Map of values.
92      */

93     public Node nodeFunction2() {
94         Map map = new HashMap();
95         map.put("bloe", parameter1);
96         return new org.mmbase.bridge.util.MapNode(map);
97     }
98
99
100     public Collection nodeListFunction() {
101         List result = new ArrayList();
102         result.add(nodeFunction1());
103         result.add(nodeFunction2());
104         return result;
105     }
106     public NodeList nodeListFunction1() {
107         Collection col = nodeListFunction();
108         col.add(mapFunction());
109         return new org.mmbase.bridge.util.CollectionNodeList(col);
110     }
111
112     /**
113      * A real node-function (using the node argument). Returns the next newer node of same type.
114      * Also a nice example on the difference between core and bridge.
115      */

116     public Object JavaDoc successor() {
117         if (node == null) throw new IllegalArgumentException JavaDoc("successor is a node-function");
118         if (cloud != null) {
119             log.debug("Using bridge (security restrictions will be honoured)");
120             NodeManager nm = node.getNodeManager();
121             NodeQuery q = nm.createQuery();
122             StepField field = q.getStepField(nm.getField("number"));
123             q.setConstraint(q.createConstraint(field, FieldCompareConstraint.GREATER, new Integer JavaDoc(node.getNumber())));
124             q.addSortOrder(field, SortOrder.ORDER_ASCENDING);
125             q.setMaxNumber(1);
126             NodeIterator i = nm.getList(q).nodeIterator();
127             return i.hasNext() ? i.nextNode() : null;
128         } else {
129             log.debug("Using core.");
130             MMObjectBuilder builder = MMBase.getMMBase().getBuilder(node.getNodeManager().getName());
131             NodeSearchQuery query = new NodeSearchQuery(builder);
132             StepField field = query.getField(builder.getField("number"));
133             BasicFieldValueConstraint cons = new BasicFieldValueConstraint(field, new Integer JavaDoc(node.getNumber()));
134             cons.setOperator(FieldCompareConstraint.GREATER);
135             query.setConstraint(cons);
136             query.addSortOrder(field);
137             query.setMaxNumber(1);
138             try {
139                 java.util.Iterator JavaDoc i = builder.getNodes(query).iterator();
140                 return i.hasNext() ? ((MMObjectNode) i.next()) : null;
141             } catch (Exception JavaDoc e) {
142                 return null;
143             }
144         }
145     }
146
147 }
148
Popular Tags