Skip to content

Commit b71283f

Browse files
authored
ATT-33: AbstractAttachmentHandler to delegate to ComplexViewHelper.
ATT-33: AbstractAttachmentHandler to delegate to ComplexViewHelper for non-owned handlers.
1 parent dd971a9 commit b71283f

File tree

8 files changed

+54
-53
lines changed

8 files changed

+54
-53
lines changed

api/src/main/java/org/openmrs/module/attachments/obs/AbstractAttachmentHandler.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package org.openmrs.module.attachments.obs;
22

3+
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_HEIGHT;
4+
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_WIDTH;
5+
36
import java.io.File;
47
import java.io.IOException;
5-
import net.coobird.thumbnailator.Thumbnails;
8+
69
import org.apache.commons.io.FilenameUtils;
710
import org.apache.commons.lang.StringUtils;
811
import org.apache.commons.logging.Log;
912
import org.apache.commons.logging.LogFactory;
1013
import org.openmrs.Obs;
1114
import org.openmrs.api.APIException;
1215
import org.openmrs.module.attachments.AttachmentsConstants;
13-
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_HEIGHT;
14-
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_WIDTH;
1516
import org.openmrs.obs.ComplexData;
1617
import org.openmrs.obs.ComplexObsHandler;
1718
import org.springframework.beans.factory.annotation.Autowired;
1819
import org.springframework.beans.factory.annotation.Qualifier;
1920

21+
import net.coobird.thumbnailator.Thumbnails;
22+
2023
/**
2124
* Double inheritance class. The actual implementation parent must be set through
2225
* {@link #setParentComplexObsHandler()}.
@@ -35,6 +38,14 @@ public abstract class AbstractAttachmentHandler implements ComplexObsHandler {
3538
@Qualifier(AttachmentsConstants.COMPONENT_COMPLEXDATA_HELPER)
3639
private ComplexDataHelper complexDataHelper;
3740

41+
@Autowired
42+
@Qualifier(AttachmentsConstants.COMPONENT_COMPLEXVIEW_HELPER)
43+
private ComplexViewHelper complexViewHelper;
44+
45+
public void setComplexViewHelper(ComplexViewHelper complexViewHelper) {
46+
this.complexViewHelper = complexViewHelper;
47+
}
48+
3849
public AbstractAttachmentHandler() {
3950
super();
4051
setParentComplexObsHandler();
@@ -134,7 +145,7 @@ public static String saveThumbnailOrRename(File savedFile, int imageHeight, int
134145

135146
/**
136147
* @param complexData An obs's complex data.
137-
* @return null if this is not our implementation, the custom {@link DocumentComplexData_old}
148+
* @return null if this is not our implementation, the custom {@link AttachmentComplexData}
138149
* otherwise.
139150
*/
140151
public static AttachmentComplexData fetchAttachmentComplexData(ComplexData complexData) {
@@ -143,13 +154,13 @@ public static AttachmentComplexData fetchAttachmentComplexData(ComplexData compl
143154
return null;
144155
}
145156

146-
AttachmentComplexData docData = (AttachmentComplexData) complexData;
147-
String instructions = docData.getInstructions();
157+
AttachmentComplexData attData = (AttachmentComplexData) complexData;
158+
String instructions = attData.getInstructions();
148159
if (instructions.equals(ValueComplex.INSTRUCTIONS_NONE)) {
149160
return null;
150161
}
151162

152-
return docData;
163+
return attData;
153164
}
154165

