/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.integration.copier.portfolio;

import org.springframework.util.StringUtils;

import com.opengamma.integration.copier.portfolio.reader.PortfolioReader;
import com.opengamma.integration.copier.portfolio.writer.PortfolioWriter;
import com.opengamma.master.position.ManageablePosition;
import com.opengamma.master.security.ManageableSecurity;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.tuple.ObjectsPair;

/**
 * A simple portfolio copier that copies positions from readers to the specified writer.
 */
public class SimplePortfolioCopier implements PortfolioCopier {

  private boolean _flatten;

  public SimplePortfolioCopier() {
    _flatten = false;
  }

  public SimplePortfolioCopier(boolean flatten) {
    _flatten = flatten;
  }

  @Override
  public void copy(PortfolioReader portfolioReader, PortfolioWriter portfolioWriter) {
    copy(portfolioReader, portfolioWriter, null);
  }

  public void copy(PortfolioReader portfolioReader, PortfolioWriter portfolioWriter, PortfolioCopierVisitor visitor) {

    ArgumentChecker.notNull(portfolioWriter, "portfolioWriter");
    ArgumentChecker.notNull(portfolioReader, "portfolioReader");
    
    ObjectsPair<ManageablePosition, ManageableSecurity[]> next;

    // Read in next row, checking for EOF
    while ((next = portfolioReader.readNext()) != null) {
      
      // Is position and security data is available for the current row?
      if (next.getFirst() != null && next.getSecond() != null) {
        
        // Set current path
        String[] path = _flatten ? new String[0] : portfolioReader.getCurrentPath();
        portfolioWriter.setPath(path);
        
        // Write position and security data
        ObjectsPair<ManageablePosition, ManageableSecurity[]> written = 
            portfolioWriter.writePosition(next.getFirst(), next.getSecond());
        
        if (visitor != null && written != null) {
          visitor.info(StringUtils.arrayToDelimitedString(path, "/"), written.getFirst(), written.getSecond());
        }
      } else {
        if (visitor != null) {
          if (next.getFirst() == null) {
            visitor.error("Could not load position");
          }
          if (next.getSecond() == null) {
            visitor.error("Could not load security(ies)");
          }
        }
      }

    }

  }
}

@@ -185,16 +185,73 @@
 folio;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -287,16 +287,16 @@
 gUtils;%0A
-
 %0Aimport 
@@ -801,16 +801,106 @@
 pier %7B%0A%0A
+  private static final Logger s_logger = LoggerFactory.getLogger(PortfolioCopier.class);%0A%0A
   privat
@@ -1535,16 +1535,38 @@
  next;%0A%0A
+    while (true) %7B%0A%0A  
     // R
@@ -1599,76 +1599,348 @@
 for 
-EOF%0A    while ((next = portfolioReader.readNext()) != null) %7B%0A      
+errors and EOF%0A      try %7B%0A        next = portfolioReader.readNext();%0A      %7D catch (Exception e) %7B%0A        // skip to next row on uncaught exception while parsing row%0A        s_logger.error(%22Unable to parse row: %22 + e.getMessage());%0A        continue;%0A      %7D%0A      if (next == null) %7B%0A        // stop loading on EOF%0A        break;%0A      %7D%0A
 %0A   

Enhance SimplePortfolioCopier to handle any uncaught exceptions that might occur when parsing the current row.