-
Notifications
You must be signed in to change notification settings - Fork 2
/
Properties.java
266 lines (234 loc) · 7.76 KB
/
Properties.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2011-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 org.appng.application.manager.business;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.ActionProvider;
import org.appng.api.BusinessException;
import org.appng.api.DataContainer;
import org.appng.api.DataProvider;
import org.appng.api.Environment;
import org.appng.api.FieldProcessor;
import org.appng.api.Options;
import org.appng.api.Request;
import org.appng.api.model.Application;
import org.appng.api.model.Property;
import org.appng.api.model.Property.Type;
import org.appng.api.model.SimpleProperty;
import org.appng.api.model.Site;
import org.appng.application.manager.MessageConstants;
import org.appng.application.manager.form.PropertyForm;
import org.appng.application.manager.service.Service;
import org.appng.application.manager.service.ServiceAware;
import org.appng.core.domain.PropertyImpl;
import org.appng.xml.platform.FieldDef;
import org.appng.xml.platform.FieldType;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
/**
* Provides CRUD-operations for a {@link PropertyImpl}.
*
* @author Matthias Müller
*/
@Slf4j
@Component
public class Properties extends ServiceAware implements ActionProvider<PropertyForm>, DataProvider {
private static final String PROPERTY = "property";
private static final String PROPERTIES = "properties";
public DataContainer getData(Site site, Application application, Environment environment, Options options,
Request request, FieldProcessor fp) {
Service service = getService();
Integer siteId = options.getInteger(PROPERTIES, "siteId");
Integer applicationId = options.getInteger(PROPERTIES, "applicationId");
String propertyName = options.getString(PROPERTY, "id");
DataContainer data = null;
if (ACTION_CREATE.equals(getAction(options))) {
data = service.getNewProperty(fp);
} else {
try {
data = service.searchProperties(fp, siteId, applicationId, propertyName);
if (null != data.getItem()) {
PropertyForm propertyForm = (PropertyForm) data.getItem();
PropertyWrapper propertyWrapper = new PropertyWrapper(propertyForm.getProperty());
Type type = propertyWrapper.getType();
FieldType expectedType = getFieldTypeForPropertyType(type);
FieldDef field = fp.getField("property.value");
field.setType(expectedType);
if (FieldType.DECIMAL.equals(expectedType)) {
String value = propertyWrapper.getActualString();
int dotIdx = value.indexOf('.');
int fraction = dotIdx > 0 ? value.substring(dotIdx + 1).length() : 3;
field.setFormat("#." + StringUtils.repeat('#', fraction));
}
field.getLabel().setValue(propertyWrapper.getShortName());
propertyForm.setProperty(propertyWrapper);
} else {
@SuppressWarnings("unchecked")
Page<PropertyImpl> page = (Page<PropertyImpl>) data.getPage();
data.setPage(page.map(p -> new PropertyWrapper(p, true)));
}
} catch (BusinessException ex) {
String message = request.getMessage(ex.getMessageKey(), ex.getMessageArgs());
log.error(message, ex);
fp.addErrorMessage(message);
}
}
return data;
}
private FieldType getFieldTypeForPropertyType(Type type) {
switch (type) {
case INT:
return FieldType.INT;
case DECIMAL:
return FieldType.DECIMAL;
case BOOLEAN:
return FieldType.CHECKBOX;
case PASSWORD:
return FieldType.PASSWORD;
case MULTILINE:
return FieldType.LONGTEXT;
default:
return FieldType.TEXT;
}
}
public void perform(Site site, Application application, Environment environment, Options options, Request request,
PropertyForm propertyForm, FieldProcessor fp) {
String action = getAction(options);
String errorMessage = null;
String okMessage = null;
Service service = getService();
String propertyName = options.getString(PROPERTY, ID);
try {
if (ACTION_CREATE.equals(action)) {
errorMessage = MessageConstants.PROPERTY_CREATE_ERROR;
Integer siteId = options.getInteger(PROPERTIES, "siteId");
Integer applicationId = options.getInteger(PROPERTIES, "applicationId");
service.createProperty(request, propertyForm, siteId, applicationId, fp);
okMessage = MessageConstants.PROPERTY_CREATED;
} else if (ACTION_UPDATE.equals(action)) {
errorMessage = MessageConstants.PROPERTY_UPDATE_ERROR;
propertyForm.getProperty().setName(propertyName);
service.updateProperty(request, propertyForm, fp);
okMessage = MessageConstants.PROPERTY_UPDATED;
} else if (ACTION_DELETE.equals(action)) {
errorMessage = MessageConstants.PROPERTY_DELETE_ERROR;
service.deleteProperty(request, propertyName, fp);
okMessage = MessageConstants.PROPERTY_DELETED;
}
String message = request.getMessage(okMessage, propertyName);
fp.addOkMessage(message);
} catch (BusinessException ex) {
String message = request.getMessage(errorMessage, propertyName);
log.error(message, ex);
fp.addErrorMessage(message);
}
}
public class PropertyWrapper extends PropertyImpl {
private SimpleProperty property;
private boolean hidePassword;
PropertyWrapper(SimpleProperty property) {
this(property, true);
}
PropertyWrapper(SimpleProperty property, boolean hidePassword) {
this.property = property;
this.hidePassword = hidePassword;
}
public String getDisplayValue() {
if (Property.Type.MULTILINE.equals(getType())) {
return getClob();
}
return getActualString();
}
@Override
public String getActualString() {
String stringValue = getString();
if (StringUtils.isNotBlank(stringValue) && hidePassword && Property.Type.PASSWORD.equals(getType())) {
return stringValue.substring(0, 2) + StringUtils.repeat('*', stringValue.length() - 4)
+ stringValue.substring(stringValue.length() - 2);
}
return stringValue;
}
@Override
public Boolean getChangedValue() {
return property.getChangedValue();
}
public String getShortName() {
return getName().substring(getName().lastIndexOf('.') + 1);
}
@Override
public String getString() {
return property.getString();
}
@Override
public Boolean getBoolean() {
return property.getBoolean();
}
public void setBoolean(Boolean value) {
property.setActualString(value.toString());
}
@Override
public Integer getInteger() {
return property.getInteger();
}
public void setInteger(Integer value) {
property.setActualString(value.toString());
}
@Override
public Float getFloat() {
return property.getFloat();
}
@Override
public Double getDouble() {
return property.getDouble();
}
@Override
public byte[] getBlob() {
return property.getBlob();
}
@Override
public String getClob() {
return property.getClob();
}
@Override
public String getName() {
return property.getName();
}
@Override
public boolean isMandatory() {
return property.isMandatory();
}
@Override
public String getDefaultString() {
return property.getDefaultString();
}
@Override
public Type getType() {
return property.getType();
}
@Override
public Object getValue() {
return property.getValue();
}
@Override
public void setValue(Object value) {
property.setValue(value);
}
@Override
public String getDescription() {
return property.getDescription();
}
}
}