forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter.java
More file actions
executable file
·115 lines (98 loc) · 3.28 KB
/
Filter.java
File metadata and controls
executable file
·115 lines (98 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.db;
import java.lang.reflect.Field;
import javax.persistence.Column;
import com.cloud.utils.Pair;
import com.cloud.utils.ReflectUtil;
/**
* Try to use static initialization to help you in finding incorrect
* field names being passed in early.
*
* Something like the following:
* protected static final Filter s_NameFilter = new Filter(VMInstanceVO, name, true, null, null);
*
* Filter nameFilter = new Filter(s_nameFilter);
*
*/
public class Filter {
Long _offset;
Long _limit;
String _orderBy;
/**
* @param clazz the VO object type
* @param field name of the field
* @param offset
* @param limit
*/
public Filter(Class<?> clazz, String field, boolean ascending, Long offset, Long limit) {
_offset = offset;
_limit = limit;
addOrderBy(clazz, field, ascending);
}
public Filter(long limit) {
_orderBy = " ORDER BY RAND() LIMIT " + limit;
}
/**
* Note that this copy constructor does not copy offset and limit.
* @param that filter
*/
public Filter(Filter that) {
this._orderBy = that._orderBy;
this._limit = null;
that._limit = null;
}
public void addOrderBy(Class<?> clazz, String field, boolean ascending) {
if (field == null) {
return;
}
Field f;
Pair<Class<?>, Field> pair = ReflectUtil.getAnyField(clazz, field);
assert (pair != null) : "Can't find field " + field + " in " + clazz.getName();
clazz = pair.first();
f = pair.second();
Column column = f.getAnnotation(Column.class);
String name = column != null ? column.name() : field;
StringBuilder order = new StringBuilder();
if (column.table() == null || column.table().length() == 0) {
order.append(DbUtil.getTableName(clazz));
} else {
order.append(column.table());
}
order.append(".").append(name).append(ascending ? " ASC " : " DESC ");
if (_orderBy == null) {
_orderBy = order.insert(0, " ORDER BY ").toString();
} else {
_orderBy = order.insert(0, _orderBy).toString();
}
}
public String getOrderBy() {
return _orderBy;
}
public void setOffset(Long offset) {
_offset = offset;
}
public Long getOffset() {
return _offset;
}
public Long getLimit() {
return _limit;
}
public void setLimit(Long limit) {
_limit = limit;
}
}