~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-datamart/src/main/webapp/dhis-web-datamart/javascript/datamart.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// -----------------------------------------------------------------------------
 
3
// Data retrieval methods
 
4
// -----------------------------------------------------------------------------
 
5
 
 
6
function getDataElements()
 
7
{
 
8
        var dataElementGroupList = document.getElementById( "dataElementGroupId" );
 
9
        var dataElementGroupId = dataElementGroupList.options[ dataElementGroupList.selectedIndex ].value;
 
10
        
 
11
        if ( dataElementGroupId != null )
 
12
        {
 
13
                var url = "../dhis-web-commons-ajax/getDataElements.action?id=" + dataElementGroupId + "&aggregate=true";
 
14
                                
 
15
                var request = new Request();
 
16
            request.setResponseTypeXML( 'dataElement' );
 
17
            request.setCallbackSuccess( getDataElementsReceived );
 
18
            request.send( url );
 
19
        }
 
20
}
 
21
 
 
22
function getIndicators()
 
23
{
 
24
        var indicatorGroupList = document.getElementById( "indicatorGroupId" );
 
25
        var indicatorGroupId = indicatorGroupList.options[ indicatorGroupList.selectedIndex ].value;
 
26
        
 
27
        if ( indicatorGroupId != null )
 
28
        {
 
29
                var url = "../dhis-web-commons-ajax/getIndicators.action?id=" + indicatorGroupId;
 
30
                
 
31
                var request = new Request();
 
32
            request.setResponseTypeXML( 'indicator' );
 
33
            request.setCallbackSuccess( getIndicatorsReceived );
 
34
            request.send( url );            
 
35
        }
 
36
}
 
37
 
 
38
function getOrganisationUnits()
 
39
{
 
40
        var organisationUnitLevelList = document.getElementById( "organisationUnitLevel" );
 
41
        var organisationUnitLevel = organisationUnitLevelList.options[ organisationUnitLevelList.selectedIndex ].value;
 
42
        
 
43
        if ( organisationUnitLevel != null )
 
44
        {
 
45
                var url = "../dhis-web-commons-ajax/getOrganisationUnits.action?level=" + organisationUnitLevel;
 
46
                
 
47
                var request = new Request();
 
48
            request.setResponseTypeXML( 'organisationUnit' );
 
49
            request.setCallbackSuccess( getOrganisationUnitsReceived );
 
50
            request.send( url );            
 
51
        }
 
52
}
 
53
 
 
54
function getOrganisationUnitChildren()
 
55
{
 
56
        var organisationUnitList = document.getElementById( "availableOrganisationUnits" );
 
57
        var organisationUnitId = organisationUnitList.options[ organisationUnitList.selectedIndex ].value;
 
58
        
 
59
        if ( organisationUnitId != null )
 
60
        {
 
61
                var url = "../dhis-web-commons-ajax/getOrganisationUnitChildren.action?id=" + organisationUnitId;
 
62
                
 
63
                var request = new Request();
 
64
            request.setResponseTypeXML( 'organisationUnit' );
 
65
            request.setCallbackSuccess( getOrganisationUnitChildrenReceived );
 
66
            request.send( url );        
 
67
        }
 
68
}
 
69
 
 
70
function getPeriods()
 
71
{
 
72
        var periodTypeList = document.getElementById( "periodTypeId" );
 
73
        var periodTypeId = periodTypeList.options[ periodTypeList.selectedIndex ].value;
 
74
        
 
75
        if ( periodTypeId != null )
 
76
        {               
 
77
                var url = "../dhis-web-commons-ajax/getPeriods.action?name=" + periodTypeId;
 
78
                
 
79
                var request = new Request();
 
80
            request.setResponseTypeXML( 'period' );
 
81
            request.setCallbackSuccess( getPeriodsReceived );
 
82
            request.send( url );
 
83
        }
 
84
}
 
85
 
 
86
function getDataElementsReceived( xmlObject )
 
