Skip to content

Commit 8485f36

Browse files
authored
ATT-38: AttachmentBytesResource_* to take 'view' as parameter.
1 parent 4bbfba2 commit 8485f36

File tree

11 files changed

+318
-134
lines changed

11 files changed

+318
-134
lines changed

api-2.0/src/main/java/org/openmrs/module/attachments/obs/AttachmentComplexData2_0.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public AttachmentComplexData2_0(String instructions, String title, Object data,
2222
} else {
2323
this.setMimeType(AttachmentsConstants.UNKNOWN_MIME_TYPE);
2424
}
25-
if (!StringUtils.isEmpty(instructions))
25+
if (!StringUtils.isEmpty(instructions)) {
2626
this.instructions = instructions;
27+
}
2728
}
2829

2930
public AttachmentComplexData2_0(String title, Object data) {

api-2.0/src/main/java/org/openmrs/module/attachments/obs/ComplexViewHelper2_0.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22

33
import org.openmrs.Obs;
44
import org.openmrs.annotation.OpenmrsProfile;
5+
import org.openmrs.api.ObsService;
56
import org.openmrs.module.attachments.AttachmentsConstants;
67
import org.openmrs.obs.ComplexObsHandler;
8+
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.stereotype.Component;
810

911
@Component(AttachmentsConstants.COMPONENT_COMPLEXVIEW_HELPER)
1012
@OpenmrsProfile(openmrsPlatformVersion = "2.0.0")
1113
public class ComplexViewHelper2_0 implements ComplexViewHelper {
1214

15+
@Autowired
16+
private ObsService obsService;
17+
1318
@Override
1419
public String getView(Obs obs, String view) {
15-
// TODO: Obs obs will help the 2.x implementation support fetching the
16-
// supported views for that obs,
17-
// See:
18-
// https://issues.openmrs.org/browse/ATT-34
20+
21+
if (obsService.getHandler(obs).supportsView(view)) {
22+
return view;
23+
}
1924

2025
return ComplexObsHandler.RAW_VIEW;
26+
2127
}
2228
}

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

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

3+
import static org.openmrs.module.attachments.AttachmentsConstants.ATT_VIEW_ORIGINAL;
4+
import static org.openmrs.module.attachments.AttachmentsConstants.ATT_VIEW_THUMBNAIL;
35
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_HEIGHT;
46
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_WIDTH;
57

68
import java.io.File;
79
import java.io.IOException;
10+
import java.util.Arrays;
811

912
import org.apache.commons.io.FilenameUtils;
1013
import org.apache.commons.lang.StringUtils;
@@ -26,6 +29,8 @@
2629
*/
2730
public abstract class AbstractAttachmentHandler implements ComplexObsHandler {
2831

32+
private static final String[] supportedViews = { ATT_VIEW_ORIGINAL, ATT_VIEW_THUMBNAIL };
33+
2934
public final static String NO_THUMBNAIL_SUFFIX = "___nothumb__";
3035

3136
public final static String THUMBNAIL_SUFFIX = "_thumb";
@@ -75,6 +80,14 @@ protected ComplexDataHelper getComplexDataHelper() {
7580
*/
7681
abstract protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData);
7782

83+
public String[] getSupportedViews() {
84+
return supportedViews;
85+
}
86+
87+
public boolean supportsView(String view) {
88+
return Arrays.asList(getSupportedViews()).contains(view);
89+
}
90+
7891
protected void setParent(ComplexObsHandler complexObsHandler) {
7992
this.parent = complexObsHandler;
8093
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public static byte[] getByteArray(ComplexData complexData) {
4343
}
4444
if (data instanceof byte[]) {
4545
return (byte[]) data;
46+
} else if (data instanceof String) {
47+
return ((String) data).getBytes();
4648
} else if (RenderedImage.class.isAssignableFrom(data.getClass())) {
4749
RenderedImage image = (RenderedImage) data;
4850

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public License,
5+
v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
7+
the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
8+
9+
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
10+
graphic logo is a trademark of OpenMRS Inc.
11+
12+
-->
13+
<dataset>
14+
<concept concept_id="84069" retired="false" datatype_id="13" class_id="5" is_set="false" creator="1" date_created="2005-01-01 00:00:00.0" uuid="3fe44002-35b8-445e-9d46-692c56e6bdde"/>
15+
<concept_description concept_description_id="84069" concept_id="84069" description="TestBytesViewHandler Concept Complex" locale="en_GB" creator="1" date_created="2004-08-12 00:00:00.0" uuid="3a951971-34e9-4c88-954c-68d188f2c4de"/>
16+
17+
<concept_complex concept_id="84069" handler="TestAttachmentBytesViewHandler" />
18+
19+
<concept_name concept_id="84069" name="TestAttachmentBytesViewHandler Concept Complex" locale="en_GB" creator="1" date_created="2008-08-15 13:52:53.0" concept_name_id="8007" concept_name_type="FULLY_SPECIFIED" locale_preferred="0" voided="false" uuid="86f4f6ff-2bfb-4a17-a96b-ca633a96dd13"/>
20+
21+
<!-- This obs just exists so that there is a reference to an attachment associated with TestAttachmentBytesViewHandler to fetch it via REST -->
22+
<obs obs_id="84044" person_id="2" concept_id="84069" obs_datetime="2008-02-09 00:00:00.0" location_id="1" value_complex="m3ks | instructions.none" creator="1" date_created="2006-02-10 15:57:35.0" voided="false" uuid="bbec1b32-dbf1-4b7a-bd2b-8898543c1367"/>
23+
24+
</dataset>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.web.bind.annotation.PathVariable;
2020
import org.springframework.web.bind.annotation.RequestMapping;
2121
import org.springframework.web.bind.annotation.RequestMethod;
22+
import org.springframework.web.bind.annotation.RequestParam;
2223

2324
import javax.servlet.http.HttpServletResponse;
2425
import java.io.IOException;
@@ -35,7 +36,8 @@ public class AttachmentBytesResource1_10 extends BaseRestController {
3536
protected final Log log = LogFactory.getLog(getClass());
3637

3738
@RequestMapping(value = AttachmentsConstants.ATTACHMENT_BYTES_URI, method = RequestMethod.GET)
38-
public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse response) throws ResponseException {
39+
public void getFile(@PathVariable("uuid") String uuid, @RequestParam(required = false, value = "view") String view,
40+
HttpServletResponse response) throws ResponseException {
3941
// Getting the Core/Platform complex data object
4042
Obs obs = context.getObsService().getObsByUuid(uuid);
4143

@@ -44,7 +46,9 @@ public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse respo
4446
+ "Obs UUID: " + obs.getUuid());
4547
}
4648

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

5054
// Switching to our complex data object
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.mock.web.MockMultipartFile;
3535
import org.springframework.mock.web.MockMultipartHttpServletRequest;
3636

37-
public class AttachmentController1_10Test extends MainResourceControllerTest {
37+
public class AttachmentRestController1_10Test extends MainResourceControllerTest {
3838

3939
@Autowired
4040
protected ObsService obsService;

omod-2.0/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@
205205
</resources>
206206

207207
<testResources>
208+
<testResource>
209+
<directory>../api/src/test/resources</directory>
210+
</testResource>
208211
<testResource>
209212
<directory>src/test/resources</directory>
210213
<includes>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.openmrs.module.attachments.obs;
2+
3+
import static org.openmrs.module.attachments.obs.ValueComplex.INSTRUCTIONS_DEFAULT;
4+
5+
import org.openmrs.Obs;
6+
import org.openmrs.obs.ComplexData;
7+
import org.openmrs.obs.handler.BinaryDataHandler;
8+
9+
public class TestAttachmentBytesViewHandler extends AbstractAttachmentHandler {
10+
11+
public static final String DEFAULT_VIEW = "VIEW_1";
12+
13+
public static final String DEFAULT_VIEW_DATA = "This is a string content for VIEW_1.";
14+
15+
public static final String ALTERNATE_VIEW = "VIEW_2";
16+
17+
public static final String ALTERNATE_VIEW_DATA = "This is a string content for VIEW_2.";
18+
19+
private static final String[] supportedViews = { DEFAULT_VIEW, ALTERNATE_VIEW };
20+
21+
@Override
22+
protected void setParentComplexObsHandler() {
23+
setParent(new BinaryDataHandler());
24+
}
25+
26+
@Override
27+
protected ComplexData readComplexData(Obs obs, ValueComplex valueComplex, String view) {
28+
29+
if (ALTERNATE_VIEW.equals(view)) {
30+
return new AttachmentComplexData2_0(INSTRUCTIONS_DEFAULT, "extra_view", ALTERNATE_VIEW_DATA, "text/plain");
31+
}
32+
33+
return new AttachmentComplexData2_0(INSTRUCTIONS_DEFAULT, "default_view", DEFAULT_VIEW_DATA, "text/plain");
34+
35+
}
36+
37+
@Override
38+
protected boolean deleteComplexData(Obs obs, AttachmentComplexData complexData) {
39+
return false;
40+
}
41+
42+
@Override
43+
protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData) {
44+
return null;
45+
}
46+
47+
@Override
48+
public String[] getSupportedViews() {
49+
return supportedViews;
50+
}
51+
}

omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentController2_0Test.java

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)