Skip to content

Commit 5d02ca5

Browse files
author
Wandji69
committed
pulling updates from master
Merge branch 'master' of https://github.com/openmrs/openmrs-module-attachments into ATT-1
2 parents d0a573d + 9c61cf8 commit 5d02ca5

File tree

12 files changed

+345
-149
lines changed

12 files changed

+345
-149
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) {
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
package org.openmrs.module.attachments.obs;
22

3+
import static org.openmrs.obs.ComplexObsHandler.RAW_VIEW;
4+
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
37
import org.openmrs.Obs;
48
import org.openmrs.annotation.OpenmrsProfile;
9+
import org.openmrs.api.ObsService;
510
import org.openmrs.module.attachments.AttachmentsConstants;
611
import org.openmrs.obs.ComplexObsHandler;
12+
import org.springframework.beans.factory.annotation.Autowired;
713
import org.springframework.stereotype.Component;
814

915
@Component(AttachmentsConstants.COMPONENT_COMPLEXVIEW_HELPER)
1016
@OpenmrsProfile(openmrsPlatformVersion = "2.0.0")
1117
public class ComplexViewHelper2_0 implements ComplexViewHelper {
1218

19+
private final Log log = LogFactory.getLog(getClass());
20+
21+
@Autowired
22+
private ObsService obsService;
23+
1324
@Override
1425
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
1926

20-
return ComplexObsHandler.RAW_VIEW;
27+
ComplexObsHandler handler = obsService.getHandler(obs);
28+
29+
if (handler.supportsView(view)) {
30+
return view;
31+
} else {
32+
log.warn("The requested view '" + view + "' is not supported by the complex obs handler '"
33+
+ handler.getClass().getCanonicalName() + "', reverting to the default view '" + RAW_VIEW + "'.");
34+
}
35+
36+
return RAW_VIEW;
37+
2138
}
2239
}

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) throws IOException;
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: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
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+
39
import org.apache.commons.io.FilenameUtils;
410
import org.apache.commons.logging.Log;
511
import org.apache.commons.logging.LogFactory;
@@ -8,6 +14,7 @@
814
import org.openmrs.module.attachments.AttachmentsConstants;
915
import org.openmrs.module.attachments.AttachmentsContext;
1016
import org.openmrs.module.attachments.obs.AttachmentComplexData;
17+
import org.openmrs.module.attachments.obs.ComplexViewHelper;
1118
import org.openmrs.module.attachments.obs.ValueComplex;
1219
import org.openmrs.module.webservices.rest.web.RestConstants;
1320
import org.openmrs.module.webservices.rest.web.response.GenericRestException;
@@ -19,11 +26,7 @@
1926
import org.springframework.web.bind.annotation.PathVariable;
2027
import org.springframework.web.bind.annotation.RequestMapping;
2128
import org.springframework.web.bind.annotation.RequestMethod;
22-
23-
import javax.servlet.http.HttpServletResponse;
24-
import java.io.IOException;
25-
26-
import static org.openmrs.module.attachments.AttachmentsContext.getContentFamily;
29+
import org.springframework.web.bind.annotation.RequestParam;
2730

2831
@Controller
2932
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/" + AttachmentsConstants.ATTACHMENT_URI)
@@ -35,7 +38,8 @@ public class AttachmentBytesResource1_10 extends BaseRestController {
3538
protected final Log log = LogFactory.getLog(getClass());
3639

3740
@RequestMapping(value = AttachmentsConstants.ATTACHMENT_BYTES_URI, method = RequestMethod.GET)
38-
public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse response) throws ResponseException {
41+
public void getFile(@PathVariable("uuid") String uuid, @RequestParam(required = false, value = "view") String view,
42+
HttpServletResponse response) throws ResponseException {
3943
// Getting the Core/Platform complex data object
4044
Obs obs = context.getObsService().getObsByUuid(uuid);
4145

@@ -44,7 +48,9 @@ public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse respo
4448
+ "Obs UUID: " + obs.getUuid());
4549
}
4650

47-
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), null);
51+
ComplexViewHelper viewHelper = context.getComplexViewHelper();
52+
53+
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), viewHelper.getView(obs, view));
4854
ComplexData complexData = complexObs.getComplexData();
4955

5056
// Switching to our complex data object
@@ -54,7 +60,7 @@ public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse respo
5460

5561
String mimeType = attComplexData.getMimeType();
5662

57-
// The attachment meta data is sent as HTTP headers.
63+
// The attachment metadata is sent as HTTP headers.
5864
response.setContentType(mimeType);
5965
response.addHeader("Content-Family", getContentFamily(mimeType).name());
6066
response.addHeader("File-Name", attComplexData.getTitle());
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)