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

« back to all changes in this revision

Viewing changes to local/in/dhis-web-integration/src/main/java/org/hisp/dhis/integration/rims/api/tables/RIMS_Immun_Vaccine_Dtl.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
/*
 
2
 * Copyright (c) 2004-2008, University of Oslo
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 * * Redistributions of source code must retain the above copyright notice, this
 
8
 *   list of conditions and the following disclaimer.
 
9
 * * Redistributions in binary form must reproduce the above copyright notice,
 
10
 *   this list of conditions and the following disclaimer in the documentation
 
11
 *   and/or other materials provided with the distribution.
 
12
 * * Neither the name of the HISP project nor the names of its contributors may
 
13
 *   be used to endorse or promote products derived from this software without
 
14
 *   specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
23
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
package org.hisp.dhis.integration.rims.api.tables;
 
28
 
 
29
import java.sql.SQLException;
 
30
import org.hisp.dhis.integration.rims.api.RIMS_Mapping_DataElement;
 
31
 
 
32
/**
 
33
 * @author Leif Arne Storset
 
34
 * @version $Id$
 
35
 */
 
36
public class RIMS_Immun_Vaccine_Dtl
 
37
    extends PHCTable
 
38
{
 
39
    public String getDescription()
 
40
    {
 
41
        return "Immunization statistics";
 
42
    }
 
43
    
 
44
    public String getTableName()
 
45
    {
 
46
        return "immun_vaccine_dtl";
 
47
    }
 
48
 
 
49
    public void fillInTotals()
 
50
        throws SQLException
 
51
    {
 
52
        String query = "UPDATE " + getTableName()
 
53
            + " SET under1year_total = under1year_male + under1year_female"
 
54
            + " WHERE under1year_male IS NOT NULL AND under1year_female IS NOT NULL";
 
55
        super.executeUpdate( query );
 
56
        
 
57
        query = "UPDATE " + getTableName()
 
58
             + " SET over1year_total = over1year_male + over1year_female" +
 
59
               " WHERE over1year_male IS NOT NULL AND over1year_female IS NOT NULL";
 
60
        super.executeUpdate( query );
 
61
    }
 
62
 
 
63
    public boolean isData( RIMS_PHC phc, int month, int year,
 
64
        RIMS_Mapping_DataElement mappingDataElement ) throws SQLException
 
65
    {
 
66
        String vac_code = mappingDataElement.getVaccine_code();
 
67
        String antigen = mappingDataElement.getAntigen();
 
68
 
 
69
        String query = "SELECT * FROM " + getTableName() + " WHERE phc_code LIKE '" + phc.getCode()
 
70
            + "' AND mnth = " + month + " AND yr = " + year + " AND vaccine_code LIKE '" + vac_code + "' "
 
71
            + " AND antigen LIKE '" + antigen + "'";
 
72
 
 
73
        return existingData( query, null );
 
74
    }
 
75
 
 
76
    public int updateData( RIMS_PHC rimsOrgUnit, int month, int year,
 
77
        RIMS_Mapping_DataElement mappingDataElement, String resValue ) throws SQLException
 
78
    {
 
79
        String vac_code = mappingDataElement.getVaccine_code();
 
80
        String antigen = mappingDataElement.getAntigen();
 
81
        String phc_code = rimsOrgUnit.getPhc_code();
 
82
 
 
83
        String update = "UPDATE " + getTableName() + " SET " + mappingDataElement.getRimsColumn() + " = ? "
 
84
            + ", updated_by = ?" + " WHERE phc_code LIKE '" + phc_code + "' AND mnth = " + month + " AND yr = " + year
 
85
            + " AND vaccine_code LIKE '" + vac_code + "' " + " AND antigen LIKE '" + antigen + "'";
 
86
 
 
87
        int numberResult = Integer.parseInt( resValue );
 
88
 
 
89
        if ( numberResult != -1 || numberResult != 0 )
 
90
        {
 
91
            Object[] params = { numberResult, "DHIS2" };
 
92
            return executeUpdate( update, params );
 
93
        }
 
94
        else 
 
95
        {
 
96
            throw new RuntimeException( "No data available for data element "+ 
 
97
                mappingDataElement.getDhisExpression() );
 
98
        }
 
99
    }
 
100
 
 
101
    public int insertData( RIMS_PHC rimsOrgUnit, int month,
 
102
        int year, RIMS_Mapping_DataElement mappingDataElement, String resValue ) throws SQLException
 
103
    {
 
104
        String vac_code = mappingDataElement.getVaccine_code();
 
105
        String antigen = mappingDataElement.getAntigen();
 
106
 
 
107
        String insert = "INSERT INTO "
 
108
            + getTableName()
 
109
            + " (vaccine_code, antigen, phc_code, district_code, mnth, yr, state_code, "
 
110
            + mappingDataElement.getRimsColumn() + ", created_by)"
 
111
            + " VALUES (?,?,?,?,?,?,?,?,?)";
 
112
 
 
113
        int numberResult = Integer.parseInt( resValue );
 
114
 
 
115
        if ( numberResult != -1 || numberResult != 0 )
 
116
        {
 
117
            Object[] params = { vac_code, antigen, rimsOrgUnit.getPhc_code(),
 
118
                rimsOrgUnit.getDistrict_code(), month, year,
 
119
                rimsOrgUnit.getState_code(), numberResult, "DHIS2" };
 
120
 
 
121
            return executeUpdate( insert, params );
 
122
        }
 
123
        else
 
124
 
 
125
        {
 
126
            throw new RuntimeException( "No data available for data element "
 
127
                + mappingDataElement.getDhisExpression() );
 
128
        }
 
129
    }
 
130
    
 
131
    public void markIfComplete() throws SQLException
 
132
    {
 
133
        // TODO Make hard-coded vaccines configurable
 
134
        // Mark pregnant women
 
135
        String updateWomen = "UPDATE " + getTableName() + " SET" + " complete = 1"
 
136
            + " WHERE vaccine_code LIKE 'vac4'"
 
137
            + " AND preg_women IS NOT NULL";
 
138
        executeUpdate( updateWomen );
 
139
 
 
140
        // Mark under 1 year
 
141
        String updateUnder1 = "UPDATE " + getTableName() + " SET" + " complete = 1"
 
142
        + " WHERE vaccine_code LIKE 'vac0'"
 
143
        + " AND antigen = '0'"
 
144
        + " AND under1year_male IS NOT NULL"
 
145
        + " AND under1year_female IS NOT NULL";
 
146
        executeUpdate( updateUnder1 );
 
147
        
 
148
        // Mark over 1 year
 
149
        String updateOver1 = "UPDATE " + getTableName() + " SET" + " complete = 1"
 
150
        + " WHERE vaccine_code IN ( 'vac12', 'vac13', 'vac6', 'vac10', 'vac11' )"
 
151
        + " OR ( vaccine_code LIKE 'vac5' AND antigen IN ( '2', '3', '4', '5' ) ) "
 
152
        + " AND over1year_male IS NOT NULL"
 
153
        + " AND over1year_female IS NOT NULL";
 
154
        executeUpdate( updateOver1 );
 
155
        
 
156
        // Mark all children
 
157
        String updateChildren = "UPDATE " + getTableName() + " SET" + " complete = 1"
 
158
        + " WHERE vaccine_code IN ( 'vac1', 'vac3', 'vac7', 'vac2' ) "
 
159
        + " OR ( vaccine_code LIKE 'vac0' AND antigen IN ( '1', '2', '3' ) )"
 
160
        + " OR ( vaccine_code LIKE 'vac5' AND antigen LIKE '1' )"
 
161
        + " AND over1year_male IS NOT NULL"
 
162
        + " AND over1year_female IS NOT NULL";
 
163
        executeUpdate( updateChildren );
 
164
    }
 
165
    
 
166
    public String getDataValue( RIMS_Mapping_DataElement mappingDataElement, RIMS_PHC orgUnit, int month, int year ) throws SQLException
 
167
    {
 
168
        String getColumn = mappingDataElement.getRimsColumn();
 
169
        String query = "SELECT "+ getColumn +
 
170
                      " FROM "+ mappingDataElement.getTableName() +
 
171
                      " WHERE vaccine_code = ?"+
 
172
                      " AND antigen = ? "+
 
173
                      " AND phc_code = ?"+
 
174
                      " AND mnth = ?"+
 
175
                      " AND yr = ?";
 
176
        Object[] params = { mappingDataElement.getVaccine_code(),
 
177
                            mappingDataElement.getAntigen(),
 
178
                            orgUnit.getPhc_code(),
 
179
                            month,
 
180
                            year
 
181
        };
 
182
        
 
183
        return executeQuery( query, getColumn, params );
 
184
    }
 
185
 
 
186
 
 
187
}