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.ArrayUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.Serializable;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class Query implements Serializable {
030    
031            public static Query createCustomQuery(String identifier, String queryName) {
032                    if (Validator.isNull(identifier)) {
033                            throw new IllegalArgumentException("Query idenfier is null.");
034                    }
035    
036                    if (Validator.isNull(queryName)) {
037                            throw new IllegalArgumentException("Query string is null.");
038                    }
039    
040                    return new Query(identifier, QueryType.CUSTOM, queryName);
041            }
042    
043            public static Query createStandardQuery() {
044                    return new Query(null, QueryType.STANDARD, null);
045            }
046    
047            public void addArgument(Object object) {
048                    if (_queryType.equals(QueryType.STANDARD)) {
049                            throw new IllegalStateException(
050                                    "Standard queries cannot accept query arguments");
051                    }
052    
053                    _arguments.add(object);
054            }
055    
056            public void addArguments(List<?> arguments) {
057                    if (_queryType.equals(QueryType.STANDARD)) {
058                            throw new IllegalStateException(
059                                    "Standard queries cannot accept query arguments");
060                    }
061    
062                    _arguments.addAll(arguments);
063            }
064    
065            public void addArguments(Object[] arguments) {
066                    if (_queryType.equals(QueryType.STANDARD)) {
067                            throw new IllegalStateException(
068                                    "Standard queries cannot accept query arguments");
069                    }
070    
071                    if (ArrayUtil.isNotEmpty(arguments)) {
072                            _arguments.addAll(Arrays.asList(arguments));
073                    }
074            }
075    
076            public Object[] getArguments() {
077                    return _arguments.toArray(new Object[_arguments.size()]);
078            }
079    
080            public String getIdentifier() {
081                    return _identifier;
082            }
083    
084            public String getQueryName() {
085                    return _queryName;
086            }
087    
088            public QueryType getQueryType() {
089                    return _queryType;
090            }
091    
092            private Query(String identifier, QueryType queryType, String queryName) {
093                    _identifier = identifier;
094                    _queryType = queryType;
095                    _queryName = queryName;
096            }
097    
098            private List<Object> _arguments = new ArrayList<Object>();
099            private String _identifier;
100            private String _queryName;
101            private QueryType _queryType;
102    
103    }