org.mmbase.bridge.util
Class Queries

java.lang.Object
  extended by org.mmbase.bridge.util.Queries

public abstract class Queries
extends Object

This class contains various utility methods for manipulating and creating query objects. Most essential methods are available on the Query object itself, but too specific or legacy-ish methods are put here.

Since:
MMBase-1.7
Version:
$Id: Queries.java 42855 2010-07-11 11:38:32Z michiel $
Author:
Michiel Meeuwissen
See Also:
Query

Field Summary
static int OPERATOR_BETWEEN
           
static int OPERATOR_IN
           
static int OPERATOR_NULL
           
 
Constructor Summary
Queries()
           
 
Method Summary
static Constraint addConstraint(Query query, Constraint newConstraint)
          Adds a Constraint to the already present constraint (with AND).
static Constraint addConstraints(Query query, String constraints)
          Adds a 'legacy' constraint to the query, i.e.
static List<StepField> addFields(Query query, String fields)
          Adds a number of fields.
static List<Step> addPath(Query query, String path, String searchDirs)
          Adds path of steps to an existing query.
static NodeQuery addRelationFields(NodeQuery q, String role, String relationFields, String sortOrders)
           
static Query addSortedFields(Query q)
          Make sure all sorted fields are queried
static List<SortOrder> addSortOrders(Query query, String sorted, String directions)
          Adds sort orders to the query, using two strings.
static void addStartNodes(Query query, String startNodes)
          Add startNodes to the first step with the correct type to the given query.
static NodeList addToResult(Query q, Node n)
          Explores a query object, and creates a certain new relation object, which would make the given node appear in the query's result.
protected static Object aggregate(Query query, StepField field, int type)
           
static int applyConstraints(Query q, Step step, Node n)
           
protected static Node clusterNode(Relation relation, String relationAlias, Node node)
          Given a 'relation' node
static int compare(Node node1, Node node2, List<SortOrder> sortOrders)
          Does a field-by-field compare of two Node objects, on the fields used to order the nodes.
static int compare(Node node1, Node node2, SortOrder sortOrder)
          Compare tho nodes, with a SortOrder.
static int compare(Object value, Object value2, SortOrder sortOrder)
           
static Constraint copyConstraint(Constraint c, Step sourceStep, Query query, Step step)
          Takes a Constraint of a query, and takes al constraints on 'sourceStep' of it, and copies those Constraints to the given step of the receiving query.
static void copySortOrders(List<SortOrder> sortOrders, Step sourceStep, Query query, Step step)
          Copies SortOrders to a given step of another query.
static int count(Query query)
          Takes the query, and does a count with the same constraints (so ignoring 'offset' and 'max')
static Constraint createAgeConstraint(NodeQuery q, int minAge, int maxAge)
          As createAgeConstraint(Query, Step, int int, int), but NodeQuery's have an exceptional step, which can be taken as the default.
static Constraint createAgeConstraint(Query query, Step step, int minAge, int maxAge)
          Create a constraint for the query which limits to results to nodes of a certain age, based on its number and the 'daymarkers' table.
static Constraint createConstraint(Query query, String fieldName, int operator, Object value)
          Defaulting version of createConstraint(Query, String, int, Object, Object, boolean, int).
static Constraint createConstraint(Query query, String fieldName, int operator, Object value, Object value2, boolean caseSensitive)
          Defaulting version of createConstraint(Query, String, int, Object, Object, boolean, int).
static Constraint createConstraint(Query query, String fieldName, int operator, Object originalValue, Object value2, boolean caseSensitive, int datePart)
          Creates a constraint smartly, depending on the type of the field, the value is cast to the right type, and the right type of constraint is created.
static Constraint createMakeEmptyConstraint(Query q)
          Creates a constraint that would make the result of the query q empty.
static NodeQuery createNodeQuery(Node node)
          Returns the NodeQuery returning the given Node.
static Query createQuery(Cloud cloud, String startNodes, String nodePath, String fields, String constraints, String orderby, String directions, String searchDir, boolean distinct)
          Creates a Query object using arguments for Cloud.getList(String, String, String, String, String, String, String, boolean) (this function is of course implemented using this utility).
