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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/pdf/impl/ItextPdfService.java

  • 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
package org.hisp.dhis.pdf.impl;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import static org.hisp.dhis.system.util.PDFUtils.addTableToDocument;
 
31
import static org.hisp.dhis.system.util.PDFUtils.getCell;
 
32
import static org.hisp.dhis.system.util.PDFUtils.getHeader3Cell;
 
33
import static org.hisp.dhis.system.util.PDFUtils.getItalicCell;
 
34
import static org.hisp.dhis.system.util.PDFUtils.getPdfPTable;
 
35
import static org.hisp.dhis.system.util.PDFUtils.getTextCell;
 
36
 
 
37
import java.io.OutputStream;
 
38
import java.util.HashMap;
 
39
import java.util.Map;
 
40
 
 
41
import org.hisp.dhis.dataelement.DataElement;
 
42
import org.hisp.dhis.dataelement.DataElementService;
 
43
import org.hisp.dhis.indicator.Indicator;
 
44
import org.hisp.dhis.indicator.IndicatorService;
 
45
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
46
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
47
import org.hisp.dhis.pdf.PdfService;
 
48
import org.hisp.dhis.system.util.PDFUtils;
 
49
 
 
50
import com.lowagie.text.Document;
 
51
import com.lowagie.text.pdf.PdfPTable;
 
52
 
 
53
/**
 
54
 * @author Lars Helge Overland
 
55
 * @version $Id$
 
56
 */
 
57
public class ItextPdfService
 
58
    implements PdfService
 
