package org.atlasapi.output.annotation;

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 com.metabroadcast.common.ids.NumberToShortStringCodec;

import static com.google.common.base.Preconditions.checkNotNull;

public class BrandReferenceAnnotation extends OutputAnnotation<Content> {

    private static final String CONTAINER_FIELD = "container";

    private final ResourceRefWriter brandRefWriter;

    public BrandReferenceAnnotation(NumberToShortStringCodec idCodec) {
        super();
        brandRefWriter = new ResourceRefWriter(CONTAINER_FIELD, checkNotNull(idCodec));
    }

    @Override
    public void write(Content content, FieldWriter writer, OutputContext ctxt) throws IOException {
        if (content instanceof Item) {
            Item item = (Item) content;
            if (item.getContainerRef() == null) {
                writer.writeField(CONTAINER_FIELD, null);
            } else {
                writer.writeObject(brandRefWriter, item.getContainerRef(), ctxt);
            }
        }
    }

}

@@ -134,16 +134,52 @@
 t.Item;%0A
+import org.atlasapi.content.Series;%0A
 import o
@@ -1172,24 +1172,339 @@
           %7D%0A
+        %7D else if (content instanceof Series) %7B%0A            Series series = (Series) content;%0A            if (series.getBrandRef() == null) %7B%0A                writer.writeField(CONTAINER_FIELD, null);%0A            %7D else %7B%0A                writer.writeObject(brandRefWriter, series.getBrandRef(), ctxt);%0A            %7D%0A
         %7D%0A  

add brand ref to series (#552)