001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.storage.search.implementation;
011:
012: import java.util.*;
013: import org.mmbase.core.*;
014: import org.mmbase.module.core.MMObjectBuilder;
015: import org.mmbase.module.core.VirtualBuilder;
016: import org.mmbase.module.corebuilders.InsRel;
017: import org.mmbase.storage.search.*;
018:
019: /**
020: * A <code>NodeSearchQuery</code> implements a <code>SearchQuery</code>
021: * that retrieves nodes of one specified nodetype.
022: * <p>
023: * The constructor creates the query with all persistent fields belonging to
024: * the specified nodetype excluding byte[] type fields.
025: * Use {@link #getField(CoreField) getField()} to retrieve each of these fields.
026: * <p>
027: * Once an instance is constructed, it is not possible to add more fields/steps.
028: * Consequently calling one of these methods always results in an
029: * <code>UnsupportedOperationException</code>:
030: * <ul>
031: * <li>{@link #addStep(MMObjectBuilder) addStep()}
032: * <li>{@link #addRelationStep(InsRel,MMObjectBuilder) addRelationStep()}
033: * <li>{@link #addField(Step,CoreField) addField()}
034: * <li>{@link #addAggregatedField(Step,CoreField,int) addAggregatedField()}
035: * </ul>
036: *
037: * @author Rob van Maris
038: * @version $Id: NodeSearchQuery.java,v 1.15 2006/09/08 18:42:59 michiel Exp $
039: * @since MMBase-1.7
040: */
041: public class NodeSearchQuery extends BasicSearchQuery implements
042: SearchQuery {
043:
044: /** Builder for the specified nodetype. */
045: private MMObjectBuilder builder = null;
046:
047: /** Map, maps fields to stepfields. */
048: private Map<CoreField, BasicStepField> stepFields = new HashMap<CoreField, BasicStepField>();
049:
050: /**
051: * Creator.
052: *
053: * @param builder The builder for the nodetype, must not be a
054: * {@link org.mmbase.module.core.VirtualBuilder virtual} builder.
055: * @throws IllegalArgumentException When an invalid argument is supplied.
056: */
057: public NodeSearchQuery(MMObjectBuilder builder) {
058: if (builder == null) {
059: throw new IllegalArgumentException(
060: "Invalid builder value: " + builder);
061: }
062: if (builder instanceof VirtualBuilder) {
063: throw new IllegalArgumentException(
064: "Invalid builder type, because this is a virtual builder: "
065: + builder.getClass().getName());
066: }
067: Step step = super .addStep(builder);
068: addFields(step);
069: this .builder = builder;
070: }
071:
072: /*
073: NodeSearchQuery(SearchQuery searchQuery) {
074: super(searchQuery);
075: List steps = searchQuery.getSteps();
076: if (steps.size() != 1) throw new IllegalArgumentException("Given search-query cannot be a NodeSearchQuery");
077: BasicStep step = (BasicStep) steps.get(0);
078: fields.clear();
079: addFields(step);
080: builder = step.getBuilder();
081: }
082: */
083:
084: protected void copySteps(SearchQuery q) {
085: // no need, can be done by clone
086: }
087:
088: protected void copyFields(SearchQuery q) {
089: // no need, can be done by clone
090: }
091:
092: /**
093: * Returns the stepfield corresponding to the specified field.
094: *
095: * @param field The field.
096: * @return The corresponding stepfield.
097: * @throws IllegalArgumentException When the field is not a
098: * persistent field of the associated nodetype.
099: */
100: public BasicStepField getField(CoreField field) {
101: BasicStepField stepField = stepFields.get(field);
102: if (stepField == null) {
103: // Not found.
104: throw new IllegalArgumentException(
105: "Not a persistent field of builder "
106: + builder.getTableName() + ": " + field);
107: }
108: return stepField;
109: }
110:
111: /**
112: * Returns the builder for the specified nodetype.
113: *
114: * @return The builder.
115: */
116: public MMObjectBuilder getBuilder() {
117: return builder;
118: }
119:
120: // javadoc is inherited
121: public BasicStep addStep(MMObjectBuilder builder) {
122: throw new UnsupportedOperationException(
123: "Adding more steps to NodeSearchQuery not supported.");
124: }
125:
126: // javadoc is inherited
127: public BasicRelationStep addRelationStep(InsRel builder,
128: MMObjectBuilder nextBuilder) {
129: throw new UnsupportedOperationException(
130: "Adding more steps to NodeSearchQuery not supported.");
131: }
132:
133: // javadoc is inherited
134: public BasicStepField addField(Step step, CoreField fieldDefs) {
135: if (builder != null) { // this means: inited already.
136: throw new UnsupportedOperationException(
137: "Adding more fields to NodeSearchQuery not supported.");
138: } else {
139: return super .addField(step, fieldDefs);
140: }
141: }
142:
143: // MM
144: protected void mapField(CoreField field, BasicStepField stepField) {
145: stepFields.put(field, stepField);
146: }
147:
148: // javadoc is inherited
149: public BasicAggregatedField addAggregatedField(Step step,
150: CoreField fieldDefs, int aggregationType) {
151: throw new UnsupportedOperationException(
152: "Adding more fields to NodeSearchQuery not supported.");
153: }
154:
155: }
|