001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: ComposedSubquery.java 1678 2007-09-24 15:36:19Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.list.engine;
025:
026: import java.util.Enumeration;
027: import java.util.Vector;
028:
029: /**
030: * A subquery of a composed query
031: *
032: * @author Cristian Bogdan
033: * @version $Id: ComposedSubquery.java 1678 2007-09-24 15:36:19Z manuel_gay $
034: */
035: public class ComposedSubquery extends ComposedQuery {
036:
037: /** The enclosing query */
038: ComposedQuery super Query;
039:
040: /**
041: * Make a subquery of the indicated query, from the given sections.
042: *
043: * @param usesHQL
044: * <code>true</code> if we use Hibernate to execute the query
045: */
046: public ComposedSubquery(String[] subsections, ComposedQuery cq,
047: String queryLanguage) {
048: super (subsections, queryLanguage);
049: super Query = cq;
050: super Query.addSubquery(this );
051: derivedSections = new String[5];
052: deriveFrom(FROM);
053: deriveFrom(VARFROM);
054:
055: concat(derivedSections, super Query.derivedSections, sections,
056: WHERE, " AND ", true);
057: // concat(derivedSections, superQuery.derivedSections, sections, GROUPBY, ",", false);
058: String gpb = sections[GROUPBY];
059: if (gpb != null)
060: derivedSections[GROUPBY] = gpb;
061:
062: // concat(derivedSections, superQuery.derivedSections, sections, "ORDERBY", ",");
063: String order = sections[ORDERBY];
064: if (order != null)
065: derivedSections[ORDERBY] = order;
066: }
067:
068: /**
069: * Derives the FROM section using the sections of the superQuery
070: *
071: * @param n
072: * the index of the section
073: */
074: void deriveFrom(int n) {
075: derivedSections[n] = super Query.derivedSections[n];
076: if (sections[n] != null)
077: if (derivedSections[n] != null)
078: if (sections[n].trim().toLowerCase().startsWith("join"))
079: // FIXME: this seems to be a dirty fix...maybe the separator should be join in all cases?
080: derivedSections[n] += " " + sections[n];
081: else
082: derivedSections[n] += "," + sections[n];
083: else
084: derivedSections[n] = sections[n];
085: }
086:
087: /**
088: * Concatenates sections on the given index, with the given separator
089: *
090: * @param result
091: * the String array containing the result
092: * @param h1
093: * first array of sections
094: * @param h2
095: * second array of sections
096: * @param what
097: * the index at which we should concatenate
098: * @pram sep the separator used for concatenation
099: * @param paran
100: * do we want parenthesis
101: */
102: static void concat(String[] result, String[] h1, String[] h2,
103: int what, String sep, boolean paran) {
104: String lp = "";
105: String rp = "";
106: if (paran) {
107: lp = "(";
108: rp = ")";
109: }
110: String s1 = h1[what];
111: String s2 = h2[what];
112: if (s1 != null && s1.trim().length() == 0)
113: s1 = null;
114:
115: if (s2 != null && s2.trim().length() == 0)
116: s2 = null;
117:
118: if (s1 == null && s2 != null) {
119: result[what] = s2;
120: return;
121: }
122: if (s2 == null && s1 != null) {
123: result[what] = s1;
124: return;
125: }
126: if (s2 != null && s1 != null)
127: result[what] = lp + s1 + rp + sep + lp + s2 + rp;
128: }
129:
130: /**
131: * Initializes the keysets by adding the superquery's previousKeyset to its keyset.
132: */
133: protected void initKeysets() {
134: previousKeyset = (Vector) super Query.previousKeyset.clone();
135: keyset = (Vector) super Query.keyset.clone();
136: keysetLabels = (Vector) super Query.keysetLabels.clone();
137:
138: for (Enumeration e = keysetLabels.elements(); e
139: .hasMoreElements();)
140: addProjection((String) e.nextElement());
141: previousKeyset.addElement(superQuery.keyset);
142: }
143:
144: }
|