001    /**
002     * Copyright (c) 2000-2010 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(
031                    String identifier, String queryName) {
032    
033                    if (Validator.isNull(identifier)) {
034                            throw new IllegalArgumentException("Query idenfier is null.");
035                    }
036    
037                    if (Validator.isNull(queryName)) {
038                            throw new IllegalArgumentException("Query string is null.");
039                    }
040    
041                    return new Query(identifier, QueryType.CUSTOM, queryName);
042            }
043    
044            public static Query createStandardQuery() {
045                    return new Query(null, QueryType.STANDARD, null);
046            }
047    
048            public void addArgument(Object object) {
049                    if (_queryType.equals(QueryType.STANDARD)) {
050                            throw new IllegalStateException(
051                                    "Standard queries cannot accept query arguments");
052                    }
053    
054                    _arguments.add(object);
055            }
056    
057            public void addArguments(List<?> arguments) {
058                    if (_queryType.equals(QueryType.STANDARD)) {
059                            throw new IllegalStateException(
060                                    "Standard queries cannot accept query arguments");
061                    }
062    
063                    _arguments.addAll(arguments);
064            }
065    
066            public void addArguments(Object[] arguments) {
067                    if (_queryType.equals(QueryType.STANDARD)) {
068                            throw new IllegalStateException(
069                                    "Standard queries cannot accept query arguments");
070                    }
071    
072                    if ((arguments != null) && (arguments.length > 0)) {
073                            _arguments.addAll(Arrays.asList(arguments));
074                    }
075            }
076    
077            public Object[] getArguments() {
078                    return _arguments.toArray(new Object[_arguments.size()]);
079            }
080    
081            public String getIdentifier() {
082                    return _identifier;
083            }
084    
085            public String getQueryName() {
086                    return _queryName;
087            }
088    
089            public QueryType getQueryType() {
090                    return _queryType;
091            }
092    
093            private Query(String identifier, QueryType queryType, String queryName) {
094                    _identifier = identifier;
095                    _queryType = queryType;
096                    _queryName = queryName;
097            }
098    
099            private List<Object> _arguments = new ArrayList<Object>();
100            private String _identifier;
101            private String _queryName;
102            private QueryType _queryType;
103    
104    }