87
{       
 
88
        var availableDataElements = document.getElementById( "availableDataElements" );
 
89
        var selectedDataElements = document.getElementById( "selectedDataElements" );
 
90
        
 
91
        clearList( availableDataElements );
 
92
        
 
93
        var dataElements = xmlObject.getElementsByTagName( "dataElement" );
 
94
        
 
95
        for ( var i = 0; i < dataElements.length; i++ )
 
96
        {
 
97
                var id = dataElements[ i ].getElementsByTagName( "id" )[0].firstChild.nodeValue;
 
98
                var dataElementName = dataElements[ i ].getElementsByTagName( "name" )[0].firstChild.nodeValue;
 
99
                
 
100
                if ( listContains( selectedDataElements, id ) == false )
 
101
                {                               
 
102
                        var option = document.createElement( "option" );
 
103
                        option.value = id;
 
104
                        option.text = dataElementName;
 
105
                        availableDataElements.add( option, null );
 
106
                }
 
107
        }
 
108
}
 
109
 
 
110
function getIndicatorsReceived( xmlObject )
 
111
{       
 
112
        var availableIndicators = document.getElementById( "availableIndicators" );
 
113
        var selectedIndicators = document.getElementById( "selectedIndicators" );
 
114
        
 
115
        clearList( availableIndicators );
 
116
        
 
117
        var indicators = xmlObject.getElementsByTagName( "indicator" );
 
118
        
 
119
        for ( var i = 0; i < indicators.length; i++ )
 
120
        {
 
121
                var id = indicators[ i ].getElementsByTagName( "id" )[0].firstChild.nodeValue;
 
122
                var indicatorName = indicators[ i ].getElementsByTagName( "name" )[0].firstChild.nodeValue;
 
123
                
 
124
                if ( listContains( selectedIndicators, id ) == false )
 
125
                {                               
 
126
                        var option = document.createElement( "option" );
 
127
                        option.value = id;
 
128
                        option.text = indicatorName;
 
129
                        availableIndicators.add( option, null );
 
130
                }
 
131
        }
 
132
}
 
133
 
 
134
function getOrganisationUnitsReceived( xmlObject )
 
135
{       
 
136
        var availableOrganisationUnits = document.getElementById( "availableOrganisationUnits" );
 
137
        var selectedOrganisationUnits = document.getElementById( "selectedOrganisationUnits" );
 
138
        
 
139
        clearList( availableOrganisationUnits );
 
140
        
 
141
        var organisationUnits = xmlObject.getElementsByTagName( "organisationUnit" );
 
142
        
 
143
        for ( var i = 0; i < organisationUnits.length; i++ )
 
144
        {
 
145
                var id = organisationUnits[ i ].getElementsByTagName( "id" )[0].firstChild.nodeValue;
 
146
                var organisationUnitName = organisationUnits[ i ].getElementsByTagName( "name" )[0].firstChild.nodeValue;
 
147
                
 
148
                if ( listContains( selectedOrganisationUnits, id ) == false )
 
149
                {                                               
 
150
                        var option = document.createElement( "option" );
 
151
                        option.value = id;
 
152
                        option.text = organisationUnitName;
 
153
                        availableOrganisationUnits.add( option, null );
 
154
                }
 
155
        }
 
156
}
 
157
 
 
158
function getOrganisationUnitChildrenReceived( xmlObject )
 
159
{
 
160
        var selectedOrganisationUnits = document.getElementById( "selectedOrganisationUnits" );
 
161
        
 
162
        var organisationUnits = xmlObject.getElementsByTagName( "organisationUnit" );
 
163
        
 
164
        for ( var i = 0; i < organisationUnits.length; i++ )
 
165
        {
 
166
                var id = organisationUnits[ i ].getElementsByTagName( "id" )[0].firstChild.nodeValue;
 
167
                
 
168
                var organisationUnitName = organisationUnits[ i ].getElementsByTagName( "name" )[0].firstChild.nodeValue;
 
169
                
 
170
                if ( listContains( selectedOrganisationUnits, id ) == false )
 
171
                {
 
172
                        var option = document.createElement( "option" );
 
173
                        option.value = id;
 
174
                        option.text = organisationUnitName;
 
175
                        selectedOrganisationUnits.add( option, null );
 
176
                }
 
177
        }
 
178
}
 
179
 
 
180
function getPeriodsReceived( xmlObject )
 
