package cz.hobrasoft.pdfmu.operation.metadata;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import cz.hobrasoft.pdfmu.Console;
import cz.hobrasoft.pdfmu.operation.Operation;
import cz.hobrasoft.pdfmu.operation.OperationCommon;
import cz.hobrasoft.pdfmu.operation.OperationException;
import cz.hobrasoft.pdfmu.operation.args.InOutPdfArgs;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

public class OperationMetadataSet extends OperationCommon {

    private final MetadataParameters metadataParameters = new MetadataParameters();

    private final InOutPdfArgs inout = new InOutPdfArgs();

    @Override
    public Subparser configureSubparser(Subparser subparser) {
        String help = "Update metadata in a PDF document";

        // Configure the subparser
        subparser.help(help)
                .description(help)
                .defaultHelp(true)
                .setDefault("command", OperationMetadataSet.class);

        inout.addArguments(subparser);
        metadataParameters.addArguments(subparser);

        return subparser;
    }

    @Override
    public void execute(Namespace namespace) throws OperationException {
        inout.setFromNamespace(namespace);
        metadataParameters.setFromNamespace(namespace);
        set(inout, metadataParameters);
    }

    private static void set(InOutPdfArgs inout, MetadataParameters metadataParameters) throws OperationException {
        inout.open();

        PdfReader reader = inout.getPdfReader();
        PdfStamper stp = inout.getPdfStamper();

        set(reader, stp, metadataParameters);

        inout.close();
    }

    private static void set(PdfReader reader, PdfStamper stamper, MetadataParameters metadataParameters) {
        Map<String, String> info = metadataParameters.getInfo(reader);
        set(stamper, info);
    }

    private static final List<String> ignoredProperties
            = Arrays.asList(new String[]{"Producer", "ModDate"});

    public static void set(PdfStamper stp, Map<String, String> info) {
        assert stp != null;
        assert info != null;

        for (String key : ignoredProperties) {
            if (info.containsKey(key)) {
                String value = info.get(key);
                Console.println(String.format("Warning: The property %s is set automatically. The value \"%s\" will be ignored.", key, value));
            }
        }

        stp.setMoreInfo(info);
        Console.println("PDF metadata have been set.");
    }

    private static Operation instance = null;

    public static Operation getInstance() {
        if (instance == null) {
            instance = new OperationMetadataSet();
        }
        return instance;
    }

    private OperationMetadataSet() {
        // Singleton
    }

}

@@ -126,43 +126,8 @@
 er;%0A
-import cz.hobrasoft.pdfmu.Console;%0A
 impo
@@ -165,24 +165,24 @@
 .Operation;%0A
+
 import cz.ho
@@ -403,16 +403,49 @@
 il.Map;%0A
+import java.util.logging.Logger;%0A
 import n
@@ -535,16 +535,16 @@
 arser;%0A%0A
-
 public c
@@ -596,16 +596,114 @@
 mmon %7B%0A%0A
+    private static final Logger logger = Logger.getLogger(OperationMetadataSet.class.getName());%0A%0A
     priv
@@ -2465,31 +2465,30 @@
         
-Console.println
+logger.warning
 (String.
@@ -2660,23 +2660,19 @@
     
-Console.println
+logger.info
 (%22PD

Use a Logger instead of Console