-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataSetFileStorage.cs
190 lines (167 loc) · 5.62 KB
/
DataSetFileStorage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using DevExpress.Xpf.Reports.UserDesigner;
using DevExpress.Xpf.Reports.UserDesigner.Native;
using DevExpress.XtraReports.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using WpfApplication38.Storages;
namespace WpfApplication38
{
public class DataSetFileStorage : IReportStorage
{
public DataSetFileStorage()
{
}
const string fileName = "ReportStorage.xml";
StorageDataSet dataSet;
public string GetErrorMessage(Exception exception)
{
return ExceptionHelper.GetInnerErrorMessage(exception);
}
StorageDataSet.ReportStorageRow FindRow(string id)
{
DataRow[] result = ReportStorage.Select(string.Format("ID = '{0}'", id));
if (result.Length > 0)
return result[0] as StorageDataSet.ReportStorageRow;
return null;
}
public string Save(string reportID, IReportProvider reportProvider, bool saveAs, string reportTitle, IReportDesignerUI designer)
{
XtraReport report = reportProvider.GetReport();
if (reportID == null)
{
reportID = Guid.NewGuid().ToString();
saveAs = true;
}
if (!saveAs)
{
SetData(reportID, reportTitle, report);
}
else
{
if (ShowSaveAsDialog(ref reportTitle, designer))
{
SetData(reportID, reportTitle, report);
}
else return null;
}
return reportID;
}
public bool ShowSaveAsDialog(ref string recordName, IReportDesignerUI designer)
{
StorageEditorForm form = CreateForm();
form.Owner = Window.GetWindow(designer as DependencyObject);
form.listBox1.IsEnabled = true;
form.textBox1.IsEnabled = true;
bool? result = form.ShowDialog();
recordName = form.textBox1.Text;
return result.Value;
}
public string ShowSaveDialog(string filePath, string reportTitle, IReportDesignerUI designer)
{
StorageEditorForm form = CreateForm();
form.textBox1.Text = reportTitle;
form.listBox1.IsEnabled = false;
bool? result = form.ShowDialog();
if (result.HasValue && result.Value)
{
string title = form.textBox1.Text;
if (!string.IsNullOrEmpty(title))
{
return title;
}
else
{
MessageBox.Show("Incorrect report name", "Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
}
}
return string.Empty;
}
public string Open(IReportDesignerUI designer)
{
StorageEditorForm form = CreateForm();
form.Owner = Window.GetWindow(designer as DependencyObject);
form.textBox1.IsEnabled = false;
bool? result = form.ShowDialog();
if (result.HasValue && result.Value)
return (string)form.textBox1.Tag;
else return string.Empty;
}
StorageEditorForm CreateForm()
{
StorageEditorForm form = new StorageEditorForm();
form.listBox1.ItemsSource = this.ReportStorage.DefaultView;
return form;
}
public void SetData(string reportId, string title, XtraReport report)
{
StorageDataSet.ReportStorageRow row = FindRow(reportId);
if (row != null)
row.Buffer = GetBuffer(report);
else
{
row = ReportStorage.AddReportStorageRow(reportId, title, GetBuffer(report));
}
DataSet.WriteXml(StoragePath, XmlWriteMode.WriteSchema);
}
byte[] GetBuffer(XtraReport report)
{
using (MemoryStream stream = new MemoryStream())
{
report.SaveLayout(stream);
return stream.ToArray();
}
}
string StoragePath {
get {
string dirName = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
return Path.Combine(dirName, fileName);
}
}
StorageDataSet DataSet {
get {
if (dataSet == null) {
dataSet = new StorageDataSet();
if (File.Exists(StoragePath))
dataSet.ReadXml(StoragePath, XmlReadMode.ReadSchema);
}
return dataSet;
}
}
StorageDataSet.ReportStorageDataTable ReportStorage {
get {
return DataSet.ReportStorage;
}
}
public bool CanCreateNew()
{
return true;
}
public bool CanOpen()
{
return true;
}
public XtraReport CreateNew()
{
return new XtraReport1();
}
public XtraReport CreateNewSubreport()
{
return new XtraReport1();
}
public XtraReport Load(string reportID, IReportSerializer designerReportSerializer)
{
StorageDataSet.ReportStorageRow row = FindRow(reportID);
using (MemoryStream ms = new MemoryStream(row.Buffer))
{
return XtraReport.FromStream(ms, true);
}
}
}
}