/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.beam.runners.flink.translation.wrappers;

import org.apache.beam.runners.flink.translation.utils.SerializedPipelineOptions;
import org.apache.beam.sdk.io.Sink;
import org.apache.beam.sdk.io.Write;
import org.apache.beam.sdk.options.PipelineOptions;

import org.apache.flink.api.common.io.OutputFormat;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.util.AbstractID;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * Wrapper class to use generic Write.Bound transforms as sinks.
 * @param <T> The type of the incoming records.
 */
public class SinkOutputFormat<T> implements OutputFormat<T> {

  private final Sink<T> sink;

  private final SerializedPipelineOptions serializedOptions;

  private Sink.WriteOperation<T, ?> writeOperation;
  private Sink.Writer<T, ?> writer;

  private AbstractID uid = new AbstractID();

  public SinkOutputFormat(Write.Bound<T> transform, PipelineOptions pipelineOptions) {
    this.sink = extractSink(transform);
    this.serializedOptions = new SerializedPipelineOptions(pipelineOptions);
  }

  private Sink<T> extractSink(Write.Bound<T> transform) {
    // TODO possibly add a getter in the upstream
    try {
      Field sinkField = transform.getClass().getDeclaredField("sink");
      sinkField.setAccessible(true);
      @SuppressWarnings("unchecked")
      Sink<T> extractedSink = (Sink<T>) sinkField.get(transform);
      return extractedSink;
    } catch (NoSuchFieldException | IllegalAccessException e) {
      throw new RuntimeException("Could not acquire custom sink field.", e);
    }
  }

  @Override
  public void configure(Configuration configuration) {
    writeOperation = sink.createWriteOperation(serializedOptions.getPipelineOptions());
    try {
      writeOperation.initialize(serializedOptions.getPipelineOptions());
    } catch (Exception e) {
      throw new RuntimeException("Failed to initialize the write operation.", e);
    }
  }

  @Override
  public void open(int taskNumber, int numTasks) throws IOException {
    try {
      writer = writeOperation.createWriter(serializedOptions.getPipelineOptions());
    } catch (Exception e) {
      throw new IOException("Couldn't create writer.", e);
    }
    try {
      writer.open(uid + "-" + String.valueOf(taskNumber));
    } catch (Exception e) {
      throw new IOException("Couldn't open writer.", e);
    }
  }

  @Override
  public void writeRecord(T record) throws IOException {
    try {
      writer.write(record);
    } catch (Exception e) {
      throw new IOException("Couldn't write record.", e);
    }
  }

  @Override
  public void close() throws IOException {
    try {
      writer.close();
    } catch (Exception e) {
      throw new IOException("Couldn't close writer.", e);
    }
  }

}

@@ -1796,29 +1796,26 @@
 k = 
-ex
 tra
-ctSink(transform
+nsform.getSink(
 );%0A 
@@ -1899,517 +1899,8 @@
  %7D%0A%0A
-  private Sink%3CT%3E extractSink(Write.Bound%3CT%3E transform) %7B%0A    // TODO possibly add a getter in the upstream%0A    try %7B%0A      Field sinkField = transform.getClass().getDeclaredField(%22sink%22);%0A      sinkField.setAccessible(true);%0A      @SuppressWarnings(%22unchecked%22)%0A      Sink%3CT%3E extractedSink = (Sink%3CT%3E) sinkField.get(transform);%0A      return extractedSink;%0A    %7D catch (NoSuchFieldException %7C IllegalAccessException e) %7B%0A      throw new RuntimeException(%22Could not acquire custom sink field.%22, e);%0A    %7D%0A  %7D%0A%0A
   @O

replace obsolete reflection call