Package net.sf.cvschangelog.actions

Source Code of net.sf.cvschangelog.actions.ChangeLogJob

/*
* Created on May 15, 2005 by Michael Spiegel
*
*/
package net.sf.cvschangelog.actions;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import net.sf.cvschangelog.CVSChangeLogPlugin;
import net.sf.cvschangelog.model.ChangeLogMap;
import net.sf.cvschangelog.model.ChangeLogRepositoryMap;

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.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSTag;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.core.ILogEntry;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.ui.operations.RemoteLogOperation;
import org.eclipse.team.internal.ccvs.ui.operations.RemoteLogOperation.LogEntryCache;
import org.eclipse.ui.IWorkbenchPart;

/**
* @author Michael Spiegel
*/
public class ChangeLogJob extends Job {

    IProject project = null;
    RemoteLogOperation rLogOperation = null;
    LogEntryCache logEntryCache = null;   
    Vector<ILogEntry> newChangeLogEntries = null;
    IWorkbenchPart workbenchPart = null;
    ChangeLogRepositoryMap changeLogRepositoryMap = null;

    /**
     * @param name
     * @param project
     * @param workbenchPart
     * @param action
     */
    public ChangeLogJob(String name, IProject project,
            IWorkbenchPart workbenchPart) {
        super(name);
        this.project = project;
        this.workbenchPart = workbenchPart;
        newChangeLogEntries = new Vector<ILogEntry>();
    }

    private Status displayErrorMessage(Exception ex) {
        StringWriter sWriter = new StringWriter();
        PrintWriter pWriter = new PrintWriter(sWriter);
        ex.printStackTrace(pWriter);
        return new Status(IStatus.ERROR, "CVSChangeLog", IStatus.OK, sWriter
                .toString(), ex);
    }

    protected IResource[] getAllSelectedResources() throws CoreException {
        List<IResource> resources = new ArrayList<IResource>();
        project.accept(new ChangeLogResourceVisitor(resources));
        return (IResource[]) resources.toArray(new IResource[resources.size()]);
    }

    public void getLogEntryForResource(IResource resource) throws CVSException,
            TeamException {
        ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
        if (cvsResource == null) {
            return;
        }
        ICVSRemoteResource remote = CVSWorkspaceRoot
                .getRemoteResourceFor(cvsResource);
        if (remote == null) {
            return;
        }
        ResourceSyncInfo syncInfo = remote.getSyncInfo();
        syncInfo.getTag();
        ILogEntry entries[] = logEntryCache.getLogEntries(remote);
        if (entries != null) {
            newChangeLogEntries.addAll(Arrays.asList(entries));
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
     */
    protected IStatus run(IProgressMonitor monitor) {
        try {
            ICVSRemoteResource remoteProject = CVSWorkspaceRoot
                    .getRemoteResourceFor(project);
            ICVSRepositoryLocation repository = remoteProject.getRepository();
            ChangeLogMap changeLogMap = CVSChangeLogPlugin.getDefault().getChangeLogMap();
            changeLogRepositoryMap = changeLogMap.getRepository(repository);
            IResource[] resources = getAllSelectedResources();
            List<ICVSRemoteResource> remoteResources = getRemoteResources(resources);
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            logEntryCache = changeLogRepositoryMap.getCache();
            Date date1 = changeLogRepositoryMap.getCacheDate();
            Date date2 = new Date();
            CVSTag tag1 = null;
            CVSTag tag2 = null;
            if (date1 != null) {
                tag1 = new CVSTag(date1);
                tag2 = new CVSTag(date2);
            }
            ICVSRemoteResource[] remoteArray = remoteResources.toArray(new ICVSRemoteResource[remoteResources.size()]);
            if (tag1 != null && tag2 != null) {
                rLogOperation = new RemoteLogOperation(workbenchPart, remoteArray, tag1, tag2, logEntryCache);
            } else {
                rLogOperation = new RemoteLogOperation(workbenchPart, remoteArray, null, null, logEntryCache);
            }
            rLogOperation.execute(monitor);
            for (int i = 0; i < resources.length; i++) {
                this.getLogEntryForResource(resources[i]);
                if (monitor.isCanceled()) {
                    throw new OperationCanceledException();
                }
            }
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            changeLogRepositoryMap.addCVSEntries(newChangeLogEntries);
            changeLogRepositoryMap.compactLogEntries();
            changeLogRepositoryMap.setCacheDate(new Date());
        } catch (CVSException ex) {
            return (displayErrorMessage(ex));
        } catch (TeamException ex) {
            return (displayErrorMessage(ex));
        } catch (CoreException ex) {
            return (displayErrorMessage(ex));
        } catch (OperationCanceledException ex) {
            return new Status(IStatus.CANCEL, "CVSChangeLog", IStatus.OK,
                    "Cancelling CVS ChangeLog operation.", null);
        } catch (Exception ex) {
            return (displayErrorMessage(ex));
        }
        return new Status(IStatus.OK, "CVSChangeLog", IStatus.OK, "Finished",
                null);

    }

    /**
     * @param resources
     * @return
     * @throws CVSException
     */
    private List<ICVSRemoteResource> getRemoteResources(IResource[] resources) throws CVSException {
        List<ICVSRemoteResource> remoteResources = new ArrayList<ICVSRemoteResource>();
        for (int i = 0; i < resources.length; i++) {
            ICVSResource cvsResource = CVSWorkspaceRoot
                    .getCVSResourceFor(resources[i]);
            ICVSRemoteResource remote = CVSWorkspaceRoot
                    .getRemoteResourceFor(cvsResource);
            if (remote != null) {
                remoteResources.add(remote);
            }
        }
        return remoteResources;
    }

}
TOP

Related Classes of net.sf.cvschangelog.actions.ChangeLogJob

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.