/* * The original code created by Software AG * is part of Software AG Tamino X-Application and * Copyright (c) 2001 - 2002 Software AG, All Rights Reserved. * * Software AG highly appreciate the efforts and contributions of * the Source Partners. For more information, please see * http://www.softwareag.com/developer/x-application/guide.htm * * You can redistribute this software and/or modify it under the terms of * the Software AG Source Add-On Software License * http://www.softwareag.com/developer/x-application/license.htm */ package com.softwareag.xtools.xapplication.businessdocument; import java.util.Iterator; import com.softwareag.xtools.xapplication.common.PreconditionViolation; /** ** This is the QueryBuilder class which contains the method ** buildQueryExpression for building an XQL expression from all ** the objects in a QueryContainer instance. ** ** @author Software AG Tamino X-Application Developers ** @version $Revision: 1.31 $ **/ public class QueryBuilder { // holds the local reference to the querycontainer private QueryContainer container; /** Constructor that instantiates a new builder ** ** @pre container != null ** ** @param container container for which XQL expressions should be generated **/ public QueryBuilder(QueryContainer container) { if (container==null) { throw new PreconditionViolation("container cannot be null"); } this.container=container; } /** This method builds the XQL statement from the container that was referenced ** when instantiating the querybuilder. This method will never return null, ** and can be used multiple times on the same container instance. Even if ** changes to the container are made, they will be automatically reflected ** in a call to this method ** ** @post return != null ** ** @return XQL string **/ public String buildQueryExpression() { String rootPath = "/" + container.getDoctype(); StringBuffer queryBuffer=new StringBuffer(); queryBuffer.append(rootPath); boolean first=true; // first iterate over the items to search for groups and elements Iterator items = container.items(); while (items.hasNext()) { Object o = items.next(); if (o instanceof QueryElement || o instanceof QueryGroup || o instanceof QueryRange || o instanceof QueryFilter) { QueryItem qi=(QueryItem)o; String work=qi.buildQueryExpression(rootPath); if (first) { queryBuffer.append("["); } else { queryBuffer.append(" and "); } queryBuffer.append(work); first=false; } } if (first==false) { queryBuffer.append("]"); } // second iterate over the item to search for sort with sort sequence first=true; for (int seq = 1; seq < 10; seq++) { items = container.items(); while (items.hasNext()) { Object o = items.next(); if (o instanceof QuerySort) { QuerySort qs = (QuerySort)o; if (qs.getSequence() == seq) { if (first) { queryBuffer.append(" sortby ("); first = false; } else { queryBuffer.append(", "); } queryBuffer.append(qs.buildQueryExpression()); } } } } // third iterate over the item to search for sort with no sort sequence items=container.items(); while (items.hasNext()) { Object o = items.next(); if (o instanceof QuerySort) { QuerySort qs=(QuerySort)o; if (qs.getSequence() == 0) { if (first) { queryBuffer.append(" sortby ("); first = false; } else { queryBuffer.append(", "); } queryBuffer.append(qs.buildQueryExpression()); } } } if (first==false) { queryBuffer.append(")"); } //System.err.println("QueryBuilder::buildQueryExpression - queryBuffer: " + queryBuffer.toString()); return queryBuffer.toString(); } }