static NodeQuery createRelatedNodesQuery(Node node, NodeManager otherNodeManager, String role, String direction)
          Returns a query to find the nodes related to the given node.
static NodeQuery createRelationNodesQuery(Node node, NodeManager otherNodeManager, String role, String direction)
          Returns a query to find the relations nodes of the given node.
static NodeQuery createRelationNodesQuery(Node node, Node otherNode, String role, String direction)
          Returns a query to find the relations nodes between two given nodes.
static Query fixQuery(Query query, Map<Integer,Integer> resolution)
          Fixes the nodes of the steps of query.
static Comparator<Node> getComparator(Query q)
          Returns a Node comparator associated with the SortOrders of the given Query (See SearchQuery.getSortOrders()).
protected static Object getCompareValue(int fieldType, int operator, Object value)
          Used in implementation of createConstraint
protected static Object getCompareValue(int fieldType, int operator, Object value, int datePart, Cloud cloud)
          Used in implementation of createConstraint
static List<FieldConstraint> getConstraints(Constraint constraint, Step step)
           
static int getDateTimePart(String s)
          Creates a part constant for use by createConstraint
protected static int getDayMark(Cloud cloud, int age)
           
static String getFieldAlias(StepField sf)
          Returns the string which must be used for Node.getValue(java.lang.String) in the result set of the query of the given StepField.
protected static Number getNumberValue(String stringValue)
          Used in implementation of createConstraint
static int getOperator(String s)
          Creates a operator constant for use by createConstraint
static NodeList getRelatedNodes(Node node, NodeManager otherNodeManager, String role, String direction, String relationFields, String sortOrders)
          Queries a list of cluster nodes, using a NodeQuery (so al fields of one step are available), plus some fields of the relation step.
static List<Node> getRelatedNodesInTransaction(Node startNode, NodeQuery q)
          Returns the related nodes of a certain node (defined by the query), including the one that where related to it in the current transaction.
static NodeList getRelations(Query q, Node n)
          Explores a query object, returns the relations the node has within the query.
static int getRelationStepDirection(String search)
          Translates a string to a search direction constant.
static int getSortOrder(String dir)
          Converts a String to a SortOrder constant
static Object getSortOrderFieldValue(Node node, SortOrder sortOrder)
          Obtains a value for the field of a sortorder from a given node.
static Node getStartNode(NodeQuery nq, Cloud cloud)
          This method is the counterpart of setStartNode(org.mmbase.bridge.NodeQuery, org.mmbase.bridge.Node) and receives 'the' startnode from the Query (which may not be committed).
static NodeQuery getSubQuery(Query q, Node node, int step)
          Defaulting version getSubQuery(Query, Node, int, int).
static NodeQuery getSubQuery(Query q, Node node, int elementStep, int lastStep)
          Given a Query, and Node, produces a new query, where the first part of the query is replaced by the Node.
static Object max(Query query, StepField field)
           
static Object min(Query query, StepField field)
           
static boolean removeConstraint(Query q, Constraint cons)
           
static String removeDigits(String complete)
          Returns substring of given string without the leading digits (used in 'paths')
static NodeList removeFromResult(Query q, Node n)
          Deletes the relations with a node from a queries resulting relations list.
static int reorderResult(NodeQuery q, List<Integer> desiredOrder)
          Will 'reorder' the result of a query with a sort order.
static Step searchStep(List<Step> steps, String stepAlias)
          Searches a list of Steps for a step with a certain name.
