/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.team;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.team.ui.synchronize.SubscriberParticipant;
import org.eclipse.team.ui.synchronize.SynchronizeModelOperation;
import de.innovationgate.eclipse.wgadesigner.models.WGARemoteServer;
import de.innovationgate.wgaservices.types.RemoteSession;
public class CommitOperation extends SynchronizeModelOperation {
private ISynchronizePageConfiguration _configuration;
protected CommitOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
super(configuration, elements);
_configuration = configuration;
}
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
RemoteSession session = null;
SyncInfo infos[] = getSyncInfoSet().getSyncInfos();
try {
monitor.beginTask("Committing resource changes", -1);
List<IResource> committedResources = new ArrayList<IResource>();
for (SyncInfo info : infos) {
if (info instanceof WGAFSDesignResourceSyncInfo) {
WGAFSDesignResourceSyncInfo wgaSyncInfo = (WGAFSDesignResourceSyncInfo) info;
try {
if (session == null) {
wgaSyncInfo.getServer().connectToServer();
session = wgaSyncInfo.getServer().getSession();
}
int change = SyncInfo.getChange(wgaSyncInfo.getKind());
IResource resource = wgaSyncInfo.getLocal();
String remotePath = wgaSyncInfo.getRemotePath();
if (remotePath != null) {
if (change == SyncInfo.DELETION) {
monitor.setTaskName("deleting '" + remotePath + "'.");
wgaSyncInfo.getServer().getServices().deleteFSDesignResource(session, remotePath);
committedResources.add(resource);
} else if ((change == SyncInfo.ADDITION || change == SyncInfo.CHANGE)) {
if (change == SyncInfo.ADDITION) {
monitor.setTaskName("adding '" + remotePath + "'.");
} else {
monitor.setTaskName("updating '" + remotePath + "'.");
}
if (resource.getType() == IResource.FILE) {
DataSource content = new FileDataSource(((IFile)resource).getLocation().toFile());
wgaSyncInfo.getServer().getServices().updateFSDesignResource(session, remotePath, content, resource.getLocalTimeStamp());
committedResources.add(resource);
} else if (resource.getType() == IResource.FOLDER) {
wgaSyncInfo.getServer().getServices().mkFSDesignDir(session, remotePath);
committedResources.add(resource);
}
}
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
} else if (info instanceof WGAPluginResourceSyncInfo) {
try {
WGAPluginResourceSyncInfo wgaSyncInfo = (WGAPluginResourceSyncInfo) info;
if ((wgaSyncInfo.getKind() & SyncInfo.CONFLICTING) != SyncInfo.CONFLICTING) {
int change = SyncInfo.getChange(wgaSyncInfo.getKind());
if (change == SyncInfo.DELETION) {
WGARemoteServer remote = wgaSyncInfo.getServer();
remote.getServices().deactivatePlugin(remote.getSession(), ((WGAPluginResourceVariant)wgaSyncInfo.getRemote()).getPluginInfo());
committedResources.add(wgaSyncInfo.getLocal());
} else if (change == SyncInfo.ADDITION) {
WGARemoteServer local = wgaSyncInfo.getRuntime().createRemoteServer();
local.connectToServer();
DataSource plugin = local.getServices().downloadPlugin(local.getSession(), wgaSyncInfo.getLocalPluginInfo());
WGARemoteServer remote = wgaSyncInfo.getServer();
remote.getServices().installPlugins(remote.getSession(), Collections.singletonList(plugin));
remote.getServices().activatePlugin(remote.getSession(), wgaSyncInfo.getLocalPluginInfo());
committedResources.add(wgaSyncInfo.getLocal());
}
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
}
if (_configuration.getParticipant() instanceof SubscriberParticipant) {
Subscriber subscriber = ((SubscriberParticipant)_configuration.getParticipant()).getSubscriber();
try {
subscriber.refresh(committedResources.toArray(new IResource[0]), IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, -1));
} catch (TeamException e) {
throw new InvocationTargetException(e);
}
}
} finally {
monitor.done();
}
}
@Override
protected boolean canRunAsJob() {
return false;
}
@Override
protected boolean shouldRun() {
return MessageDialog.openConfirm(getShell(), "Perform commit?", "Commit the selected resources to the server?");
}
}