/
SystemSettingResource1_9.java
348 lines (315 loc) · 12.3 KB
/
SystemSettingResource1_9.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9;
import java.util.List;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.StringProperty;
import org.apache.commons.lang.StringUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.api.APIException;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.customdatatype.CustomDatatype;
import org.openmrs.customdatatype.CustomDatatypeUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.RefRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
/**
* {@link Resource} for {@link GlobalProperty}, supporting standard CRUD operations
*/
@Resource(name = RestConstants.VERSION_1 + "/systemsetting", supportedClass = GlobalProperty.class, supportedOpenmrsVersions = {
"1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*", "2.3.*", "2.4.*" })
public class SystemSettingResource1_9 extends DelegatingCrudResource<GlobalProperty> {
public static final String GENERAL = "General Settings";
/**
* @see DelegatingCrudResource#getRepresentationDescription(Representation)
*/
@Override
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
if (rep instanceof RefRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("display");
description.addSelfLink();
return description;
} else if (rep instanceof DefaultRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("property");
description.addProperty("value");
description.addProperty("description");
description.addProperty("display");
description.addSelfLink();
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
return description;
} else if (rep instanceof FullRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("property");
description.addProperty("value");
description.addProperty("description");
description.addProperty("display");
description.addProperty("datatypeClassname");
description.addProperty("datatypeConfig");
description.addProperty("preferredHandlerClassname");
description.addProperty("handlerConfig");
description.addSelfLink();
return description;
}
return null;
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
*/
@Override
public DelegatingResourceDescription getCreatableProperties() {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addRequiredProperty("property");
description.addProperty("description");
description.addProperty("datatypeClassname");
description.addProperty("datatypeConfig");
description.addProperty("preferredHandlerClassname");
description.addProperty("handlerConfig");
description.addProperty("value");
return description;
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getUpdatableProperties()
*/
@Override
public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException {
DelegatingResourceDescription description = getCreatableProperties();
description.removeProperty("property");
return description;
}
@Override
public Model getGETModel(Representation rep) {
ModelImpl model = ((ModelImpl) super.getGETModel(rep));
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
model
.property("uuid", new StringProperty())
.property("property", new StringProperty())
.property("value", new StringProperty())
.property("description", new StringProperty())
.property("display", new StringProperty());
}
if (rep instanceof FullRepresentation) {
model
.property("datatypeClassname", new StringProperty())
.property("datatypeConfig", new StringProperty())
.property("preferredHandlerClassname", new StringProperty())
.property("handlerConfig", new StringProperty());
}
return model;
}
@Override
public Model getCREATEModel(Representation rep) {
return new ModelImpl()
.property("property", new StringProperty())
.property("description", new StringProperty())
.property("datatypeClassname", new StringProperty())
.property("datatypeConfig", new StringProperty())
.property("preferredHandlerClassname", new StringProperty())
.property("handlerConfig", new StringProperty())
.property("value", new StringProperty())
.required("property");
}
@Override
public Model getUPDATEModel(Representation rep) {
Model model = getCREATEModel(rep);
model.getProperties().remove("property");
return model;
}
/**
* @see DelegatingCrudResource#newDelegate()
*/
@Override
public GlobalProperty newDelegate() {
return new GlobalProperty();
}
/**
* @see DelegatingCrudResource#save(java.lang.Object)
*/
@Override
public GlobalProperty save(GlobalProperty property) {
return Context.getAdministrationService().saveGlobalProperty(property);
}
/**
* Fetches a global property by uuid, if no match is found, it tries to look up one with a
* matching name with the assumption that the passed parameter is a global property name
*
* @see DelegatingCrudResource#getByUniqueId(java.lang.String)
*/
@Override
public GlobalProperty getByUniqueId(String uuid) {
GlobalProperty gp = Context.getAdministrationService().getGlobalPropertyByUuid(uuid);
if (gp == null) {
//We assume the caller is fetching by name
gp = Context.getAdministrationService().getGlobalPropertyObject(uuid);
}
return gp;
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#delete(java.lang.Object,
* java.lang.String, org.openmrs.module.webservices.rest.web.RequestContext)
*/
@Override
protected void delete(GlobalProperty property, String reason, RequestContext context) throws ResponseException {
if (property == null) {
return;
}
Context.getAdministrationService().purgeGlobalProperty(property);
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#purge(java.lang.Object,
* org.openmrs.module.webservices.rest.web.RequestContext)
*/
@Override
public void purge(GlobalProperty property, RequestContext context) throws ResponseException {
if (property == null) {
return;
}
Context.getAdministrationService().purgeGlobalProperty(property);
}
/**
* Get all the global properties
*
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext)
*/
@Override
protected NeedsPaging<GlobalProperty> doGetAll(RequestContext context) {
List<GlobalProperty> allGlobalPropertys = Context.getAdministrationService().getAllGlobalProperties();
return new NeedsPaging<GlobalProperty>(allGlobalPropertys, context);
}
/**
* GlobalProperty searches support the following additional query parameters:
*
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(RequestContext)
*/
@Override
protected PageableResult doSearch(RequestContext context) {
AdministrationService service = Context.getAdministrationService();
List<GlobalProperty> searchResults;
searchResults = service.getGlobalPropertiesByPrefix(context.getParameter("q"));
PageableResult result = new NeedsPaging<GlobalProperty>(searchResults, context);
return result;
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion()
*/
@Override
public String getResourceVersion() {
return "1.9";
}
/**
* Gets the display name of the global property delegate
*
* @param instance the delegate instance to get the display name off
* @return string as "section - name = value"
*/
@PropertyGetter("display")
public static String getDisplayString(GlobalProperty globalProperty) {
return getSection(globalProperty) + " - " + getName(globalProperty) + " = " + globalProperty.getValue();
}
/**
* @return the section
*/
private static String getSection(GlobalProperty globalProperty) {
String section = GENERAL;
int sectionEnd = globalProperty.getProperty().indexOf(".");
if (sectionEnd > 0) {
section = globalProperty.getProperty().substring(0, sectionEnd);
section = beautify(section);
}
return section;
}
/**
* @return the name
*/
private static String getName(GlobalProperty globalProperty) {
String name = globalProperty.getProperty();
int sectionEnd = globalProperty.getProperty().indexOf(".");
if (sectionEnd > 0) {
name = globalProperty.getProperty().substring(sectionEnd + 1);
}
name = beautify(name);
return name;
}
/**
* Beautifies a string
*
* @param section
* @return
*/
private static String beautify(String section) {
section = section.replace("_", " ");
section = section.replace(".", " ");
String[] sections = StringUtils.splitByCharacterTypeCamelCase(section);
section = StringUtils.join(sections, " ");
sections = StringUtils.split(section);
for (int i = 0; i < sections.length; i++) {
sections[i] = StringUtils.capitalize(sections[i]);
}
section = StringUtils.join(sections, " ");
return section;
}
/**
* Gets the value of the global property delegate
*
* @param instance the delegate instance to get the value off
* @return value object
*/
@PropertyGetter("value")
public static Object getValue(GlobalProperty globalProperty) {
if (StringUtils.isNotEmpty(globalProperty.getDatatypeClassname())
&& StringUtils.isNotEmpty(globalProperty.getDatatypeConfig())) {
return globalProperty.getValue();
} else {
return globalProperty.getPropertyValue();
}
}
/**
* Sets value for given property.
*
* @param property
* @param value
*/
@PropertySetter("value")
public static void setValue(GlobalProperty property, String value) throws Exception {
if (StringUtils.isNotEmpty(property.getDatatypeClassname())) {
CustomDatatype customDataType = CustomDatatypeUtil.getDatatype(property.getDatatypeClassname(),
property.getDatatypeConfig());
if (customDataType != null) {
try {
property.setValue(customDataType.fromReferenceString(value));
}
catch (Exception ex) {
throw new APIException("Exception in converting value to custom data type", ex);
}
} else
throw new APIException("Custom data type is null as per provided parameters");
} else {
property.setPropertyValue(value);
}
}
}