static void setStartNode(NodeQuery query, Node startNode)
          This puts the node as 'startnode' in the query (propably a 'related nodes' query.
static Query sortUniquely(Query q)
          Add a sortorder (DESCENDING) on al the'number' fields of the query, on which there is not yet a sortorder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPERATOR_BETWEEN

public static final int OPERATOR_BETWEEN
See Also:
Constant Field Values

OPERATOR_IN

public static final int OPERATOR_IN
See Also:
Constant Field Values

OPERATOR_NULL

public static final int OPERATOR_NULL
See Also:
Constant Field Values
Constructor Detail

Queries

public Queries()
Method Detail

getRelationStepDirection

public static int getRelationStepDirection(String search)
Translates a string to a search direction constant. If the string is null then 'BOTH' is returned.

Parameters:
search - string representation of the searchdir constant
Returns:
Searchdir constant as in RelationStep
See Also:
The same function, only with another return value if String is null

createQuery

public static Query createQuery(Cloud cloud,
                                String startNodes,
                                String nodePath,
                                String fields,
                                String constraints,
                                String orderby,
                                String directions,
                                String searchDir,
                                boolean distinct)
Creates a Query object using arguments for Cloud.getList(String, String, String, String, String, String, String, boolean) (this function is of course implemented using this utility). This is useful to convert (legacy) code which uses getList, but you want to use new Query features without rewriting the complete thing. It can also be simply handy to specify things as Strings.

Parameters:
cloud -
startNodes -
nodePath -
fields -
constraints -
orderby -
directions -
searchDir -
distinct -
Returns:
New query object
To Do:
Should this method be part of Cloud itself?

addConstraints

public static Constraint addConstraints(Query query,
                                        String constraints)
Adds a 'legacy' constraint to the query, i.e. constraint(s) represented by a string. Alreading existing constraints remain ('AND' is used).

Parameters:
query - query to add constraint to
constraints - string representation of constraints
Returns:
The new constraint, or null if nothing changed added.

addConstraint

public static Constraint addConstraint(Query query,
                                       Constraint newConstraint)
Adds a Constraint to the already present constraint (with AND).

Parameters:
query - query to add the constraint to
newConstraint - constraint to add
Returns:
The new constraint.

getConstraints

public static List<FieldConstraint> getConstraints(Constraint constraint,
                                                   Step step)
Since:
MMBase-1.9.4

removeConstraint

public static boolean removeConstraint(Query q,
                                       Constraint cons)
Since:
MMBase-1.9.4

getOperator

public static int getOperator(String s)
Creates a operator constant for use by createConstraint

Parameters:
s - String representation of operator
Returns:
FieldCompareConstraint operator constant
See Also:
createConstraint(Query, String, int, Object), createConstraint(Query, String, int, Object, Object, boolean)

getDateTimePart

public static int getDateTimePart(String s)
Creates a part constant for use by createConstraint

Parameters:
s - String representation of a datetime part
Returns:
FieldValueDateConstraint part constant
See Also:
createConstraint(Query, String, int, Object, Object, boolean, int)

getNumberValue

protected static Number getNumberValue(String stringValue)
                                throws BridgeException
Used in implementation of createConstraint

Parameters:
stringValue - string representation of a number
Returns:
Number object
Throws:
BridgeException - when failed to convert the string

getCompareValue

protected static Object getCompareValue(int fieldType,
                                        int operator,
                                        Object value)
Used in implementation of createConstraint

Parameters:
fieldType - Field Type constant (@link Field)
operator - Compare operator
value - value to convert
Returns:
new Compare value

getCompareValue

protected static Object getCompareValue(int fieldType,
                                        int operator,
                                        Object value,
                                        int datePart,
                                        Cloud cloud)
Used in implementation of createConstraint

Parameters:
fieldType - Field Type constant (@link Field)
operator - Compare operator
value - value to convert
cloud - The cloud may be used to pass locale sensitive properties which may be needed for comparisons (locales, timezones)
Returns:
new Compare value
Since:
MMBase-1.8.2

createConstraint

public static Constraint createConstraint(Query query,
                                          String fieldName,
                                          int operator,
                                          Object value)
Defaulting version of createConstraint(Query, String, int, Object, Object, boolean, int). Casesensitivity defaults to false, value2 to null (so 'BETWEEN' cannot be used), datePart set to -1 (so no date part comparison)

Parameters:
query - The query to create the constraint for
fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number'
operator - The operator to use. This constant can be produces from a string using getOperator(String).
value - The value to compare with, which must be of the right type. If field is number it might also be an alias.
Returns:
The new constraint, or null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set)

createConstraint

public static Constraint createConstraint(Query query,
                                          String fieldName,
                                          int operator,
                                          Object value,
                                          Object value2,
                                          boolean caseSensitive)
Defaulting version of createConstraint(Query, String, int, Object, Object, boolean, int). DatePart set to -1 (so no date part comparison)

