/*******************************************************************************
- * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Copyright (c) 2012,2017 Association for Decentralized Information Management in
* Industry THTH ry.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
-import java.util.List;
import java.util.Set;
import org.eclipse.core.commands.ExecutionException;
import org.simantics.databoard.serialization.SerializationException;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
-import org.simantics.db.Session;
-import org.simantics.db.common.NamedResource;
import org.simantics.db.common.request.UniqueRead;
import org.simantics.db.common.utils.Logger;
import org.simantics.db.exception.DatabaseException;
}
void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
-
try {
doExport(mon, exportModel.exportLocation, exportModel);
} catch (ExecutionException e) {
} finally {
mon.setWorkRemaining(0);
}
-
}
-
- private static Set<Resource> resolveContainingModels(final Collection<NamedResource> res) throws DatabaseException {
+
+ private static Set<Resource> resolveContainingModels(Collection<Resource> res) throws DatabaseException {
return Simantics.getSession().syncRequest(new UniqueRead<Set<Resource>>() {
@Override
public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
- Set<Resource> models = new HashSet<Resource>();
- for (NamedResource r : res) {
- Resource m = graph.syncRequest(new PossibleModel(r.getResource()));
+ Set<Resource> models = new HashSet<>();
+ for (Resource r : res) {
+ Resource m = graph.syncRequest(new PossibleModel(r));
if (m != null)
models.add(m);
}
}
});
}
-
+
public static void doExport(IProgressMonitor monitor, final File f, final CSVExportPlan plan) throws ExecutionException, IOException {
-
IScopeContext context = InstanceScope.INSTANCE;
Preferences node = context.getNode(CSVPreferences.P_NODE);
Set<Resource> models;
try {
- models = resolveContainingModels(plan.models);
+ models = resolveContainingModels(plan.items);
} catch (DatabaseException e3) {
throw new ExecutionException("Containing model resolution failed.", e3);
}
throw new ExecutionException("Selected resources are part of several models, only subscriptions from a single model can be selected");
Resource model = models.iterator().next();
Key chartDataKey = ChartKeys.chartSourceKey(model);
-
+
final ChartData data = Simantics.getProject().getHint(chartDataKey);
if ( data == null ) {
throw new ExecutionException("There is no "+chartDataKey);
csv.setFloatFormat( FormattingUtils.significantDigitFormat( plan.floatDigits ) );
csv.setNumberFormat( FormattingUtils.significantDigitFormat( plan.doubleDigits ) );
- try {
- Session session = Simantics.getSession();
- List<Resource> list = new ArrayList<Resource>();
- for(NamedResource nr : plan.models) list.add(nr.getResource());
- session.sync( new CSVParamsQuery(data.history, csv, list) );
- csv.sort();
- } catch (DatabaseException e2) {
- throw new ExecutionException(e2.getMessage(), e2);
- } catch (HistoryException e) {
- throw new ExecutionException(e.getMessage(), e);
- }
-
- try {
- // Ensure all views are built.
- monitor.beginTask("Exporting Time Series as CSV...", IProgressMonitor.UNKNOWN);
- try {
- data.collector.flush();
- if ( !f.exists() ) {
- f.createNewFile();
- } else {
- RandomAccessFile raf = new RandomAccessFile(f, "rw");
- raf.setLength(0);
- raf.close();
- }
-
- FileOutputStream fos = new FileOutputStream(f, true);
- BufferedOutputStream bos = new BufferedOutputStream( fos );
- try {
- PrintStream ps = new PrintStream( bos );
- csv.formulate2( new CSVProgressMonitor( monitor ), ps );
- bos.flush();
- } finally {
- fos.close();
- }
- } catch (HistoryException e) {
- throw new ExecutionException(e.getMessage(), e);
- } catch (IOException e1) {
- throw new ExecutionException(e1.getMessage(), e1);
- }
- monitor.setTaskName("Done");
- } finally {
- monitor.done();
- }
-
+ try {
+ Simantics.getSession().syncRequest(
+ new CSVParamsQuery(data.history, csv,
+ new ArrayList<>(plan.items)) );
+ csv.sort();
+
+ // Ensure all views are built.
+ monitor.beginTask("Exporting Time Series as CSV...", IProgressMonitor.UNKNOWN);
+ data.collector.flush();
+
+ // Truncate existing file it if happens to exist.
+ try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
+ raf.setLength(0);
+ }
+
+ // Write CSV
+ try (PrintStream ps = new PrintStream(
+ new BufferedOutputStream(
+ new FileOutputStream(f, true) )))
+ {
+ csv.formulate2( new CSVProgressMonitor( monitor ), ps );
+ ps.flush();
+ }
+
+ monitor.setTaskName("Done");
+ } catch (DatabaseException e2) {
+ throw new ExecutionException(e2.getMessage(), e2);
+ } catch (HistoryException e) {
+ throw new ExecutionException(e.getMessage(), e);
+ } finally {
+ monitor.done();
+ }
}
-
+
}