/hippo/src/main/java/com/googlecode/hdbc/view/policy/ActiveExperimentsOutputPolicy.java

http://hdbc.googlecode.com/ · Java · 35 lines · 23 code · 8 blank · 4 comment · 1 complexity · 7deec39fccdd332bbe664b50193b6507 MD5 · raw file

  1. package com.googlecode.hdbc.view.policy;
  2. import java.util.List;
  3. import java.util.Map;
  4. import net.sf.json.JSONArray;
  5. import net.sf.json.JSONObject;
  6. import com.googlecode.hdbc.controller.ModelAttributes;
  7. import com.googlecode.hdbc.model.record.ExperimentData;
  8. import com.googlecode.hdbc.view.JsonKeys;
  9. /**
  10. * Creates a view string of the form
  11. * {data: [{ExperimentData.toJson()}, {...}, ...]}
  12. */
  13. public class ActiveExperimentsOutputPolicy implements ICustomOutputPolicy {
  14. @SuppressWarnings("unchecked")
  15. @Override
  16. public Map<String, Object> customOutput(Map<String, Object> model) {
  17. List<ExperimentData> experiments = (List<ExperimentData>) model.get(ModelAttributes.COLLECTION);
  18. JSONArray array = new JSONArray();
  19. for (ExperimentData experiment : experiments) {
  20. JSONObject elmnt = experiment.toJson();
  21. array.add(elmnt);
  22. }
  23. JSONObject jsn = new JSONObject();
  24. jsn.put(JsonKeys.data, array);
  25. return jsn;
  26. }
  27. }