use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DataValueController method getDataValue.
// ---------------------------------------------------------------------
// GET
// ---------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<String> getDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, Model model, HttpServletResponse response) throws WebMessageException {
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = getAndValidateDataElement(de);
DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
Period period = getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
// ---------------------------------------------------------------------
// Get data value
// ---------------------------------------------------------------------
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
if (dataValue == null) {
throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
}
List<String> value = new ArrayList<>();
value.add(dataValue.getValue());
return value;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DataValueController method getDataValueFile.
// ---------------------------------------------------------------------
// GET file
// ---------------------------------------------------------------------
@RequestMapping(value = "/files", method = RequestMethod.GET)
public void getDataValueFile(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = getAndValidateDataElement(de);
if (!dataElement.isFileType()) {
throw new WebMessageException(WebMessageUtils.conflict("DataElement must be of type file"));
}
DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
Period period = getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
// ---------------------------------------------------------------------
// Get data value
// ---------------------------------------------------------------------
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
if (dataValue == null) {
throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
}
// ---------------------------------------------------------------------
// Get file resource
// ---------------------------------------------------------------------
String uid = dataValue.getValue();
FileResource fileResource = fileResourceService.getFileResource(uid);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(WebMessageUtils.notFound("A data value file resource with id " + uid + " does not exist."));
}
FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();
if (storageStatus != FileResourceStorageStatus.STORED) {
// Special case:
// The FileResource exists and has been tied to this DataValue, however, the underlying file
// content is still not stored to the (most likely external) file store provider.
// HTTP 409, for lack of a more suitable status code
WebMessage webMessage = WebMessageUtils.conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.");
webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
throw new WebMessageException(webMessage);
}
ByteSource content = fileResourceService.getFileResourceContent(fileResource);
if (content == null) {
throw new WebMessageException(WebMessageUtils.notFound("The referenced file could not be found"));
}
// ---------------------------------------------------------------------
// Attempt to build signed URL request for content and redirect
// ---------------------------------------------------------------------
URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(uid);
if (signedGetUri != null) {
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
return;
}
// ---------------------------------------------------------------------
// Build response and return
// ---------------------------------------------------------------------
response.setContentType(fileResource.getContentType());
response.setContentLength(new Long(fileResource.getContentLength()).intValue());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
// ---------------------------------------------------------------------
// Request signing is not available, stream content back to client
// ---------------------------------------------------------------------
InputStream inputStream = null;
try {
inputStream = content.openStream();
IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
} finally {
IOUtils.closeQuietly(inputStream);
}
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class AnalyticsUtils method getCocNameMap.
/**
* Returns a mapping between the category option combo identifiers and names
* for the given query.
*
* @param params the data query parameters.
* @returns a mapping between identifiers and names.
*/
public static Map<String, String> getCocNameMap(DataQueryParams params) {
Map<String, String> metaData = new HashMap<>();
List<DimensionalItemObject> des = params.getAllDataElements();
if (!des.isEmpty()) {
for (DimensionalItemObject de : des) {
DataElement dataElement = (DataElement) de;
for (DataElementCategoryOptionCombo coc : dataElement.getCategoryOptionCombos()) {
metaData.put(coc.getUid(), coc.getName());
}
}
}
return metaData;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testGetCocNameMap.
@Test
public void testGetCocNameMap() {
DataElementCategoryCombo ccA = createCategoryCombo('A', new DataElementCategory[0]);
DataElementCategoryCombo ccB = createCategoryCombo('B', new DataElementCategory[0]);
DataElementCategoryOptionCombo cocA = createCategoryOptionCombo('A');
DataElementCategoryOptionCombo cocB = createCategoryOptionCombo('B');
ccA.getOptionCombos().add(cocA);
ccB.getOptionCombos().add(cocB);
DataElement deA = createDataElement('A');
DataElement deB = createDataElement('B');
deA.setDataElementCategoryCombo(ccA);
deB.setDataElementCategoryCombo(ccB);
DimensionalObject dx = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, Lists.newArrayList(deA, deB));
DataQueryParams params = DataQueryParams.newBuilder().addDimension(dx).withDisplayProperty(DisplayProperty.NAME).build();
Map<String, String> map = AnalyticsUtils.getCocNameMap(params);
assertEquals(map.get(cocA.getUid()), cocA.getName());
assertEquals(map.get(cocB.getUid()), cocB.getName());
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testHandleGridForDataValueSet.
@Test
public void testHandleGridForDataValueSet() {
IndicatorType itA = new IndicatorType();
DataElementCategoryOptionCombo ocA = createCategoryOptionCombo('A');
ocA.setUid("ceabcdefghA");
DataElement dxA = createDataElement('A');
dxA.setUid("deabcdefghA");
dxA.setValueType(ValueType.INTEGER);
DataElement dxB = createDataElement('B');
dxB.setUid("deabcdefghB");
dxB.setValueType(ValueType.NUMBER);
Indicator dxC = createIndicator('C', itA);
dxC.setUid("deabcdefghC");
dxC.setDecimals(0);
dxC.setAggregateExportAttributeOptionCombo("ceabcdefghA");
Indicator dxD = createIndicator('D', itA);
dxD.setUid("deabcdefghD");
dxD.setDecimals(2);
dxD.setAggregateExportCategoryOptionCombo("ceabcdefghB");
DataElementOperand dxE = new DataElementOperand(dxA, ocA);
DataElementOperand dxF = new DataElementOperand(dxB, ocA);
DataQueryParams params = DataQueryParams.newBuilder().addDimension(new BaseDimensionalObject(DATA_X_DIM_ID, DimensionType.DATA_X, Lists.newArrayList(dxA, dxB, dxC, dxD, dxE, dxF))).build();
Grid grid = new ListGrid();
grid.addHeader(new GridHeader(DimensionalObject.DATA_X_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.ORGUNIT_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.PERIOD_DIM_ID));
grid.addHeader(new GridHeader(VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false));
grid.addRow().addValuesAsList(Lists.newArrayList("deabcdefghA", "ouA", "peA", 1d));
grid.addRow().addValuesAsList(Lists.newArrayList("deabcdefghB", "ouA", "peA", 2d));
grid.addRow().addValuesAsList(Lists.newArrayList("deabcdefghC", "ouA", "peA", 3d));
grid.addRow().addValuesAsList(Lists.newArrayList("deabcdefghD", "ouA", "peA", 4d));
grid.addRow().addValuesAsList(Lists.newArrayList("deabcdefghA.ceabcdefghA", "ouA", "peA", 5d));
grid.addRow().addValuesAsList(Lists.newArrayList("deabcdefghB.ceabcdefghA", "ouA", "peA", 6d));
assertEquals(4, grid.getWidth());
assertEquals(6, grid.getHeight());
AnalyticsUtils.handleGridForDataValueSet(params, grid);
assertEquals(6, grid.getWidth());
assertEquals(6, grid.getHeight());
assertEquals("deabcdefghA", grid.getRow(0).get(0));
assertNull(grid.getRow(0).get(3));
assertNull(grid.getRow(0).get(4));
assertEquals(1, grid.getRow(0).get(5));
assertEquals("deabcdefghB", grid.getRow(1).get(0));
assertNull(grid.getRow(1).get(3));
assertNull(grid.getRow(1).get(4));
assertEquals(2d, (Double) grid.getRow(1).get(5), 0.01);
assertEquals("deabcdefghC", grid.getRow(2).get(0));
assertNull(grid.getRow(2).get(3));
assertEquals("ceabcdefghA", grid.getRow(2).get(4));
assertEquals(3, grid.getRow(2).get(5));
assertEquals("deabcdefghD", grid.getRow(3).get(0));
assertEquals("ceabcdefghB", grid.getRow(3).get(3));
assertNull(grid.getRow(3).get(4));
assertEquals(4d, (Double) grid.getRow(3).get(5), 0.01);
assertEquals("deabcdefghA", grid.getRow(4).get(0));
assertEquals("ceabcdefghA", grid.getRow(4).get(3));
assertNull(grid.getRow(4).get(4));
assertEquals(5, grid.getRow(4).get(5));
assertEquals("deabcdefghB", grid.getRow(5).get(0));
assertEquals("ceabcdefghA", grid.getRow(5).get(3));
assertNull(grid.getRow(5).get(4));
assertEquals(6d, (Double) grid.getRow(5).get(5), 0.01);
}
Aggregations