/*
 * Copyright 2018, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.tools.compiler.enrichment;

import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import io.spine.Resources;
import io.spine.tools.properties.PropertiesWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Collection;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;
import static io.spine.tools.proto.FileDescriptors.parseSkipStandard;

/**
 * Parses descriptor set file and creates enrichments map for discovered enrichment
 * definitions.
 *
 * @author Alexander Yevsyukov
 */
public class EnrichmentLookup {

    /** Prevents instantiation of this utility class. */
    private EnrichmentLookup() {
    }

    public static void processDescriptorSetFile(File setFile, String targetDir) {
        final Collection<FileDescriptorProto> files = parseSkipStandard(setFile.getPath());

        final Map<String, String> propsMap = findAll(files);

        if (propsMap.isEmpty()) {
            log().debug("Enrichment lookup complete. No enrichments found.");
            return;
        }

        writeTo(propsMap, targetDir);
    }

    private static Map<String, String> findAll(Iterable<FileDescriptorProto> files) {
        final Map<String, String> propsMap = newHashMap();
        for (FileDescriptorProto file : files) {
            final EnrichmentFinder lookup = new EnrichmentFinder(file);
            final Map<String, String> enrichments = lookup.findAll();
            propsMap.putAll(enrichments);
        }
        return propsMap;
    }

    private static void writeTo(Map<String, String> propsMap, String targetDir) {
        log().debug("Writing the enrichment description to {}/{}",
                                     targetDir, Resources.ENRICHMENTS);

        final PropertiesWriter writer =
                new PropertiesWriter(targetDir, Resources.ENRICHMENTS);
        writer.write(propsMap);
    }

    private enum LogSingleton {
        INSTANCE;

        @SuppressWarnings("NonSerializableFieldInSerializableClass")
        private final Logger value = LoggerFactory.getLogger(EnrichmentLookup.class);
    }

    private static Logger log() {
        return LogSingleton.INSTANCE.value;
    }
}

@@ -1564,16 +1564,69 @@
 ons.%0A *%0A
+ * @author Alexander Litus%0A * @author Alex Tymchenko%0A
  * @auth
@@ -1778,24 +1778,250 @@
 () %7B%0A    %7D%0A%0A
+    /**%0A     * Takes enrichment information from all the proto types found in the passed descriptor%0A     * set file and generates the %7B@linkplain Resources#ENRICHMENTS resource file%7D in the%0A     * specified directory.%0A     */%0A
     public s
@@ -3022,33 +3022,16 @@
         
-                 
 targetDi
@@ -3057,17 +3057,16 @@
 MENTS);%0A
-%0A
         
@@ -3096,32 +3096,16 @@
 writer =
-%0A               
  new Pro

Add Javadoc