blob: cd40c8f71ce723c2196edfca1671dd35703d3f2b [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.xtext.datainterchange.common;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.osbp.core.api.persistence.IPersistenceService;
import org.eclipse.osbp.datainterchange.api.IDataInterchangeExecutionEvent;
import org.eclipse.osbp.datainterchange.api.IDataInterchangeExecutionEventListener;
import org.eclipse.osbp.runtime.common.event.IEventDispatcher;
import org.eclipse.osbp.ui.api.customfields.IBlobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.UI;
public abstract class WorkerThreadRunnable implements Runnable, IDataInterchangeExecutionEventListener {
static final Logger log = LoggerFactory.getLogger(WorkerThreadRunnable.class);
private String name = null;
private String status = null;
private IEventDispatcher eventDispatcher;
private ProgressBar progressBar;
private HorizontalLayout progressBarArea;
private IPersistenceService persistenceService;
private long counter = 0 ;
private long length = 0;
private long avgElementSize = 0;
private static float previousValue = 0.0f;
private UI ui = null;
public enum Direction {IMPORT, EXPORT}
private Direction direction = Direction.IMPORT;
private IBlobService blobService;
private boolean deleteFileAfterImport=false;
private boolean failedExecution = false;
public WorkerThreadRunnable() {
progressBarArea = new HorizontalLayout();
progressBar = new ProgressBar();
progressBarArea.addComponent(progressBar);
}
public void setLength(long length) {
this.length = length;
}
public void setAvgElementSize(long avgElementSize) {
this.avgElementSize = avgElementSize;
}
public void setProgressBarStyleName(String styleName) {
progressBar.addStyleName(styleName);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
progressBar.setDescription(name);
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public IEventDispatcher getEventDispatcher() {
return eventDispatcher;
}
public void setEventDispatcher(IEventDispatcher eventDispatcher) {
this.eventDispatcher = eventDispatcher;
}
public void setBlobService(IBlobService blobService) {
this.blobService = blobService;
}
public IBlobService getBlobService() {
return blobService;
}
public IPersistenceService getPersistenceService() {
return persistenceService;
}
public void setPersistenceService(IPersistenceService persistenceService) {
this.persistenceService = persistenceService;
}
public ProgressBar getProgressBar() {
return progressBar;
}
public HorizontalLayout getProgressBarArea() {
return progressBarArea;
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public UI getUi() {
return ui;
}
public void setUi(UI ui) {
this.ui = ui;
}
public void setProgressBarEnabled(final boolean enable) {
if (ui != null) {
ui.access(() -> progressBar.setEnabled(enable));
}
}
public void setProgressIndeterminated(final boolean indeterminate) {
if (ui != null) {
ui.access(() -> progressBar.setIndeterminate(indeterminate));
}
}
@Override
public void onEvent(IDataInterchangeExecutionEvent event) {
if(ui != null) {
switch(event.getType()) {
case STARTED:
log.debug("started");
ui.access(() -> {
progressBar.setValue(0.0f);
progressBar.setEnabled(false);
});
break;
case FINISHED:
log.debug("finished");
ui.access(() -> {
progressBar.setValue(1.0f);
progressBar.setIndeterminate(true);
});
break;
case ELEMENT:
log.debug("element");
counter ++;
if (length != 0 && Math.abs(((float)counter*avgElementSize)/length-previousValue) > 0.01) {
ui.access(() -> {
log.debug("new value:"+((float)counter*avgElementSize)/length);
progressBar.setValue(((float)counter*avgElementSize)/length);
});
}
break;
}
}
}
public boolean isDeleteFileAfterImport() {
return deleteFileAfterImport;
}
public void setDeleteFileAfterImport(boolean deleteFileAfterImport) {
this.deleteFileAfterImport = deleteFileAfterImport;
}
/**
* Make sure that the given file is deleted.
*/
public void deleteFile(final Path file) {
try {
if(Files.deleteIfExists(file)){
log.debug("File: " + file + " successfuly removed.");
}
else{
log.debug("File: " + file + " doesn't exist and could not be deleted.");
}
} catch (IOException e) {
log.error("File: " + file + " could not be deleted.\n" + e.getMessage());
}
}
public boolean hasExecutionFailed() {
return failedExecution;
}
public void setExecutionFailed(boolean failedExecution) {
this.failedExecution = failedExecution;
}
}