package org.atlasapi.output.annotation;

import org.atlasapi.channel.ChannelGroup;
import org.atlasapi.channel.Region;
import org.atlasapi.channel.ResolvedChannelGroup;
import org.atlasapi.output.FieldWriter;
import org.atlasapi.output.OutputContext;
import org.atlasapi.output.writers.ChannelGroupWriter;
import org.atlasapi.query.common.exceptions.MissingResolvedDataException;

import java.io.IOException;
import java.util.Optional;

public class PlatformAnnotation extends OutputAnnotation<ResolvedChannelGroup> {

    private static final ChannelGroupWriter CHANNEL_GROUP_WRITER = new ChannelGroupWriter(
            "platforms",
            "platform"
    );

    @Override
    public void write(ResolvedChannelGroup entity, FieldWriter writer, OutputContext ctxt)
            throws IOException {
        if (!(entity.getChannelGroup() instanceof Region)) {
            return;
        }

        Optional<ChannelGroup<?>> channelGroup = entity.getPlatformChannelGroup();
        if (channelGroup.isPresent()) {
            writer.writeObject(CHANNEL_GROUP_WRITER, channelGroup.get(), ctxt);
        } else {
            throw new MissingResolvedDataException(CHANNEL_GROUP_WRITER.fieldName(entity.getChannelGroup()));
        }
    }
}

@@ -1109,16 +1109,193 @@
  else %7B%0A
+            if (!((Region) entity.getChannelGroup()).getPlatform().isPresent()) %7B%0A                writer.writeObject(CHANNEL_GROUP_WRITER, null, ctxt);%0A            %7D else %7B%0A    
         
@@ -1396,16 +1396,30 @@
 up()));%0A
+            %7D%0A
         

Allow null platform field in PlatformAnnotation