/*******************************************************************************
* 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.io.ByteArrayOutputStream;
import java.text.DateFormat;
import java.util.Date;
import javax.activation.DataSource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.variants.IResourceVariant;
import de.innovationgate.eclipse.wgadesigner.models.WGARemoteServer;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.wgaservices.types.FSDesignResourceState;
public class WGAFSDesignResourceVariant implements IResourceVariant {
private final FSDesignResourceState _state;
private WGARemoteServer _server;
public WGAFSDesignResourceVariant(FSDesignResourceState state) {
_server = null;
_state = state;
}
public WGAFSDesignResourceVariant(WGARemoteServer server, FSDesignResourceState state) {
_server = server;
_state = state;
}
public String getName() {
return _state.getPath();
}
public boolean isContainer() {
if (_state.getType() == FSDesignResourceState.TYPE_FOLDER) {
return true;
} else {
return false;
}
}
public IStorage getStorage(IProgressMonitor monitor) {
if (_server != null) {
try {
monitor.beginTask("Retrieving content of design resource '" + _state.getPath() + "'.", 1);
_server.connectToServer();
DataSource source = _server.getServices().retrieveFSDesignResourceContent(_server.getSession(), _state);
ByteArrayOutputStream out = new ByteArrayOutputStream();
WGUtils.inToOut(source.getInputStream(), out, 1024);
monitor.worked(1);
return new WGAFSDesignResourceStorage(_state, out.toByteArray());
} catch (Exception e) {
monitor.setCanceled(true);
return null;
} finally {
monitor.done();
}
} else {
return null;
}
}
public String getContentIdentifier() {
return DateFormat.getDateTimeInstance().format(new Date(_state.getLastmodified()));
}
public byte[] asBytes() {
if (_server != null) {
try {
_server.connectToServer();
DataSource source = _server.getServices().retrieveFSDesignResourceContent(_server.getSession(), _state);
ByteArrayOutputStream out = new ByteArrayOutputStream();
WGUtils.inToOut(source.getInputStream(), out, 1024);
return out.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public FSDesignResourceState getState() {
return _state;
}
}