KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > VirtualNodeManager


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.implementation;
12
13 import javax.servlet.*;
14 import java.util.*;
15 import org.mmbase.bridge.*;
16 import org.mmbase.bridge.util.*;
17 import org.mmbase.datatypes.*;
18 import org.mmbase.core.CoreField;
19 import org.mmbase.core.util.Fields;
20 import org.mmbase.module.core.*;
21 import org.mmbase.storage.search.*;
22 import org.mmbase.util.logging.*;
23 import org.mmbase.util.LocalizedString;
24
25 /**
26  * This class represents a virtual node type information object.
27  * It has the same functionality as BasicNodeType, but it's nodes are vitrtual - that is,
28  * constructed based on the results of a search over multiple node managers.
29  * As such, it is not possible to search on this node type, nor to create new nodes.
30  * It's sole function is to provide a type definition for the results of a search.
31  * @author Rob Vermeulen
32  * @author Pierre van Rooden
33  * @version $Id: VirtualNodeManager.java,v 1.45 2006/07/18 13:50:51 michiel Exp $
34  */

35 public class VirtualNodeManager extends AbstractNodeManager implements NodeManager {
36     private static final Logger log = Logging.getLoggerInstance(VirtualNodeManager.class);
37
38     private static final boolean allowNonQueriedFields = true; // not yet configurable
39

40     // field types
41
final protected Map fieldTypes = new HashMap();
42
43     final MMObjectBuilder builder;
44     private SearchQuery query;
45
46     /**
47      * Instantiated a Virtual NodeManager, and tries its best to find reasonable values for the field-types.
48      */

49     VirtualNodeManager(org.mmbase.module.core.VirtualNode node, Cloud cloud) {
50         super(cloud);
51         // determine fields and field types
52
if (node.getBuilder() instanceof VirtualBuilder) {
53             VirtualBuilder virtualBuilder = (VirtualBuilder) node.getBuilder();;
54             Map fields = virtualBuilder.getFields(node);
55             Iterator i = fields.entrySet().iterator();
56             while (i.hasNext()) {
57                 Map.Entry entry = (Map.Entry) i.next();
58                 String JavaDoc fieldName = (String JavaDoc) entry.getKey();
59                 CoreField fd = (CoreField) entry.getValue();
60                 Field ft = new BasicField(fd, this);
61                 fieldTypes.put(fieldName, ft);
62             }
63             builder = null;
64             setStringValue("name", "virtual builder");
65             setStringValue("description", "virtual builder");
66         } else {
67             builder = node.getBuilder();
68             BasicNodeManager.sync(builder, fieldTypes, this);
69         }
70     }
71
72     /**
73      * @since MMBase-1.8
74      */

75     VirtualNodeManager(Query query, Cloud cloud) {
76         super(cloud);
77         if (query instanceof NodeQuery) {
78             builder = BasicCloudContext.mmb.getBuilder(((NodeQuery) query).getNodeManager().getName());
79             BasicNodeManager.sync(builder, fieldTypes, this);
80         } else {
81             builder = null;
82             if (log.isDebugEnabled()) {
83                 log.debug("Creating NodeManager for " + query.toSql());
84             }
85             // fieldTypes map will be filled 'lazily' on first call to getFieldTypes.
86
this.query = query; // query instanceof BasicQuery ? ((BasicQuery ) query).getQuery() : query;
87
setStringValue("name", "cluster builder");
88             setStringValue("description", "cluster builder");
89         }
90
91     }
92
93     /**
94
95     /**
96      * Returns the fieldlist of this nodemanager after making sure the manager is synced with the builder.
97      * @since MMBase-1.8
98      */

99     protected Map getFieldTypes() {
100         if (builder != null) {
101             return fieldTypes;
102         } else {
103             if (query != null) { // means not yet called (lazy loading of fields)
104
// code to solve the fields.
105
Iterator steps = query.getSteps().iterator();
106                 while (steps.hasNext()) {
107                     Step step = (Step) steps.next();
108                     DataType nodeType = DataTypes.getDataType("node");
109                     String JavaDoc name = step.getAlias();
110                     if (name == null) name = step.getTableName();
111                     CoreField fd = Fields.createField(name, Field.TYPE_NODE, Field.TYPE_UNKNOWN, Field.STATE_VIRTUAL, nodeType);
112                     fd.finish();
113                     Field ft = new VirtualNodeManagerField(fd, name);
114                     fieldTypes.put(name, ft);
115
116                     if (allowNonQueriedFields && ! query.isAggregating()) {
117                         /// if hasField returns true also for unqueried fields
118
FieldIterator fields = cloud.getNodeManager(step.getTableName()).getFields().fieldIterator();
119                         while (fields.hasNext()) {
120                             Field f = fields.nextField();
121                             final String JavaDoc fieldName = name + "." + f.getName();
122                             fieldTypes.put(fieldName, new VirtualNodeManagerField(f, fieldName));
123                         }
124                     }
125                 }
126                 if (! allowNonQueriedFields || query.isAggregating()) {
127                     //hasField only returns true for queried fields
128
Iterator fields = query.getFields().iterator();
129                     while(fields.hasNext()) {
130                         StepField field = (StepField) fields.next();
131                         Step step = field.getStep();
132                         Field f = cloud.getNodeManager(step.getTableName()).getField(field.getFieldName());
133                         String JavaDoc name = field.getAlias();
134                         if (name == null) {
135                             name = step.getAlias();
136                             if (name == null) name = step.getTableName();
137                             name += "." + field.getFieldName();
138                         }
139                         final String JavaDoc fieldName = name;
140                         fieldTypes.put(name, new VirtualNodeManagerField(f, fieldName));
141
142                     }
143                 }
144                 query = null;
145             }
146             return fieldTypes;
147         }
148     }
149
150
151
152     public String JavaDoc getGUIName(int plurality, Locale locale) {
153         if (locale == null) locale = cloud.getLocale();
154         if (builder != null) {
155             if (plurality == NodeManager.GUI_SINGULAR) {
156                 return builder.getSingularName(locale.getLanguage());
157             } else {
158                 return builder.getPluralName(locale.getLanguage());
159             }
160         } else {
161             return getName();
162         }
163     }
164
165     public String JavaDoc getName() {
166         return builder == null ? getStringValue("name") : builder.getTableName();
167     }
168     public String JavaDoc getDescription() {
169         return getDescription(null);
170     }
171
172     public String JavaDoc getDescription(Locale locale) {
173         if (builder == null) return getStringValue("description");
174         if (locale == null) locale = cloud.getLocale();
175         return builder.getDescription(locale.getLanguage());
176     }
177
178     /**
179      */

180     private class VirtualNodeManagerField extends FieldWrapper {
181
182         private final String JavaDoc name;
183         VirtualNodeManagerField(Field field, String JavaDoc name) {
184             super(field);
185             this.name = name;
186         }
187         public NodeManager getNodeManager() {
188             return VirtualNodeManager.this;
189         }
190         public String JavaDoc getName() {
191             return name;
192         }
193     }
194
195 }
196
Popular Tags