Skip to content

Commit

Permalink
fixes for single_choice prompt response output for the CSV output format
Browse files Browse the repository at this point in the history
  • Loading branch information
jshslsky committed Jun 24, 2011
1 parent 8cd7a93 commit e358872
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/org/ohmage/domain/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public int getIndexForPrompt(String surveyId, String promptId) {

return ((Prompt) survey.getSurveyItemMap().get(promptId)).getIndex();
}

public boolean isPromptInRepeatableSet(String surveyId, String promptId) {
return null != _surveyMap.get(surveyId).getRepeatableSetIdForPromptId(promptId);
}

public int getNumberOfPromptsInSurvey(String surveyId) {
return _surveyMap.get(surveyId).getSurveyItemMap().keySet().size();
Expand Down
6 changes: 4 additions & 2 deletions src/org/ohmage/domain/ConfigurationValueMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void merge(SurveyResponseReadResult result, Configuration configuration)
if(PromptTypeUtils.isSingleChoiceType(result.getPromptType())) {

result.setChoiceGlossary(configuration.getChoiceGlossaryFor(result.getSurveyId(), result.getRepeatableSetId(), result.getPromptId()));
result.setSingleChoiceOrdinalValue(configuration.getValueForChoiceKey(result.getSurveyId(), result.getRepeatableSetId(), result.getPromptId(), String.valueOf(result.getResponse())));
result.setSingleChoiceOrdinalValue(convertToNumber(configuration.getValueForChoiceKey(result.getSurveyId(), result.getRepeatableSetId(), result.getPromptId(), String.valueOf(result.getResponse()))));
result.setSingleChoiceLabel(configuration.getLabelForChoiceKey(result.getSurveyId(), result.getRepeatableSetId(), result.getPromptId(), String.valueOf(result.getResponse())));
setDisplayValueFromSingleChoice(result, configuration, true);

} else if(PromptTypeUtils.isMultiChoiceType(result.getPromptType())) {
Expand All @@ -72,7 +73,8 @@ public void merge(SurveyResponseReadResult result, Configuration configuration)
if(PromptTypeUtils.isSingleChoiceType(result.getPromptType())) {

result.setChoiceGlossary(configuration.getChoiceGlossaryFor(result.getSurveyId(), result.getPromptId()));
result.setSingleChoiceOrdinalValue(configuration.getValueForChoiceKey(result.getSurveyId(), result.getPromptId(), String.valueOf(result.getResponse())));
result.setSingleChoiceOrdinalValue(convertToNumber(configuration.getValueForChoiceKey(result.getSurveyId(), result.getPromptId(), String.valueOf(result.getResponse()))));
result.setSingleChoiceLabel(configuration.getLabelForChoiceKey(result.getSurveyId(), result.getPromptId(), String.valueOf(result.getResponse())));
setDisplayValueFromSingleChoice(result, configuration, false);

} else if (PromptTypeUtils.isMultiChoiceType(result.getPromptType())) {
Expand Down
41 changes: 41 additions & 0 deletions src/org/ohmage/domain/SingleChoicePromptValueAndLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright 2011 The Regents of the University of California
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package org.ohmage.domain;

/**
* Immutable wrapper for prompt values and labels.
*
* @author Joshua Selsky
*/
public class SingleChoicePromptValueAndLabel {

private Object _value;
private String _label;

public SingleChoicePromptValueAndLabel(Object value, String label) {
_value = value;
_label = label;
}

public Object getValue() {
return _value;
}

public String getLabel() {
return _label;
}
}
25 changes: 21 additions & 4 deletions src/org/ohmage/domain/SurveyResponseReadIndexedResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SurveyResponseReadIndexedResult {
// Keep the original result around for metadata/result context
private SurveyResponseReadResult _originalResult;

public SurveyResponseReadIndexedResult(SurveyResponseReadResult result) {
public SurveyResponseReadIndexedResult(SurveyResponseReadResult result, boolean isCsv) {
_key = new SurveyResponseReadIndexedResultKey(result.getUsername(), result.getTimestamp(),
result.getSurveyId(), result.getRepeatableSetId(), result.getRepeatableSetIteration());

Expand All @@ -50,19 +50,32 @@ public SurveyResponseReadIndexedResult(SurveyResponseReadResult result) {
result.getPromptText(),
result.getPromptType(),
result.getUnit());

_promptResponseMetadataMap = new HashMap<String, PromptResponseMetadata>();
_promptResponseMetadataMap.put(result.getPromptId(), promptResponseMetadata);
_promptResponseMap = new HashMap<String, Object>();
_promptResponseMap.put(result.getPromptId(), result.getDisplayValue());

if("single_choice".equals(result.getPromptType()) && isCsv) {
_promptResponseMap.put(result.getPromptId(), new SingleChoicePromptValueAndLabel(result.getSingleChoiceOrdinalValue(), result.getSingleChoiceLabel()));
}
else {
_promptResponseMap.put(result.getPromptId(), result.getDisplayValue());
}

_choiceGlossaryMap = new HashMap<String, Map<String, PromptProperty>>();

if(null != result.getChoiceGlossary()) {
_choiceGlossaryMap.put(result.getPromptId(), result.getChoiceGlossary());
}
}

public void addPromptResponse(SurveyResponseReadResult result) {
_promptResponseMap.put(result.getPromptId(), result.getDisplayValue());
public void addPromptResponse(SurveyResponseReadResult result, boolean isCsv) {
if("single_choice".equals(result.getPromptType()) && isCsv) {
_promptResponseMap.put(result.getPromptId(), new SingleChoicePromptValueAndLabel(result.getSingleChoiceOrdinalValue(), result.getSingleChoiceLabel()));
}
else {
_promptResponseMap.put(result.getPromptId(), result.getDisplayValue());
}

PromptResponseMetadata promptResponseMetadata
= new PromptResponseMetadata(result.getDisplayLabel(),
Expand Down Expand Up @@ -127,6 +140,10 @@ public Object getSingleChoiceOrdinalValue() {
return _originalResult.getSingleChoiceOrdinalValue();
}

public Object getSingleChoiceLabel() {
return _originalResult.getSingleChoiceLabel();
}

public String getSurveyId() {
return _originalResult.getSurveyId();
}
Expand Down
9 changes: 9 additions & 0 deletions src/org/ohmage/domain/SurveyResponseReadResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SurveyResponseReadResult {
private String _displayLabel;
private Object _displayValue;
private Object _singleChoiceOrdinalValue;
private String _singleChoiceLabel;
private String _unit;
private String _displayType;
private String _utcTimestamp;
Expand Down Expand Up @@ -246,6 +247,14 @@ public Object getSingleChoiceOrdinalValue() {
public void setSingleChoiceOrdinalValue(Object singleChoiceOrdinalValue) {
_singleChoiceOrdinalValue = singleChoiceOrdinalValue;
}

public String getSingleChoiceLabel() {
return _singleChoiceLabel;
}

public void setSingleChoiceLabel(String singleChoiceLabel) {
_singleChoiceLabel = singleChoiceLabel;
}

public Map<String, PromptProperty> getChoiceGlossary() {
return _choiceGlossary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.ohmage.domain.Configuration;
import org.ohmage.domain.PromptProperty;
import org.ohmage.domain.PromptResponseMetadata;
import org.ohmage.domain.SingleChoicePromptValueAndLabel;
import org.ohmage.domain.SurveyResponseReadIndexedResult;
import org.ohmage.request.SurveyResponseReadAwRequest;

Expand Down Expand Up @@ -181,7 +182,7 @@ public String createMultiResultOutput(int numberOfSurveys,
}
}

_logger.info(surveyIdToPromptIdListMap);
//_logger.info(surveyIdToPromptIdListMap);

Iterator<String> surveyIdToPromptIdListMapIterator = surveyIdToPromptIdListMap.keySet().iterator();
while(surveyIdToPromptIdListMapIterator.hasNext()) {
Expand All @@ -194,7 +195,7 @@ public String createMultiResultOutput(int numberOfSurveys,
canonicalColumnList.addAll(sortedColumnMapKeySet);
sortedColumnMapKeySet.clear();

_logger.info(canonicalColumnList);
//_logger.info(canonicalColumnList);

// Build the column headers
// For the CSV output, user advocates have requested that the column names be made shorter and that single_choice
Expand All @@ -208,9 +209,12 @@ public String createMultiResultOutput(int numberOfSurveys,
if(key.startsWith("urn:ohmage:context")) {
shortHeader = key.replace("urn:ohmage:context", "sys");
} else if(key.startsWith("urn:ohmage:prompt:id")) {
String type = promptResponseMetadataMap.get(key.substring("urn:ohmage:prompt:id:".length())).getPromptType();

String internalPromptId = key.substring("urn:ohmage:prompt:id:".length());
String type = promptResponseMetadataMap.get(internalPromptId).getPromptType();

if("single_choice".equals(type)) {
shortHeader = key.replace("urn:ohmage:prompt:id:", "") + ":label," + key.replace("urn:ohmage:prompt:id:", "") + ":value";
shortHeader = internalPromptId + ":label," + internalPromptId + ":value";
}
else {
shortHeader = key.replace("urn:ohmage:prompt:id:", "");
Expand Down Expand Up @@ -351,19 +355,23 @@ else if("urn:ohmage:survey:privacy_state".equals(canonicalColumnId)) {
builder.append(result.getPrivacyState()).append(",");
}
else if(canonicalColumnId.contains("prompt:id")) {

Object value = result.getPromptResponseMap().get(canonicalColumnId.substring("urn:ohmage:prompt:id:".length()));

PromptResponseMetadata promptResponseMetadata = promptResponseMetadataMap.get(canonicalColumnId.substring("urn:ohmage:prompt:id:".length()));
String promptId = canonicalColumnId.substring("urn:ohmage:prompt:id:".length()); // _logger.info(promptId);

if("single_choice".equals(promptResponseMetadata.getPromptType())) {
PromptResponseMetadata promptResponseMetadata = promptResponseMetadataMap.get(promptId);
Map<String, Object> promptResponseMap = result.getPromptResponseMap();

builder.append(promptResponseMetadata.getDisplayLabel()).append(",");
builder.append(result.getSingleChoiceOrdinalValue()).append(",");
if("single_choice".equals(promptResponseMetadata.getPromptType())) {

SingleChoicePromptValueAndLabel valueLabel = (SingleChoicePromptValueAndLabel) promptResponseMap.get(promptId);

builder.append(valueLabel.getLabel()).append(",").append(valueLabel.getValue());

}
else {

Object value = promptResponseMap.get(promptId);

if(value instanceof JSONObject) { // single_choice_custom, multi_choice_custom
builder.append(((JSONObject) value).toString().replace(",", ";"));
}
Expand All @@ -375,7 +383,7 @@ else if(value instanceof JSONArray) { // multi_choice
else if (value instanceof String) { // clean up text for easier input into spreadsheets
String string = (String) value;

if("text".equals(promptResponseMetadataMap.get(canonicalColumnId.substring("urn:ohmage:prompt:id:".length())).getPromptType())) {
if("text".equals(promptResponseMetadataMap.get(promptId).getPromptType())) {
builder.append(cleanAndQuoteString(string));
} else {
builder.append(string);
Expand All @@ -384,9 +392,9 @@ else if (value instanceof String) { // clean up text for easier input into sprea
else {
builder.append(value);
}

builder.append(",");
}

builder.append(",");
}

}
Expand Down Expand Up @@ -451,6 +459,9 @@ public String createZeroResultOutput(SurveyResponseReadAwRequest req, List<Strin
}

private String cleanAndQuoteString(String string) {
return "\"" + string.replaceAll("\\s", " ").replaceAll("\"", "'") + "\"";
if(null != string) {
return "\"" + string.trim().replaceAll("\\s", " ").replaceAll("\"", "'") + "\"";
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ public void write(HttpServletRequest request, HttpServletResponse response, AwRe
// with their associated survey response and metadata. Each prompt response is returned from the db in its
// own row and the rows can have different sort orders.

boolean isCsv = "csv".equals(req.getOutputFormat());

for(SurveyResponseReadResult result : results) {

if(indexedResults.isEmpty()) { // first time thru
indexedResults.add(new SurveyResponseReadIndexedResult(result));
indexedResults.add(new SurveyResponseReadIndexedResult(result, isCsv));
}
else {
int numberOfIndexedResults = indexedResults.size();
Expand All @@ -159,11 +161,11 @@ public void write(HttpServletRequest request, HttpServletResponse response, AwRe
result.getRepeatableSetIteration())) {

found = true; //_logger.info("found");
indexedResults.get(i).addPromptResponse(result);
indexedResults.get(i).addPromptResponse(result, isCsv);
}
}
if(! found) {
indexedResults.add(new SurveyResponseReadIndexedResult(result));
indexedResults.add(new SurveyResponseReadIndexedResult(result, isCsv));
}
}
}
Expand Down

0 comments on commit e358872

Please sign in to comment.