0001: /*
0002:
0003: This software is OSI Certified Open Source Software.
0004: OSI Certified is a certification mark of the Open Source Initiative.
0005:
0006: The license (Mozilla version 1.0) can be read at the MMBase site.
0007: See http://www.MMBase.org/license
0008:
0009: */
0010: package org.mmbase.bridge.util;
0011:
0012: import java.util.*;
0013:
0014: import org.mmbase.bridge.*;
0015: import org.mmbase.storage.search.*;
0016:
0017: /**
0018: * This utility provides methods to easily create and execute queries in the bridge.
0019: * These methods are replacements for frequently used code snippets which are present in
0020: * code which uses the query objects in the mmbase bridge.
0021: * The implementation of these methods can also be used as documentation how to use the
0022: * search query api.
0023: *
0024: * @author Nico Klasens
0025: * @version $Id: SearchUtil.java,v 1.19 2007/11/25 17:57:07 nklasens Exp $
0026: */
0027: public class SearchUtil {
0028:
0029: /** A Search direction of relations in queries */
0030: public static final String SOURCE = "SOURCE";
0031: /** A Search direction of relations in queries */
0032: public static final String DESTINATION = "DESTINATION";
0033:
0034: private SearchUtil() {
0035: // Utility
0036: }
0037:
0038: /**
0039: * Search for a node which contains a field value. The value is matched on equality (exact match).
0040: * For a string field type the match is case-insensitive.
0041: * If multiple nodes are found with the same value then the first node is returned
0042: *
0043: * @param cloud - user cloud to search in
0044: * @param managerName - name of manager to search with
0045: * @param fieldname - name of field to search with
0046: * @param value - value to search for in the field
0047: * @return Single node which matches the field value
0048: */
0049: public static Node findNode(Cloud cloud, String managerName,
0050: String fieldname, String value) {
0051: return findNode(cloud, managerName, fieldname, value, null,
0052: null);
0053: }
0054:
0055: /**
0056: * Search for a node which contains a field value. The value is matched on equality (exact match).
0057: * For a string field type the match is case-insensitive.
0058: * If multiple nodes are found with the same value then the first node is returned
0059: *
0060: * @param cloud - user cloud to search in
0061: * @param managerName - name of manager to search with
0062: * @param fieldname - name of field to search with
0063: * @param value - value to search for in the field
0064: * @param sortName - name of field to sort on.
0065: * @return Single node which matches the field value
0066: */
0067: public static Node findNode(Cloud cloud, String managerName,
0068: String fieldname, String value, String sortName) {
0069: return findNode(cloud, managerName, fieldname, value, sortName,
0070: null);
0071: }
0072:
0073: /**
0074: * Retrieve the first node based on the sort field
0075: *
0076: * @param cloud - user cloud to search in
0077: * @param managerName - name of manager to search with
0078: * @param sortName - name of field to sort on.
0079: * @return Single node
0080: */
0081: public static Node findOrderedNode(Cloud cloud, String managerName,
0082: String sortName) {
0083: return findNode(cloud, managerName, null, null, sortName, null);
0084: }
0085:
0086: /**
0087: * Retrieve the first node based on the sort field
0088: *
0089: * @param cloud - user cloud to search in
0090: * @param managerName - name of manager to search with
0091: * @param sortName - name of field to sort on.
0092: * @param sortDirection - direction of the sort (UP, DOWN)
0093: * @return Single node
0094: */
0095: public static Node findOrderedNode(Cloud cloud, String managerName,
0096: String sortName, String sortDirection) {
0097: return findNode(cloud, managerName, null, null, sortName,
0098: sortDirection);
0099: }
0100:
0101: /**
0102: * Search for a node which contains a field value. The value is matched on equality (exact match).
0103: * For a string field type the match is case-insensitive.
0104: * If multiple nodes are found with the same value then the first node is returned
0105: *
0106: * @param cloud - user cloud to search in
0107: * @param managerName - name of manager to search with
0108: * @param fieldname - name of field to search with
0109: * @param value - value to search for in the field
0110: * @param sortName - name of field to sort on.
0111: * @param sortDirection - direction of the sort (UP, DOWN)
0112: * @return Single node which matches the field value
0113: */
0114: public static Node findNode(Cloud cloud, String managerName,
0115: String fieldname, String value, String sortName,
0116: String sortDirection) {
0117: NodeList list = findNodeList(cloud, managerName, fieldname,
0118: value, sortName, sortDirection);
0119: if (list.size() > 0) {
0120: return list.getNode(0);
0121: }
0122: return null;
0123: }
0124:
0125: /**
0126: * Search for nodes which contain a field value. The value is matched on equality (exact match).
0127: * For a string field type the match is case-insensitive.
0128: *
0129: * @param cloud - user cloud to search in
0130: * @param managerName - name of manager to search with
0131: * @return list of nodes
0132: */
0133: public static NodeList findNodeList(Cloud cloud, String managerName) {
0134: return findNodeList(cloud, managerName, null, null, null, null);
0135: }
0136:
0137: /**
0138: * Search for nodes which contain a field value. The value is matched on equality (exact match).
0139: * For a string field type the match is case-insensitive.
0140: *
0141: * @param cloud - user cloud to search in
0142: * @param managerName - name of manager to search with
0143: * @param fieldname - name of field to search with
0144: * @param value - value to search for in the field
0145: * @return list of nodes which match the field value
0146: */
0147: public static NodeList findNodeList(Cloud cloud,
0148: String managerName, String fieldname, Object value) {
0149: return findNodeList(cloud, managerName, fieldname, value, null,
0150: null);
0151: }
0152:
0153: /**
0154: * Search for nodes which contain a field value. The value is matched on equality (exact match).
0155: * For a string field type the match is case-insensitive.
0156: *
0157: * @param cloud - user cloud to search in
0158: * @param managerName - name of manager to search with
0159: * @param fieldname - name of field to search with
0160: * @param value - value to search for in the field
0161: * @param sortName - name of field to sort on.
0162: * @return list of nodes which match the field value
0163: */
0164: public static NodeList findNodeList(Cloud cloud,
0165: String managerName, String fieldname, Object value,
0166: String sortName) {
0167: return findNodeList(cloud, managerName, fieldname, value,
0168: sortName, null);
0169: }
0170:
0171: /**
0172: * Retrieve nodes which are sorted on the field name
0173: *
0174: * @param cloud - user cloud to search in
0175: * @param managerName - name of manager to search with
0176: * @param sortName - name of field to sort on.
0177: * @return list of nodes
0178: */
0179: public static NodeList findOrderedNodeList(Cloud cloud,
0180: String managerName, String sortName) {
0181: return findNodeList(cloud, managerName, null, null, sortName,
0182: null);
0183: }
0184:
0185: /**
0186: * Retrieve nodes which are sorted on the field name
0187: *
0188: * @param cloud - user cloud to search in
0189: * @param managerName - name of manager to search with
0190: * @param sortName - name of field to sort on.
0191: * @param sortDirection - direction of the sort (UP, DOWN)
0192: * @return list of nodes
0193: */
0194: public static NodeList findOrderedNodeList(Cloud cloud,
0195: String managerName, String sortName, String sortDirection) {
0196: return findNodeList(cloud, managerName, null, null, sortName,
0197: sortDirection);
0198: }
0199:
0200: /**
0201: * Retrieve nodes which contain a field value. The value is matched on equality (exact match).
0202: * For a string field type the match is case-insensitive.
0203: *
0204: * @param cloud - user cloud to search in
0205: * @param managerName - name of manager to search with
0206: * @param fieldname - name of field to search with
0207: * @param value - value to search for in the field
0208: * @param sortName - name of field to sort on.
0209: * @param sortDirection - direction of the sort (UP, DOWN)
0210: * @return list of nodes which match the field value
0211: */
0212: public static NodeList findNodeList(Cloud cloud,
0213: String managerName, String fieldname, Object value,
0214: String sortName, String sortDirection) {
0215: NodeManager manager = cloud.getNodeManager(managerName);
0216: NodeQuery query = manager.createQuery();
0217: addEqualConstraint(query, manager, fieldname, value);
0218: if (!isEmptyOrWhitespace(sortName)) {
0219: addSortOrder(query, manager, sortName, sortDirection);
0220: }
0221: return manager.getList(query);
0222: }
0223:
0224: /**
0225: * Search for a node which is related to the parent node.
0226: * If multiple nodes are found then the first node is returned
0227: *
0228: * @param parent - node to start the search from
0229: * @param managerName - name of manager to search with
0230: * @param role - name of relation (relation role in the mmbase system)
0231: * @return Single node
0232: */
0233: public static Node findRelatedNode(Node parent, String managerName,
0234: String role) {
0235: return findRelatedNode(parent, managerName, role, null, null,
0236: null, null);
0237: }
0238:
0239: /**
0240: * Search for a node which is related to the parent node and contains a field value.
0241: * The value is matched on equality (exact match).
0242: * For a string field type the match is case-insensitive.
0243: * If multiple nodes are found with the same value then the first node is returned
0244: *
0245: * @param parent - node to start the search from
0246: * @param managerName - name of manager to search with
0247: * @param role - name of relation (relation role in the mmbase system)
0248: * @param fieldname - name of field to search with
0249: * @param value - value to search for in the field
0250: * @return Single node which matches the field value
0251: */
0252: public static Node findRelatedNode(Node parent, String managerName,
0253: String role, String fieldname, Object value) {
0254: return findRelatedNode(parent, managerName, role, fieldname,
0255: value, null, null);
0256: }
0257:
0258: /**
0259: * Search for a node which is related to the parent node and contains a field value.
0260: * The value is matched on equality (exact match).
0261: * For a string field type the match is case-insensitive.
0262: * If multiple nodes are found with the same value then the first node is returned
0263: *
0264: * @param parent - node to start the search from
0265: * @param managerName - name of manager to search with
0266: * @param role - name of relation (relation role in the mmbase system)
0267: * @param fieldname - name of field to search with
0268: * @param value - value to search for in the field
0269: * @param sortName - name of field to sort on.
0270: * @return Single node which matches the field value
0271: */
0272: public static Node findRelatedNode(Node parent, String managerName,
0273: String role, String fieldname, Object value, String sortName) {
0274: return findRelatedNode(parent, managerName, role, fieldname,
0275: value, sortName, null);
0276: }
0277:
0278: /**
0279: * Retrieve a node which is related to the parent node and is sorted in a field.
0280: * If multiple nodes are found with the same value then the first node is returned
0281: *
0282: * @param parent - node to start the search from
0283: * @param managerName - name of manager to search with
0284: * @param role - name of relation (relation role in the mmbase system)
0285: * @param sortName - name of field to sort on.
0286: * @return Single node
0287: */
0288: public static Node findRelatedOrderedNode(Node parent,
0289: String managerName, String role, String sortName) {
0290: return findRelatedNode(parent, managerName, role, null, null,
0291: sortName, null);
0292: }
0293:
0294: /**
0295: * Retrieve a node which is related to the parent node and is sorted in a field.
0296: * If multiple nodes are found with the same value then the first node is returned
0297: *
0298: * @param parent - node to start the search from
0299: * @param managerName - name of manager to search with
0300: * @param role - name of relation (relation role in the mmbase system)
0301: * @param sortName - name of field to sort on.
0302: * @param sortDirection - direction of the sort (UP, DOWN)
0303: * @return Single node
0304: */
0305: public static Node findRelatedOrderedNode(Node parent,
0306: String managerName, String role, String sortName,
0307: String sortDirection) {
0308: return findRelatedNode(parent, managerName, role, null, null,
0309: sortName, sortDirection);
0310: }
0311:
0312: /**
0313: * Search for a node which is related to the parent node and contains a field value.
0314: * The value is matched on equality (exact match).
0315: * For a string field type the match is case-insensitive.
0316: * If multiple nodes are found with the same value then the first node is returned
0317: *
0318: * @param parent - node to start the search from
0319: * @param managerName - name of manager to search with
0320: * @param role - name of relation (relation role in the mmbase system)
0321: * @param fieldname - name of field to search with
0322: * @param value - value to search for in the field
0323: * @param sortName - name of field to sort on.
0324: * @param sortDirection - direction of the sort (UP, DOWN)
0325: * @return Single node which matches the field value
0326: */
0327: public static Node findRelatedNode(Node parent, String managerName,
0328: String role, String fieldname, Object value,
0329: String sortName, String sortDirection) {
0330: NodeList list = findRelatedNodeList(parent, managerName, role,
0331: fieldname, value, sortName, sortDirection);
0332: if (list.size() > 0) {
0333: return list.getNode(0);
0334: }
0335: return null;
0336: }
0337:
0338: /**
0339: * Search for nodes which are related to the parent node.
0340: *
0341: * @param parent - node to start the search from
0342: * @param managerName - name of manager to search with
0343: * @param role - name of relation (relation role in the mmbase system)
0344: * @return a list of nodes
0345: */
0346: public static NodeList findRelatedNodeList(Node parent,
0347: String managerName, String role) {
0348: return findRelatedNodeList(parent, managerName, role, null,
0349: null, null, null);
0350: }
0351:
0352: /**
0353: * Search for nodes which are related to the parent node and contain a field value.
0354: * The value is matched on equality (exact match).
0355: * For a string field type the match is case-insensitive.
0356: *
0357: * @param parent - node to start the search from
0358: * @param managerName - name of manager to search with
0359: * @param role - name of relation (relation role in the mmbase system)
0360: * @param fieldname - name of field to search with
0361: * @param value - value to search for in the field
0362: * @return a list of nodes which match the field value
0363: */
0364: public static NodeList findRelatedNodeList(Node parent,
0365: String managerName, String role, String fieldname,
0366: Object value) {
0367: return findRelatedNodeList(parent, managerName, role,
0368: fieldname, value, null, null);
0369: }
0370:
0371: /**
0372: * Search for nodes which are related to the parent node and contain a field value.
0373: * The value is matched on equality (exact match).
0374: * For a string field type the match is case-insensitive.
0375: *
0376: * @param parent - node to start the search from
0377: * @param managerName - name of manager to search with
0378: * @param role - name of relation (relation role in the mmbase system)
0379: * @param fieldname - name of field to search with
0380: * @param value - value to search for in the field
0381: * @param sortName - name of field to sort on.
0382: * @return a list of nodes which match the field value
0383: */
0384: public static NodeList findRelatedNodeList(Node parent,
0385: String managerName, String role, String fieldname,
0386: Object value, String sortName) {
0387: return findRelatedNodeList(parent, managerName, role,
0388: fieldname, value, sortName, null);
0389: }
0390:
0391: /**
0392: * Search for nodes which are related to the parent node and contain a field value.
0393: * The value is matched on equality (exact match).
0394: * For a string field type the match is case-insensitive.
0395: *
0396: * @param parent - node to start the search from
0397: * @param managerName - name of manager to search with
0398: * @param role - name of relation (relation role in the mmbase system)
0399: * @param sortName - name of field to sort on.
0400: * @return a list of nodes
0401: */
0402: public static NodeList findRelatedOrderedNodeList(Node parent,
0403: String managerName, String role, String sortName) {
0404: return findRelatedNodeList(parent, managerName, role, null,
0405: null, sortName, null);
0406: }
0407:
0408: /**
0409: * Search for nodes which are related to the parent node and contain a field value.
0410: * The value is matched on equality (exact match).
0411: * For a string field type the match is case-insensitive.
0412: *
0413: * @param parent - node to start the search from
0414: * @param managerName - name of manager to search with
0415: * @param role - name of relation (relation role in the mmbase system)
0416: * @param sortName - name of field to sort on.
0417: * @param sortDirection - direction of the sort (UP, DOWN)
0418: * @return a list of nodes
0419: */
0420: public static NodeList findRelatedOrderedNodeList(Node parent,
0421: String managerName, String role, String sortName,
0422: String sortDirection) {
0423: return findRelatedNodeList(parent, managerName, role, null,
0424: null, sortName, sortDirection);
0425: }
0426:
0427: /**
0428: * Search for nodes which are related to the parent node and contain a field value.
0429: * The value is matched on equality (exact match).
0430: * For a string field type the match is case-insensitive.
0431: *
0432: * @param parent - node to start the search from
0433: * @param managerName - name of manager to search with
0434: * @param role - name of relation (relation role in the mmbase system)
0435: * @param fieldname - name of field to search with
0436: * @param value - value to search for in the field
0437: * @param sortName - name of field to sort on.
0438: * @param sortDirection - direction of the sort (UP, DOWN)
0439: * @return a list of nodes which match the field value
0440: */
0441: public static NodeList findRelatedNodeList(Node parent,
0442: String managerName, String role, String fieldname,
0443: Object value, String sortName, String sortDirection) {
0444: NodeQuery query = createRelatedNodeListQuery(parent,
0445: managerName, role, fieldname, value, sortName,
0446: sortDirection);
0447: return query.getNodeManager().getList(query);
0448: }
0449:
0450: /**
0451: * Search for nodes which are related to the parent node and contain a field value.
0452: * The value is matched on equality (exact match).
0453: * For a string field type the match is case-insensitive.
0454: *
0455: * @param parent - node to start the search from
0456: * @param managerName - name of manager to search with
0457: * @param role - name of relation (relation role in the mmbase system)
0458: * @param fieldname - name of field to search with
0459: * @param value - value to search for in the field
0460: * @param sortName - name of field to sort on.
0461: * @param sortDirection - direction of the sort (UP, DOWN)
0462: * @param searchdir - direction of the relation (source, destination, both)
0463: * @return a list of nodes which match the field value
0464: */
0465: public static NodeList findRelatedNodeList(Node parent,
0466: String managerName, String role, String fieldname,
0467: Object value, String sortName, String sortDirection,
0468: String searchdir) {
0469: NodeQuery query = createRelatedNodeListQuery(parent,
0470: managerName, role, fieldname, value, sortName,
0471: sortDirection, searchdir);
0472: return query.getNodeManager().getList(query);
0473: }
0474:
0475: /**
0476: * Create a query for a list of nodes which are related to the parent node and contain a field value.
0477: * The value is matched on equality (exact match).
0478: * For a string field type the match is case-insensitive.
0479: * Relation direction is destination
0480: *
0481: * @param parent - node to start the search from
0482: * @param managerName - name of manager to search with
0483: * @param role - name of relation (relation role in the mmbase system)
0484: * @param fieldname - name of field to search with
0485: * @param value - value to search for in the field
0486: * @param sortName - name of field to sort on.
0487: * @param sortDirection - direction of the sort (UP, DOWN)
0488: * @return a query for a list of nodes which match the field value
0489: */
0490: public static NodeQuery createRelatedNodeListQuery(Node parent,
0491: String managerName, String role, String fieldname,
0492: Object value, String sortName, String sortDirection) {
0493: return createRelatedNodeListQuery(parent, managerName, role,
0494: fieldname, value, sortName, sortDirection, DESTINATION);
0495: }
0496:
0497: /**
0498: * Create a query for a list of nodes which are related to the parent node and contain a field value.
0499: * The value is matched on equality (exact match).
0500: * For a string field type the match is case-insensitive.
0501: *
0502: * @param parent - node to start the search from
0503: * @param managerName - name of manager to search with
0504: * @param role - name of relation (relation role in the mmbase system)
0505: * @param fieldname - name of field to search with
0506: * @param value - value to search for in the field
0507: * @param sortName - name of field to sort on.
0508: * @param sortDirection - direction of the sort (UP, DOWN)
0509: * @param searchdir - direction of the relation (source, destination, both)
0510: * @return a query for a list of nodes which match the field value
0511: */
0512: public static NodeQuery createRelatedNodeListQuery(Node parent,
0513: String managerName, String role, String fieldname,
0514: Object value, String sortName, String sortDirection,
0515: String searchdir) {
0516: NodeQuery query = createRelatedNodeListQuery(parent,
0517: managerName, role, searchdir);
0518: addFeatures(query, parent, managerName, role, fieldname, value,
0519: sortName, sortDirection, searchdir);
0520: return query;
0521: }
0522:
0523: /**
0524: * Create a query for a list of nodes which are related to the parent node and contain a field value.
0525: * The value is matched on equality (exact match).
0526: * For a string field type the match is case-insensitive.
0527: *
0528: * @param parent - node to start the search from
0529: * @param managerName - name of manager to search with
0530: * @param role - name of relation (relation role in the mmbase system)
0531: * @return a query for a list of nodes
0532: */
0533: public static NodeQuery createRelatedNodeListQuery(Node parent,
0534: String managerName, String role) {
0535: return createRelatedNodeListQuery(parent, managerName, role,
0536: DESTINATION);
0537: }
0538:
0539: /**
0540: * Create a query for a list of nodes which are related to the parent node and contain a field value.
0541: * The value is matched on equality (exact match).
0542: * For a string field type the match is case-insensitive.
0543: *
0544: * @param parent - node to start the search from
0545: * @param managerName - name of manager to search with
0546: * @param role - name of relation (relation role in the mmbase system)
0547: * @param searchdir - direction of the relation (source, destination, both)
0548: * @return a query for a list of nodes
0549: */
0550: public static NodeQuery createRelatedNodeListQuery(Node parent,
0551: String managerName, String role, String searchdir) {
0552: NodeManager manager;
0553: if (isEmptyOrWhitespace(managerName)) {
0554: manager = parent.getCloud().getNodeManager("object");
0555: } else {
0556: manager = parent.getCloud().getNodeManager(managerName);
0557: }
0558:
0559: NodeQuery query = parent.getCloud().createNodeQuery();
0560: Step step1 = query.addStep(parent.getNodeManager());
0561: query.addNode(step1, parent);
0562:
0563: RelationStep step2 = query.addRelationStep(manager, role,
0564: searchdir);
0565: Step step3 = step2.getNext();
0566: query.setNodeStep(step3); // makes it ready for use as NodeQuery
0567: return query;
0568: }
0569:
0570: /**
0571: * Create a query for a list of nodes which are related to the parent node and contain a field value.
0572: * The value is matched on equality (exact match).
0573: * For a string field type the match is case-insensitive.
0574: *
0575: * @param parentNodes - nodes to start the search from
0576: * @param managerName - name of manager to search with
0577: * @param role - name of relation (relation role in the mmbase system)
0578: * @return a query for a list of nodes
0579: */
0580: public static NodeQuery createRelatedNodeListQuery(
0581: NodeList parentNodes, String managerName, String role) {
0582: return createRelatedNodeListQuery(parentNodes, managerName,
0583: role, DESTINATION);
0584: }
0585:
0586: /**
0587: * Create a query for a list of nodes which are related to the parent node and contain a field value.
0588: * The value is matched on equality (exact match).
0589: * For a string field type the match is case-insensitive.
0590: *
0591: * @param parentNodes - nodes to start the search from
0592: * @param managerName - name of manager to search with
0593: * @param role - name of relation (relation role in the mmbase system)
0594: * @param searchdir - direction of the relation (source, destination, both)
0595: * @return a query for a list of nodes
0596: */
0597: public static NodeQuery createRelatedNodeListQuery(
0598: NodeList parentNodes, String managerName, String role,
0599: String searchdir) {
0600: if (parentNodes.isEmpty()) {
0601: throw new IllegalArgumentException(
0602: "paretnodes is empty. should be at leat one");
0603: }
0604: Node parent = parentNodes.getNode(0);
0605:
0606: NodeManager manager = parent.getCloud().getNodeManager(
0607: managerName);
0608:
0609: NodeQuery query = parent.getCloud().createNodeQuery();
0610: Step step1 = query.addStep(parent.getNodeManager());
0611: for (Node element : parentNodes) {
0612: Node parentNode = element;
0613: query.addNode(step1, parentNode);
0614: }
0615:
0616: RelationStep step2 = query.addRelationStep(manager, role,
0617: searchdir);
0618: Step step3 = step2.getNext();
0619: query.setNodeStep(step3); // makes it ready for use as NodeQuery
0620: return query;
0621: }
0622:
0623: /**
0624: * Search for a list of relations which are related to the parent node.
0625: *
0626: * @param parent - node to start the search from
0627: * @param managerName - name of manager to search with
0628: * @param role - name of relation (relation role in the mmbase system)
0629: * @param sortName - name of field to sort on.
0630: * @param sortDirection - direction of the sort (UP, DOWN)
0631: * @return a list of relations
0632: */
0633: public static RelationList findRelations(Node parent,
0634: String managerName, String role, String sortName,
0635: String sortDirection) {
0636: return findRelations(parent, managerName, role, sortName,
0637: sortDirection, DESTINATION);
0638: }
0639:
0640: /**
0641: * Search for a list of relations which are related to the parent node.
0642: *
0643: * @param parent - node to start the search from
0644: * @param managerName - name of manager to search with
0645: * @param role - name of relation (relation role in the mmbase system)
0646: * @param sortName - name of field to sort on.
0647: * @param sortDirection - direction of the sort (UP, DOWN)
0648: * @param searchdir - direction of the relation (source, destination, both)
0649: * @return a list of relations
0650: */
0651: public static RelationList findRelations(Node parent,
0652: String managerName, String role, String sortName,
0653: String sortDirection, String searchdir) {
0654: NodeManager manager = parent.getCloud().getNodeManager(
0655: managerName);
0656: NodeQuery query = parent.getCloud().createNodeQuery();
0657: Step step1 = query.addStep(parent.getNodeManager());
0658: query.addNode(step1, parent);
0659: RelationStep step = query.addRelationStep(manager, role,
0660: searchdir);
0661: query.setNodeStep(step);
0662:
0663: if (!isEmptyOrWhitespace(sortName)) {
0664: RelationManager relationManager = parent.getCloud()
0665: .getRelationManager(parent.getNodeManager(),
0666: manager, role);
0667: addRelationSortOrder(query, relationManager, sortName,
0668: sortDirection);
0669: }
0670:
0671: NodeManager nm = query.getNodeManager();
0672: return new CollectionRelationList(nm.getList(query), parent
0673: .getCloud());
0674: }
0675:
0676: /**
0677: * Finds the relation-nodes between two specified nodes
0678: * @param source - source node
0679: * @param destination - destination node
0680: * @param role - name of relation (relation role in the mmbase system)
0681: * @param searchdir - direction of the relation (source, destination, both)
0682: * @return a list of relations
0683: *
0684: * @since MMBase-1.8.5
0685: */
0686: public static RelationList findRelations(Node source,
0687: Node destination, String role, String searchdir) {
0688:
0689: Cloud cloud = source.getCloud();
0690: RelationManager relationManager = role == null ? cloud
0691: .getNodeManager("insrel").toRelationManager() : cloud
0692: .getRelationManager(source.getNodeManager(),
0693: destination.getNodeManager(), role);
0694:
0695: NodeQuery q = relationManager.createQuery();
0696: if (DESTINATION.equalsIgnoreCase(searchdir)) {
0697: Queries.addConstraint(q, Queries.createConstraint(q,
0698: "snumber", FieldCompareConstraint.EQUAL, source));
0699: Queries.addConstraint(q, Queries.createConstraint(q,
0700: "dnumber", FieldCompareConstraint.EQUAL,
0701: destination));
0702: } else if (SOURCE.equalsIgnoreCase(searchdir)) {
0703: Queries.addConstraint(q, Queries.createConstraint(q,
0704: "dnumber", FieldCompareConstraint.EQUAL, source));
0705: Queries.addConstraint(q, Queries.createConstraint(q,
0706: "snumber", FieldCompareConstraint.EQUAL,
0707: destination));
0708: } else {
0709: Queries.addConstraint(q, Queries.createConstraint(q,
0710: "snumber", FieldCompareConstraint.EQUAL, source));
0711: Queries.addConstraint(q, Queries.createConstraint(q,
0712: "dnumber", FieldCompareConstraint.EQUAL,
0713: destination));
0714: if (Queries.count(q) == 0) {
0715: RelationManager relationManager2 = role == null ? cloud
0716: .getNodeManager("insrel").toRelationManager()
0717: : cloud.getRelationManager(destination
0718: .getNodeManager(), source
0719: .getNodeManager(), role);
0720: NodeQuery q2 = relationManager2.createQuery();
0721: Queries.addConstraint(q2, Queries
0722: .createConstraint(q2, "dnumber",
0723: FieldCompareConstraint.EQUAL, source));
0724: Queries.addConstraint(q2, Queries.createConstraint(q2,
0725: "snumber", FieldCompareConstraint.EQUAL,
0726: destination));
0727: if (Queries.count(q2) > 0) {
0728: q = q2;
0729: relationManager = relationManager2;
0730: }
0731: }
0732: }
0733: return new CollectionRelationList(relationManager.getList(q),
0734: cloud);
0735: }
0736:
0737: /**
0738: * Add constraints and sort orders to a query.
0739: * The field value is matched on equality (exact match).
0740: * For a string field type the match is case-insensitive.
0741: *
0742: * @param query - the query to add the constrains and sort orders to
0743: * @param parent - node to start the search from
0744: * @param managerName - name of manager to search with
0745: * @param role - name of relation (relation role in the mmbase system)
0746: * @param fieldname - name of field to search with
0747: * @param value - value to search for in the field
0748: * @param sortName - name of field to sort on.
0749: * @param sortDirection - direction of the sort (UP, DOWN)
0750: */
0751: public static void addFeatures(NodeQuery query, Node parent,
0752: String managerName, String role, String fieldname,
0753: Object value, String sortName, String sortDirection) {
0754: addFeatures(query, parent, managerName, role, fieldname, value,
0755: sortName, sortDirection, DESTINATION);
0756: }
0757:
0758: /**
0759: * Add constraints and sort orders to a query.
0760: * The field value is matched on equality (exact match).
0761: * For a string field type the match is case-insensitive.
0762: *
0763: * @param query - the query to add the constrains and sort orders to
0764: * @param parent - node to start the search from
0765: * @param managerName - name of manager to search with
0766: * @param role - name of relation (relation role in the mmbase system)
0767: * @param fieldname - name of field to search with
0768: * @param value - value to search for in the field
0769: * @param sortName - name of field to sort on.
0770: * @param sortDirection - direction of the sort (UP, DOWN)
0771: * @param searchdir - direction of the relation (source, destination, both)
0772: */
0773: public static void addFeatures(NodeQuery query, Node parent,
0774: String managerName, String role, String fieldname,
0775: Object value, String sortName, String sortDirection,
0776: String searchdir) {
0777: NodeManager manager = null;
0778: if (!isEmptyOrWhitespace(managerName)) {
0779: manager = parent.getCloud().getNodeManager(managerName);
0780: addEqualConstraint(query, manager, fieldname, value);
0781: }
0782:
0783: if (!isEmptyOrWhitespace(sortName)) {
0784: if (sortName.startsWith(role + ".")) {
0785: String sortField = sortName
0786: .substring(role.length() + 1);
0787: RelationManager relationManager = null;
0788: if (managerName == null) {
0789: if (hasAllowedRelation(parent, role, searchdir)) {
0790: relationManager = parent.getCloud()
0791: .getRelationManager(role);
0792: }
0793: } else {
0794: if (SOURCE.equals(searchdir)) {
0795: relationManager = parent.getCloud()
0796: .getRelationManager(manager,
0797: parent.getNodeManager(), role);
0798: } else {
0799: relationManager = parent.getCloud()
0800: .getRelationManager(
0801: parent.getNodeManager(),
0802: manager, role);
0803: }
0804: }
0805: if (relationManager == null) {
0806: throw new IllegalArgumentException("Relation "
0807: + role + " not possible between "
0808: + parent.getNodeManager().getName()
0809: + " and " + managerName);
0810: }
0811: addRelationSortOrder(query, relationManager, sortField,
0812: sortDirection);
0813: } else {
0814: if (manager != null) {
0815: addSortOrder(query, manager, sortName,
0816: sortDirection);
0817: }
0818: }
0819: }
0820: }
0821:
0822: /**
0823: * Check to see if a relation is allowed from the parent node
0824: * @param parent - node to start from
0825: * @param role - name of relation (relation role in the mmbase system)
0826: * @param searchdir - direction of the relation (source, destination, both)
0827: * @return true when relation is allowed
0828: */
0829: public static boolean hasAllowedRelation(Node parent, String role,
0830: String searchdir) {
0831: return parent.getNodeManager().getAllowedRelations(
0832: (String) null, role, searchdir) != null;
0833: }
0834:
0835: /**
0836: * Add a sort order to a query for a field of the manager.
0837: *
0838: * @param query - the query to add the sort order to
0839: * @param manager - manager of the sort field
0840: * @param sortName - name of field to sort on.
0841: * @param sortDirection - direction of the sort (UP, DOWN)
0842: */
0843: public static void addSortOrder(NodeQuery query,
0844: NodeManager manager, String sortName, String sortDirection) {
0845: StepField sf = query.getStepField(manager.getField(sortName));
0846: addSortOrder(query, sf, sortDirection);
0847: }
0848:
0849: /**
0850: * Add a sort order to a query for a field of the relation manager.
0851: *
0852: * @param query - the query to add the sort order to
0853: * @param role - relation manager (relation role in the mmbase system)
0854: * @param sortName - name of field to sort on.
0855: * @param sortDirection - direction of the sort (UP, DOWN)
0856: */
0857: public static void addRelationSortOrder(NodeQuery query,
0858: RelationManager role, String sortName, String sortDirection) {
0859: Field field = role.getField(sortName);
0860: StepField sf = query.createStepField(query.getStep(role
0861: .getForwardRole()), field);
0862: addSortOrder(query, sf, sortDirection);
0863: }
0864:
0865: /**
0866: * Add a sort order to a query for a field of the manager.
0867: *
0868: * @param query - the query to add the sort order to
0869: * @param sf - StepField of the sort order
0870: * @param sortName - name of field to sort on.
0871: * @param sortDirection - direction of the sort (UP, DOWN)
0872: */
0873: public static void addSortOrder(NodeQuery query, StepField sf,
0874: String sortDirection) {
0875: int dir = SortOrder.ORDER_ASCENDING;
0876: if ("DOWN".equalsIgnoreCase(sortDirection)) {
0877: dir = SortOrder.ORDER_DESCENDING;
0878: }
0879: query.addSortOrder(sf, dir);
0880: }
0881:
0882: /**
0883: * Add a constraint to a query
0884: * The value is matched on equality (exact match).
0885: * For a string field type the match is case-insensitive.
0886: *
0887: * @param query - the query to add the constraint to
0888: * @param manager - manager of the constraint field
0889: * @param fieldname - name of field to search with
0890: * @param value - value to search for in the field
0891: */
0892: public static void addEqualConstraint(NodeQuery query,
0893: NodeManager manager, String fieldname, String value) {
0894: FieldValueConstraint constraint = createEqualConstraint(query,
0895: manager, fieldname, value);
0896: addConstraint(query, constraint);
0897: }
0898:
0899: /**
0900: * Add a constraint to a query
0901: * The value is matched on equality (exact match).
0902: * For a string field type the match is case-insensitive.
0903: *
0904: * @param query - the query to add the constraint to
0905: * @param manager - manager of the constraint field
0906: * @param fieldname - name of field to search with
0907: * @param value - value to search for in the field
0908: */
0909: public static void addEqualConstraint(Query query,
0910: NodeManager manager, String fieldname, String value) {
0911: FieldValueConstraint constraint = createEqualConstraint(query,
0912: manager, fieldname, value);
0913: addConstraint(query, constraint);
0914: }
0915:
0916: /**
0917: * Create a constraint for a query
0918: * The value is matched on equality (exact match).
0919: * For a string field type the match is case-insensitive.
0920: *
0921: * @param query - the query to add the constraint to
0922: * @param manager - manager of the constraint field
0923: * @param fieldname - name of field to search with
0924: * @param value - value to search for in the field
0925: * @return constraint which matches a field value
0926: */
0927: public static FieldValueConstraint createEqualConstraint(
0928: NodeQuery query, NodeManager manager, String fieldname,
0929: String value) {
0930: Field keyField = manager.getField(fieldname);
0931: FieldValueConstraint constraint = createEqualConstraint(query,
0932: keyField, value);
0933: return constraint;
0934: }
0935:
0936: /**
0937: * Create a constraint for a query
0938: * The value is matched on equality (exact match).
0939: * For a string field type the match is case-insensitive.
0940: *
0941: * @param query - the query to add the constraint to
0942: * @param manager - manager of the constraint field
0943: * @param fieldname - name of field to search with
0944: * @param value - value to search for in the field
0945: * @return constraint which matches a field value
0946: */
0947: public static FieldValueConstraint createEqualConstraint(
0948: Query query, NodeManager manager, String fieldname,
0949: String value) {
0950: Field keyField = manager.getField(fieldname);
0951: FieldValueConstraint constraint = createEqualConstraint(query,
0952: keyField, value);
0953: return constraint;
0954: }
0955:
0956: /**
0957: * Add a constraint to a query
0958: * The value is matched on equality (exact match).
0959: *
0960: * @param query - the query to add the constraint to
0961: * @param manager - manager of the constraint field
0962: * @param fieldname - name of field to search with
0963: * @param value - value to search for in the field
0964: */
0965: public static void addEqualConstraint(NodeQuery query,
0966: NodeManager manager, String fieldname, Integer value) {
0967: FieldValueConstraint constraint = createEqualConstraint(query,
0968: manager, fieldname, value);
0969: addConstraint(query, constraint);
0970: }
0971:
0972: /**
0973: * Add a constraint to a query
0974: * The value is matched on equality (exact match).
0975: *
0976: * @param query - the query to add the constraint to
0977: * @param manager - manager of the constraint field
0978: * @param fieldname - name of field to search with
0979: * @param value - value to search for in the field
0980: */
0981: public static void addEqualConstraint(Query query,
0982: NodeManager manager, String fieldname, Integer value) {
0983: FieldValueConstraint constraint = createEqualConstraint(query,
0984: manager, fieldname, value);
0985: addConstraint(query, constraint);
0986: }
0987:
0988: /**
0989: * Create a constraint for a query
0990: * The value is matched on equality (exact match).
0991: *
0992: * @param query - the query to add the constraint to
0993: * @param manager - manager of the constraint field
0994: * @param fieldname - name of field to search with
0995: * @param value - value to search for in the field
0996: * @return constraint which matches a field value
0997: */
0998: public static FieldValueConstraint createEqualConstraint(
0999: NodeQuery query, NodeManager manager, String fieldname,
1000: Integer value) {
1001: Field keyField = manager.getField(fieldname);
1002: return createEqualConstraint(query, keyField, value);
1003: }
1004:
1005: /**
1006: * Create a constraint for a query
1007: * The value is matched on equality (exact match).
1008: *
1009: * @param query - the query to add the constraint to
1010: * @param manager - manager of the constraint field
1011: * @param fieldname - name of field to search with
1012: * @param value - value to search for in the field
1013: * @return constraint which matches a field value
1014: */
1015: public static FieldValueConstraint createEqualConstraint(
1016: Query query, NodeManager manager, String fieldname,
1017: Integer value) {
1018: Field keyField = manager.getField(fieldname);
1019: return createEqualConstraint(query, keyField, value);
1020: }
1021:
1022: /**
1023: * Add a constraint to a query
1024: * The value is matched on equality (exact match).
1025: *
1026: * @param query - the query to add the constraint to
1027: * @param manager - manager of the constraint field
1028: * @param fieldname - name of field to search with
1029: * @param value - value to search for in the field
1030: */
1031: public static void addEqualConstraint(NodeQuery query,
1032: NodeManager manager, String fieldname, Boolean value) {
1033: FieldValueConstraint constraint = createEqualConstraint(query,
1034: manager, fieldname, value);
1035: addConstraint(query, constraint);
1036: }
1037:
1038: /**
1039: * Add a constraint to a query
1040: * The value is matched on equality (exact match).
1041: *
1042: * @param query - the query to add the constraint to
1043: * @param manager - manager of the constraint field
1044: * @param fieldname - name of field to search with
1045: * @param value - value to search for in the field
1046: */
1047: public static void addEqualConstraint(Query query,
1048: NodeManager manager, String fieldname, Boolean value) {
1049: FieldValueConstraint constraint = createEqualConstraint(query,
1050: manager, fieldname, value);
1051: addConstraint(query, constraint);
1052: }
1053:
1054: /**
1055: * Create a constraint for a query
1056: * The value is matched on equality (exact match).
1057: *
1058: * @param query - the query to add the constraint to
1059: * @param manager - manager of the constraint field
1060: * @param fieldname - name of field to search with
1061: * @param value - value to search for in the field
1062: * @return constraint which matches a field value
1063: */
1064: public static FieldValueConstraint createEqualConstraint(
1065: NodeQuery query, NodeManager manager, String fieldname,
1066: Boolean value) {
1067: Field keyField = manager.getField(fieldname);
1068: return createEqualConstraint(query, keyField, value);
1069: }
1070:
1071: /**
1072: * Create a constraint for a query
1073: * The value is matched on equality (exact match).
1074: *
1075: * @param query - the query to add the constraint to
1076: * @param manager - manager of the constraint field
1077: * @param fieldname - name of field to search with
1078: * @param value - value to search for in the field
1079: * @return constraint which matches a field value
1080: */
1081: public static FieldValueConstraint createEqualConstraint(
1082: Query query, NodeManager manager, String fieldname,
1083: Boolean value) {
1084: Field keyField = manager.getField(fieldname);
1085: return createEqualConstraint(query, keyField, value);
1086: }
1087:
1088: /**
1089: * Add a constraint to a query
1090: * The value is matched on equality (exact match).
1091: * For a string field type the match is case-insensitive.
1092: *
1093: * @param query - the query to add the constraint to
1094: * @param manager - manager of the constraint field
1095: * @param fieldname - name of field to search with
1096: * @param value - value to search for in the field
1097: */
1098: public static void addEqualConstraint(NodeQuery query,
1099: NodeManager manager, String fieldname, Object value) {
1100: if (!isEmptyOrWhitespace(fieldname)) {
1101: if (value instanceof String) {
1102: addEqualConstraint(query, manager, fieldname,
1103: (String) value);
1104: } else if (value instanceof Integer) {
1105: addEqualConstraint(query, manager, fieldname,
1106: (Integer) value);
1107: } else if (value instanceof Boolean) {
1108: addEqualConstraint(query, manager, fieldname,
1109: (Boolean) value);
1110: } else {
1111: FieldValueConstraint constraint = createEqualConstraint(
1112: query, manager, fieldname, value);
1113: addConstraint(query, constraint);
1114: }
1115: }
1116: }
1117:
1118: /**
1119: * Add a constraint to a query
1120: * The value is matched on equality (exact match).
1121: * For a string field type the match is case-insensitive.
1122: *
1123: * @param query - the query to add the constraint to
1124: * @param manager - manager of the constraint field
1125: * @param fieldname - name of field to search with
1126: * @param value - value to search for in the field
1127: */
1128: public static void addEqualConstraint(Query query,
1129: NodeManager manager, String fieldname, Object value) {
1130: FieldValueConstraint constraint = createEqualConstraint(query,
1131: manager, fieldname, value);
1132: addConstraint(query, constraint);
1133: }
1134:
1135: /**
1136: * Create a constraint for a query
1137: * The value is matched on equality (exact match).
1138: * For a string field type the match is case-insensitive.
1139: *
1140: * @param query - the query to add the constraint to
1141: * @param manager - manager of the constraint field
1142: * @param fieldname - name of field to search with
1143: * @param value - value to search for in the field
1144: * @return constraint which matches a field value
1145: */
1146: public static FieldValueConstraint createEqualConstraint(
1147: NodeQuery query, NodeManager manager, String fieldname,
1148: Object value) {
1149: Field keyField = manager.getField(fieldname);
1150: return createEqualConstraint(query, keyField, value);
1151: }
1152:
1153: /**
1154: * Create a constraint for a query
1155: * The value is matched on equality (exact match).
1156: * For a string field type the match is case-insensitive.
1157: *
1158: * @param query - the query to add the constraint to
1159: * @param manager - manager of the constraint field
1160: * @param fieldname - name of field to search with
1161: * @param value - value to search for in the field
1162: * @return constraint which matches a field value
1163: */
1164: public static FieldValueConstraint createEqualConstraint(
1165: Query query, NodeManager manager, String fieldname,
1166: Object value) {
1167: Field keyField = manager.getField(fieldname);
1168: return createEqualConstraint(query, keyField, value);
1169: }
1170:
1171: /**
1172: * Add a constraint to a query
1173: * The value is matched on equality (exact match).
1174: * For a string field type the match is case-insensitive.
1175: *
1176: * @param query - the query to add the constraint to
1177: * @param field - the constraint field
1178: * @param value - value to search for in the field
1179: */
1180: public static void addEqualConstraint(NodeQuery query, Field field,
1181: String value) {
1182: FieldValueConstraint constraint = createEqualConstraint(query,
1183: field, value);
1184: addConstraint(query, constraint);
1185: }
1186:
1187: /**
1188: * Add a constraint to a query
1189: * The value is matched on equality (exact match).
1190: * For a string field type the match is case-insensitive.
1191: *
1192: * @param query - the query to add the constraint to
1193: * @param field - the constraint field
1194: * @param value - value to search for in the field
1195: */
1196: public static void addEqualConstraint(Query query, Field field,
1197: String value) {
1198: FieldValueConstraint constraint = createEqualConstraint(query,
1199: field, value);
1200: addConstraint(query, constraint);
1201: }
1202:
1203: /**
1204: * Create a constraint for a query
1205: * The value is matched on equality (exact match).
1206: * For a string field type the match is case-insensitive.
1207: *
1208: * @param query - the query to add the constraint to
1209: * @param field - the constraint field
1210: * @param value - value to search for in the field
1211: * @return constraint which matches a field value
1212: */
1213: public static FieldValueConstraint createEqualConstraint(
1214: NodeQuery query, Field field, String value) {
1215: boolean caseSensitive = false;
1216: return createEqualConstraint(query, field, value, caseSensitive);
1217: }
1218:
1219: /**
1220: * Create a constraint for a query
1221: * The value is matched on equality (exact match).
1222: *
1223: * @param query - the query to add the constraint to
1224: * @param field - the constraint field
1225: * @param value - value to search for in the field
1226: * @param caseSensitive - case sensitivity of the value
1227: * @return constraint which matches a field value
1228: */
1229: public static FieldValueConstraint createEqualConstraint(
1230: NodeQuery query, Field field, String value,
1231: boolean caseSensitive) {
1232: FieldValueConstraint constraint = query.createConstraint(query
1233: .getStepField(field), FieldCompareConstraint.EQUAL,
1234: value);
1235: query.setCaseSensitive(constraint, caseSensitive);
1236: return constraint;
1237: }
1238:
1239: /**
1240: * Create a constraint for a query
1241: * The value is matched on equality (exact match).
1242: * For a string field type the match is case-insensitive.
1243: *
1244: * @param query - the query to add the constraint to
1245: * @param field - the constraint field
1246: * @param value - value to search for in the field
1247: * @return constraint which matches a field value
1248: */
1249: public static FieldValueConstraint createEqualConstraint(
1250: Query query, Field field, String value) {
1251: boolean caseSensitive = false;
1252: return createEqualConstraint(query, field, value, caseSensitive);
1253: }
1254:
1255: /**
1256: * Create a constraint for a query
1257: * The value is matched on equality (exact match).
1258: *
1259: * @param query - the query to add the constraint to
1260: * @param field - the constraint field
1261: * @param value - value to search for in the field
1262: * @param caseSensitive - case sensitivity of the value
1263: * @return constraint which matches a field value
1264: */
1265: public static FieldValueConstraint createEqualConstraint(
1266: Query query, Field field, String value,
1267: boolean caseSensitive) {
1268: StepField equalsField = findField(query, field);
1269: FieldValueConstraint constraint = query.createConstraint(
1270: equalsField, FieldCompareConstraint.EQUAL, value);
1271: query.setCaseSensitive(constraint, caseSensitive);
1272: return constraint;
1273: }
1274:
1275: /**
1276: * Add a constraint to a query
1277: * The value is matched on equality (exact match).
1278: *
1279: * @param query - the query to add the constraint to
1280: * @param field - the constraint field
1281: * @param value - value to search for in the field
1282: */
1283: public static void addEqualConstraint(NodeQuery query, Field field,
1284: Integer value) {
1285: FieldValueConstraint constraint = createEqualConstraint(query,
1286: field, value);
1287: addConstraint(query, constraint);
1288: }
1289:
1290: /**
1291: * Add a constraint to a query
1292: * The value is matched on equality (exact match).
1293: *
1294: * @param query - the query to add the constraint to
1295: * @param field - the constraint field
1296: * @param value - value to search for in the field
1297: */
1298: public static void addEqualConstraint(Query query, Field field,
1299: Integer value) {
1300: FieldValueConstraint constraint = createEqualConstraint(query,
1301: field, value);
1302: addConstraint(query, constraint);
1303: }
1304:
1305: /**
1306: * Create a constraint for a query
1307: * The value is matched on equality (exact match).
1308: *
1309: * @param query - the query to add the constraint to
1310: * @param field - the constraint field
1311: * @param value - value to search for in the field
1312: * @return constraint which matches a field value
1313: */
1314: public static FieldValueConstraint createEqualConstraint(
1315: NodeQuery query, Field field, Integer value) {
1316: FieldValueConstraint constraint = query.createConstraint(query
1317: .getStepField(field), FieldCompareConstraint.EQUAL,
1318: value);
1319: return constraint;
1320: }
1321:
1322: /**
1323: * Create a constraint for a query
1324: * The value is matched on equality (exact match).
1325: *
1326: * @param query - the query to add the constraint to
1327: * @param field - the constraint field
1328: * @param value - value to search for in the field
1329: * @return constraint which matches a field value
1330: */
1331: public static FieldValueConstraint createEqualConstraint(
1332: Query query, Field field, Integer value) {
1333: StepField equalsField = findField(query, field);
1334: FieldValueConstraint constraint = query.createConstraint(
1335: equalsField, FieldCompareConstraint.EQUAL, value);
1336: return constraint;
1337: }
1338:
1339: /**
1340: * Add a constraint to a query
1341: * The value is matched on equality (exact match).
1342: *
1343: * @param query - the query to add the constraint to
1344: * @param field - the constraint field
1345: * @param value - value to search for in the field
1346: */
1347: public static void addEqualConstraint(NodeQuery query, Field field,
1348: Boolean value) {
1349: FieldValueConstraint constraint = createEqualConstraint(query,
1350: field, value);
1351: addConstraint(query, constraint);
1352: }
1353:
1354: /**
1355: * Add a constraint to a query
1356: * The value is matched on equality (exact match).
1357: *
1358: * @param query - the query to add the constraint to
1359: * @param field - the constraint field
1360: * @param value - value to search for in the field
1361: */
1362: public static void addEqualConstraint(Query query, Field field,
1363: Boolean value) {
1364: FieldValueConstraint constraint = createEqualConstraint(query,
1365: field, value);
1366: addConstraint(query, constraint);
1367: }
1368:
1369: /**
1370: * Create a constraint for a query
1371: * The value is matched on equality (exact match).
1372: *
1373: * @param query - the query to add the constraint to
1374: * @param field - the constraint field
1375: * @param value - value to search for in the field
1376: * @return constraint which matches a field value
1377: */
1378: public static FieldValueConstraint createEqualConstraint(
1379: NodeQuery query, Field field, Boolean value) {
1380: FieldValueConstraint constraint = query.createConstraint(query
1381: .getStepField(field), FieldCompareConstraint.EQUAL,
1382: value);
1383: return constraint;
1384: }
1385:
1386: /**
1387: * Create a constraint for a query
1388: * The value is matched on equality (exact match).
1389: *
1390: * @param query - the query to add the constraint to
1391: * @param field - the constraint field
1392: * @param value - value to search for in the field
1393: * @return constraint which matches a field value
1394: */
1395: public static FieldValueConstraint createEqualConstraint(
1396: Query query, Field field, Boolean value) {
1397: StepField equalsField = findField(query, field);
1398: FieldValueConstraint constraint = query.createConstraint(
1399: equalsField, FieldCompareConstraint.EQUAL, value);
1400: return constraint;
1401: }
1402:
1403: /**
1404: * Add a constraint to a query
1405: * The value is matched on equality (exact match).
1406: * For a string field type the match is case-insensitive.
1407: *
1408: * @param query - the query to add the constraint to
1409: * @param field - the constraint field
1410: * @param value - value to search for in the field
1411: */
1412: public static void addEqualConstraint(NodeQuery query, Field field,
1413: Object value) {
1414: FieldValueConstraint constraint = createEqualConstraint(query,
1415: field, value);
1416: addConstraint(query, constraint);
1417: }
1418:
1419: /**
1420: * Add a constraint to a query
1421: * The value is matched on equality (exact match).
1422: * For a string field type the match is case-insensitive.
1423: *
1424: * @param query - the query to add the constraint to
1425: * @param field - the constraint field
1426: * @param value - value to search for in the field
1427: */
1428: public static void addEqualConstraint(Query query, Field field,
1429: Object value) {
1430: FieldValueConstraint constraint = createEqualConstraint(query,
1431: field, value);
1432: addConstraint(query, constraint);
1433: }
1434:
1435: /**
1436: * Create a constraint for a query
1437: * The value is matched on equality (exact match).
1438: * For a string field type the match is case-insensitive.
1439: *
1440: * @param query - the query to add the constraint to
1441: * @param field - the constraint field
1442: * @param value - value to search for in the field
1443: * @return constraint which matches a field value
1444: */
1445: public static FieldValueConstraint createEqualConstraint(
1446: NodeQuery query, Field field, Object value) {
1447: FieldValueConstraint constraint = query.createConstraint(query
1448: .getStepField(field), FieldCompareConstraint.EQUAL,
1449: value);
1450: return constraint;
1451: }
1452:
1453: /**
1454: * Create a constraint for a query
1455: * The value is matched on equality (exact match).
1456: * For a string field type the match is case-insensitive.
1457: *
1458: * @param query - the query to add the constraint to
1459: * @param field - the constraint field
1460: * @param value - value to search for in the field
1461: * @return constraint which matches a field value
1462: */
1463: public static FieldValueConstraint createEqualConstraint(
1464: Query query, Field field, Object value) {
1465: StepField equalsField = findField(query, field);
1466: FieldValueConstraint constraint = query.createConstraint(
1467: equalsField, FieldCompareConstraint.EQUAL, value);
1468: return constraint;
1469: }
1470:
1471: /**
1472: * Find a step field in a query based on a bridge field
1473: *
1474: * @param query - the query
1475: * @param field - the bridge field which belongs to a node manager
1476: * @return a step field in the query
1477: */
1478: public static StepField findField(Query query, Field field) {
1479: StepField equalsField = null;
1480: Iterator<StepField> fields = query.getFields().iterator();
1481: while (fields.hasNext()) {
1482: StepField stepField = fields.next();
1483: if (stepField.getStep().getTableName().equals(
1484: field.getNodeManager().getName())) {
1485: if (stepField.getFieldName().equals(field.getName())) {
1486: equalsField = stepField;
1487: }
1488: }
1489: }
1490: if (equalsField == null) {
1491: Step equalsStep = query.getStep(field.getNodeManager()
1492: .getName());
1493: if (equalsStep == null) {
1494: throw new IllegalArgumentException("Step "
1495: + field.getNodeManager().getName()
1496: + " not found in query");
1497:
1498: }
1499: equalsField = query.createStepField(equalsStep, field);
1500: }
1501: if (equalsField == null) {
1502: throw new IllegalArgumentException("Field "
1503: + field.getName() + " not found in query");
1504: }
1505:
1506: return equalsField;
1507: }
1508:
1509: /**
1510: * Add a constraint to a query
1511: * The value is matched on likelihood (wildcard % match).
1512: * For a string field type the match is case-insensitive.
1513: *
1514: * @param query - the query to add the constraint to
1515: * @param field - the constraint field
1516: * @param value - value to search for in the field
1517: */
1518: public static void addLikeConstraint(NodeQuery query, Field field,
1519: String value) {
1520: FieldValueConstraint constraint = createLikeConstraint(query,
1521: field, value);
1522: addConstraint(query, constraint);
1523: }
1524:
1525: /**
1526: * Create a constraint for a query
1527: * The value is matched on likelihood (wildcard % match).
1528: * For a string field type the match is case-insensitive.
1529: *
1530: * @param query - the query to add the constraint to
1531: * @param field - the constraint field
1532: * @param value - value to search for in the field
1533: * @return constraint which matches a field value
1534: */
1535: public static FieldValueConstraint createLikeConstraint(
1536: NodeQuery query, Field field, String value) {
1537: StepField stepField = query.getStepField(field);
1538: return createLikeConstraint(query, stepField, value);
1539: }
1540:
1541: /**
1542: * Add a constraint to a query
1543: * The value is matched on likelihood (wildcard % match).
1544: * For a string field type the match is case-insensitive.
1545: *
1546: * @param query - the query to add the constraint to
1547: * @param field - the constraint field
1548: * @param value - value to search for in the field
1549: */
1550: public static void addLikeConstraint(Query query, Field field,
1551: String value) {
1552: FieldValueConstraint constraint = createLikeConstraint(query,
1553: field, value);
1554: addConstraint(query, constraint);
1555: }
1556:
1557: /**
1558: * Create a constraint for a query
1559: * The value is matched on likelihood (wildcard % match).
1560: * For a string field type the match is case-insensitive.
1561: *
1562: * @param query - the query to add the constraint to
1563: * @param field - the constraint field
1564: * @param value - value to search for in the field
1565: * @return constraint which matches a field value
1566: */
1567: public static FieldValueConstraint createLikeConstraint(
1568: Query query, Field field, String value) {
1569: StepField stepField = findField(query, field);
1570: return createLikeConstraint(query, stepField, value);
1571: }
1572:
1573: /**
1574: * Create a constraint for a query
1575: * The value is matched on likelihood (wildcard % match).
1576: * For a string field type the match is case-insensitive.
1577: *
1578: * @param query - the query to add the constraint to
1579: * @param field - the constraint field
1580: * @param value - value to search for in the field
1581: * @return constraint which matches a field value
1582: */
1583: public static FieldValueConstraint createLikeConstraint(
1584: Query query, StepField stepField, String value) {
1585: FieldValueConstraint constraint = query.createConstraint(
1586: stepField, FieldCompareConstraint.LIKE, "%" + value
1587: + "%");
1588: query.setCaseSensitive(constraint, false);
1589: return constraint;
1590: }
1591:
1592: /**
1593: * Add a date and time constraint to a query where the value is between now
1594: * and the days passed in. The days can be a negative number.
1595: *
1596: * @param query - the query to add the constraint to
1597: * @param manager - manager of the constraint field
1598: * @param fieldname - name of field to search with
1599: * @param daysToCompare - value to search for in the field
1600: */
1601: public static void addDayConstraint(NodeQuery query,
1602: NodeManager manager, String fieldname, String daysToCompare) {
1603:
1604: if (!isEmptyOrWhitespace(daysToCompare)
1605: && !daysToCompare.equals("0")
1606: && daysToCompare.matches("\\-?\\d+")) {
1607:
1608: Field field = manager.getField(fieldname);
1609:
1610: long now = (System.currentTimeMillis());
1611:
1612: long msecondsToCompare = (1000 * 60 * 60 * 24 * Long
1613: .parseLong(daysToCompare));
1614: long date = now + msecondsToCompare;
1615:
1616: long compareField1 = (msecondsToCompare < 0) ? date : now;
1617: long compareField2 = (msecondsToCompare < 0) ? now : date;
1618:
1619: addDatetimeConstraint(query, field, compareField1,
1620: compareField2);
1621: }
1622: }
1623:
1624: /**
1625: * Add a date and time constraint to a query
1626: *
1627: * @param query - the query to add the constraint to
1628: * @param field - the constraint field
1629: * @param from - from value to search for in the field
1630: * @param to - to value to search for in the field
1631: */
1632: public static void addDatetimeConstraint(NodeQuery query,
1633: Field field, long from, long to) {
1634: FieldValueBetweenConstraint constraint = createDatetimeConstraint(
1635: query, field, from, to);
1636: addConstraint(query, constraint);
1637: }
1638:
1639: /**
1640: * Create a date and time constraint for a query
1641: * The value is matched on likelihood (wildcard % match).
1642: * For a string field type the match is case-insensitive.
1643: *
1644: * @param query - the query to add the constraint to
1645: * @param field - the constraint field
1646: * @param from - from value to search for in the field
1647: * @param to - to value to search for in the field
1648: * @return constraint which matches a field value
1649: */
1650: public static FieldValueBetweenConstraint createDatetimeConstraint(
1651: NodeQuery query, Field field, long from, long to) {
1652: FieldValueBetweenConstraint constraint = null;
1653: if (field.getType() == Field.TYPE_DATETIME) {
1654: constraint = query.createConstraint(query
1655: .getStepField(field), new Date(from), new Date(to));
1656: } else {
1657: constraint = query.createConstraint(query
1658: .getStepField(field), Long.valueOf(from), Long
1659: .valueOf(to));
1660: }
1661: return constraint;
1662: }
1663:
1664: /**
1665: * Limit the result set of the query.
1666: * Note: converting a query with a limit to a counted query
1667: * {@link org.mmbase.bridge.util.Queries#count(Query)} will only limit the
1668: * result set for that query A result set is than always one.
1669: * @param query - the query to add the constraint to
1670: * @param offset - the offset where the result set should start
1671: * @param maxNumber - the maximum number of results which are allowed to return
1672: */
1673: public static void addLimitConstraint(NodeQuery query, int offset,
1674: int maxNumber) {
1675: if (offset > 0) {
1676: query.setOffset(offset);
1677: }
1678: if (maxNumber > 0) {
1679: query.setMaxNumber(maxNumber);
1680: }
1681: }
1682:
1683: /**
1684: * Add a constraint to the query which limits the node types of the nodes in the result
1685: * @param query - the query to add the constraint to
1686: * @param types - names of node managers
1687: */
1688: public static void addTypeConstraints(NodeQuery query,
1689: List<String> types) {
1690: FieldValueInConstraint constraint = createTypeConstraints(
1691: query, types);
1692: addConstraint(query, constraint);
1693: }
1694:
1695: /**
1696: * Create a constraint for the query which limits the node types of the nodes in the result
1697: * @param query - the query to add the constraint to
1698: * @param types - names of node managers
1699: * @return constraint with node types
1700: */
1701: public static FieldValueInConstraint createTypeConstraints(
1702: NodeQuery query, List<String> types) {
1703: Cloud cloud = query.getCloud();
1704: SortedSet<Integer> set = new TreeSet<Integer>();
1705: for (String type : types) {
1706: NodeManager manager = cloud.getNodeManager(type);
1707: set.add(manager.getNumber());
1708: }
1709: Field field = query.getNodeManager().getField("otype");
1710: return createInConstraint(query, field, set);
1711: }
1712:
1713: /**
1714: * Add a constraint to the query which limits the nodes in the result based on the number
1715: * @param query - the query to add the constraint to
1716: * @param field - the constraint field
1717: * @param nodes - node which should be used for the constraint
1718: */
1719: public static void addNodesConstraints(Query query, Field field,
1720: NodeList nodes) {
1721: SortedSet<Integer> set = createNodesConstraints(nodes);
1722: addInConstraint(query, field, set);
1723: }
1724:
1725: /**
1726: * Create a set with the node numbers of the list of nodes
1727: * @param nodes - list of nodes
1728: * @return Set sorted on node number
1729: */
1730: public static SortedSet<Integer> createNodesConstraints(
1731: NodeList nodes) {
1732: SortedSet<Integer> set = new TreeSet<Integer>();
1733: for (Node element : nodes) {
1734: Node node = element;
1735: set.add(node.getNumber());
1736: }
1737: return set;
1738: }
1739:
1740: /**
1741: * Add a constraint to the query which limits the values in the result based on the set
1742: * @param query - the query to add the constraint to
1743: * @param field - the constraint field
1744: * @param set - set with possible values
1745: */
1746: public static void addInConstraint(Query query, Field field,
1747: SortedSet<? extends Object> set) {
1748: FieldValueInConstraint constraint = createInConstraint(query,
1749: field, set);
1750: addConstraint(query, constraint);
1751: }
1752:
1753: /**
1754: * Create a constraint for the query which limits the values in the result based on the set
1755: * @param query - the query to add the constraint to
1756: * @param field - the constraint field
1757: * @param set - set with possible values
1758: * @return in-constraint
1759: */
1760: public static FieldValueInConstraint createInConstraint(
1761: Query query, Field field, SortedSet<? extends Object> set) {
1762: query.getStep(field.getNodeManager().getName());
1763: StepField stepfield = findField(query, field);
1764: FieldValueInConstraint constraint = query.createConstraint(
1765: stepfield, set);
1766: return constraint;
1767: }
1768:
1769: /**
1770: * Add a constraint to the query. When there is already a constraint then
1771: * the constraint is added with an AND operator
1772: * @param query - the query to add the constraint to
1773: * @param constraint - the constraint
1774: */
1775: public static void addConstraint(Query query, Constraint constraint) {
1776: addConstraint(query, constraint,
1777: CompositeConstraint.LOGICAL_AND);
1778: }
1779:
1780: /**
1781: * Create a AND composite constraint for the query
1782: * @param query - the query to add the constraint to
1783: * @param first - first constraint
1784: * @param second - second constraint
1785: * @return composite constraint
1786: */
1787: public static Constraint createANDConstraint(Query query,
1788: Constraint first, Constraint second) {
1789: return createLogicalConstraint(query, first, second,
1790: CompositeConstraint.LOGICAL_OR);
1791: }
1792:
1793: /**
1794: * Add a constraint to the query. When there is already a constraint then
1795: * the constraint is added with an OR operator
1796: * @param query - the query to add the constraint to
1797: * @param constraint - the constraint
1798: */
1799: public static void addORConstraint(Query query,
1800: Constraint constraint) {
1801: addConstraint(query, constraint, CompositeConstraint.LOGICAL_OR);
1802: }
1803:
1804: /**
1805: * Create a OR composite constraint for the query
1806: * @param query - the query to add the constraint to
1807: * @param first - first constraint
1808: * @param second - second constraint
1809: * @return composite constraint
1810: */
1811: public static Constraint addORConstraint(Query query,
1812: Constraint first, Constraint second) {
1813: return createLogicalConstraint(query, first, second,
1814: CompositeConstraint.LOGICAL_OR);
1815: }
1816:
1817: /**
1818: * Add a constraint to the query. When there is already a constraint then
1819: * the constraint is added with the operator specified
1820: * @param query - the query to add the constraint to
1821: * @param constraint - the constraint
1822: * @param operator - the logical operator (CompositeConstraint.LOGICAL_OR, CompositeConstraint.LOGICAL_AND)
1823: */
1824: public static void addConstraint(Query query,
1825: Constraint constraint, int operator) {
1826: if (query.getConstraint() == null) {
1827: query.setConstraint(constraint);
1828: } else {
1829: CompositeConstraint newc = createLogicalConstraint(query,
1830: query.getConstraint(), constraint, operator);
1831: query.setConstraint(newc);
1832: }
1833: }
1834:
1835: /**
1836: * Create a composite constraint for the query
1837: * @param query - the query to add the constraint to
1838: * @param first - first constraint
1839: * @param second - second constraint
1840: * @param operator - the logical operator (CompositeConstraint.LOGICAL_OR, CompositeConstraint.LOGICAL_AND)
1841: * @return composite constraint
1842: */
1843: public static CompositeConstraint createLogicalConstraint(
1844: Query query, Constraint first, Constraint second,
1845: int operator) {
1846: CompositeConstraint newc = query.createConstraint(first,
1847: operator, second);
1848: return newc;
1849: }
1850:
1851: /**
1852: * is Empty Or Whitespace.String
1853: *
1854: * @param str String to check emptiness
1855: * @return boolean is it empty
1856: */
1857: public static boolean isEmptyOrWhitespace(String str) {
1858: return (str == null) || "".equals(str.trim());
1859: }
1860:
1861: }
|