package org.eclipse.team.examples.filesystem.subscriber;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.variants.ThreeWaySynchronizer;
import org.eclipse.team.examples.filesystem.CRCUtils;

import it.micegroup.voila2.cartridge.library.javaservice.FileCopyUtils;

public class FileSystemThreeWaySynchronizer extends ThreeWaySynchronizer {

	public FileSystemThreeWaySynchronizer(QualifiedName name) {
		super(name);
	}

	/**
	 * Return whether the local resource has been modified since the last time
	 * the base bytes were set. This method will return <code>false</code>
	 * for ignored resources and <code>true</code> for non-existant resources
	 * that have base bytes cached.
	 * @param resource the resource
	 * @return <code>true</code> if the resource has been modified since the
	 * last time the base bytes were set.
	 * @throws TeamException
	 */
	public boolean isLocallyModified(IResource resource) throws TeamException {
		if (getBaseBytes(resource) != null && !FileCopyUtils.exists(resource)) {
			return true;
		}
		byte[] localBytes = Long.toString(CRCUtils.calculateCRC(
				new java.io.File(resource.getLocationURI()))).getBytes();

		if (!CRCUtils.equals(localBytes, getBaseBytes(resource))) {
			return true;
		}
		return false;
	}

}
