1 | /* |
2 | * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
3 | * |
4 | * WSO2 Inc. licenses this file to you under the Apache License, |
5 | * Version 2.0 (the "License"); you may not use this file except |
6 | * in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, |
12 | * software distributed under the License is distributed on an |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | * KIND, either express or implied. See the License for the |
15 | * specific language governing permissions and limitations |
16 | * under the License. |
17 | */ |
18 | package org.wso2.siddhi.query.api.query.projection; |
19 | |
20 | import org.wso2.siddhi.query.api.condition.Condition; |
21 | import org.wso2.siddhi.query.api.exception.AttributeAlreadyExist; |
22 | import org.wso2.siddhi.query.api.expression.Expression; |
23 | import org.wso2.siddhi.query.api.expression.Variable; |
24 | import org.wso2.siddhi.query.api.query.projection.attribute.AggregationAttribute; |
25 | import org.wso2.siddhi.query.api.query.projection.attribute.OutputAttribute; |
26 | import org.wso2.siddhi.query.api.query.projection.attribute.SimpleAttribute; |
27 | |
28 | import java.util.ArrayList; |
29 | import java.util.List; |
30 | |
31 | public class Projector { |
32 | private List<OutputAttribute> projectionList = new ArrayList<OutputAttribute>(); |
33 | private List<Variable> groupByList = new ArrayList<Variable>(); |
34 | private Call call; |
35 | private Condition havingCondition; |
36 | |
37 | public Projector project(String rename, Expression expression) { |
38 | OutputAttribute outputAttribute = new SimpleAttribute(rename, expression); |
39 | checkProjection(outputAttribute); |
40 | projectionList.add(outputAttribute); |
41 | return this; |
42 | } |
43 | |
44 | private void checkProjection(OutputAttribute newAttribute) { |
45 | for (OutputAttribute attribute : projectionList) { |
46 | if (attribute.getRename().equals(newAttribute.getRename())) { |
47 | throw new AttributeAlreadyExist(attribute.getRename() + " is already defined as an output attribute "); |
48 | } |
49 | } |
50 | } |
51 | |
52 | public Projector project(String rename, String aggregationName, Expression... expressions) { |
53 | OutputAttribute outputAttribute = new AggregationAttribute(rename, aggregationName, expressions); |
54 | checkProjection(outputAttribute); |
55 | projectionList.add(outputAttribute); |
56 | return this; |
57 | } |
58 | |
59 | public Projector having(Condition condition) { |
60 | havingCondition = condition; |
61 | return this; |
62 | } |
63 | |
64 | public Projector groupBy(String streamId, String attributeName) { |
65 | groupByList.add(new Variable(streamId, attributeName)); |
66 | return this; |
67 | } |
68 | |
69 | public Projector groupBy(String attributeName) { |
70 | groupByList.add(new Variable(attributeName)); |
71 | return this; |
72 | } |
73 | |
74 | public Projector addGroupByList(List<Variable> list) { |
75 | if(list!=null){ |
76 | groupByList.addAll(list); |
77 | } |
78 | return this; |
79 | } |
80 | |
81 | |
82 | public Projector call(String callName, Object... parameters) { |
83 | call = new Call(callName, parameters); |
84 | return this; |
85 | } |
86 | |
87 | public List<OutputAttribute> getProjectionList() { |
88 | return projectionList; |
89 | } |
90 | |
91 | public List<Variable> getGroupByList() { |
92 | return groupByList; |
93 | } |
94 | |
95 | public Call getCall() { |
96 | return call; |
97 | } |
98 | |
99 | public Condition getHavingCondition() { |
100 | return havingCondition; |
101 | } |
102 | |
103 | public Projector addProjectionList(List<OutputAttribute> projectionList) { |
104 | for (OutputAttribute outputAttribute : projectionList) { |
105 | checkProjection(outputAttribute); |
106 | this.projectionList.add(outputAttribute); |
107 | } |
108 | return this; |
109 | } |
110 | } |