<commit_before>package de.cooperateproject.ui.nature.tasks;

import java.util.stream.Stream;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewInvalidationEvent;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.io.IOUtil;

import de.cooperateproject.cdo.util.connection.CDOConnectionManager;
import de.cooperateproject.ui.Activator;
import de.cooperateproject.ui.properties.ProjectPropertiesDTO;

public abstract class CDOHandlingBackgroundTask extends CooperateProjectBackgroundTask {

	private class ChangeListener implements IListener {

		@Override
		public void notifyEvent(IEvent arg0) {
			if (arg0 instanceof CDOViewInvalidationEvent) {
				CDOViewInvalidationEvent event = (CDOViewInvalidationEvent) arg0;
				if (isRelevantChange(event)) {
					handleChange(event);					
				}
			} else {
				handleChange(arg0);
			}
		}
	}

	private final ChangeListener cdoChangeListener = new ChangeListener();
	private CDOResourceFolder repositoryFolder;

	protected CDOHandlingBackgroundTask(IProject project, ProjectPropertiesDTO properties) {
		super(project, properties);
	}
	
	private boolean isRelevantChange(CDOViewInvalidationEvent event) {
		Stream<CDOObject> changedObjects = Stream.concat(event.getDetachedObjects().stream(), event.getDirtyObjects().stream());
		return changedObjects.anyMatch(this::isDirectlyContainedInRepositoryFolder);
	}
	
	private boolean isDirectlyContainedInRepositoryFolder(CDOObject o) {
		if (repositoryFolder.equals(o)) {
			return true;
		}
		CDOResource resource = o.cdoResource();
		if (resource == null) {
			return false;
		}
		CDOResourceFolder folder = resource.getFolder();
		return repositoryFolder.equals(folder);
	}
	
	@Override
	public void start() throws TaskException {
		try {
			repositoryFolder = createCDOProjectFolder();
		} catch (CommitException e) {
			throw new TaskException("Could not create folder in CDO.", e);
		}
		doStart();<commit_msg>#26 Fixed exception when deleting file in CDO repository.<commit_after>package de.cooperateproject.ui.nature.tasks;

import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewInvalidationEvent;
import org.eclipse.emf.common.util.URI;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.io.IOUtil;

import de.cooperateproject.cdo.util.connection.CDOConnectionManager;
import de.cooperateproject.ui.Activator;
import de.cooperateproject.ui.properties.ProjectPropertiesDTO;

public abstract class CDOHandlingBackgroundTask extends CooperateProjectBackgroundTask {

	private class ChangeListener implements IListener {

		@Override
		public void notifyEvent(IEvent arg0) {
			if (arg0 instanceof CDOViewInvalidationEvent) {
				CDOViewInvalidationEvent event = (CDOViewInvalidationEvent) arg0;
				if (isRelevantChange(event)) {
					handleChange(event);					
				}
			} else {
				handleChange(arg0);
			}
		}
	}

	private final ChangeListener cdoChangeListener = new ChangeListener();
	private CDOResourceFolder repositoryFolder;

	protected CDOHandlingBackgroundTask(IProject project, ProjectPropertiesDTO properties) {
		super(project, properties);
	}
	
	private boolean isRelevantChange(CDOViewInvalidationEvent event) {
		Stream<CDOObject> changedObjects = Stream.concat(event.getDetachedObjects().stream(), event.getDirtyObjects().stream());
		return changedObjects.anyMatch(this::isDirectlyContainedInRepositoryFolder);
	}
	
	private boolean isDirectlyContainedInRepositoryFolder(CDOObject o) {
		if (repositoryFolder.equals(o)) {
			return true;
		}
		
		if (o instanceof CDOResource) {
			CDOResource r = (CDOResource)o;
			URI resourceURI = r.getURI();
			return isChild(repositoryFolder.getURI(), resourceURI);
		}
		
		CDOResource resource = o.cdoResource();
		if (resource == null) {
			return false;
		}
		CDOResourceFolder folder = resource.getFolder();
		return repositoryFolder.equals(folder);
	}
	
	private static boolean isChild(URI baseURI, URI candidate) {
		if (baseURI == null || candidate == null) {
			return false;
		}
		String baseURIString = baseURI.toString();
		String commonPrefix = StringUtils.getCommonPrefix(baseURIString, candidate.toString());
		return baseURIString.equals(commonPrefix); 
	}
	
	@Override
	public void start() throws TaskException {
		try {
			repositoryFolder = createCDOProjectFolder();
		} catch (CommitException e) {
			throw new TaskException("Could not create folder in CDO.", e);
		}
		doStart();
{'fraction_non_alphanumeric': 0.10066895088921521, 'fraction_numerical': 0.0027736988089410997, 'mean_word_length': 5.4867724867724865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '23215', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1878, 'n_words': 347}