Parameters:
query - The query to create the constraint for
fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number'
operator - The operator to use. This constant can be produces from a string using getOperator(String).
value - The value to compare with, which must be of the right type. If field is number it might also be an alias.
value2 - The other value (only relevant if operator is BETWEEN, the only terniary operator)
caseSensitive - Whether it should happen case sensitively (not relevant for number fields)
Returns:
The new constraint, or null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set)

createConstraint

public static Constraint createConstraint(Query query,
                                          String fieldName,
                                          int operator,
                                          Object originalValue,
                                          Object value2,
                                          boolean caseSensitive,
                                          int datePart)
Creates a constraint smartly, depending on the type of the field, the value is cast to the right type, and the right type of constraint is created. This is used in taglib implementation, but could be useful more generally.

Parameters:
query - The query to create the constraint for
fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number'
operator - The operator to use. This constant can be produces from a string using getOperator(String).
originalValue - The value to compare with, which must be of the right type. If field is number it might also be an alias.
value2 - The other value (only relevant if operator is BETWEEN, the only terniary operator)
caseSensitive - Whether it should happen case sensitively (not relevant for number fields)
datePart - The part of a DATETIME value that is to be checked
Returns:
The new constraint, or null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set)

copyConstraint

public static Constraint copyConstraint(Constraint c,
                                        Step sourceStep,
                                        Query query,
                                        Step step)
Takes a Constraint of a query, and takes al constraints on 'sourceStep' of it, and copies those Constraints to the given step of the receiving query. Constraints on different steps then the given 'sourceStep' are ignored. CompositeConstraints cause recursion and would work too (but same limitation are valid for the childs).

Parameters:
c - The constrain to be copied (for example the result of sourceQuery.getConstraint()).
sourceStep - The step in the 'source' query.
query - The receiving query
step - The step of the receiving query which must 'receive' the sort orders.
Returns:
The new constraint or null
Throws:
IllegalArgumentException - If the given constraint is not compatible with the given step.
UnsupportedOperationException - If CompareFieldsConstraints or LegacyConstraints are encountered.
Since:
MMBase-1.7.1
See Also:
Functions are similar

copySortOrders

public static void copySortOrders(List<SortOrder> sortOrders,
                                  Step sourceStep,
                                  Query query,
                                  Step step)
Copies SortOrders to a given step of another query. SortOrders which do not sort the given 'sourceStep' are ignored.

Parameters:
sortOrders - A list of SortOrders (for example the result of sourceQuery.getSortOrders()).
sourceStep - The step in the 'source' query.
query - The receiving query
step - The step of the receiving query which must 'receive' the sort orders.
Since:
MMBase-1.7.1

getSortOrder

public static int getSortOrder(String dir)
Converts a String to a SortOrder constant

Parameters:
dir - string representation of direction of sortorder
Returns:
SortOrder constant
Since:
MMBase-1.7.1

addSortOrders

public static List<SortOrder> addSortOrders(Query query,
                                            String sorted,
                                            String directions)
Adds sort orders to the query, using two strings. Like in 'getList' of Cloud. Several tag-attributes need this.

Parameters:
query - query to add the sortorders to
sorted - string with comma-separated fields
directions - string with comma-separated directions
Returns:
The new sort orders
To Do:
implement for normal query.

removeDigits

public static String removeDigits(String complete)
Returns substring of given string without the leading digits (used in 'paths')

Parameters:
complete - string with leading digits
Returns:
string with digits removed

addPath

public static List<Step> addPath(Query query,
                                 String path,
                                 String searchDirs)
Adds path of steps to an existing query. The query may contain steps already. Per step also the 'search direction' may be specified.

Parameters:
query - extend this query
path - create steps from this path
searchDirs - add steps with these relation directions
Returns:
The new steps.

addFields

public static List<StepField> addFields(Query query,
                                        String fields)
Adds a number of fields. Fields is represented as a comma separated string.

Parameters:
query - The query where the fields should be added to
fields - a comma separated string of fields
Returns:
The new stepfields

addStartNodes

public static void addStartNodes(Query query,
                                 String startNodes)