181
{       
 
182
        var availablePeriods = document.getElementById( "availablePeriods" );
 
183
        var selectedPeriods = document.getElementById( "selectedPeriods" );
 
184
        
 
185
        clearList( availablePeriods );
 
186
        
 
187
        var periods = xmlObject.getElementsByTagName( "period" );
 
188
        
 
189
        for ( var i = 0; i < periods.length; i++ )
 
190
        {
 
191
                var id = periods[ i ].getElementsByTagName( "id" )[0].firstChild.nodeValue;
 
192
                var periodName = periods[ i ].getElementsByTagName( "name" )[0].firstChild.nodeValue;
 
193
                
 
194
                if ( listContains( selectedPeriods, id ) == false )
 
195
                {                                               
 
196
                        var option = document.createElement( "option" );
 
197
                        option.value = id;
 
198
                        option.text = periodName;
 
199
                        availablePeriods.add( option, null );
 
200
                }                       
 
201
        }
 
202
}
 
203
 
 
204
// -----------------------------------------------------------------------------
 
205
// DataMartExport details
 
206
// -----------------------------------------------------------------------------
 
207
 
 
208
function showDataMartExportDetails( id )
 
209
{
 
210
    var request = new Request();
 
211
    request.setResponseTypeXML( 'dataMartExport' );
 
212
    request.setCallbackSuccess( dataMartExportReceived );
 
213
    request.send( 'getDataMartExport.action?id=' + id );
 
214
}
 
215
 
 
216
function dataMartExportReceived( xmlObject )
 
217
{
 
218
    setFieldValue( "nameField", getElementValue( xmlObject, "name" ) );
 
219
    setFieldValue( "dataElementField", getElementValue( xmlObject, "dataElements" ) );
 
220
    setFieldValue( "indicatorField", getElementValue( xmlObject, "indicators" ) );
 
221
    setFieldValue( "organisationUnitField", getElementValue( xmlObject, "organisationUnits" ) );
 
222
    setFieldValue( "periodField", getElementValue( xmlObject, "periods" ) );
 
223
    
 
224
    showDetails();
 
225
}
 
226
 
 
227
// -----------------------------------------------------------------------------
 
228
// DatmartExport
 
229
// -----------------------------------------------------------------------------
 
230
 
 
231
function saveExport()
 
232
{
 
233
    if ( validateCollections() )
 
234
    {
 
235
        var exportId = document.getElementById( "exportId" ).value;
 
236
        var exportName = document.getElementById( "exportName" ).value;
 
237
        
 
238
        var url = "validateDataMartExport.action?id=" + exportId + "&name=" + exportName;
 
239
        
 
240
        var request = new Request();
 
241
        request.setResponseTypeXML( 'message' );
 
242
        request.setCallbackSuccess( saveExportReceived );
 
243
        request.send( url );
 
244
    }
 
245
}
 
246
 
 
247
function saveExportReceived( messageElement )
 
248
{
 
249
    var type = messageElement.getAttribute( 'type' );
 
250
    var message = messageElement.firstChild.nodeValue;
 
251
    
 
252
    if ( type == "input" )
 
253
    {
 
254
        setMessage( message );
 
255
    }
 
256
    else if ( type == "success" )
 
257
    {
 
258
        var params = getExportParams();
 
259
 
 
260
        var url = "saveDataMartExport.action";
 
261
            
 
262
        var request = new Request();
 
263
        request.sendAsPost( params );
 
264
        request.setCallbackSuccess( saveReceived );    
 
265
        request.send( url );
 
266
    }
 
267
}
 
268
 
 
269
function saveReceived( messageElement )
 
270
{
 
271
    setMessage( i18n_export_saved );
 
272
}
 
273
 
 
274
// -----------------------------------------------------------------------------
 
275
// Export
 
276
// -----------------------------------------------------------------------------
 
277
 
 
278
function exportValues()
 
279
{
 
280
    if ( validateCollections() )
 
281
    {
 
282
        var params = getExportParams();
 
283
        
 
284
        var url = "export.action";
 
285
                
 
286
        var request = new Request();
 
287
        request.sendAsPost( params );
 
288
        request.setCallbackSuccess( exportReceived );    
 
289
        request.send( url );
 
290
    }
 
291
}
 
292
 
 
293
function exportReceived( messageElement )
 
294
{
 
295
        waitAndGetExportStatus( 500 );
 
296
}
 
297
 
 
298
function getExportStatus()
 