59
{
 
60
    // -------------------------------------------------------------------------
 
61
    // Dependencies
 
62
    // -------------------------------------------------------------------------
 
63
 
 
64
    private DataElementService dataElementService;
 
65
 
 
66
    public void setDataElementService( DataElementService dataElementService )
 
67
    {
 
68
        this.dataElementService = dataElementService;
 
69
    }
 
70
 
 
71
    private IndicatorService indicatorService;
 
72
 
 
73
    public void setIndicatorService( IndicatorService indicatorService )
 
74
    {
 
75
        this.indicatorService = indicatorService;
 
76
    }
 
77
    
 
78
    private OrganisationUnitService organisationUnitService;
 
79
 
 
80
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
81
    {
 
82
        this.organisationUnitService = organisationUnitService;
 
83
    }
 
84
 
 
85
    // -------------------------------------------------------------------------
 
86
    // PdfService implementation
 
87
    // -------------------------------------------------------------------------
 
88
 
 
89
    public void writeAllDataElements( OutputStream outputStream )
 
90
    {
 
91
        Document document = PDFUtils.openDocument( outputStream );
 
92
        
 
93
        for ( DataElement element : dataElementService.getAllDataElements() )        
 
94
        {
 
95
            PdfPTable table = getPdfPTable( true, 0.40f, 0.60f );
 
96
            
 
97
            table.addCell( getHeader3Cell( element.getName(), 2 ) );
 
98
            
 
99
            table.addCell( getCell( 2, 15 ) );
 
100
            
 
101
            table.addCell( getItalicCell( "Short name", 1 ) );
 
102
            table.addCell( getTextCell( element.getShortName() ) );
 
103
            
 
104
            table.addCell( getItalicCell( "Alternative name", 1 ) );
 
105
            table.addCell( getTextCell( element.getAlternativeName() ) );
 
106
            
 
107
            table.addCell( getItalicCell( "Code", 1 ) );
 
108
            table.addCell( getTextCell( element.getCode() ) );
 
109
            
 
110
            table.addCell( getItalicCell( "Description", 1 ) );
 
111
            table.addCell( getTextCell( element.getDescription() ) );
 
112
            
 
113
            table.addCell( getItalicCell( "Active", 1 ) );
 
114
            table.addCell( getTextCell( getBoolean().get( element.isActive() ) ) );
 
115
            
 
116
            table.addCell( getItalicCell( "Type", 1 ) );
 
117
            table.addCell( getTextCell( getType().get( element.getType() ) ) );            
 
118
            
 
119
            table.addCell( getItalicCell( "Aggregaton operator", 1 ) );
 
120
            table.addCell( getTextCell( getAggregationOperator().get( element.getAggregationOperator() ) ) );
 
121
 
 
122
            table.addCell( getCell( 2, 30 ) );
 
123
            
 
124
            addTableToDocument( document, table );
 
125
        }
 
126
        
 
127
        PDFUtils.closeDocument( document );
 
128
    }
 
129
 
 
130
    public void writeAllIndicators( OutputStream outputStream )
 
131
    {
 
132
        Document document = PDFUtils.openDocument( outputStream );
 
133
        
 
134
        for ( Indicator indicator : indicatorService.getAllIndicators() )
 
135
        {
 
136
            PdfPTable table = getPdfPTable( true, 0.40f, 0.60f );
 
137
    
 
138
            table.addCell( getHeader3Cell( indicator.getName(), 2 ) );
 
139
 
 
140
            table.addCell( getCell( 2, 15 ) );
 
141
            
 
142
            table.addCell( getItalicCell( "Short name", 1 ) );
 
143
            table.addCell( getTextCell( indicator.getShortName() ) );
 
144
 
 
145
            table.addCell( getItalicCell( "Alternative name", 1 ) );
 
146
            table.addCell( getTextCell( indicator.getAlternativeName() ) );
 
147
 
 
148
            table.addCell( getItalicCell( "Code", 1 ) );
 
149
            table.addCell( getTextCell( indicator.getCode() ) );
 
150
 
 
151
            table.addCell( getItalicCell( "Description", 1 ) );
 
152
            table.addCell( getTextCell( indicator.getDescription() ) );
 
153
 
 
154
            table.addCell( getItalicCell( "Annualized", 1 ) );
 
155
            table.addCell( getTextCell( getBoolean().get( indicator.getAnnualized() ) ) );
 
156
            
 
157
            table.addCell( getItalicCell( "Indicator type", 1 ) );
 
158
            table.addCell( getTextCell( indicator.getIndicatorType().getName() ) );
 
159
 
 
160
            table.addCell( getItalicCell( "Numerator description", 1 ) );
 
161
            table.addCell( getTextCell( indicator.getNumeratorDescription() ) );
 
162
 
 
163
            table.addCell( getItalicCell( "Denominator description", 1 ) );
 
164
            table.addCell( getTextCell( indicator.getDenominatorDescription() ) );
 
165
 
 
166
            table.addCell( getCell( 2, 30 ) );
 
167
            
 
168
            addTableToDocument( document, table );
 
169
        }
 
170
        
 
171
        PDFUtils.closeDocument( document );
 
172
    }
 
173
    
 
174
    public void writeAllOrganisationUnits( OutputStream outputStream )
 
175
    {
 
176
        Document document = PDFUtils.openDocument( outputStream );
 
177
        
 
178
        for ( OrganisationUnit unit : organisationUnitService.getAllOrganisationUnits() )
 
179
        {
 
180
            PdfPTable table = getPdfPTable( true, 0.40f, 0.60f );
 
181
            
 
182
            table.addCell( getHeader3Cell( unit.getName(), 2 ) );
 
183
            
 
184
            table.addCell( getCell( 2, 15 ) );            
 
185
 
 
186
            table.addCell( getItalicCell( "Short name", 1 ) );
 
187
            table.addCell( getTextCell( unit.getShortName() ) );
 
188
 
 
189
            table.addCell( getItalicCell( "Code", 1 ) );
 
190
            table.addCell( getTextCell( unit.getCode() ) );
 
191
 
 
192
            table.addCell( getItalicCell( "Opening date", 1 ) );
 
193
            table.addCell( getTextCell( unit.getOpeningDate() != null ? unit.getOpeningDate().toString() : "" ) );
 
194
 
 
195
            table.addCell( getItalicCell( "Closed date", 1 ) );
 
196
            table.addCell( getTextCell( unit.getClosedDate() != null ? unit.getClosedDate().toString() : "" ) );
 
197
 
 
198
            table.addCell( getItalicCell( "Active", 1 ) );
 
199
            table.addCell( getTextCell( getBoolean().get( unit.isActive() ) ) );
 
200
 
 
201
            table.addCell( getItalicCell( "Comment", 1 ) );
 
202
            table.addCell( getTextCell( unit.getComment() ) );
 
203
 
 
204
            table.addCell( getCell( 2, 30 ) );
 
205
            
 
206
            addTableToDocument( document, table );     
 
207
        }
 
208
        
 
209
        PDFUtils.closeDocument( document );
 
210
    }
 
211
 
 
212
    // -------------------------------------------------------------------------
 
213
    // Supportive methods
 
214
    // -------------------------------------------------------------------------
 
215
 
 
216
    private Map<Boolean, String> getBoolean()
 
217
    {
 
218
        Map<Boolean, String> map = new HashMap<Boolean, String>();
 
219
        map.put( true, "Yes" );
 
220
        map.put( false, "No" );
 
221
        return map;
 
222
    }
 
223
    
 
224
    private Map<String, String> getType()
 
225
    {
 
226
        Map<String, String> map = new HashMap<String, String>();
 
227
        map.put( DataElement.TYPE_STRING, "Text" );
 
228
        map.put( DataElement.TYPE_INT, "Number" );
 
229
        map.put( DataElement.TYPE_BOOL, "Yes/No" );
 
230
        return map;
 
231
    }
 
232
    
 
233
    private Map<String, String> getAggregationOperator()
 
234
    {
 
235
        Map<String, String> map = new HashMap<String, String>();
 
236
        map.put( DataElement.AGGREGATION_OPERATOR_SUM, "Sum" );
 
237
        map.put( DataElement.AGGREGATION_OPERATOR_AVERAGE, "Average" );
 
238
        map.put( DataElement.AGGREGATION_OPERATOR_COUNT, "Count" );
 
239
        return map;
 
240
    }
 
241
}