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