299
{
 
300
    var url = "getExportStatus.action";
 
301
        
 
302
        var request = new Request();
 
303
    request.setResponseTypeXML( 'status' );
 
304
    request.setCallbackSuccess( exportStatusReceived );
 
305
    request.send( url );
 
306
}
 
307
 
 
308
function exportStatusReceived( xmlObject )
 
309
{
 
310
    var message = getElementValue( xmlObject, "message" );
 
311
    var running = getElementValue( xmlObject, "running" );
 
312
    
 
313
    setMessage( message );
 
314
    
 
315
    if ( running == "true" )
 
316
    {
 
317
           waitAndGetExportStatus( 2000 );
 
318
    }
 
319
}
 
320
 
 
321
function waitAndGetExportStatus( millis )
 
322
{
 
323
        setTimeout( "getExportStatus();", millis );
 
324
}
 
325
 
 
326
function cancelExport()
 
327
{
 
328
        var url = "cancelExport.action";
 
329
                
 
330
        var request = new Request();  
 
331
    request.send( url );
 
332
}
 
333
 
 
334
function removeDatamartExport( exportId, exportName )
 
335
{
 
336
    var result = window.confirm( i18n_confirm_delete + '\n\n' + exportName );
 
337
    
 
338
    if ( result )
 
339
    {
 
340
        window.location.href = 'removeDataMartExport.action?id=' + exportId;
 
341
    }
 
342
}
 
343
 
 
344
function getExportParams()
 
345
{
 
346
    var exportId = document.getElementById( "exportId" ).value;
 
347
    var exportName = document.getElementById( "exportName" ).value;
 
348
    var dataElements = document.getElementById( "selectedDataElements" );
 
349
    var indicators = document.getElementById( "selectedIndicators" );
 
350
    var organisationUnits = document.getElementById( "selectedOrganisationUnits" );
 
351
    var periods = document.getElementById( "selectedPeriods" );
 
352
    
 
353
    var params = "id=" + exportId + "&name=" + exportName;
 
354
    
 
355
    for ( var i = 0; i < dataElements.options.length; i++ )
 
356
    {
 
357
        params += "&selectedDataElements=" + dataElements.options[i].value;
 
358
    }
 
359
    
 
360
    for ( var i = 0; i < indicators.options.length; i++ )
 
361
    {
 
362
        params += "&selectedIndicators=" + indicators.options[i].value;
 
363
    }
 
364
        
 
365
    for ( var i = 0; i < organisationUnits.options.length; i++ )
 
366
    {
 
367
        params += "&selectedOrganisationUnits=" + organisationUnits.options[i].value;
 
368
    }
 
369
    
 
370
    for ( var i = 0; i < periods.options.length; i++ )
 
371
    {
 
372
        params += "&selectedPeriods=" + periods.options[i].value;
 
373
    }
 
374
    
 
375
    return params;
 
376
}
 
377
 
 
378
// -----------------------------------------------------------------------------
 
379
// Validation
 
380
// -----------------------------------------------------------------------------
 
381
 
 
382
function validateCollections()
 
383
{
 
384
    if ( !hasElements( "selectedDataElements" ) && !hasElements( "selectedIndicators" ) )
 
385
    {
 
386
        setMessage( i18n_must_select_at_least_one_dataelement_or_indicator );
 
387
        
 
388
        return false;
 
389
    }
 
390
    
 
391
    if ( !hasElements( "selectedOrganisationUnits" ) )
 
392
    {
 
393
        setMessage( i18n_must_select_at_least_one_organisation_unit );
 
394
        
 
395
        return false;
 
396
    }
 
397
    
 
398
    if ( !hasElements( "selectedPeriods" ) )
 
399
    {
 
400
        setMessage( i18n_must_select_at_least_one_period );
 
401
        
 
402
        return false;
 
403
    }
 
404
    
 
405
    return true;
 
406
}
 
407
 
 
408
// -----------------------------------------------------------------------------
 
409
// Dashboard
 
410
// -----------------------------------------------------------------------------
 
411
 
 
412
function addToDashboard( id )
 
413
{
 
414
    var dialog = window.confirm( i18n_confirm_add_to_dashboard );
 
415
    
 
416
    if ( dialog )
 
417
    {
 
418
        var request = new Request(); 
 
419
        request.send( "addDataMartExportToDashboard.action?id=" + id );
 
420
    }
 
421
}