Add startNodes to the first step with the correct type to the given query. The nodes are identified by a String, which could be prefixed with a step-alias, if you want to add the nodes to another then this found step. Furthermore may the nodes by identified by their alias, if they have one.

Parameters:
query - query to add the startnodes
startNodes - start nodes
See Also:
(this is essentially a 'bridge' version of the startnodes part)

count

public static int count(Query query)
Takes the query, and does a count with the same constraints (so ignoring 'offset' and 'max')

Parameters:
query - query as base for the count
Returns:
number of results

aggregate

protected static Object aggregate(Query query,
                                  StepField field,
                                  int type)
Since:
MMBase-1.8

min

public static Object min(Query query,
                         StepField field)
Since:
MMBase-1.8

max

public static Object max(Query query,
                         StepField field)
Since:
MMBase-1.8

searchStep

public static Step searchStep(List<Step> steps,
                              String stepAlias)
Searches a list of Steps for a step with a certain name. (alias or tableName)

Parameters:
steps - steps to search through
stepAlias - alias to search for
Returns:
The Step if found, otherwise null
Throws:
ClassCastException - if list does not contain only Steps

createNodeQuery

public static NodeQuery createNodeQuery(Node node)
Returns the NodeQuery returning the given Node. This query itself is not very useful, because you already have its result (the node), but it is convenient as a base query for many other goals. If the node is uncommited, it cannot be queried, and the node query returning all nodes from the currect type will be returned.

Parameters:
node - Node to create the query from
Returns:
A new NodeQuery object

createRelatedNodesQuery

public static NodeQuery createRelatedNodesQuery(Node node,
                                                NodeManager otherNodeManager,
                                                String role,
                                                String direction)
Returns a query to find the nodes related to the given node.

Parameters:
node - start node
otherNodeManager - node manager on the other side of the relation
role - role of the relation
direction - direction of the relation
Returns:
A new NodeQuery object

createRelationNodesQuery

public static NodeQuery createRelationNodesQuery(Node node,
                                                 NodeManager otherNodeManager,
                                                 String role,
                                                 String direction)
Returns a query to find the relations nodes of the given node.

Parameters:
node - start node
otherNodeManager - node manager on the other side of the relation
role - role of the relation
direction - direction of the relation
Returns:
A new NodeQuery object

createRelationNodesQuery

public static NodeQuery createRelationNodesQuery(Node node,
                                                 Node otherNode,
                                                 String role,
                                                 String direction)
Returns a query to find the relations nodes between two given nodes. To test whether to nodes are related you can use e.g.: if (Queries.count(Queries.createRelationNodesQuery(node1, node2, "posrel", null)) > 0) { .. }

Parameters:
node - start node
otherNode - node on the other side of the relation
role - role of the relation
direction - direction of the relation
Returns:
A new NodeQuery object
Since:
MMBase-1.8

getRelatedNodes

public static NodeList getRelatedNodes(Node node,
                                       NodeManager otherNodeManager,
                                       String role,
                                       String direction,
                                       String relationFields,
                                       String sortOrders)
Queries a list of cluster nodes, using a NodeQuery (so al fields of one step are available), plus some fields of the relation step. The actual node can be got from the node cache by doing a Node.getNodeValue(java.lang.String) with the NodeList.NODESTEP_PROPERTY property. The fields of the relation can be got by prefixing their names by the role and a dot (as normal in multilevel results).

Parameters:
node - start node
otherNodeManager - node manager on the other side of the relation
role - role of the relation
direction - direction of the relation
relationFields - Comma separated string of fields which must be queried from the relation step
sortOrders - Comma separated string of fields of sortorders, or the empty string or null So, this methods is targeted at the use of 'posrel' and similar fields, because sorting on other fields isn't possible right now.
Since:
MMBase-1.8
To Do:
EXPERIMENTAL

addRelationFields

public static NodeQuery addRelationFields(NodeQuery q,
                                          String role,
                                          String relationFields,
                                          String sortOrders)
Since:
MMBase-1.8

sortUniquely

public static Query sortUniquely(Query q)
Add a sortorder (DESCENDING) on al the'number' fields of the query, on which there is not yet a sortorder. This ensures that the query result is ordered uniquely.