155166
/*
@@ -160,15 +171,16 @@ final public Obs getObs(Obs obs, String view) {
160171

161172
ValueComplex valueComplex = new ValueComplex(obs.getValueComplex());
162173
if (valueComplex.isOwnImplementation() == false) { // not our implementation
174+
view = complexViewHelper.getView(obs, view);
163175
return getParent().getObs(obs, view);
164176
}
165177

166178
if (StringUtils.isEmpty(view)) {
167179
view = AttachmentsConstants.ATT_VIEW_ORIGINAL;
168180
}
169181

170-
ComplexData docData = readComplexData(obs, valueComplex, view);
171-
obs.setComplexData(docData);
182+
ComplexData attData = readComplexData(obs, valueComplex, view);
183+
obs.setComplexData(attData);
172184
return obs;
173185
}
174186

api/src/main/java/org/openmrs/module/attachments/obs/ImageAttachmentHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.openmrs.module.attachments.obs;
22

3+
import java.awt.image.BufferedImage;
34
import java.io.File;
45
import java.io.IOException;
5-
import java.awt.image.BufferedImage;
6+
67
import javax.imageio.ImageIO;
7-
import org.apache.commons.io.FilenameUtils;
8-
import org.apache.commons.lang.StringUtils;
8+
99
import org.openmrs.Obs;
1010
import org.openmrs.api.APIException;
1111
import org.openmrs.module.attachments.AttachmentsConstants;

api/src/test/java/org/openmrs/module/attachments/obs/ImageAttachmentHandlerTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ImageAttachmentHandlerTest {
5555

5656
private ComplexObsHandler imageHandler = new ImageHandler();
5757

58-
private ComplexObsHandler imageDocumentHandler = new ImageAttachmentHandler();
58+
private ImageAttachmentHandler imageAttachmentHandler = new ImageAttachmentHandler();
5959

6060
@Before
6161
public void setUp() throws IOException, APIException, URISyntaxException {
@@ -69,6 +69,8 @@ public void setUp() throws IOException, APIException, URISyntaxException {
6969

7070
PowerMockito.mockStatic(Context.class);
7171
when(Context.getAdministrationService()).thenReturn(adminService);
72+
73+
imageAttachmentHandler.setComplexViewHelper(mock(ComplexViewHelper.class));
7274
}
7375

7476
@Test
@@ -79,7 +81,7 @@ public void imageDocumentHandler_shouldSaveLikeImageHandler() throws FileNotFoun
7981
Obs savedObs = new Obs();
8082
savedObs.setComplexData(
8183
(new AttachmentComplexData1_10(imgFileName, new FileInputStream(imageFile))).asComplexData());
82-
imageDocumentHandler.saveObs(savedObs);
84+
imageAttachmentHandler.saveObs(savedObs);
8385
Obs fetchedObs = imageHandler.getObs(savedObs, randomView);
8486
ComplexData complexData = fetchedObs.getComplexData();
8587

@@ -98,7 +100,7 @@ public void imageDocumentHandler_shouldFetchLikeImageHandler() throws FileNotFou
98100
Obs savedObs = new Obs();
99101
savedObs.setComplexData(new ComplexData(imgFileName, new FileInputStream(imageFile)));
100102
imageHandler.saveObs(savedObs); // Saving with core's handler
101-
Obs fetchedObs = imageDocumentHandler.getObs(savedObs, randomView);
103+
Obs fetchedObs = imageAttachmentHandler.getObs(savedObs, randomView);
102104
ComplexData complexData = fetchedObs.getComplexData(); // Fetching with our handler
103105

104106
// Verification

omod-1.10/src/main/java/org/openmrs/module/attachments/rest/AttachmentBytesResource1_10.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package org.openmrs.module.attachments.rest;
22

3-
import static org.openmrs.module.attachments.AttachmentsContext.getContentFamily;
4-
5-
import java.io.IOException;
6-
7-
import javax.servlet.http.HttpServletResponse;
8-
93
import org.apache.commons.io.FilenameUtils;
104
import org.apache.commons.logging.Log;
115
import org.apache.commons.logging.LogFactory;
@@ -14,7 +8,6 @@
148
import org.openmrs.module.attachments.AttachmentsConstants;
159
import org.openmrs.module.attachments.AttachmentsContext;
1610
import org.openmrs.module.attachments.obs.AttachmentComplexData;
17-
import org.openmrs.module.attachments.obs.ComplexViewHelper;
1811
import org.openmrs.module.attachments.obs.ValueComplex;
1912
import org.openmrs.module.webservices.rest.web.RestConstants;
2013
import org.openmrs.module.webservices.rest.web.response.GenericRestException;
@@ -27,6 +20,11 @@
2720
import org.springframework.web.bind.annotation.RequestMapping;
2821
import org.springframework.web.bind.annotation.RequestMethod;
2922

23+
import javax.servlet.http.HttpServletResponse;
24+
import java.io.IOException;
25+
26+
import static org.openmrs.module.attachments.AttachmentsContext.getContentFamily;
27+
3028
@Controller
3129
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/" + AttachmentsConstants.ATTACHMENT_URI)
3230
public class AttachmentBytesResource1_10 extends BaseRestController {
@@ -46,9 +44,7 @@ public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse respo
4644
+ "Obs UUID: " + obs.getUuid());
4745
}
4846

49-
ComplexViewHelper viewHelper = context.getComplexViewHelper();
50-
51-
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), viewHelper.getView(obs, null));
47+
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), null);
5248
ComplexData complexData = complexObs.getComplexData();
5349

5450
// Switching to our complex data object

omod-1.10/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource1_10.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.openmrs.module.attachments.AttachmentsService;
2121
import org.openmrs.module.attachments.ComplexObsSaver;
2222
import org.openmrs.module.attachments.obs.Attachment;
23-
import org.openmrs.module.attachments.obs.ComplexViewHelper;
2423
import org.openmrs.module.attachments.obs.ValueComplex;
2524
import org.openmrs.module.webservices.rest.web.ConversionUtil;
2625
import org.openmrs.module.webservices.rest.web.RequestContext;
@@ -52,9 +51,6 @@ public class AttachmentResource1_10 extends DataDelegatingCrudResource<Attachmen
5251
AttachmentsContext attachmentsContext = Context.getRegisteredComponent(AttachmentsConstants.COMPONENT_ATT_CONTEXT,
5352
AttachmentsContext.class);
5453

55-
ComplexViewHelper viewHelper = Context.getRegisteredComponent(AttachmentsConstants.COMPONENT_COMPLEXVIEW_HELPER,
56-
ComplexViewHelper.class);
57-
5854
@Override
5955
public Attachment newDelegate() {
6056
return new Attachment();
@@ -72,8 +68,7 @@ public Attachment getByUniqueId(String uniqueId) {
7268
if (!obs.isComplex())
7369
throw new GenericRestException(uniqueId + " does not identify a complex obs.", null);
7470
else {
75-
obs = Context.getObsService().getComplexObs(obs.getId(),
76-
viewHelper.getView(obs, AttachmentsConstants.ATT_VIEW_CRUD));
71+
obs = Context.getObsService().getComplexObs(obs.getId(), AttachmentsConstants.ATT_VIEW_CRUD);
7772
return new Attachment(obs);
7873
}
7974
}
@@ -104,7 +99,7 @@ public Object upload(MultipartFile file, RequestContext context) throws Response
10499
String instructions = context.getParameter("instructions");
105100

106101
// Verify File Size
107-
if (attachmentsContext.getMaxUploadFileSize() * 1024 * 1024 < file.getSize()) {
102+
if (attachmentsContext.getMaxUploadFileSize() * 1024 * 1024 < (double) file.getSize()) {
108103
throw new IllegalRequestException("The file exceeds the maximum size");
109104
}
110105

omod/src/main/java/org/openmrs/module/attachments/page/controller/AttachmentsPageController.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@Component
2828
public class AttachmentsPageController {
29-
29+
3030
public void controller(@RequestParam("patient") Patient patient,
3131
@RequestParam(value = "visit", required = false) Visit visit, UiSessionContext sessionContext, UiUtils ui,
3232
@InjectBeans AttachmentsContext context, @SpringBean DomainWrapperFactory domainWrapperFactory,
@@ -36,21 +36,21 @@ public void controller(@RequestParam("patient") Patient patient,
3636
//
3737
Map<String, Object> jsonConfig = ClientConfigFragmentController.getClientConfig(context, ui);
3838
jsonConfig.put("patient", convertToRef(patient));
39-
39+
4040
VisitDomainWrapper visitWrapper = getVisitDomainWrapper(domainWrapperFactory, patient, visit,
4141
context.getAdtService(), sessionContext.getSessionLocation());
4242
jsonConfig.put("visit", visitWrapper == null ? null : convertVisit(visitWrapper.getVisit()));
43-
43+
4444
jsonConfig.put("contentFamilyMap", getContentFamilyMap());
4545
jsonConfig.put("associateWithVisit", context.associateWithVisit());
46-
46+
4747
model.put("jsonConfig", ui.toJson(jsonConfig));
48-
48+
4949
// For Core Apps's patient header.
5050
model.put("patient", patient);
5151
model.put("visit", visitWrapper);
5252
}
53-
53+
5454
protected VisitDomainWrapper getVisitDomainWrapper(DomainWrapperFactory domainWrapperFactory, Patient patient,
5555
Visit visit, AdtService adtService, Location sessionLocation) {
5656
VisitDomainWrapper visitWrapper = null;
@@ -63,13 +63,13 @@ protected VisitDomainWrapper getVisitDomainWrapper(DomainWrapperFactory domainWr
6363
}
6464
return visitWrapper;
6565
}
66-
66+
6767
protected Object convertVisit(Object object) {
6868
return object == null ? null
6969
: ConversionUtil.convertToRepresentation(object,
7070
new CustomRepresentation(AttachmentsConstants.REPRESENTATION_VISIT));
7171
}
72-
72+
7373
protected Object convertToRef(Object object) {
7474
return object == null ? null : ConversionUtil.convertToRepresentation(object, Representation.REF);
7575
}

omod/src/main/java/org/openmrs/module/attachments/web/controller/AttachmentsController.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
package org.openmrs.module.attachments.web.controller;
22

3-
import static org.openmrs.module.attachments.AttachmentsContext.getContentFamily;
4-
5-
import java.io.IOException;
6-
import java.util.Iterator;
7-
8-
import javax.servlet.http.HttpServletResponse;
9-
103
import org.apache.commons.lang.StringUtils;
114
import org.apache.commons.logging.Log;
125
import org.apache.commons.logging.LogFactory;
13-
import org.openmrs.Encounter;
146
import org.openmrs.Obs;
157
import org.openmrs.Patient;
168
import org.openmrs.Provider;
179
import org.openmrs.Visit;
10+
import org.openmrs.Encounter;
1811
import org.openmrs.module.attachments.AttachmentsConstants;
1912
import org.openmrs.module.attachments.AttachmentsContext;
2013
import org.openmrs.module.attachments.ComplexObsSaver;
2114
import org.openmrs.module.attachments.VisitCompatibility;
2215
import org.openmrs.module.attachments.obs.AttachmentComplexData;
23-
import org.openmrs.module.attachments.obs.ComplexViewHelper;
2416
import org.openmrs.module.attachments.obs.ValueComplex;
2517
import org.openmrs.module.attachments.rest.AttachmentBytesResource1_10;
2618
import org.openmrs.module.webservices.rest.web.ConversionUtil;
@@ -36,6 +28,12 @@
3628
import org.springframework.web.multipart.MultipartFile;
3729
import org.springframework.web.multipart.MultipartHttpServletRequest;
3830

31+
import javax.servlet.http.HttpServletResponse;
32+
import java.io.IOException;
33+
import java.util.Iterator;
34+
35+
import static org.openmrs.module.attachments.AttachmentsContext.getContentFamily;
36+
3937
/*
4038
* @Deprecated and replaced by
4139
* {@link org.openmrs.module.attachments.rest.AttachmentBytesResource1_10 AttachmentBytesResource}
@@ -110,11 +108,9 @@ public void downloadDocument(@RequestParam("obs") String obsUuid,
110108
if (StringUtils.isEmpty(view))
111109
view = AttachmentsConstants.ATT_VIEW_ORIGINAL;
112110

113-
ComplexViewHelper viewHelper = context.getComplexViewHelper();
114-
115111
// Getting the Core/Platform complex data object
116112
Obs obs = context.getObsService().getObsByUuid(obsUuid);
117-
Obs complexObs = context.getObsService().getComplexObs(obs.getObsId(), viewHelper.getView(obs, view));
113+
Obs complexObs = context.getObsService().getComplexObs(obs.getObsId(), view);
118114
ComplexData complexData = complexObs.getComplexData();
119115

120116
// Switching to our complex data object

omod/src/main/webapp/resources/scripts/directives/modalWebcam.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ angular.module('att.widget.modalWebcam')
5656
$scope.snap = function() {
5757
Webcam.snap(function(dataUri) {
5858
$scope.dataUri = dataUri;
59-
59+
6060
});
6161
}
6262

0 commit comments

Comments
 (0)