1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.bi.rules;
24  
25  import com.liferay.portal.kernel.util.Validator;
26  
27  import java.io.Serializable;
28  
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.List;
32  
33  /**
34   * <a href="Query.java.html"><b><i>View Source</i></b></a>
35   *
36   * <a href="RuleEngineQuery.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Michael C. Han
39   */
40  public class Query implements Serializable {
41  
42      public static Query createCustomQuery(
43          String identifier, String queryName) {
44  
45          if (Validator.isNull(identifier)) {
46              throw new IllegalArgumentException("Query idenfier is null.");
47          }
48  
49          if (Validator.isNull(queryName)) {
50              throw new IllegalArgumentException("Query string is null.");
51          }
52  
53          return new Query(identifier, QueryType.CUSTOM, queryName);
54      }
55  
56      public static Query createStandardQuery() {
57          return new Query(null, QueryType.STANDARD, null);
58      }
59  
60      public void addArgument(Object object) {
61          if (_queryType.equals(QueryType.STANDARD)) {
62              throw new IllegalStateException(
63                  "Standard queries cannot accept query arguments");
64          }
65  
66          _arguments.add(object);
67      }
68  
69      public void addArguments(List<?> arguments) {
70          if (_queryType.equals(QueryType.STANDARD)) {
71              throw new IllegalStateException(
72                  "Standard queries cannot accept query arguments");
73          }
74  
75          _arguments.addAll(arguments);
76      }
77  
78      public void addArguments(Object[] arguments) {
79          if (_queryType.equals(QueryType.STANDARD)) {
80              throw new IllegalStateException(
81                  "Standard queries cannot accept query arguments");
82          }
83  
84          if ((arguments != null) && (arguments.length > 0)) {
85              _arguments.addAll(Arrays.asList(arguments));
86          }
87      }
88  
89      public Object[] getArguments() {
90          return _arguments.toArray(new Object[_arguments.size()]);
91      }
92  
93      public String getIdentifier() {
94          return _identifier;
95      }
96  
97      public String getQueryName() {
98          return _queryName;
99      }
100 
101     public QueryType getQueryType() {
102         return _queryType;
103     }
104 
105     private Query(String identifier, QueryType queryType, String queryName) {
106         _identifier = identifier;
107         _queryType = queryType;
108         _queryName = queryName;
109     }
110 
111     private List<Object> _arguments = new ArrayList<Object>();
112     private String _identifier;
113     private String _queryName;
114     private QueryType _queryType;
115 
116 }