/*
 * This file is part of Nuts Framework.
 * Copyright (C) 2009 Nuts Develop Team.
 *
 * Nuts Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License any later version.
 * 
 * Nuts Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nuts Framework. If not, see <http://www.gnu.org/licenses/>.
 */
package nuts.gae.vfs.ngfs.provider;

import nuts.core.dao.DaoClient;
import nuts.core.lang.Arrays;
import nuts.core.lang.Collections;
import nuts.gae.dao.GaeDataAccessClient;
import nuts.gae.dao.GaeDataAccessClientFactory;
import nuts.gae.vfs.ngfs.GaeDataMetaData;
import nuts.gae.vfs.ngfs.GaeFileMetaData;

import java.util.Collection;
import java.util.Properties;

import org.apache.commons.vfs2.Capability;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;

/**
 * A file system provider, which uses direct file access.
 * 
 */
public class GaeFileProvider extends AbstractOriginatingFileProvider {
	public final static Collection<Capability> capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[] {
			Capability.CREATE, Capability.DELETE, Capability.RENAME,
			Capability.GET_TYPE, Capability.GET_LAST_MODIFIED,
			Capability.SET_LAST_MODIFIED_FILE,
			Capability.SET_LAST_MODIFIED_FOLDER, Capability.LIST_CHILDREN,
			Capability.READ_CONTENT, Capability.URI, Capability.WRITE_CONTENT,
			Capability.APPEND_CONTENT, Capability.RANDOM_ACCESS_READ,
			Capability.RANDOM_ACCESS_WRITE }));

	private static DaoClient dataAccessClient;

	/**
	 * Constructor
	 */
	public GaeFileProvider() {
		if (dataAccessClient == null) {
			Properties props = new Properties();
			props.setProperty(GaeDataAccessClient.META_PREFIX + GaeFileMetaData.TABLE_NAME, GaeFileMetaData.class.getName());
			props.setProperty(GaeDataAccessClient.META_PREFIX + GaeDataMetaData.TABLE_NAME, GaeDataMetaData.class.getName());
			
			dataAccessClient = GaeDataAccessClientFactory.buildDataAccessClient("ngfs", props);
		}
	}

	/**
	 * @return the dataAccessClient
	 */
	public static DaoClient getDataAccessClient() {
		return dataAccessClient;
	}

	/**
	 * @param dataAccessClient the dataAccessClient to set
	 */
	public static void setDataAccessClient(DaoClient dataAccessClient) {
		GaeFileProvider.dataAccessClient = dataAccessClient;
	}

    /**
     * Get the filesystem capabilities.<br>
     * These are the same as on the filesystem, but available before the first filesystem was
     * instanciated.
     */
	public Collection<Capability> getCapabilities() {
		return capabilities;
	}

	/**
	 * Creates the File System.
	 */
	protected FileSystem doCreateFileSystem(final FileName name,
			final FileSystemOptions options)
			throws FileSystemException {
		return new GaeFileSystem(name, options, dataAccessClient);
	}
	
}
