Package com.sos.VirtualFileSystem.DataElements

Source Code of com.sos.VirtualFileSystem.DataElements.SOSFileList

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** 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.
********************************************************** end of preamble*/
/**
*
*/
package com.sos.VirtualFileSystem.DataElements;

import java.io.File;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sos.JSHelper.Basics.JSToolBox;
import com.sos.JSHelper.DataElements.JSDataElementDate;
import com.sos.JSHelper.DataElements.JSDateFormat;
import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.JSHelper.Logging.Log4JHelper;
import com.sos.VirtualFileSystem.DataElements.SOSFileListEntry.enuTransferStatus;
import com.sos.VirtualFileSystem.FTP.SOSFTPOptions;
import com.sos.VirtualFileSystem.Interfaces.ISOSVFSHandler;
import com.sos.VirtualFileSystem.Interfaces.ISOSVfsFileTransfer;
import com.sos.VirtualFileSystem.Interfaces.ISOSVirtualFile;
import com.sos.i18n.annotation.I18NResourceBundle;
import com.sos.scheduler.model.SchedulerObjectFactory;

/**
* @author KB
*
*/
@I18NResourceBundle(baseName = "SOSVirtualFileSystem", defaultLocale = "en")

public class SOSFileList extends JSToolBox   {

  private static String        conClassName      = "SOSFileList";
  private static Logger        logger          = Logger.getLogger(SOSFileList.class);
  @SuppressWarnings("unused")
  private static Log4JHelper      objLogger        = null;

  private SOSFTPOptions            objOptions        = null;
  private Vector<SOSFileListEntry>  objFileListEntries    = new Vector<SOSFileListEntry>();
  long                lngSuccessfulTransfers  = 0;
  long                lngFailedTransfers    = 0;
  long                lngSkipedTransfers    = 0;

  @SuppressWarnings("unused")
  private SOSFileList          objParent        = null;

  private ISOSVFSHandler        objVFS          = null;
  public ISOSVfsFileTransfer      objDataTargetClient    = null;
  public ISOSVfsFileTransfer      objDataSourceClient    = null;

  private JSDataElementDate      dteTransactionStart    = new JSDataElementDate(Now(), JSDateFormat.dfTIMESTAMPS);
  private JSDataElementDate      dteTransactionEnd    = new JSDataElementDate(Now(), JSDateFormat.dfTIMESTAMPS);

  public SchedulerObjectFactory  objFactory    = null;

  public void VFSHandler(final ISOSVFSHandler pobjVFS) {
    this.objVFS = pobjVFS;
  }

  public ISOSVFSHandler VFSHandler() {
    return objVFS;
  }

  public void setParent(final SOSFileList pobjParent) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::setParent";

