package org.oskari.print;

import fi.nls.oskari.service.ServiceException;

import fi.nls.oskari.service.capabilities.CapabilitiesCacheService;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.oskari.print.request.PrintLayer;
import org.oskari.print.request.PrintRequest;
import org.oskari.print.wmts.WMTSCapabilitiesCache;

public class PrintService {

    private WMTSCapabilitiesCache wmtsCapsCache;

    public PrintService() {
        this(new WMTSCapabilitiesCache());
    }

    public PrintService(CapabilitiesCacheService capCacheService) {
        this(new WMTSCapabilitiesCache(capCacheService));
    }

    public PrintService(WMTSCapabilitiesCache wmtsCapsCache) {
        this.wmtsCapsCache = wmtsCapsCache;
    }

    public BufferedImage getPNG(PrintRequest request) throws ServiceException {
        request.setLayers(filterLayersWithZeroOpacity(request.getLayers()));
        return PNG.getBufferedImage(request, wmtsCapsCache);
    }

    public void getPDF(PrintRequest request, PDDocument doc)
            throws IOException, ServiceException {
        request.setLayers(filterLayersWithZeroOpacity(request.getLayers()));
        PDF.getPDF(request, doc, wmtsCapsCache);
    }

    private static List<PrintLayer> filterLayersWithZeroOpacity(List<PrintLayer> layers) {
        List<PrintLayer> filtered = new ArrayList<>();
        for (PrintLayer layer : layers) {
            if (layer.getOpacity() > 0) {
                filtered.add(layer);
            }
        }
        return filtered;
    }

}

@@ -211,61 +211,8 @@
 on;%0D
-%0Aimport java.util.ArrayList;%0D%0Aimport java.util.List;%0D
 %0A%0D%0Ai
@@ -260,53 +260,8 @@
 t;%0D%0A
-import org.oskari.print.request.PrintLayer;%0D%0A
 impo
@@ -863,86 +863,8 @@
  %7B%0D%0A
-        request.setLayers(filterLayersWithZeroOpacity(request.getLayers()));%0D%0A
     
@@ -1056,458 +1056,47 @@
     
-request.setLayers(filterLayersWithZeroOpacity(request.getLayers()));%0D%0A        PDF.getPDF(request, doc, wmtsCapsCache);%0D%0A    %7D%0D%0A%0D%0A    private static List%3CPrintLayer%3E filterLayersWithZeroOpacity(List%3CPrintLayer%3E layers) %7B%0D%0A        List%3CPrintLayer%3E filtered = new ArrayList%3C%3E();%0D%0A        for (PrintLayer layer : layers) %7B%0D%0A            if (layer.getOpacity() %3E 0) %7B%0D%0A                filtered.add(layer);%0D%0A            %7D%0D%0A        %7D%0D%0A        return filtered
+PDF.getPDF(request, doc, wmtsCapsCache)
 ;%0D%0A 

Remove filtering zero opacity layers from PrintService, this is already handled by the ActionHandler