/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.core.client;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.util.SyncFileWriter;

/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class TemplateHandler extends ResponseHandler {

	/**
	 * @see org.eclipse.team.internal.ccvs.core.client.ResponseHandler#getResponseID()
	 */
	public String getResponseID() {
		return "Template"; //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.team.internal.ccvs.core.client.ResponseHandler#handle(org.eclipse.team.internal.ccvs.core.client.Session, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void handle(Session session, String localDir, IProgressMonitor monitor) throws CVSException {
		String remoteDir = session.readLine();
		ICVSFolder localFolder = getExistingFolder(session, localDir);
		IContainer container = (IContainer)localFolder.getIResource();
		ICVSStorage templateFile;
		if (container == null) {
			// Create a dummy storage handle to recieve the contents from the server
			templateFile = new ICVSStorage() {
				public String getName() {
					return "Template"; //$NON-NLS-1$
				}
				public void setContents(
					InputStream stream,
					int responseType,
					boolean keepLocalHistory,
					IProgressMonitor monitor)
					throws CVSException {

					try {
						// Transfer the contents
						OutputStream out = new ByteArrayOutputStream();
						try {
							byte[] buffer = new byte[1024];
							int read;
							while ((read = stream.read(buffer)) >= 0) {
								Policy.checkCanceled(monitor);
								out.write(buffer, 0, read);
							}
						} finally {
							out.close();
						}
					} catch (IOException e) {
						throw CVSException.wrapException(e); //$NON-NLS-1$
					} finally {
						try {
							stream.close();
						} catch (IOException e1) {
							// Ignore close errors
						}
					}
				}
				public long getSize() {
					return 0;
				}
				public InputStream getContents() throws CVSException {
					return new ByteArrayInputStream(new byte[0]);
				}
			};
		} else {
			templateFile = CVSWorkspaceRoot.getCVSFileFor(SyncFileWriter.getTemplateFile(container));
		}
		session.receiveFile(templateFile, false, UpdatedHandler.HANDLE_UPDATED, monitor);
	}

}
