1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.bi.rules;
16  
17  import com.liferay.portal.kernel.util.Validator;
18  
19  import java.io.Serializable;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  /**
26   * <a href="Query.java.html"><b><i>View Source</i></b></a>
27   *
28   * <a href="RuleEngineQuery.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Michael C. Han
31   */
32  public class Query implements Serializable {
33  
34      public static Query createCustomQuery(
35          String identifier, String queryName) {
36  
37          if (Validator.isNull(identifier)) {
38              throw new IllegalArgumentException("Query idenfier is null.");
39          }
40  
41          if (Validator.isNull(queryName)) {
42              throw new IllegalArgumentException("Query string is null.");
43          }
44  
45          return new Query(identifier, QueryType.CUSTOM, queryName);
46      }
47  
48      public static Query createStandardQuery() {
49          return new Query(null, QueryType.STANDARD, null);
50      }
51  
52      public void addArgument(Object object) {
53          if (_queryType.equals(QueryType.STANDARD)) {
54              throw new IllegalStateException(
55                  "Standard queries cannot accept query arguments");
56          }
57  
58          _arguments.add(object);
59      }
60  
61      public void addArguments(List<?> arguments) {
62          if (_queryType.equals(QueryType.STANDARD)) {
63              throw new IllegalStateException(
64                  "Standard queries cannot accept query arguments");
65          }
66  
67          _arguments.addAll(arguments);
68      }
69  
70      public void addArguments(Object[] arguments) {
71          if (_queryType.equals(QueryType.STANDARD)) {
72              throw new IllegalStateException(
73                  "Standard queries cannot accept query arguments");
74          }
75  
76          if ((arguments != null) && (arguments.length > 0)) {
77              _arguments.addAll(Arrays.asList(arguments));
78          }
79      }
80  
81      public Object[] getArguments() {
82          return _arguments.toArray(new Object[_arguments.size()]);
83      }
84  
85      public String getIdentifier() {
86          return _identifier;
87      }
88  
89      public String getQueryName() {
90          return _queryName;
91      }
92  
93      public QueryType getQueryType() {
94          return _queryType;
95      }
96  
97      private Query(String identifier, QueryType queryType, String queryName) {
98          _identifier = identifier;
99          _queryType = queryType;
100         _queryName = queryName;
101     }
102 
103     private List<Object> _arguments = new ArrayList<Object>();
104     private String _identifier;
105     private String _queryName;
106     private QueryType _queryType;
107 
108 }