package org.atlasapi.output.annotation;

import static org.atlasapi.content.Broadcast.ACTIVELY_PUBLISHED;

import java.io.IOException;

import org.atlasapi.content.Content;
import org.atlasapi.content.Item;
import org.atlasapi.output.FieldWriter;
import org.atlasapi.output.OutputContext;
import org.atlasapi.output.writers.BroadcastWriter;

import com.google.common.collect.Iterables;
import com.metabroadcast.common.ids.NumberToShortStringCodec;

public class BroadcastsAnnotation extends OutputAnnotation<Content> {
    
    private final BroadcastWriter broadcastWriter;
    
    public BroadcastsAnnotation(NumberToShortStringCodec codec) {
        broadcastWriter = new BroadcastWriter("broadcasts", codec);
    }

    @Override
    public void write(Content entity, FieldWriter writer, OutputContext ctxt) throws IOException {
        if (entity instanceof Item) {
            writeBroadcasts(writer, (Item) entity, ctxt);
        }
    }

    private void writeBroadcasts(FieldWriter writer, Item item, OutputContext ctxt) throws IOException {
        writer.writeList(broadcastWriter, Iterables.filter(item.getBroadcasts(), ACTIVELY_PUBLISHED), ctxt);
    }

}

@@ -124,24 +124,149 @@
 OException;%0A
+import java.time.LocalDateTime;%0Aimport java.util.Map;%0Aimport java.util.stream.Stream;%0A%0Aimport org.atlasapi.content.Broadcast;
 %0Aimport org.
@@ -565,16 +565,140 @@
 ngCodec;
+%0Aimport org.atlasapi.util.ImmutableCollectors;%0Aimport org.joda.time.DateTime;%0Aimport org.joda.time.format.DateTimeFormatter;
 %0A%0Apublic
@@ -1306,99 +1306,1902 @@
     
-writer.writeList(broadcastWriter, Iterables.filter(item.getBroadcasts(), ACTIVELY_PUBLISHED
+Stream%3CBroadcast%3E broadcastStream = item.getBroadcasts().stream()%0A                .filter(b -%3E ACTIVELY_PUBLISHED.apply(b));%0A        Map params = ctxt.getRequest().getParameterMap();%0A%0A        if (params.containsKey(%22broadcasts.transmissionStartTime.gt%22)) %7B%0A            String%5B%5D paramVals = (String%5B%5D) params.get(%22broadcasts.transmissionStartTime.gt%22);%0A            if (paramVals.length %3E 1 && paramVals%5B0%5D != null) %7B%0A                DateTime time = DateTime.parse(paramVals%5B0%5D);%0A                broadcastStream.filter(b -%3E b.getTransmissionTime().isAfter(time));%0A            %7D%0A        %7D%0A%0A        if (params.containsKey(%22broadcasts.transmissionStartTime.lt%22)) %7B%0A            String%5B%5D paramVals = (String%5B%5D) params.get(%22broadcasts.transmissionStartTime.lt%22);%0A            if (paramVals.length %3E 1 && paramVals%5B0%5D != null) %7B%0A                DateTime time = DateTime.parse(paramVals%5B0%5D);%0A                broadcastStream.filter(b -%3E b.getTransmissionTime().isBefore(time));%0A            %7D%0A        %7D%0A%0A        if (params.containsKey(%22broadcasts.transmissionEndTime.gt%22)) %7B%0A            String%5B%5D paramVals = (String%5B%5D) params.get(%22broadcasts.transmissionEndTime.gt%22);%0A            if (paramVals.length %3E 1 && paramVals%5B0%5D != null) %7B%0A                DateTime time = DateTime.parse(paramVals%5B0%5D);%0A                broadcastStream.filter(b -%3E b.getTransmissionEndTime().isAfter(time));%0A            %7D%0A        %7D%0A%0A        if (params.containsKey(%22broadcasts.transmissionStartTime.gt%22)) %7B%0A            String%5B%5D paramVals = (String%5B%5D) params.get(%22broadcasts.transmissionStartTime.lt%22);%0A            if (paramVals.length %3E 1 && paramVals%5B0%5D != null) %7B%0A                DateTime time = DateTime.parse(paramVals%5B0%5D);%0A                broadcastStream.filter(b -%3E b.getTransmissionEndTime().isBefore(time));%0A            %7D%0A        %7D%0A%0A        writer.writeList(broadcastWriter, broadcastStream.collect(ImmutableCollectors.toList()
 ), c

Add filter on broadcasts start and end times