001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.db;
029
030import java.util.Collections;
031import java.util.List;
032
033import com.google.common.collect.Lists;
034
035/**
036 * A query fragment which aggregates the results from a list of other query fragments.<p>
037 *
038 * @since 8.0.0
039 */
040public class CmsCompositeQueryFragment implements I_CmsQueryFragment {
041
042    /** The list of query fragments which should be aggregated. */
043    private List<I_CmsQueryFragment> m_fragments = Lists.newArrayList();
044
045    /** The prefix string. */
046    private String m_prefix;
047
048    /** The separator string which should be inserted between the SQL of each constituent query fragment. */
049    private String m_separator;
050
051    /** The suffix string. */
052    private String m_suffix;
053
054    /**
055     * Adds a new query fragment.<p>
056     *
057     * @param node the query fragment
058     */
059    public void add(I_CmsQueryFragment node) {
060
061        m_fragments.add(node);
062    }
063
064    /**
065     * Returns the wrapped query fragments.<p>
066     *
067     * @return a list of query fragments
068     */
069    public List<I_CmsQueryFragment> getNodes() {
070
071        return m_fragments;
072    }
073
074    /**
075     * Sets the prefix string (will be inserted before the other fragments).<p>
076     *
077     * @param prefix the prefix string
078     */
079    public void setPrefix(String prefix) {
080
081        m_prefix = prefix;
082    }
083
084    /**
085     * Sets the separator which should be inserted between the constituent query fragments.<p>
086     *
087     * @param separator the separator string
088     */
089    public void setSeparator(String separator) {
090
091        m_separator = separator;
092    }
093
094    /**
095     * Sets the suffix string (will be inserted after the other fragments).<p>
096     *
097     * @param suffix the suffix string
098     */
099    public void setSuffix(String suffix) {
100
101        m_suffix = suffix;
102    }
103
104    /**
105     * @see org.opencms.db.I_CmsQueryFragment#visit(org.opencms.db.CmsStatementBuilder)
106     */
107    public void visit(CmsStatementBuilder builder) {
108
109        boolean first = true;
110        if (m_prefix != null) {
111            builder.add(m_prefix);
112        }
113        for (I_CmsQueryFragment node : m_fragments) {
114            if (!first && (m_separator != null)) {
115                builder.add(m_separator, Collections.<Object> emptyList());
116            }
117            node.visit(builder);
118            first = false;
119
120        }
121        if (m_prefix != null) {
122            builder.add(m_suffix);
123        }
124    }
125}