    this.objParent = pobjParent;
  } // private void setParent

  /**
   *
   */
  public SOSFileList() {
    // TODO Auto-generated constructor stub
  }

  public SOSFileList(ISOSVFSHandler pobjVFS) {
    this.VFSHandler(pobjVFS);
  }

  public SOSFTPOptions Options() {
    return objOptions;
  }

  public void Options(final SOSFTPOptions pobjOptions) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::Options";

    this.objOptions = pobjOptions;

  } // private void Options

  public long count() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::count";

    return objFileListEntries.size();
  } // private long count

  private void CountStatus() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::CountStatus";

    lngSuccessfulTransfers = 0;
    lngFailedTransfers = 0;
    lngSkipedTransfers = 0;

    for (SOSFileListEntry objEntry : objFileListEntries) {
      switch (objEntry.getTransferStatus()) {
        case transferred:
          lngSuccessfulTransfers++;
          break;

        case transfer_has_errors:
          lngFailedTransfers++;
          break;

        case transfer_skipped:
          lngSkipedTransfers++;
          break;

        default:
          break;
      }
    }
  } // private void CountStatus

  /**
   *
   * \brief SuccessfulTransfers
   *
   * \details
   *
   * \return long
   *
   * @return
   */
  public long SuccessfulTransfers() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::count";
    this.CountStatus();
    return lngSuccessfulTransfers;
  } // private long SuccessfulTransfers

  /**
   *
   * \brief FailedTransfers
   *
   * \details
   *
   * \return long
   *
   * @return
   */
  public long FailedTransfers() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::count";
    CountStatus();
    return lngFailedTransfers;
  } // private long SuccessfulTransfers

  /**
   *
   * \brief SOSFileList
   *
   * \details
   *
   * @param pstrFileList
   */
  public SOSFileList(final String[] pstrFileList) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::";

    for (String strFileName : pstrFileList) {
      this.add(strFileName);
    }
  } // private SOSFileList

  public Vector<SOSFileListEntry> List() {

    if (objFileListEntries == null) {
      objFileListEntries = new Vector<SOSFileListEntry>();
    }
    return objFileListEntries;
  } // public Vector <SOSFileListEntry> List ()

  /**
   *
   * \brief SOSFileList
   *
   * \details
   *
   * @param pvecFileList
   */
  public SOSFileList(final Vector<File> pvecFileList) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::";

    for (File fleFileName : pvecFileList) {
      this.add(fleFileName.getAbsolutePath());
    }
  } // private SOSFileList

  /**
   *
   * \brief addAll
   *
   * \details
   *
   * \return void
   *
   * @param pobjFileList
   */
  public void addAll(final SOSFileList pobjFileList) {

    for (SOSFileListEntry objFile : pobjFileList.List()) {
      this.add(objFile.SourceFileName());
    }
  }

  /**
   *
   * \brief add
   *
   * \details
   *
   * \return void
   *
   * @param String[] pstrA
   */
  public void add(final String[] pstrA, final String pstrFolderName) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::add";

    if (pstrA == null) {
      return;
    }
    File fleDir = new File(pstrFolderName);
    String strDir = fleDir.getPath();
    for (String strFileName : pstrA) {
      File fleT = new File(strFileName);
      String strP = fleT.getParent();
      if (strP == null) {
        strFileName = strDir + "/" + strFileName;
      }
      this.add(strFileName);
    }

  } // private SOSFileListEntry add

  public void addFileNames(final Vector<String> pstrA) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::add";

    for (String strFileName : pstrA) {
      this.add(strFileName);
    }

  } // private SOSFileListEntry add

  public void addFiles(final Vector<File> pfleA) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::add";

    for (File fleFileName : pfleA) {
       this.add(fleFileName.getAbsolutePath());
    }

  } // private SOSFileListEntry add

  /**
   *
   * \brief add    
   *    
   * \details
   *
   * \return SOSFileListEntry
   *
   * @param pstrLocalFileName
   * @return
   */
  public SOSFileListEntry add(final String pstrLocalFileName) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::add";

    if (objFileListEntries == null) {
      objFileListEntries = new Vector<SOSFileListEntry>();
    }

    SOSFileListEntry objEntry = this.Find(pstrLocalFileName);
    if (objEntry == null) {
      objEntry = new SOSFileListEntry(pstrLocalFileName);
      objEntry.VfsHandler((ISOSVfsFileTransfer) objVFS);
      objEntry.setParent(this);
      objFileListEntries.add(objEntry);
      objEntry.Options(objOptions);
      objEntry.setStatus(SOSFileListEntry.enuTransferStatus.waiting4transfer);
    }

    return objEntry;
  } // private SOSFileListEntry add

  public SOSFileListEntry Find(final String pstrLocalFileName) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::Find";

    for (SOSFileListEntry objEntry : objFileListEntries) {
      if (pstrLocalFileName.equalsIgnoreCase(objEntry.SourceFileName())) {
        return objEntry;
      }
    }

    return null;
  } // private SOSFileListEntry Find

  // TODO Diese Methoden m�ssen in die SOSFileList-Klasse, da sie nicht unmittelbar etwas mit FTP zu tun haben (Die FileListKlasse
  // wird auch noch an anderen Stellen verwendet als nur in dieser Klasse).
  public void sendMails() throws Exception {
    // if (zeroByteCount > 0 && fileZeroByteNotificationTo != null && fileZeroByteNotificationTo.length() > 0) {
    // sendMail(fileZeroByteNotificationTo, fileZeroByteNotificationCC, fileZeroByteNotificationBCC, fileZeroByteNotificationSubject,
    // fileZeroByteNotificationBody);
    // }
    //
    // if (count > 0 && fileNotificationTo != null && fileNotificationTo.length() > 0) {
    // sendMail(fileNotificationTo, fileNotificationCC, fileNotificationBCC, fileNotificationSubject, fileNotificationBody);
    // }
  }

  /**
   * Erst wenn alle Dateien erfolgreich transferieriert wurden, dann sollen die lokalen Dateien gel�scht werden.
   * Parameter = objOptions.transactional.value() = yes und remove_files=yes
   * @throws Exception
   */
  public void DeleteSourceFiles() throws Exception {
    if (objOptions.remove_files.value() == true) {
      logger.debug("start erasing local files due to 'remove_files=true' ");

      for (SOSFileListEntry objListItem : objFileListEntries) {
        if (objListItem.getTransferStatus() == enuTransferStatus.transferred) {
          objListItem.DeleteSourceFile();
        }
      }
    }
  }

  /**
   * Erst bei Erfolgreichen transferieren aller Dateien, wird der atomic suffix umbennant
   * Bedingung: Parameter objOptions.transactional.value() = yes
   * @throws Exception
   */

  public void renameAtomicTransferFiles() throws Exception {

    try {
      if (objOptions.isAtomicTransfer() && objOptions.transactional.value() == true) {
        logger.debug("Starting renameing of atomic files ...");

        for (SOSFileListEntry objListItem : objFileListEntries) {
          String strFromFilename = objListItem.TargetTransferName();
          String strToFilename = MakeFullPathName(objOptions.TargetDir.Value(), objListItem.TargetFileName());
          ISOSVirtualFile objF = null;
          if (objListItem.FileExists() == true) {
            objF = objDataTargetClient.getFileHandle(strToFilename);
            objF.delete();
          }
          objF = objDataTargetClient.getFileHandle(MakeFullPathName(objOptions.TargetDir.Value(), strFromFilename));
          objF.rename(strToFilename);
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace(System.err);
      throw new JobSchedulerException("error in  " + sos.util.SOSClassUtil.getMethodName() + " ,\nreason: " + e);
    }
  }

  /**
   *
   * \brief EndTransaction
   *
   * \details
   *
   * \return void
   *
   */
  public void EndTransaction() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::EndTransaction";

    dteTransactionEnd.setParsePattern(JSDateFormat.dfTIMESTAMPS);
    dteTransactionEnd.Value(Now());
    this.sendTransferHistory();
    this.WriteHistory();

  } // private void EndTransaction

  /**
   *
   * \brief StartTransaction
   *
   * \details
   *
   * \return void
   *
   */
  public void StartTransaction() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::StartTransaction";

    dteTransactionStart.setParsePattern(JSDateFormat.dfTIMESTAMPS);
    dteTransactionStart.Value(Now());

  } // private void StartTransaction

  /**
   *
   * \brief sendTransferHistory
   *
   * \details
   * Send the transfer history for all transferred files to the background service.
   *
   * \return void
   *
   */
  public void sendTransferHistory() {
 
  @SuppressWarnings("unused")
  final String  conMethodName  = conClassName + "::sendTransferHistory";
 
  for (SOSFileListEntry objEntry : objFileListEntries) {
    objEntry.sendTransferHistory();
  }

} // private void sendTransferHistory

  /**
   *
   * \brief Rollback
   *
   * \details
   *
   * \return void
   *
   */
  public void Rollback() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::Rollback";
    // TODO check out, on which stage the process was aborted. Example: 1) source2target, 2)
    // renameAtomic, 3) DeleteSource, 4) deleteOnly, 5) renameOnly

    // TODO l�schen der Dateien mit Atomic-Prefix und -Suffix auf dem Target
    if (objOptions.isAtomicTransfer()) {
      for (SOSFileListEntry objListItem : objFileListEntries) {
        String strAtomicFileName = objListItem.getAtomicFileName();
        if (isNotEmpty(strAtomicFileName)) {
          objDataTargetClient.getFileHandle(strAtomicFileName).delete();
          logger.info(String.format("SetBack. File %1$s deleted", strAtomicFileName));
          objListItem.setAtomicFileName(EMPTY_STRING);
          objListItem.setStatus(enuTransferStatus.setBack);
        }
      }
    }

    if (objOptions.transactional.value() == true) {

    }
    this.EndTransaction();

    // TODO rules, decisions and coding

  } // private void Rollback

  public void WriteHistory() {
    dteTransactionEnd.FormatString(JSDateFormat.dfTIMESTAMPS);
    dteTransactionStart.FormatString(JSDateFormat.dfTIMESTAMPS);
    long lngDuration = dteTransactionEnd.getDateObject().getTime() - dteTransactionStart.getDateObject().getTime();
    String strWhat2Do = objOptions.operation.Value();
    logger.info(String.format("Transfer-operation '%4$s' started at %1$s, ended at %2$s, duration %3$d", dteTransactionStart.FormattedValue(),
        dteTransactionEnd.FormattedValue(), lngDuration, strWhat2Do));
    for (SOSFileListEntry objEntry : objFileListEntries) {
      logger.info(objEntry.toString());
    }
  }
}

TOP

Related Classes of com.sos.VirtualFileSystem.DataElements.SOSFileList

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.