/
DataSourceAdapter.java
308 lines (238 loc) · 8.94 KB
/
DataSourceAdapter.java
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package org.eclipse.birt.report.model.adapter.oda.impl;
import org.eclipse.birt.report.model.api.CommandStack;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.util.PropertyValueValidationUtil;
import org.eclipse.datatools.connectivity.oda.design.DataSourceDesign;
import org.eclipse.datatools.connectivity.oda.design.Properties;
import org.eclipse.datatools.connectivity.oda.design.Property;
import org.eclipse.datatools.connectivity.oda.design.util.DesignUtil;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
/*******************************************************************************
* Copyright (c) 2004 Actuate Corporation. All rights reserved. This program and
* the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Actuate Corporation - initial API and implementation
*******************************************************************************/
public class DataSourceAdapter extends AbstractDataAdapter
{
private final boolean updateName;
/**
*
*/
DataSourceAdapter( )
{
this( true );
}
DataSourceAdapter( boolean updateName )
{
super( );
this.updateName = updateName;
}
/**
* @param sourceHandle
* @return
*/
public DataSourceDesign createDataSourceDesign(
OdaDataSourceHandle sourceHandle )
{
if ( sourceHandle == null )
return null;
DataSourceDesign sourceDesign = designFactory.createDataSourceDesign( );
updateDataSourceDesign( sourceHandle, sourceDesign );
return sourceDesign;
}
/**
* @param sourceHandle
* @param sourceDesign
*/
public void updateDataSourceDesign( OdaDataSourceHandle sourceHandle,
DataSourceDesign sourceDesign )
{
// properties on ReportElement, like name, displayNames, etc.
sourceDesign.setName( sourceHandle.getName( ) );
String displayName = sourceHandle.getDisplayName( );
String displayNameKey = sourceHandle.getDisplayNameKey( );
if ( displayName != null || displayNameKey != null )
{
sourceDesign.setDisplayName( displayName );
sourceDesign.setDisplayNameKey( displayNameKey );
}
// properties such as comments, extends, etc are kept in
// DataSourceHandle, not DataSourceDesign.
// scripts of DataSource are kept in
// DataSourceHandle, not DataSourceDesign.
sourceDesign.setOdaExtensionId( sourceHandle.getExtensionID( ) );
// override the private and public properties in oda design by those in
// element handle
sourceDesign
.setPrivateProperties( newOdaPrivateProperties( sourceHandle
.privateDriverPropertiesIterator( ) ) );
sourceDesign.setPublicProperties( newOdaPublicProperties(
sourceHandle.getExtensionPropertyDefinitionList( ),
sourceHandle ) );
updateODAMessageFile( sourceDesign, sourceHandle.getModuleHandle( ) );
}
public OdaDataSourceHandle createDataSourceHandle(
DataSourceDesign sourceDesign, ModuleHandle module )
throws SemanticException, IllegalStateException
{
if ( sourceDesign == null )
return null;
// validate the source design to make sure it is valid
DesignUtil.validateObject( sourceDesign );
OdaDataSourceHandle sourceHandle = module.getElementFactory( )
.newOdaDataSource( sourceDesign.getName( ),
sourceDesign.getOdaExtensionId( ) );
if ( sourceHandle == null )
return null;
adaptDataSourceDesign( sourceDesign, sourceHandle );
return sourceHandle;
}
public void updateDataSourceHandle( DataSourceDesign sourceDesign,
OdaDataSourceHandle sourceHandle ) throws SemanticException
{
if ( sourceDesign == null || sourceHandle == null )
return;
DesignUtil.validateObject( sourceDesign );
CommandStack stack = sourceHandle.getModuleHandle( ).getCommandStack( );
stack.startTrans( null );
try
{
// extension id is set without undo/redo support.
sourceHandle.setProperty( OdaDataSourceHandle.EXTENSION_ID_PROP,
sourceDesign.getOdaExtensionId( ) );
if ( updateName )
sourceHandle.setName( sourceDesign.getName( ) );
sourceHandle.setDisplayName( sourceDesign.getDisplayName( ) );
sourceHandle.setDisplayNameKey( sourceDesign.getDisplayNameKey( ) );
// set public properties.
Properties props = sourceDesign.getPublicProperties( );
if ( props != null )
{
EList propList = props.getProperties( );
for ( int i = 0; i < propList.size( ); i++ )
{
Property prop = (Property) propList.get( i );
sourceHandle
.setProperty( prop.getName( ), prop.getValue( ) );
}
}
// updateROMPropertyBindings( props, sourceHandle );
// set private properties.
props = sourceDesign.getPrivateProperties( );
if ( props != null )
{
EList propList = props.getProperties( );
for ( int i = 0; i < propList.size( ); i++ )
{
Property prop = (Property) propList.get( i );
sourceHandle.setPrivateDriverProperty( prop.getName( ),
prop.getValue( ) );
}
}
updateROMMessageFile( sourceDesign, sourceHandle.getModuleHandle( ) );
}
catch ( SemanticException e )
{
stack.rollback( );
throw e;
}
stack.commit( );
}
public boolean isEqualDataSourceDesign( DataSourceDesign designFromHandle,
DataSourceDesign design )
{
if ( designFromHandle == null && design == null )
return true;
if ( designFromHandle != null && design == null )
return false;
if ( designFromHandle == null && design != null )
return false;
assert designFromHandle != null;
// both must be not null
Properties handleProps = designFromHandle.getPublicProperties( );
Properties props = design.getPublicProperties( );
if ( handleProps == null && props == null )
return true;
if ( handleProps != null && props == null )
return false;
if ( handleProps == null && props != null )
return false;
assert handleProps != null;
assert props != null;
EList publicProps = handleProps.getProperties( );
for ( int i = 0; i < publicProps.size( ); i++ )
{
Property prop = (Property) publicProps.get( i );
String propValue = prop.getValue( );
String propName = prop.getName( );
if ( propValue == null )
{
String value = props.getProperty( propName );
if ( value != null && value.trim( ).equals( "" ) ) //$NON-NLS-1$
prop.setNameValue( prop.getName( ), "" ); //$NON-NLS-1$
}
}
DataSourceDesign tmpDesign = (DataSourceDesign) EcoreUtil.copy( design );
tmpDesign.setHostResourceIdentifiers( null );
return new EcoreUtil.EqualityHelper( ).equals( designFromHandle,
tmpDesign );
}
/**
* Copies values of <code>sourceDesign</code> to <code>sourceHandle</code>.
* Values in <code>sourceDesign</code> are validated before maps to values
* in OdaDataSourceHandle.
*
* @param sourceDesign
* the ODA data source design
* @param sourceHandle
* the Model handle
* @throws SemanticException
* if any value is invalid.
*
*/
private void adaptDataSourceDesign( DataSourceDesign sourceDesign,
OdaDataSourceHandle sourceHandle ) throws SemanticException
{
Object value = null;
// properties on ReportElement, like name, displayNames, etc.
value = sourceDesign.getName( );
PropertyValueValidationUtil.validateProperty( sourceHandle,
OdaDataSourceHandle.NAME_PROP, value );
sourceHandle.getElement( ).setName( sourceDesign.getName( ) );
// properties on ReportElement, like name, displayNames, etc.
value = sourceDesign.getDisplayName( );
PropertyValueValidationUtil.validateProperty( sourceHandle,
OdaDataSourceHandle.DISPLAY_NAME_PROP, value );
sourceHandle.getElement( ).setProperty(
OdaDataSourceHandle.DISPLAY_NAME_PROP,
sourceDesign.getDisplayName( ) );
value = sourceDesign.getDisplayNameKey( );
PropertyValueValidationUtil.validateProperty( sourceHandle,
OdaDataSourceHandle.DISPLAY_NAME_ID_PROP, value );
sourceHandle.getElement( ).setProperty(
OdaDataSourceHandle.DISPLAY_NAME_ID_PROP,
sourceDesign.getDisplayNameKey( ) );
// properties such as comments, extends, etc are kept in
// DataSourceHandle, not DataSourceDesign.
// scripts of DataSource are kept in
// DataSourceHandle, not DataSourceDesign.
// set null or empty list if the return list is empty.
value = newROMPrivateProperties( sourceDesign.getPrivateProperties( ) );
PropertyValueValidationUtil.validateProperty( sourceHandle,
OdaDataSourceHandle.PRIVATE_DRIVER_PROPERTIES_PROP, value );
sourceHandle.getElement( ).setProperty(
OdaDataSourceHandle.PRIVATE_DRIVER_PROPERTIES_PROP, value );
updateROMPublicProperties( sourceDesign.getPublicProperties( ),
sourceHandle );
// udpate property bindings, report parameters and so on.
// updateROMPropertyBindings( sourceDesign.getPublicProperties( ),
// sourceHandle );
updateROMMessageFile( sourceDesign, sourceHandle.getModuleHandle( ) );
}
}