package org.ambraproject.rhino.view.journal;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import org.ambraproject.rhino.model.Journal;
import org.ambraproject.rhino.model.Volume;
import org.ambraproject.rhino.view.JsonOutputView;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A view of a {@link Journal} object that shows all nested volume and issue lists as JSON objects keyed by their
 * identifying URIs. Compare {@link JournalNonAssocView}, which omits the nested lists entirely.
 */
public class JournalOutputView implements JsonOutputView {

  public static class Factory {
    @Autowired
    private IssueOutputView.Factory issueOutputViewFactory;
    @Autowired
    private VolumeOutputView.Factory volumeOutputViewFactory;

    public JournalOutputView getView(Journal journal) {
      return new JournalOutputView(issueOutputViewFactory, volumeOutputViewFactory, journal);
    }
  }

  private final Journal journal;
  private IssueOutputView.Factory issueOutputViewFactory;
  private VolumeOutputView.Factory volumeOutputViewFactory;

  public JournalOutputView(IssueOutputView.Factory issueOutputViewFactory,
                           VolumeOutputView.Factory volumeOutputViewFactory, Journal journal) {
    this.journal = Objects.requireNonNull(journal);
    this.issueOutputViewFactory = Objects.requireNonNull(issueOutputViewFactory);
    this.volumeOutputViewFactory = Objects.requireNonNull(volumeOutputViewFactory);
  }

  @Override
  public JsonElement serialize(JsonSerializationContext context) {
    JsonObject serialized = context.serialize(journal).getAsJsonObject();
    serialized.remove("currentIssue");
    IssueOutputView currentIssueView = issueOutputViewFactory.getView(journal.getCurrentIssue());
    serialized.add("currentIssue", currentIssueView.serialize(context));
    serialized.remove("volumes");

    List<VolumeOutputView> volumeOutputViews = new ArrayList<>();
    for (Volume volume : journal.getVolumes()) {
      VolumeOutputView volumeOutputView = volumeOutputViewFactory.getView(volume);
      volumeOutputViews.add(volumeOutputView);
    }

    serialized.add("volumes", context.serialize(volumeOutputViews));
    return serialized;
  }

}

@@ -159,16 +159,59 @@
 ontext;%0A
+import org.ambraproject.rhino.model.Issue;%0A
 import o
@@ -1804,24 +1804,25 @@
 onObject();%0A
+%0A
     serializ
@@ -1848,16 +1848,102 @@
 ssue%22);%0A
+    Issue currentIssue = journal.getCurrentIssue();%0A    if (currentIssue != null) %7B%0A  
     Issu
@@ -2004,28 +2004,17 @@
 getView(
-journal.getC
+c
 urrentIs
@@ -2020,13 +2020,13 @@
 ssue
-()
 );%0A
+  
     
@@ -2090,24 +2090,31 @@
 (context));%0A
+    %7D%0A%0A
     serializ
@@ -2135,17 +2135,16 @@
 umes%22);%0A
-%0A
     List
@@ -2378,33 +2378,32 @@
 putView);%0A    %7D%0A
-%0A
     serialized.a
@@ -2451,24 +2451,25 @@
 putViews));%0A
+%0A
     return s

Fix NPE in JournalOutputView.java