1 package com.eyeq.pivot4j.analytics.repository;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.ObjectInputStream;
6 import java.io.ObjectOutputStream;
7 import java.io.OutputStream;
8 import java.io.Serializable;
9
10 import org.apache.commons.configuration.ConfigurationException;
11 import org.apache.commons.configuration.FileConfiguration;
12 import org.apache.commons.configuration.HierarchicalConfiguration;
13 import org.apache.commons.configuration.XMLConfiguration;
14 import org.apache.commons.lang.NullArgumentException;
15 import org.olap4j.OlapDataSource;
16
17 import com.eyeq.pivot4j.PivotModel;
18 import com.eyeq.pivot4j.analytics.datasource.ConnectionInfo;
19 import com.eyeq.pivot4j.analytics.datasource.DataSourceManager;
20 import com.eyeq.pivot4j.analytics.state.ViewState;
21 import com.eyeq.pivot4j.impl.PivotModelImpl;
22 import com.eyeq.pivot4j.ui.table.TableRenderer;
23
24 public class ReportContent implements Serializable {
25
26 private static final long serialVersionUID = 8261947657917338352L;
27
28 private transient HierarchicalConfiguration configuration;
29
30
31
32
33 public ReportContent(ViewState state) {
34 if (state == null) {
35 throw new NullArgumentException("state");
36 }
37
38 this.configuration = createConfiguration();
39
40 ConnectionInfo connectionInfo = state.getConnectionInfo();
41
42 if (connectionInfo != null) {
43 configuration.addProperty("connection", "");
44 connectionInfo.saveSettings(configuration.configurationAt(
45 "connection", true));
46 }
47
48 PivotModel model = state.getModel();
49 if (model != null) {
50 configuration.addProperty("model", "");
51 model.saveSettings(configuration.configurationAt("model", true));
52 }
53
54 if (state.getRendererState() != null) {
55 TableRenderer renderer = new TableRenderer();
56
57 renderer.restoreState(state.getRendererState());
58
59 configuration.addProperty("render", "");
60 renderer.saveSettings(configuration.configurationAt("render"));
61 }
62 }
63
64
65
66
67
68 public ReportContent(InputStream in) throws ConfigurationException {
69 if (in == null) {
70 throw new NullArgumentException("in");
71 }
72
73 FileConfiguration config = (FileConfiguration) createConfiguration();
74 config.load(in);
75
76 this.configuration = (HierarchicalConfiguration) config;
77 }
78
79
80
81
82 ReportContent() {
83 }
84
85
86
87
88
89 public void write(OutputStream out) throws ConfigurationException {
90 FileConfiguration config = (FileConfiguration) this.configuration;
91 config.save(out);
92 }
93
94
95
96
97 protected HierarchicalConfiguration createConfiguration() {
98 XMLConfiguration config = new XMLConfiguration();
99
100 config.setRootElementName("report");
101 config.setDelimiterParsingDisabled(true);
102
103 return config;
104 }
105
106
107
108
109
110
111 public ViewState read(ViewState state, DataSourceManager manager)
112 throws ConfigurationException, DataSourceNotFoundException {
113 ConnectionInfo connectionInfo = new ConnectionInfo();
114
115 try {
116 connectionInfo.restoreSettings(configuration
117 .configurationAt("connection"));
118 } catch (IllegalArgumentException e) {
119 }
120
121 state.setConnectionInfo(connectionInfo);
122
123 OlapDataSource dataSource = manager.getDataSource(connectionInfo);
124
125 if (dataSource == null) {
126 throw new DataSourceNotFoundException(connectionInfo);
127 }
128
129 PivotModel model = new PivotModelImpl(dataSource);
130
131 try {
132 model.restoreSettings(configuration.configurationAt("model"));
133 } catch (IllegalArgumentException e) {
134 }
135
136 state.setModel(model);
137
138 try {
139 TableRenderer renderer = new TableRenderer();
140 renderer.restoreSettings(configuration.configurationAt("render"));
141
142 state.setRendererState(renderer.saveState());
143 } catch (IllegalArgumentException e) {
144 }
145
146 return state;
147 }
148
149
150
151
152
153 private void readObject(ObjectInputStream in) throws IOException {
154 this.configuration = createConfiguration();
155
156 FileConfiguration fileConfig = (FileConfiguration) configuration;
157
158 try {
159 fileConfig.load(in);
160 } catch (ConfigurationException e) {
161 throw new IOException(e);
162 }
163 }
164
165
166
167
168
169 private void writeObject(ObjectOutputStream out) throws IOException {
170 FileConfiguration fileConfig = (FileConfiguration) configuration;
171
172 try {
173 fileConfig.save(out);
174 } catch (ConfigurationException e) {
175 throw new IOException(e);
176 }
177 }
178 }