/*
 * Copyright (C) 2014-2015 LinkedIn Corp. All rights reserved.
 *
 * 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.
 */
package gobblin.data.management.copy.publisher;

import gobblin.configuration.ConfigurationKeys;
import gobblin.configuration.State;
import gobblin.configuration.WorkUnitState;
import gobblin.configuration.WorkUnitState.WorkingState;
import gobblin.data.management.copy.CopySource;
import gobblin.data.management.copy.CopyableFile;
import gobblin.data.management.copy.publisher.CopyDataPublisher;
import gobblin.util.HadoopUtils;

import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;


/**
 * A {@link CopyDataPublisher} that deletes files on the source fileSystem for all the {@link WorkUnitState}s that are
 * successfully committed/published
 */
@Slf4j
public class DeletingCopyDataPublisher extends CopyDataPublisher {

  private final FileSystem sourceFs;

  public DeletingCopyDataPublisher(State state) throws IOException {
    super(state);
    Configuration conf = HadoopUtils.getConfFromState(state);
    String uri = state.getProp(ConfigurationKeys.SOURCE_FILEBASED_FS_URI, ConfigurationKeys.LOCAL_FS_URI);
    this.sourceFs = FileSystem.get(URI.create(uri), conf);
  }

  @Override
  public void publishData(Collection<? extends WorkUnitState> states) throws IOException {
    super.publishData(states);
    for (WorkUnitState state : states) {
      if (state.getWorkingState() == WorkingState.COMMITTED) {
        try {
          deleteFilesOnSource(state);
        } catch (Throwable t) {
          log.warn(
              String.format("Failed to delete one or more files on source in %s",
                  state.getProp(CopySource.SERIALIZED_COPYABLE_FILES)), t);
        }
      } else {
        log.info(String.format("Not deleting files %s on source fileSystem as the workunit state is %s.",
            state.getProp(CopySource.SERIALIZED_COPYABLE_FILES), state.getWorkingState()));
      }
    }
  }

  private void deleteFilesOnSource(WorkUnitState state) throws IOException {
    List<CopyableFile> copyableFiles = CopySource.deserializeCopyableFiles(state);
    for (CopyableFile copyableFile : copyableFiles) {
      if (this.sourceFs.exists(copyableFile.getOrigin().getPath())) {
        log.info(String.format("Deleting %s on source fileSystem.", copyableFile.getOrigin().getPath()));
        if (!this.sourceFs.delete(copyableFile.getOrigin().getPath(), true)) {
          throw new IOException("Delete failed for " + copyableFile.getOrigin().getPath());
        }
      }
    }
  }
}

@@ -843,73 +843,8 @@
 le;%0A
-import gobblin.data.management.copy.publisher.CopyDataPublisher;%0A
 impo
@@ -872,16 +872,16 @@
 pUtils;%0A
+
 %0Aimport 
@@ -1095,16 +1095,50 @@
 System;%0A
+import org.apache.hadoop.fs.Path;%0A
 %0A%0A/**%0A *
@@ -1369,16 +1369,16 @@
 sher %7B%0A%0A
-
   privat
@@ -1405,16 +1405,71 @@
 ourceFs;
+%0A  private static final String READY_SUFFIX = %22.ready%22;
 %0A%0A  publ
@@ -2744,19 +2744,38 @@
 %7B%0A      
-if 
+HadoopUtils.deletePath
 (this.so
@@ -2784,16 +2784,10 @@
 ceFs
-.exists(
+, 
 copy
@@ -2820,132 +2820,47 @@
 th()
-)) %7B%0A        log.info(String.format(%22Deleting %25s on source fileSystem.%22, copyableFile.getOrigin().getPath()));%0A        if (!
+, false);%0A      HadoopUtils.deletePath(
 this
@@ -2868,23 +2868,26 @@
 sourceFs
-.delete
+, new Path
 (copyabl
@@ -2917,73 +2917,21 @@
 th()
-, true)) %7B%0A          throw new IOException(%22Delete failed for %22 +
+.getParent(),
  cop
@@ -2931,32 +2931,43 @@
 (), copyableFile
+%0A          
 .getOrigin().get
@@ -2972,36 +2972,61 @@
 etPath()
-);%0A        %7D%0A      %7D
+.getName()%0A          + READY_SUFFIX), false);
 %0A    %7D%0A 

Delete .ready files after publish