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;
import org.datacite.conres.model.Pair;

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));
            }

            String type = "misc";
            for (Pair p : mr.getResourceTypes()){
                if (p.getKey().toUpperCase().equals("DATASET")){
                    type = "dataset";
                    break;
                }
            }

            return new JSONObject()
                    .put("type", type)
                    .put("DOI", mr.getDoi())
                    .put("URL", "https://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;
        }
    }
}

@@ -520,32 +520,238 @@
 %0A            %7D%0A%0A
+            JSONObject issued = new JSONObject();%0A            Integer year = Integer.valueOf(mr.getPublicationYear());%0A            issued.put(%22date-parts%22, new JSONArray().put(new JSONArray().put(year)));%0A%0A
             Stri
@@ -768,16 +768,16 @@
 %22misc%22;%0A
-
         
@@ -1029,28 +1029,24 @@
             
-    
 .put(%22type%22,
@@ -1052,20 +1052,16 @@
 , type)%0A
-    
         
@@ -1105,28 +1105,24 @@
             
-   
- 
 .put(%22URL%22, 
@@ -1147,36 +1147,32 @@
  + mr.getDoi())%0A
-    
                 
@@ -1231,28 +1231,24 @@
             
-    
 .put(%22publis
@@ -1284,28 +1284,24 @@
             
- 
-   
 .put(%22issued
@@ -1307,66 +1307,16 @@
 d%22, 
-new JSONObject().put(%22raw%22, mr.getPublicationYear()))%0A    
+issued)%0A
     

Correctly format publication date in citeproc JSON. Closes #27