/***************************************************************************
 * Copyright 2001-2006 The eXo Platform SARL         All rights reserved.  *
 * Please look at license.txt in info directory for more license detail.   *
 **************************************************************************/
package org.exoplatform.services.jcr.impl.storage.value.jdbc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.sql.DataSource;

import org.exoplatform.services.log.Log;
import org.exoplatform.services.jcr.datamodel.PropertyData;
import org.exoplatform.services.jcr.impl.dataflow.DataManager;
import org.exoplatform.services.jcr.impl.storage.value.fs.SimpleFileValueStorage;
import org.exoplatform.services.jcr.storage.WorkspaceStorageConnection;
import org.exoplatform.services.jcr.storage.swap.BinaryValueSwapStorage;
import org.exoplatform.services.jcr.storage.value.ValueIOChannel;
import org.exoplatform.services.jcr.storage.value.ValueStoragePlugin;
import org.exoplatform.services.jcr.storage.value.ValueStoragePluginProvider;
import org.exoplatform.services.log.ExoLogger;

/**
 * Created by The eXo Platform SARL
 *
 * 04.09.2006
 *
 * @author <a href="mailto:peter.nedonosko@exoplatform.com.ua">Peter Nedonosko</a>
 * @version $Id: JDBCValueStoragePluginProvider.java1 11837 2008-03-11 12:48:56Z gazarenkov $
 */
public class JDBCValueStoragePluginProvider extends ArrayList<ValueStoragePlugin> implements ValueStoragePluginProvider {

  private static Log log = ExoLogger.getLogger("jcr.JDBCValueStoragePluginProvider");
  
  protected final String containerName;
  protected final boolean multiDb;
  protected final DataSource dataSource;
  protected final ValueStoragePluginProvider standaloneProvider;
  protected final JDBCValueStorage defaultStorage;
  
  public JDBCValueStoragePluginProvider(String containerName, boolean multiDb,
      DataSource dataSource, ValueStoragePluginProvider standaloneProvider) {
    
    this.containerName = containerName;
    this.multiDb = multiDb;
    this.dataSource = dataSource;
    
    this.standaloneProvider = standaloneProvider;
    
    this.defaultStorage = new JDBCValueStorage(containerName, multiDb, dataSource);
    this.defaultStorage.initBinarySwap(getBinaryValueSwap());
  }

  public ValueIOChannel getApplicableChannel(PropertyData property) throws IOException {
    ValueIOChannel channel = standaloneProvider.getApplicableChannel(property);
    if (channel == null)
      channel = defaultStorage.openIOChannel();
    return channel;
  }

  public BinaryValueSwapStorage getBinaryValueSwap() {
    return standaloneProvider.getBinaryValueSwap();
  }
  
  public Iterator<ValueStoragePlugin> plugins() {
    
    return null;
  }

  /**
   * TODO Experimental feature.
   * Applicable for SimpleFileValueStorage instances only.
   */
  public void checkConsistency(WorkspaceStorageConnection dataConnection) {
    Iterator<ValueStoragePlugin> plugins = standaloneProvider.plugins();
    while(plugins.hasNext()) {
      ValueStoragePlugin plugin = plugins.next();
      // TODO magic for file value storage
      if (plugin instanceof SimpleFileValueStorage) {
        ((SimpleFileValueStorage) plugin).checkConsistency(dataConnection);
      }
    }
  }
  
}
