001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.bi.rules;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.io.Serializable;
020    
021    import java.util.ArrayList;
022    import java.util.Arrays;
023    import java.util.List;
024    
025    /**
026     * @author Michael C. Han
027     */
028    public class Query implements Serializable {
029    
030            public static Query createCustomQuery(String identifier, String queryName) {
031                    if (Validator.isNull(identifier)) {
032                            throw new IllegalArgumentException("Query idenfier is null.");
033                    }
034    
035                    if (Validator.isNull(queryName)) {
036                            throw new IllegalArgumentException("Query string is null.");
037                    }
038    
039                    return new Query(identifier, QueryType.CUSTOM, queryName);
040            }
041    
042            public static Query createStandardQuery() {
043                    return new Query(null, QueryType.STANDARD, null);
044            }
045    
046            public void addArgument(Object object) {
047                    if (_queryType.equals(QueryType.STANDARD)) {
048                            throw new IllegalStateException(
049                                    "Standard queries cannot accept query arguments");
050                    }
051    
052                    _arguments.add(object);
053            }
054    
055            public void addArguments(List<?> arguments) {
056                    if (_queryType.equals(QueryType.STANDARD)) {
057                            throw new IllegalStateException(
058                                    "Standard queries cannot accept query arguments");
059                    }
060    
061                    _arguments.addAll(arguments);
062            }
063    
064            public void addArguments(Object[] arguments) {
065                    if (_queryType.equals(QueryType.STANDARD)) {
066                            throw new IllegalStateException(
067                                    "Standard queries cannot accept query arguments");
068                    }
069    
070                    if ((arguments != null) && (arguments.length > 0)) {
071                            _arguments.addAll(Arrays.asList(arguments));
072                    }
073            }
074    
075            public Object[] getArguments() {
076                    return _arguments.toArray(new Object[_arguments.size()]);
077            }
078    
079            public String getIdentifier() {
080                    return _identifier;
081            }
082    
083            public String getQueryName() {
084                    return _queryName;
085            }
086    
087            public QueryType getQueryType() {
088                    return _queryType;
089            }
090    
091            private Query(String identifier, QueryType queryType, String queryName) {
092                    _identifier = identifier;
093                    _queryType = queryType;
094                    _queryName = queryName;
095            }
096    
097            private List<Object> _arguments = new ArrayList<Object>();
098            private String _identifier;
099            private String _queryName;
100            private QueryType _queryType;
101    
102    }