7e6bdc3b7cc07a0eda98b618b608e1f9ff516b00
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / export / ExportChartCSV.java
1 package org.simantics.charts.export;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.simantics.Simantics;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.binding.mutable.Variant;
11 import org.simantics.databoard.util.Bean;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.export.core.ExportContext;
14 import org.simantics.export.core.error.ExportException;
15 import org.simantics.export.core.intf.ExportClass;
16 import org.simantics.export.core.manager.Content;
17 import org.simantics.history.History;
18 import org.simantics.history.HistoryException;
19 import org.simantics.history.HistoryManager;
20 import org.simantics.history.ItemManager;
21 import org.simantics.history.util.subscription.SamplingFormat;
22 import org.simantics.simulation.experiment.IExperiment;
23 import org.simantics.simulation.experiment.IHistoryExperiment;
24 import org.simantics.simulation.export.CSVItemsQuery;
25 import org.simantics.simulation.export.CSVItemsQuery.CSVItem;
26 import org.simantics.simulation.export.CSVWriter;
27 import org.simantics.simulation.export.ExperimentExportClass;
28 import org.simantics.simulation.project.ExperimentManager;
29 import org.simantics.simulation.project.IExperimentManager;
30 import org.simantics.utils.datastructures.MapList;
31
32 public class ExportChartCSV extends org.simantics.simulation.export.ExperimentExportClass implements ExportClass {
33
34         
35         @Override
36         public void export(List<Content> contents, 
37                         Object writer_,
38                         ExportContext context, 
39                         Variant options,
40                         IProgressMonitor monitor, 
41                         MapList<Content, Content> attachmentMap
42                         ) throws ExportException {
43                 
44                 // Flush all experiments, just in case.
45                 IExperimentManager em = Simantics.getProject().getHint( ExperimentManager.KEY_EXPERIMENT_MANAGER );
46                 if ( em != null ) {
47                         for (IExperiment exp : em.getExperiments()) {
48                                 if ( exp instanceof IHistoryExperiment ) {
49                                         IHistoryExperiment he = (IHistoryExperiment) exp;
50                                         try {
51                                                 he.flushHistory();
52                                         } catch (HistoryException e) {
53                                         }
54                                 }
55                         }
56                 }
57                 
58                 CSVWriter writer = (CSVWriter) writer_;
59                 
60                 List<HistoryManager> historiesToClose = new ArrayList<HistoryManager>(); 
61                 try {
62                         List<org.simantics.simulation.export.ExperimentExportClass.ModelRef> models = ExperimentExportClass.getResult(context, options, true);                  
63
64                         // Label format is the following
65                         // "MODEL EXPERIMENT/RUN ITEM" 
66                         // MODEL is omited, if itemCount <= 1
67                         // RUN is omited, if runCount <= 1 
68                         int runCount = 0;
69                         int modelCount = models.size();
70                         for (ModelRef model : models) runCount += model.enabledRunCount();
71                         
72                         List<CSVItem> items = new ArrayList<CSVItem>();
73                         for (Content content : contents) {
74                                 items.addAll( context.session.syncRequest( new CSVItemsQuery( content.url ) ) );
75                         }
76
77                         // 
78                         for (ModelRef model : models) {
79                                 for (ExperimentRef experiment : model.experiments) {
80                                         if ( experiment.runs.isEmpty() ) continue;                                      
81                                         for (RunRef run : experiment.runs) {                                            
82                                                 if ( run.historyFolder == null || !run.historyFolder.exists() ) continue;
83                         
84                                                 HistoryManager history = History.openFileHistory( run.historyFolder );
85                                                 historiesToClose.add( history );
86                                         ItemManager im = new ItemManager( history.getItems() );
87                                                 
88                                                 for ( CSVItem item : items ) {
89                                                         if ( !item.modelUri.equals( model.uri ) ) continue;
90                                                         StringBuilder labelBuilder = new StringBuilder();
91                                                         labelBuilder.append( item.label );
92                                                         if ( modelCount>1 ) {
93                                                                 labelBuilder.append(" (");
94                                                                 labelBuilder.append(model.label);
95                                                                 labelBuilder.append(")");
96                                                         }
97                                                         if ( runCount>1 ) {
98                                                                 labelBuilder.append(" (");
99                                                                 labelBuilder.append( experiment.label );
100                                                                 labelBuilder.append( "\\" );
101                                                                 labelBuilder.append( run.label );
102                                                                 labelBuilder.append(")");
103                                                         }
104                                                         
105                                         List<Bean> historyItems = im.search("variableId", item.variableReference);
106                                         Collections.sort(historyItems, SamplingFormat.INTERVAL_COMPARATOR);
107                                         if (items.isEmpty()) continue;
108                                         Bean config = historyItems.get(0);
109                                         String historyId = (String) config.getFieldUnchecked("id");
110                                                                                                                 
111                                                         writer.addItem(history, historyId, labelBuilder.toString(), item.variableReference, item.unit);
112                                                 }
113                                                 
114                                         }
115                                 }
116                         }
117                                         
118                 } catch (DatabaseException e) {
119                         throw new ExportException( e );
120                 } catch (HistoryException e) {
121                         throw new ExportException( e );
122                 } catch (BindingException e) {
123                         throw new ExportException( e );
124                 } finally {
125                         for (HistoryManager history : historiesToClose) history.close();
126                 }
127                 
128         }
129
130         @Override
131         public List<String> validate(String contentUri, ExportContext context, Variant options) {
132                 return Collections.emptyList();
133         }
134         
135 }