Skip to content

Commit 8741da1

Browse files
authored
O3-4473 - NPE when trying to add a new queue entry via POST /visit-queue-entry (#81)
1 parent 40678a7 commit 8741da1

5 files changed

Lines changed: 124 additions & 58 deletions

File tree

omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryResource.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818

1919
import io.swagger.models.Model;
2020
import io.swagger.models.ModelImpl;
21-
import io.swagger.models.properties.*;
22-
import lombok.Getter;
21+
import io.swagger.models.properties.BooleanProperty;
22+
import io.swagger.models.properties.DateProperty;
23+
import io.swagger.models.properties.DoubleProperty;
24+
import io.swagger.models.properties.RefProperty;
25+
import io.swagger.models.properties.StringProperty;
26+
import lombok.Setter;
2327
import lombok.extern.slf4j.Slf4j;
2428
import org.openmrs.PersonName;
2529
import org.openmrs.api.context.Context;
@@ -51,16 +55,14 @@
5155
@SuppressWarnings("unused")
5256
@Resource(name = RestConstants.VERSION_1 + "/queue-entry", supportedClass = QueueEntry.class, supportedOpenmrsVersions = {
5357
"2.3 - 9.*" })
58+
@Setter
5459
public class QueueEntryResource extends DelegatingCrudResource<QueueEntry> {
5560

56-
private final QueueServicesWrapper services;
61+
private QueueServicesWrapper services;
5762

58-
@Getter
59-
private final QueueEntrySearchCriteriaParser searchCriteriaParser;
63+
private QueueEntrySearchCriteriaParser searchCriteriaParser;
6064

6165
public QueueEntryResource() {
62-
services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
63-
searchCriteriaParser = Context.getRegisteredComponents(QueueEntrySearchCriteriaParser.class).get(0);
6466
}
6567

6668
public QueueEntryResource(QueueServicesWrapper services, QueueEntrySearchCriteriaParser searchCriteriaParser) {
@@ -70,7 +72,7 @@ public QueueEntryResource(QueueServicesWrapper services, QueueEntrySearchCriteri
7072

7173
@Override
7274
public QueueEntry getByUniqueId(@NotNull String uuid) {
73-
Optional<QueueEntry> queueEntryOptional = services.getQueueEntryService().getQueueEntryByUuid(uuid);
75+
Optional<QueueEntry> queueEntryOptional = getServices().getQueueEntryService().getQueueEntryByUuid(uuid);
7476
if (!queueEntryOptional.isPresent()) {
7577
throw new ObjectNotFoundException("Could not find visit queue entry with uuid " + uuid);
7678
}
@@ -79,7 +81,7 @@ public QueueEntry getByUniqueId(@NotNull String uuid) {
7981

8082
@Override
8183
protected void delete(QueueEntry qe, String reason, RequestContext requestContext) throws ResponseException {
82-
services.getQueueEntryService().voidQueueEntry(qe, reason);
84+
getServices().getQueueEntryService().voidQueueEntry(qe, reason);
8385
}
8486

8587
@Override
@@ -89,27 +91,27 @@ public QueueEntry newDelegate() {
8991

9092
@Override
9193
public QueueEntry save(QueueEntry queueEntry) {
92-
return services.getQueueEntryService().saveQueueEntry(queueEntry);
94+
return getServices().getQueueEntryService().saveQueueEntry(queueEntry);
9395
}
9496

9597
@Override
9698
public void purge(QueueEntry queueEntry, RequestContext requestContext) throws ResponseException {
97-
services.getQueueEntryService().purgeQueueEntry(queueEntry);
99+
getServices().getQueueEntryService().purgeQueueEntry(queueEntry);
98100
}
99101

100102
@Override
101103
protected PageableResult doGetAll(RequestContext requestContext) throws ResponseException {
102104
QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria();
103-
List<QueueEntry> activeEntries = services.getQueueEntryService().getQueueEntries(criteria);
105+
List<QueueEntry> activeEntries = getServices().getQueueEntryService().getQueueEntries(criteria);
104106
return new NeedsPaging<>(new ArrayList<>(activeEntries), requestContext);
105107
}
106108

107109
@Override
108110
@SuppressWarnings("unchecked")
109111
protected PageableResult doSearch(RequestContext requestContext) {
110112
Map<String, String[]> parameters = requestContext.getRequest().getParameterMap();
111-
QueueEntrySearchCriteria criteria = searchCriteriaParser.constructFromRequest(parameters);
112-
List<QueueEntry> queueEntries = services.getQueueEntryService().getQueueEntries(criteria);
113+
QueueEntrySearchCriteria criteria = getSearchCriteriaParser().constructFromRequest(parameters);
114+
List<QueueEntry> queueEntries = getServices().getQueueEntryService().getQueueEntries(criteria);
113115
return new NeedsPaging<>(queueEntries, requestContext);
114116
}
115117

@@ -285,11 +287,25 @@ public String getDisplay(QueueEntry queueEntry) {
285287

286288
@PropertyGetter("previousQueueEntry")
287289
public QueueEntry getPreviousQueueEntry(QueueEntry queueEntry) {
288-
return services.getQueueEntryService().getPreviousQueueEntry(queueEntry);
290+
return getServices().getQueueEntryService().getPreviousQueueEntry(queueEntry);
289291
}
290292

291293
@Override
292294
public String getResourceVersion() {
293295
return "2.3";
294296
}
297+
298+
public QueueServicesWrapper getServices() {
299+
if (services == null) {
300+
services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
301+
}
302+
return services;
303+
}
304+
305+
public QueueEntrySearchCriteriaParser getSearchCriteriaParser() {
306+
if (searchCriteriaParser == null) {
307+
searchCriteriaParser = Context.getRegisteredComponents(QueueEntrySearchCriteriaParser.class).get(0);
308+
}
309+
return searchCriteriaParser;
310+
}
295311
}

omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntrySubResource.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.swagger.models.Model;
2020
import io.swagger.models.ModelImpl;
2121
import io.swagger.models.properties.*;
22+
import lombok.Setter;
2223
import org.openmrs.api.context.Context;
2324
import org.openmrs.module.queue.api.QueueServicesWrapper;
2425
import org.openmrs.module.queue.api.search.QueueEntrySearchCriteria;
@@ -44,12 +45,12 @@
4445
@SuppressWarnings("unused")
4546
@SubResource(parent = QueueResource.class, path = "entry", supportedClass = QueueEntry.class, supportedOpenmrsVersions = {
4647
"2.3 - 9.*" }, order = 10)
48+
@Setter
4749
public class QueueEntrySubResource extends DelegatingSubResource<QueueEntry, Queue, QueueResource> {
4850

49-
private final QueueServicesWrapper services;
51+
private QueueServicesWrapper services;
5052

5153
public QueueEntrySubResource() {
52-
this.services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
5354
}
5455

5556
public QueueEntrySubResource(QueueServicesWrapper services) {
@@ -71,13 +72,13 @@ public PageableResult doGetAll(Queue queue, RequestContext requestContext) throw
7172
QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria();
7273
criteria.setQueues(Collections.singletonList(queue));
7374
criteria.setIsEnded(false);
74-
Collection<QueueEntry> queueEntries = services.getQueueEntryService().getQueueEntries(criteria);
75+
Collection<QueueEntry> queueEntries = getServices().getQueueEntryService().getQueueEntries(criteria);
7576
return new NeedsPaging<>(new ArrayList<>(queueEntries), requestContext);
7677
}
7778

7879
@Override
7980
public QueueEntry getByUniqueId(@NotNull String uuid) {
80-
Optional<QueueEntry> queueEntryOptional = services.getQueueEntryService().getQueueEntryByUuid(uuid);
81+
Optional<QueueEntry> queueEntryOptional = getServices().getQueueEntryService().getQueueEntryByUuid(uuid);
8182
if (!queueEntryOptional.isPresent()) {
8283
throw new ObjectNotFoundException("Could not find queue entry with UUID " + uuid);
8384
}
@@ -86,7 +87,7 @@ public QueueEntry getByUniqueId(@NotNull String uuid) {
8687

8788
@Override
8889
protected void delete(QueueEntry queueEntry, String voidReason, RequestContext requestContext) throws ResponseException {
89-
services.getQueueEntryService().voidQueueEntry(queueEntry, voidReason);
90+
getServices().getQueueEntryService().voidQueueEntry(queueEntry, voidReason);
9091
}
9192

9293
@Override
@@ -96,12 +97,12 @@ public QueueEntry newDelegate() {
9697

9798
@Override
9899
public QueueEntry save(QueueEntry queueEntry) {
99-
return services.getQueueEntryService().saveQueueEntry(queueEntry);
100+
return getServices().getQueueEntryService().saveQueueEntry(queueEntry);
100101
}
101102

102103
@Override
103104
public void purge(QueueEntry queueEntry, RequestContext requestContext) throws ResponseException {
104-
services.getQueueEntryService().purgeQueueEntry(queueEntry);
105+
getServices().getQueueEntryService().purgeQueueEntry(queueEntry);
105106
}
106107

107108
@Override
@@ -238,4 +239,11 @@ public String getDisplay(QueueEntry queueEntry) {
238239
public String getResourceVersion() {
239240
return "2.3";
240241
}
242+
243+
public QueueServicesWrapper getServices() {
244+
if (services == null) {
245+
services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
246+
}
247+
return services;
248+
}
241249
}

omod/src/main/java/org/openmrs/module/queue/web/resources/QueueResource.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.swagger.models.ModelImpl;
2121
import io.swagger.models.properties.RefProperty;
2222
import io.swagger.models.properties.StringProperty;
23+
import lombok.Setter;
2324
import org.openmrs.api.context.Context;
2425
import org.openmrs.module.queue.api.QueueServicesWrapper;
2526
import org.openmrs.module.queue.api.search.QueueSearchCriteria;
@@ -45,15 +46,14 @@
4546
@SuppressWarnings("unused")
4647
@Resource(name = RestConstants.VERSION_1 + "/queue", supportedClass = Queue.class, supportedOpenmrsVersions = {
4748
"2.3 - 9.*" })
49+
@Setter
4850
public class QueueResource extends DelegatingCrudResource<Queue> {
4951

50-
private final QueueServicesWrapper services;
52+
private QueueServicesWrapper services;
5153

52-
private final QueueSearchCriteriaParser searchCriteriaParser;
54+
private QueueSearchCriteriaParser searchCriteriaParser;
5355

5456
public QueueResource() {
55-
this.services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
56-
this.searchCriteriaParser = Context.getRegisteredComponents(QueueSearchCriteriaParser.class).get(0);
5757
}
5858

5959
public QueueResource(QueueServicesWrapper services, QueueSearchCriteriaParser searchCriteriaParser) {
@@ -63,12 +63,12 @@ public QueueResource(QueueServicesWrapper services, QueueSearchCriteriaParser se
6363

6464
@Override
6565
public NeedsPaging<Queue> doGetAll(RequestContext requestContext) throws ResponseException {
66-
return new NeedsPaging<>(new ArrayList<>(services.getQueueService().getAllQueues()), requestContext);
66+
return new NeedsPaging<>(new ArrayList<>(getServices().getQueueService().getAllQueues()), requestContext);
6767
}
6868

6969
@Override
7070
public Queue getByUniqueId(@NotNull String uuid) {
71-
Optional<Queue> optionalQueue = services.getQueueService().getQueueByUuid(uuid);
71+
Optional<Queue> optionalQueue = getServices().getQueueService().getQueueByUuid(uuid);
7272
if (!optionalQueue.isPresent()) {
7373
throw new ObjectNotFoundException("Could not find queue with UUID " + uuid);
7474
}
@@ -77,11 +77,11 @@ public Queue getByUniqueId(@NotNull String uuid) {
7777

7878
@Override
7979
protected void delete(Queue queue, String retireReason, RequestContext requestContext) throws ResponseException {
80-
Optional<Queue> optionalQueue = services.getQueueService().getQueueByUuid(queue.getUuid());
80+
Optional<Queue> optionalQueue = getServices().getQueueService().getQueueByUuid(queue.getUuid());
8181
if (!optionalQueue.isPresent()) {
8282
throw new ObjectNotFoundException("Could not find queue with uuid " + queue.getUuid());
8383
}
84-
services.getQueueService().retireQueue(queue, retireReason);
84+
getServices().getQueueService().retireQueue(queue, retireReason);
8585
}
8686

8787
@Override
@@ -91,12 +91,12 @@ public Queue newDelegate() {
9191

9292
@Override
9393
public Queue save(Queue queue) {
94-
return services.getQueueService().saveQueue(queue);
94+
return getServices().getQueueService().saveQueue(queue);
9595
}
9696

9797
@Override
9898
public void purge(Queue queue, RequestContext requestContext) throws ResponseException {
99-
services.getQueueService().purgeQueue(queue);
99+
getServices().getQueueService().purgeQueue(queue);
100100
}
101101

102102
@Override
@@ -205,8 +205,8 @@ public Model getUPDATEModel(Representation rep) {
205205
@SuppressWarnings("unchecked")
206206
protected PageableResult doSearch(RequestContext requestContext) {
207207
Map<String, String[]> parameters = requestContext.getRequest().getParameterMap();
208-
QueueSearchCriteria criteria = searchCriteriaParser.constructFromRequest(parameters);
209-
List<Queue> queueEntries = services.getQueueService().getQueues(criteria);
208+
QueueSearchCriteria criteria = getSearchCriteriaParser().constructFromRequest(parameters);
209+
List<Queue> queueEntries = getServices().getQueueService().getQueues(criteria);
210210
return new NeedsPaging<>(queueEntries, requestContext);
211211
}
212212

@@ -217,16 +217,30 @@ public String getDisplay(Queue queue) {
217217

218218
@PropertyGetter("allowedPriorities")
219219
public Object getAllowedPriorities(Queue delegate) {
220-
return services.getAllowedPriorities(delegate);
220+
return getServices().getAllowedPriorities(delegate);
221221
}
222222

223223
@PropertyGetter("allowedStatuses")
224224
public Object getAllowedStatuses(Queue delegate) {
225-
return services.getAllowedStatuses(delegate);
225+
return getServices().getAllowedStatuses(delegate);
226226
}
227227

228228
@Override
229229
public String getResourceVersion() {
230230
return "2.3";
231231
}
232+
233+
public QueueServicesWrapper getServices() {
234+
if (services == null) {
235+
services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
236+
}
237+
return services;
238+
}
239+
240+
public QueueSearchCriteriaParser getSearchCriteriaParser() {
241+
if (searchCriteriaParser == null) {
242+
searchCriteriaParser = Context.getRegisteredComponents(QueueSearchCriteriaParser.class).get(0);
243+
}
244+
return searchCriteriaParser;
245+
}
232246
}

omod/src/main/java/org/openmrs/module/queue/web/resources/QueueRoomResource.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.swagger.models.ModelImpl;
1919
import io.swagger.models.properties.RefProperty;
2020
import io.swagger.models.properties.StringProperty;
21+
import lombok.Setter;
2122
import org.openmrs.api.context.Context;
2223
import org.openmrs.module.queue.api.QueueServicesWrapper;
2324
import org.openmrs.module.queue.api.search.QueueRoomSearchCriteria;
@@ -43,15 +44,14 @@
4344
@SuppressWarnings("unused")
4445
@Resource(name = RestConstants.VERSION_1 + "/queue-room", supportedClass = QueueRoom.class, supportedOpenmrsVersions = {
4546
"2.3 - 9.*" })
47+
@Setter
4648
public class QueueRoomResource extends DelegatingCrudResource<QueueRoom> {
4749

48-
private final QueueServicesWrapper services;
50+
private QueueServicesWrapper services;
4951

50-
private final QueueRoomSearchCriteriaParser searchCriteriaParser;
52+
private QueueRoomSearchCriteriaParser searchCriteriaParser;
5153

5254
public QueueRoomResource() {
53-
this.services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
54-
this.searchCriteriaParser = Context.getRegisteredComponents(QueueRoomSearchCriteriaParser.class).get(0);
5555
}
5656

5757
public QueueRoomResource(QueueServicesWrapper services, QueueRoomSearchCriteriaParser searchCriteriaParser) {
@@ -61,12 +61,12 @@ public QueueRoomResource(QueueServicesWrapper services, QueueRoomSearchCriteriaP
6161

6262
@Override
6363
public NeedsPaging<QueueRoom> doGetAll(RequestContext requestContext) throws ResponseException {
64-
return new NeedsPaging<>(new ArrayList<>(services.getQueueRoomService().getAllQueueRooms()), requestContext);
64+
return new NeedsPaging<>(new ArrayList<>(getServices().getQueueRoomService().getAllQueueRooms()), requestContext);
6565
}
6666

6767
@Override
6868
public QueueRoom getByUniqueId(String uuid) {
69-
Optional<QueueRoom> optionalQueueRoom = services.getQueueRoomService().getQueueRoomByUuid(uuid);
69+
Optional<QueueRoom> optionalQueueRoom = getServices().getQueueRoomService().getQueueRoomByUuid(uuid);
7070
if (!optionalQueueRoom.isPresent()) {
7171
throw new ObjectNotFoundException("Could not find queueRoom with UUID " + uuid);
7272
}
@@ -75,7 +75,7 @@ public QueueRoom getByUniqueId(String uuid) {
7575

7676
@Override
7777
protected void delete(QueueRoom queueRoom, String retireReason, RequestContext requestContext) throws ResponseException {
78-
services.getQueueRoomService().retireQueueRoom(queueRoom, retireReason);
78+
getServices().getQueueRoomService().retireQueueRoom(queueRoom, retireReason);
7979
}
8080

8181
@Override
@@ -85,20 +85,20 @@ public QueueRoom newDelegate() {
8585

8686
@Override
8787
public QueueRoom save(QueueRoom queueRoom) {
88-
return services.getQueueRoomService().saveQueueRoom(queueRoom);
88+
return getServices().getQueueRoomService().saveQueueRoom(queueRoom);
8989
}
9090

9191
@Override
9292
public void purge(QueueRoom queueRoom, RequestContext requestContext) throws ResponseException {
93-
services.getQueueRoomService().purgeQueueRoom(queueRoom);
93+
getServices().getQueueRoomService().purgeQueueRoom(queueRoom);
9494
}
9595

9696
@Override
9797
@SuppressWarnings("unchecked")
9898
protected PageableResult doSearch(RequestContext requestContext) {
9999
Map<String, String[]> parameters = requestContext.getRequest().getParameterMap();
100-
QueueRoomSearchCriteria criteria = searchCriteriaParser.constructFromRequest(parameters);
101-
List<QueueRoom> queueRooms = services.getQueueRoomService().getQueueRooms(criteria);
100+
QueueRoomSearchCriteria criteria = getSearchCriteriaParser().constructFromRequest(parameters);
101+
List<QueueRoom> queueRooms = getServices().getQueueRoomService().getQueueRooms(criteria);
102102
return new NeedsPaging<>(queueRooms, requestContext);
103103
}
104104

@@ -187,4 +187,18 @@ public String getDisplay(QueueRoom queueRoom) {
187187
public String getResourceVersion() {
188188
return "2.3";
189189
}
190+
191+
public QueueServicesWrapper getServices() {
192+
if (services == null) {
193+
services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0);
194+
}
195+
return services;
196+
}
197+
198+
public QueueRoomSearchCriteriaParser getSearchCriteriaParser() {
199+
if (searchCriteriaParser == null) {
200+
searchCriteriaParser = Context.getRegisteredComponents(QueueRoomSearchCriteriaParser.class).get(0);
201+
}
202+
return searchCriteriaParser;
203+
}
190204
}

0 commit comments

Comments
 (0)