package org.datacite.conres.view;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.datacite.conres.model.Model;

public class CslJsonRepresentation {
    public static JSONObject writeJSON(Model mr) {
        try {
            JSONArray authors = new JSONArray();
            for (String  p : mr.getCreators()) {
                authors.put(new JSONObject().put("literal", p));
            }
            return new JSONObject()
                    .put("type", "misc")
                    .put("DOI", mr.getDoi())
                    .put("URL", "http://dx.doi.org/" + mr.getDoi())
                    .put("title", mr.getTitles().get(0).getValue())
                    .put("publisher", mr.getPublisher())
                    .put("issued", new JSONObject().put("raw", mr.getPublicationYear()))
                    .put("author", authors);
        } catch (JSONException je) {
            return null;
        }
    }
}

@@ -207,16 +207,55 @@
 l.Model;
+%0Aimport org.datacite.conres.model.Pair;
 %0A%0Apublic
@@ -523,24 +523,272 @@
           %7D%0A
+%0A            String type = %22misc%22;%0A            for (Pair p : mr.getResourceTypes())%7B%0A                if (p.getKey().toUpperCase().equals(%22DATASET%22))%7B%0A                    type = %22dataset%22;%0A                    break;%0A                %7D%0A            %7D%0A%0A
             
@@ -844,22 +844,20 @@
 %22type%22, 
-%22misc%22
+type
 )%0A      

use dataset in CSL JSON