/**
 * Copyright 2018 Smoke Turner, LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.smoketurner.dropwizard.graphql;

import java.io.File;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.io.Resources;
import graphql.execution.AsyncExecutionStrategy;
import graphql.execution.AsyncSerialExecutionStrategy;
import graphql.execution.ExecutionStrategy;
import graphql.execution.SubscriptionExecutionStrategy;
import graphql.execution.batched.BatchedExecutionStrategy;
import graphql.execution.instrumentation.ChainedInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.tracing.TracingInstrumentation;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import graphql.schema.idl.errors.SchemaProblem;
import io.dropwizard.validation.OneOf;

public class GraphQLFactory {

    @NotEmpty
    private String schemaFile = "";

    @NotEmpty
    @OneOf({ "async", "async_serial", "subscription", "batched" })
    private String executionStrategy = "async";

    @NotNull
    private List<String> blockedFields = Collections.emptyList();

    @NotNull
    private List<Instrumentation> instrumentations = Collections
            .singletonList(new TracingInstrumentation());

    @NotNull
    private RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .build();

    @JsonProperty
    public File getSchemaFile() throws URISyntaxException {
        return new File(Resources.getResource(schemaFile).toURI());
    }

    @JsonProperty
    public void setSchemaFile(final String file) {
        this.schemaFile = file;
    }

    @JsonProperty
    public ExecutionStrategy getExecutionStrategy() {
        switch (executionStrategy) {
        case "batched":
            return new BatchedExecutionStrategy();
        case "async_serial":
            return new AsyncSerialExecutionStrategy();
        case "subscription":
            return new SubscriptionExecutionStrategy();
        case "async":
        default:
            return new AsyncExecutionStrategy();
        }
    }

    @JsonProperty
    public void setExecutionStrategy(final String strategy) {
        this.executionStrategy = strategy;
    }

    @JsonIgnore
    public RuntimeWiring getRuntimeWiring() {
        return runtimeWiring;
    }

    @JsonIgnore
    public void setRuntimeWiring(final RuntimeWiring wiring) {
        this.runtimeWiring = wiring;
    }

    @JsonProperty
    public List<String> getBlockedFields() {
        return blockedFields;
    }

    @JsonProperty
    public void setBlockedFields(final List<String> fields) {
        this.blockedFields = fields;
    }

    @JsonIgnore
    public ChainedInstrumentation getInstrumentations() {
        return new ChainedInstrumentation(instrumentations);
    }

    @JsonIgnore
    public void setInstrumentations(List<Instrumentation> instrumentations) {
        this.instrumentations = instrumentations;
    }

    public GraphQLSchema build() throws SchemaProblem, URISyntaxException {
        final SchemaParser parser = new SchemaParser();
        final TypeDefinitionRegistry registry = parser.parse(getSchemaFile());

        final SchemaGenerator generator = new SchemaGenerator();
        final GraphQLSchema schema = generator.makeExecutableSchema(registry,
                runtimeWiring);
        return schema;
    }
}

@@ -641,216 +641,8 @@
 l;%0A%0A
-import java.io.File;%0Aimport java.net.URISyntaxException;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport javax.validation.constraints.NotNull;%0Aimport org.hibernate.validator.constraints.NotEmpty;%0A
 impo
@@ -689,16 +689,16 @@
 Ignore;%0A
+
 import c
@@ -747,47 +747,8 @@
 ty;%0A
-import com.google.common.io.Resources;%0A
 impo
@@ -1499,16 +1499,296 @@
 n.OneOf;
+%0Aimport java.io.BufferedReader;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.net.URISyntaxException;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport javax.validation.constraints.NotNull;%0Aimport org.hibernate.validator.constraints.NotEmpty;
 %0A%0Apublic
@@ -2350,20 +2350,30 @@
  public 
-File
+BufferedReader
  getSche
@@ -2421,33 +2421,52 @@
     
-return new File(Resources
+InputStream in = getClass().getClassLoader()
 .get
@@ -2473,16 +2473,24 @@
 Resource
+AsStream
 (schemaF
@@ -2497,17 +2497,110 @@
 ile)
-.toURI())
+;%0A        BufferedReader reader = new BufferedReader(new InputStreamReader(in));%0A        return reader
 ;%0A  

Update GraphQLFactory.java (#6)