Parameters:
q - query to change
Returns:
The changed Query

addSortedFields

public static Query addSortedFields(Query q)
Make sure all sorted fields are queried

Since:
MMBase-1.8

getSortOrderFieldValue

public static Object getSortOrderFieldValue(Node node,
                                            SortOrder sortOrder)
Obtains a value for the field of a sortorder from a given node. Used to set constraints based on sortorder.

Since:
MMBase-1.8

compare

public static int compare(Node node1,
                          Node node2,
                          SortOrder sortOrder)
Compare tho nodes, with a SortOrder. This determins where a certain node is smaller or bigger than a certain other node, with respect to some SortOrder. This is used by compare(Node, Node, List) If node2 is only 'longer' then node1, but otherwise equal, then it is bigger.

Since:
MMBase-1.8

compare

public static int compare(Object value,
                          Object value2,
                          SortOrder sortOrder)
Since:
MMBase-1.8

compare

public static int compare(Node node1,
                          Node node2,
                          List<SortOrder> sortOrders)
Does a field-by-field compare of two Node objects, on the fields used to order the nodes. This is used to determine whether a node comes after or before another, in a certain query result.

Returns:
-1 if node1 is smaller than node 2, 0 if both nodes are equals, and +1 is node 1 is greater than node 2.
Since:
MMBase-1.8

getComparator

public static Comparator<Node> getComparator(Query q)
Returns a Node comparator associated with the SortOrders of the given Query (See SearchQuery.getSortOrders()).

Since:
MMBase-1.9.2

addToResult

public static NodeList addToResult(Query q,
                                   Node n)
Explores a query object, and creates a certain new relation object, which would make the given node appear in the query's result. You can read this as 'the query object is a related nodes query, and is used to contain information about the relation (role, startnodes)'. This currently is also the only implemented part of this method.

Returns:
Newly created node(s)
Throws:
UnsupportedOperationException - If it cannot be determined how the node should be related.
NullPointerException - if q or n is null
Since:
MMBase-1.8.6

applyConstraints

public static int applyConstraints(Query q,
                                   Step step,
                                   Node n)
Since:
MMBase-1.9.4

removeFromResult

public static NodeList removeFromResult(Query q,
                                        Node n)
Deletes the relations with a node from a queries resulting relations list. If multiple relations to a node exist all get removed.

Parameters:
q - query from which resulting list the node should be removed from
n - node to remove
Returns:
Removed relation nodes
Throws:
UnsupportedOperationException - If it cannot be determined how the node should be related.
Since:
MMBase-1.8.6

getRelations

public static NodeList getRelations(Query q,
                                    Node n)
Explores a query object, returns the relations the node has within the query.

Parameters:
q - query that constructs the list
n - node to which relations are related
Returns:
The relation nodes
Throws:
UnsupportedOperationException - If it cannot be determined how the node is related.
NullPointerException - if q or n is null
Since:
MMBase-1.9.1

getFieldAlias

public static String getFieldAlias(StepField sf)
Returns the string which must be used for Node.getValue(java.lang.String) in the result set of the query of the given StepField.

Since:
MMBase-1.8.7

getDayMark

protected static int getDayMark(Cloud cloud,
                                int age)
Since:
MMBase-1.9

createAgeConstraint

public static Constraint createAgeConstraint(NodeQuery q,
                                             int minAge,
                                             int maxAge)
As createAgeConstraint(Query, Step, int int, int), but NodeQuery's have an exceptional step, which can be taken as the default.

Since:
MMBase-1.9

createAgeConstraint

public static Constraint createAgeConstraint(Query query,
                                             Step step,
                                             int minAge,
                                             int maxAge)
Create a constraint for the query which limits to results to nodes of a certain age, based on its number and the 'daymarkers' table.

Parameters:
minAge - Minimal age in days (or -1 if it does not matter)
maxAge - Maximal age in days (or -1 if it does not matter)
Returns:
a new Constraint or null
Since:
MMBase-1.9

createMakeEmptyConstraint

public static Constraint createMakeEmptyConstraint(Query q)
Creates a constraint that would make the result of the query q empty. Currently implemented by comparing the number field of the first step to a negative integer, but this may be done otherwise.

