1
package org.hisp.dhis.reporting.dataset.report;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
import java.io.FileInputStream;
32
import java.io.FileNotFoundException;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStreamReader;
36
import java.io.OutputStreamWriter;
37
import java.io.Reader;
38
import java.io.Writer;
39
import java.util.ArrayList;
40
import java.util.Collection;
41
import java.util.Iterator;
42
import java.util.List;
44
import org.hisp.dhis.external.location.LocationManager;
45
import org.hisp.dhis.external.location.LocationManagerException;
46
import org.hisp.dhis.reporting.dataset.dataaccess.ReportDataAccess;
47
import org.hisp.dhis.reporting.dataset.dataaccess.ReportDataAccessException;
48
import org.hisp.dhis.reporting.dataset.utils.FileUtils;
49
import org.hisp.dhis.reporting.dataset.utils.JRXmlFilter;
50
import org.hisp.dhis.reporting.dataset.utils.XMLUtils;
51
import org.hisp.dhis.reporting.dataset.utils.XmlFilter;
53
import static org.hisp.dhis.reporting.dataset.utils.FileUtils.getXMLFileName;
54
import static org.hisp.dhis.reporting.dataset.utils.FileUtils.getJRXMLFileName;
55
import static org.hisp.dhis.reporting.dataset.utils.FileUtils.XML_DIR;
56
import static org.hisp.dhis.reporting.dataset.utils.FileUtils.JRXML_DIR;
58
import com.thoughtworks.xstream.XStream;
61
* @author Lars Helge Overland
62
* @version $Id: XmlReportStore.java 5282 2008-05-28 10:41:06Z larshelg $
64
public class XmlReportStore
65
implements ReportStore
67
// -------------------------------------------------------------------------
69
// -------------------------------------------------------------------------
71
private static final String CHARSET = "UTF-8";
73
private static final String NAME = "name";
75
private static final String SHORT_NAME = "shortname";
77
private String reportDisplayProperty;
79
public void setReportDisplayProperty( String reportDisplayProperty )
81
this.reportDisplayProperty = reportDisplayProperty;
84
// -------------------------------------------------------------------------
86
// -------------------------------------------------------------------------
88
private ReportDataAccess reportDataAccess;
90
public void setReportDataAccess( ReportDataAccess reportDataAccess )
92
this.reportDataAccess = reportDataAccess;
95
private LocationManager locationManager;
97
public void setLocationManager( LocationManager locationManager )
99
this.locationManager = locationManager;
102
// -------------------------------------------------------------------------
104
// -------------------------------------------------------------------------
106
public void addReport( String name, int reportType )
107
throws ReportStoreException, LocationManagerException
111
File file = locationManager.getFileForWriting( getXMLFileName( name ), XML_DIR );
113
Report report = new Report( reportType );
115
XStream xStream = new XStream();
117
Writer out = new OutputStreamWriter( new FileOutputStream( file ), CHARSET );
119
xStream.toXML( report, out );
123
catch ( IOException ex )
125
throw new ReportStoreException( "Failed to add report", ex );
129
private void updateReport( String name, Report report )
130
throws ReportStoreException, LocationManagerException
134
File file = locationManager.getFileForWriting( getXMLFileName( name ), XML_DIR );
136
XStream xStream = new XStream();
138
Writer out = new OutputStreamWriter( new FileOutputStream( file ), CHARSET );
140
xStream.toXML( report, out );
144
catch ( IOException ex )
146
throw new ReportStoreException( "Failed to add report", ex );
150
public Report getReport( String name )
151
throws ReportStoreException, LocationManagerException
155
File file = locationManager.getFileForReading( getXMLFileName( name ), XML_DIR );
157
XStream xStream = new XStream();
159
Reader in = new InputStreamReader( new FileInputStream( file ), CHARSET );
161
Report report = (Report) xStream.fromXML( in );
167
catch ( FileNotFoundException ex )
169
throw new ReportStoreException( "File not found, failed to get report", ex );
171
catch ( IOException ex )
173
throw new ReportStoreException( "Failed to close inputstream", ex );
177
public int getReportType( String reportName )
178
throws ReportStoreException, LocationManagerException
180
Report report = getReport( reportName );
182
return report.getReportType();
185
public boolean deleteReport( String name )
186
throws ReportStoreException, LocationManagerException
190
File file = locationManager.getFileForWriting( getJRXMLFileName( name ), JRXML_DIR );
194
catch ( LocationManagerException ex )
198
File file = locationManager.getFileForWriting( getXMLFileName( name ), XML_DIR );
200
return file.delete();
203
public Collection<String> getAllReports()
204
throws ReportStoreException, LocationManagerException
206
File dir = locationManager.getFileForReading( XML_DIR );
208
File[] files = dir.listFiles( new XmlFilter() );
210
List<String> list = new ArrayList<String>();
212
for ( int i = 0; i < files.length; i++ )
214
list.add( FileUtils.getBaseName( files[ i ] ) );
220
public Collection<String> getAllDesigns()
221
throws ReportStoreException, LocationManagerException
223
File dir = locationManager.getFileForReading( XML_DIR );
225
File[] files = dir.listFiles( new JRXmlFilter() );
227
List<String> list = new ArrayList<String>();
229
for ( int i = 0; i < files.length; i++ )
231
list.add( FileUtils.getBaseName( files[ i ] ) );
237
public Collection<String> getDesignsByType( int reportType )
238
throws ReportStoreException, LocationManagerException
240
List<String> designNamesByType = new ArrayList<String>();
242
for ( String designName : getAllDesigns() )
244
if ( getReportType( designName ) == reportType )
246
designNamesByType.add( designName );
250
return designNamesByType;
253
public boolean isXMLReportExists( String reportName )
257
locationManager.getFileForReading( getXMLFileName( reportName ), XML_DIR );
261
catch ( LocationManagerException ex )
267
public boolean isJRXMLReportExists( String reportName )
271
locationManager.getFileForReading( getJRXMLFileName( reportName ), JRXML_DIR );
275
catch ( LocationManagerException ex )
281
// -----------------------------------------------------------------------
283
// -----------------------------------------------------------------------
285
public void addReportElement( String reportName, String type, int elementId )
286
throws ReportStoreException, LocationManagerException
290
reportName = XMLUtils.encode( reportName );
292
Report report = getReport( reportName );
294
List<Element> reportElements = report.getReportElements();
296
String elementName = "";
298
if ( type.equals( ReportStore.DATAELEMENT ) )
300
if ( reportDisplayProperty.equals( NAME ) )
302
elementName = reportDataAccess.getDataElementName( elementId );
304
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
306
elementName = reportDataAccess.getDataElementShortName( elementId );
309
else if ( type.equals( ReportStore.INDICATOR ) )
311
if ( reportDisplayProperty.equals( NAME ) )
313
elementName = reportDataAccess.getIndicatorName( elementId );
315
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
317
elementName = reportDataAccess.getIndicatorShortName( elementId );
321
elementName = XMLUtils.encode( elementName );
323
Element element = new Element( type, elementId, elementName );
325
reportElements.add( element );
327
updateReport( reportName, report );
329
catch ( ReportDataAccessException ex )
331
throw new ReportStoreException( "Failed to retrieve data", ex );
335
public void addReportElement( String reportName, String type, int elementId, int organisationUnitId )
336
throws ReportStoreException, LocationManagerException
340
Report report = getReport( reportName );
342
List<Element> reportElements = report.getReportElements();
344
String elementName = "";
346
if ( type.equals( ReportStore.DATAELEMENT ) )
348
if ( reportDisplayProperty.equals( NAME ) )
350
elementName = reportDataAccess.getDataElementName( elementId );
352
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
354
elementName = reportDataAccess.getDataElementShortName( elementId );
357
else if ( type.equals( ReportStore.INDICATOR ) )
359
if ( reportDisplayProperty.equals( NAME ) )
361
elementName = reportDataAccess.getIndicatorName( elementId );
363
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
365
elementName = reportDataAccess.getIndicatorShortName( elementId );
369
String organisationUnitName = reportDataAccess.getOrganisationUnitShortName( organisationUnitId );
371
elementName = XMLUtils.encode( elementName );
373
organisationUnitName = XMLUtils.encode( organisationUnitName );
375
OrgUnitSpecificElement element = new OrgUnitSpecificElement( type, elementId, elementName,
376
organisationUnitId, organisationUnitName );
378
reportElements.add( element );
380
updateReport( reportName, report );
382
catch ( ReportDataAccessException ex )
384
throw new ReportStoreException( "Failed to retrieve data", ex );
388
public void removeReportElement( String reportName, String id )
389
throws ReportStoreException, LocationManagerException
391
reportName = XMLUtils.encode( reportName );
393
Report report = getReport( reportName );
395
List<Element> reportElements = report.getReportElements();
397
Element element = getReportElement( reportName, id );
399
reportElements.remove( element );
401
updateReport( reportName, report );
404
private Element getReportElement( String reportName, String id )
405
throws ReportStoreException, LocationManagerException
407
reportName = XMLUtils.encode( reportName );
409
Collection<Element> reportElements = getAllReportElements( reportName );
411
Iterator<Element> iterator = reportElements.iterator();
413
while ( iterator.hasNext() )
415
Element element = iterator.next();
417
if ( element.getId().equals( id ) )
426
public void moveUpReportElement( String reportName, String id )
427
throws ReportStoreException, LocationManagerException
429
reportName = XMLUtils.encode( reportName );
431
Report report = getReport( reportName );
433
List<Element> reportElements = report.getReportElements();
435
Element element = getReportElement( reportName, id );
437
int index = reportElements.indexOf( element );
441
Element tempElement = reportElements.get( index - 1 );
443
reportElements.set( index - 1, element );
445
reportElements.set( index, tempElement );
448
updateReport( reportName, report );
451
public void moveDownReportElement( String reportName, String id )
452
throws ReportStoreException, LocationManagerException
454
reportName = XMLUtils.encode( reportName );
456
Report report = getReport( reportName );
458
List<Element> reportElements = report.getReportElements();
460
Element element = getReportElement( reportName, id );
462
int index = reportElements.indexOf( element );
464
if ( index < reportElements.size() - 1 )
466
Element tempElement = reportElements.get( index + 1 );
468
reportElements.set( index + 1, element );
470
reportElements.set( index, tempElement );
473
updateReport( reportName, report );
476
public Collection<Element> getAllReportElements( String reportName )
477
throws ReportStoreException, LocationManagerException
479
Report report = getReport( reportName );
481
return report.getReportElements();
484
// -----------------------------------------------------------------------
486
// -----------------------------------------------------------------------
488
public void addChartElement( String reportName, String type, int elementId )
489
throws ReportStoreException, LocationManagerException
493
reportName = XMLUtils.encode( reportName );
495
Report report = getReport( reportName );
497
List<Element> chartElements = report.getChartElements();
499
String elementName = "";
501
if ( type.equals( ReportStore.DATAELEMENT ) )
503
if ( reportDisplayProperty.equals( NAME ) )
505
elementName = reportDataAccess.getDataElementName( elementId );
507
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
509
elementName = reportDataAccess.getDataElementShortName( elementId );
512
else if ( type.equals( ReportStore.INDICATOR ) )
514
if ( reportDisplayProperty.equals( NAME ) )
516
elementName = reportDataAccess.getIndicatorName( elementId );
518
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
520
elementName = reportDataAccess.getIndicatorShortName( elementId );
524
elementName = XMLUtils.encode( elementName );
526
Element element = new Element( type, elementId, elementName );
528
chartElements.add( element );
530
updateReport( reportName, report );
532
catch ( ReportDataAccessException ex )
534
throw new ReportStoreException( "Failed to retrieve data", ex );
538
public void addChartElement( String reportName, String type, int elementId, int organisationUnitId )
539
throws ReportStoreException, LocationManagerException
543
reportName = XMLUtils.encode( reportName );
545
Report report = getReport( reportName );
547
List<Element> chartElements = report.getChartElements();
549
String elementName = "";
551
if ( type.equals( ReportStore.DATAELEMENT ) )
553
if ( reportDisplayProperty.equals( NAME ) )
555
elementName = reportDataAccess.getDataElementName( elementId );
557
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
559
elementName = reportDataAccess.getDataElementShortName( elementId );
562
else if ( type.equals( ReportStore.INDICATOR ) )
564
if ( reportDisplayProperty.equals( NAME ) )
566
elementName = reportDataAccess.getIndicatorName( elementId );
568
else if ( reportDisplayProperty.equals( SHORT_NAME ) )
570
elementName = reportDataAccess.getIndicatorShortName( elementId );
574
String organisationUnitName = reportDataAccess.getOrganisationUnitShortName( organisationUnitId );
576
elementName = XMLUtils.encode( elementName );
578
organisationUnitName = XMLUtils.encode( organisationUnitName );
580
OrgUnitSpecificElement element = new OrgUnitSpecificElement( type, elementId, elementName,
581
organisationUnitId, organisationUnitName );
583
chartElements.add( element );
585
updateReport( reportName, report );
587
catch ( ReportDataAccessException ex )
589
throw new ReportStoreException( "Failed to retrieve data", ex );
593
public void removeChartElement( String reportName, String id )
594
throws ReportStoreException, LocationManagerException
596
reportName = XMLUtils.encode( reportName );
598
Report report = getReport( reportName );
600
List<Element> chartElements = report.getChartElements();
602
Iterator<Element> iterator = chartElements.iterator();
604
while ( iterator.hasNext() )
606
Element element = iterator.next();
608
if ( element.getId().equals( id ) )
614
updateReport( reportName, report );
617
private Element getChartElement( String reportName, String id )
618
throws ReportStoreException, LocationManagerException
620
reportName = XMLUtils.encode( reportName );
622
Collection<Element> chartElements = getAllChartElements( reportName );
624
Iterator<Element> iterator = chartElements.iterator();
626
while ( iterator.hasNext() )
628
Element element = iterator.next();
630
if ( element.getId().equals( id ) )
639
public void moveUpChartElement( String reportName, String id )
640
throws ReportStoreException, LocationManagerException
642
reportName = XMLUtils.encode( reportName );
644
Report report = getReport( reportName );
646
List<Element> chartElements = report.getChartElements();
648
Element element = getChartElement( reportName, id );
650
int index = chartElements.indexOf( element );
654
Element tempElement = chartElements.get( index - 1 );
656
chartElements.set( index - 1, element );
658
chartElements.set( index, tempElement );
661
updateReport( reportName, report );
664
public void moveDownChartElement( String reportName, String id )
665
throws ReportStoreException, LocationManagerException
667
reportName = XMLUtils.encode( reportName );
669
Report report = getReport( reportName );
671
List<Element> chartElements = report.getChartElements();
673
Element element = getChartElement( reportName, id );
675
int index = chartElements.indexOf( element );
677
if ( index < chartElements.size() - 1 )
679
Element tempElement = chartElements.get( index + 1 );
681
chartElements.set( index + 1, element );
683
chartElements.set( index, tempElement );
686
updateReport( reportName, report );
689
public Collection<Element> getAllChartElements( String reportName )
690
throws ReportStoreException, LocationManagerException
692
reportName = XMLUtils.encode( reportName );
694
Report report = getReport( reportName );
696
return report.getChartElements();
699
// -----------------------------------------------------------------------
701
// -----------------------------------------------------------------------
703
public void setDesignTemplate( String reportName, int number )
704
throws ReportStoreException, LocationManagerException
706
reportName = XMLUtils.encode( reportName );
708
Report report = getReport( reportName );
710
report.setDesignTemplate( number );
712
updateReport( reportName, report );
715
public int getDesignTemplate( String reportName )
716
throws ReportStoreException, LocationManagerException
718
reportName = XMLUtils.encode( reportName );
720
Report report = getReport( reportName );
722
return report.getDesignTemplate();
725
// -------------------------------------------------------------------------
727
// -------------------------------------------------------------------------
729
public void setChartTemplate( String reportName, int number )
730
throws ReportStoreException, LocationManagerException
732
reportName = XMLUtils.encode( reportName );
734
Report report = getReport( reportName );
736
report.setChartTemplate( number );
738
updateReport( reportName, report );
741
public int getChartTemplate( String reportName )
742
throws ReportStoreException, LocationManagerException
744
reportName = XMLUtils.encode( reportName );
746
Report report = getReport( reportName );
748
return report.getChartTemplate();