forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateBuilder.java
More file actions
executable file
·147 lines (124 loc) · 5.39 KB
/
Copy pathUpdateBuilder.java
File metadata and controls
executable file
·147 lines (124 loc) · 5.39 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import com.cloud.utils.Ternary;
import com.cloud.utils.exception.CloudRuntimeException;
public class UpdateBuilder implements MethodInterceptor {
protected Map<String, Ternary<Attribute, Boolean, Object>> _changes;
protected HashMap<Attribute, Object> _collectionChanges;
protected GenericDaoBase<?, ?> _dao;
protected UpdateBuilder(GenericDaoBase<?, ?> dao) {
_dao = dao;
_changes = new HashMap<String, Ternary<Attribute, Boolean, Object>>();
}
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
String name = method.getName();
if (name.startsWith("set")) {
String field = methodToField(name, 3);
makeChange(field, args[0]);
} else if (name.startsWith("incr")) {
makeIncrChange(name, args);
} else if (name.startsWith("decr")) {
makeDecrChange(name, args);
}
return methodProxy.invokeSuper(object, args);
}
private final String methodToField(String method, int start) {
char[] chs = method.toCharArray();
chs[start] = Character.toLowerCase(chs[start]);
return new String(chs, start, chs.length - start);
}
protected Attribute makeChange(String field, Object value) {
Attribute attr = _dao._allAttributes.get(field);
assert (attr == null || attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field;
if (attr != null) {
if (attr.attache == null) {
_changes.put(field, new Ternary<Attribute, Boolean, Object>(attr, null, value));
} else {
if (_collectionChanges == null) {
_collectionChanges = new HashMap<Attribute, Object>();
}
_collectionChanges.put(attr, value);
}
}
return attr;
}
protected void makeIncrChange(String method, Object[] args) {
String field = methodToField(method, 4);
Attribute attr = _dao._allAttributes.get(field);
assert (attr != null && attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field;
incr(attr, args == null || args.length == 0 ? 1 : args[0]);
}
protected void makeDecrChange(String method, Object[] args) {
String field = methodToField(method, 4);
Attribute attr = _dao._allAttributes.get(field);
assert (attr != null && attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field;
decr(attr, args == null || args.length == 0 ? 1 : args[0]);
}
public void set(Object entity, String name, Object value) {
Attribute attr = makeChange(name, value);
set(entity, attr, value);
}
public void set(Object entity, Attribute attr, Object value) {
_changes.put(attr.field.getName(), new Ternary<Attribute, Boolean, Object>(attr, null, value));
try {
attr.field.set(entity, value);
} catch (IllegalArgumentException e) {
throw new CloudRuntimeException("Unable to update " + attr.field.getName() + " with " + value, e);
} catch (IllegalAccessException e) {
throw new CloudRuntimeException("Unable to update " + attr.field.getName() + " with " + value, e);
}
}
public void incr(Attribute attr, Object value) {
_changes.put(attr.field.getName(), new Ternary<Attribute, Boolean, Object>(attr, true, value));
}
public void decr(Attribute attr, Object value) {
_changes.put(attr.field.getName(), new Ternary<Attribute, Boolean, Object>(attr, false, value));
}
public boolean hasChanges() {
return (_changes.size() + (_collectionChanges != null ? _collectionChanges.size() : 0)) != 0;
}
public boolean has(String name) {
return _changes.containsKey(name);
}
public Map<Attribute, Object> getCollectionChanges() {
return _collectionChanges;
}
public void clear() {
_changes.clear();
if (_collectionChanges != null) {
_collectionChanges.clear();
_collectionChanges = null;
}
}
public StringBuilder toSql(String tables) {
if (_changes.isEmpty()) {
return null;
}
return SqlGenerator.buildMysqlUpdateSql(tables, _changes.values());
}
public Collection<Ternary<Attribute, Boolean, Object>> getChanges() {
return _changes.values();
}
}