Since:
MMBase-1.8.7

fixQuery

public static Query fixQuery(Query query,
                             Map<Integer,Integer> resolution)
Fixes the nodes of the steps of query.

Since:
MMBase-1.9.3

reorderResult

public static int reorderResult(NodeQuery q,
                                List<Integer> desiredOrder)
Will 'reorder' the result of a query with a sort order. E.g.:
        NodeQuery q = Queries.createRelatedNodesQuery(node, cloud.getNodeManager("news"), "posrel", "destination");
        Queries.addRelationFields(q, "posrel", "pos", "UP");
        Queries.reorderResult(q, nodeNumbers);
        
If the values of 'pos' are equal to start with, they will be fixed too, and will have an increasing order. If all values are different already, values will simply be interchanged. A test-case for this is in QueriesTest#reorderResult.

Parameters:
q - The query which defines the existing order. The cloud of this object will be used, unless this is a committed transaction, then the parent Cloud.getNonTransactionalCloud() will be used to create the sub-transction.
desiredOrder - The node numbers of the nodes in the query result of q. These are the actual nodes, not the nodes which define the order (like the posrel)
Returns:
The number of alterations which are done. Depends on the used algorithm (currently Bubble sort). 0 if the list was correctly ordered allready.
Since:
MMBase-1.9.2

clusterNode

protected static Node clusterNode(Relation relation,
                                  String relationAlias,
                                  Node node)
Given a 'relation' node


setStartNode

public static void setStartNode(NodeQuery query,
                                Node startNode)
This puts the node as 'startnode' in the query (propably a 'related nodes' query. If the node is uncommited yet, this cannot be done with the normal Query.addNode(org.mmbase.storage.search.Step, org.mmbase.bridge.Node) method. The information will be put in the query in another way then, so that at least getStartNode(org.mmbase.bridge.NodeQuery, org.mmbase.bridge.Cloud) will give the correct result. If the Query object is changed such that it can contain uncommitted nodes, then this method can be made deprecated.

Since:
MMBase-1.9.2

getStartNode

public static Node getStartNode(NodeQuery nq,
                                Cloud cloud)
This method is the counterpart of setStartNode(org.mmbase.bridge.NodeQuery, org.mmbase.bridge.Node) and receives 'the' startnode from the Query (which may not be committed).

Since:
MMBase-1.9.2

getRelatedNodesInTransaction

public static List<Node> getRelatedNodesInTransaction(Node startNode,
                                                      NodeQuery q)
Returns the related nodes of a certain node (defined by the query), including the one that where related to it in the current transaction. This code understands how the MMBase 'transactions' work. If the transaction implementation changes, (which seems a good idea) this will get broken, but well, we'll fix this too, then, hopefully.

Since:
MMBase-1.9.2

getSubQuery

public static NodeQuery getSubQuery(Query q,
                                    Node node,
                                    int step)
Defaulting version getSubQuery(Query, Node, int, int). The last argument is replaced with q.getSteps().size();

Since:
MMBase-1.9.3

getSubQuery

public static NodeQuery getSubQuery(Query q,
                                    Node node,
                                    int elementStep,
                                    int lastStep)
Given a Query, and Node, produces a new query, where the first part of the query is replaced by the Node. So, e.g. if you have a query mags,posrel,news,posrel,images and a news node, you can feed this query, and the node into this method (with step is 2), to produce a query news,posrel,images, where the start node is the given news node. All constraints, nodes, and aliases on the remainings steps are copied. Sortorders are only preserved in steps <= elementStep. The query is a NodeQuery, where the NodeStep is the (normal) step after the node.

Parameters:
q - The query to base the new query
node - The node to start the query with. If this is null then step must be 0, and the original query will be returned, only converted to a NodeQuery for the first step.
elementStep - The element step. The first non-relation step after the first step where the node is fixed.
lastStep - On default the remainder of the query is copied. It you need to truncate it from the back to, use this argument.
Throws:
ClassCastException - if step + 1 is not a relationstep. (This restriction may perhaps be removed).
Since:
MMBase-1.9.4


MMBase 2.0-SNAPSHOT - null