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

« back to all changes in this revision

Viewing changes to local/in/dhis-web-dashboard/src/main/java/org/hisp/dhis/dashboard/action/GetSortedDataAction.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.dashboard.action;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.List;
 
5
 
 
6
import javax.servlet.http.HttpServletRequest;
 
7
import javax.servlet.http.HttpSession;
 
8
 
 
9
import com.opensymphony.webwork.ServletActionContext;
 
10
import com.opensymphony.xwork.Action;
 
11
import com.opensymphony.xwork.ActionContext;
 
12
 
 
13
public class GetSortedDataAction
 
14
implements Action
 
15
{
 
16
    double[][] data1;
 
17
 
 
18
    double[][] data2;
 
19
 
 
20
    String[] series1;
 
21
 
 
22
    String[] series2;
 
23
 
 
24
    String[] categories1;
 
25
 
 
26
    String[] categories2;
 
27
 
 
28
    private List<String> headingInfo;    
 
29
 
 
30
    public List<String> getHeadingInfo()
 
31
    {
 
32
        return headingInfo;
 
33
    }
 
34
 
 
35
 
 
36
    public String execute()
 
37
        throws Exception
 
38
    {
 
39
 
 
40
        headingInfo = new ArrayList<String>();
 
41
        
 
42
        ActionContext ctx = ActionContext.getContext();
 
43
        HttpServletRequest req = (HttpServletRequest) ctx.get( ServletActionContext.HTTP_REQUEST );
 
44
        String chartDisplayOption = req.getParameter( "chartDisplayOption" );
 
45
 
 
46
        HttpSession session = req.getSession();
 
47
        Double[][] objData1 = (Double[][]) session.getAttribute( "data1" );
 
48
        Double[][] objData2 = (Double[][]) session.getAttribute( "data2" );
 
49
                
 
50
 
 
51
        String[] series1S = (String[]) session.getAttribute( "series1" );
 
52
        String[] series2S = (String[]) session.getAttribute( "series2" );
 
53
        String[] categories1S = (String[]) session.getAttribute( "categories1" );
 
54
        String[] categories2S = (String[]) session.getAttribute( "categories2" );
 
55
                        
 
56
 
 
57
        initialzeAllLists(series1S, series2S, categories1S, categories2S);
 
58
        
 
59
        if(objData1 == null || objData2 == null || series1 == null || series2 == null || categories1 == null || categories2 == null )
 
60
                System.out.println("Session Objects are null");
 
61
        else
 
62
                System.out.println("Session Objects are not null");
 
63
        
 
64
        data1 = convertDoubleTodouble( objData1 );
 
65
        data2 = convertDoubleTodouble( objData2 );
 
66
        
 
67
        if(chartDisplayOption == null || chartDisplayOption.equalsIgnoreCase("none")) { }
 
68
        else if(chartDisplayOption.equalsIgnoreCase("ascend")) { sortByAscending(); }
 
69
        else if(chartDisplayOption.equalsIgnoreCase("desend")) { sortByDesscending(); }
 
70
        else if(chartDisplayOption.equalsIgnoreCase("alphabet")) { sortByAlphabet(); }          
 
71
        
 
72
        initializeDataLists();
 
73
        
 
74
        System.out.println(headingInfo);
 
75
        
 
76
        return SUCCESS;
 
77
    }// execute end
 
78
 
 
79
    
 
80
    public void initializeDataLists()
 
81
    {
 
82
        int i;
 
83
        headingInfo.add( "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border-style: dotted\" bordercolor=\"#111111\" width=\"100%\"><tr><td class=\"TableHeadingCellStyles\" style=\"border-style: dotted; border-width: 1\">Service Name</td>" );
 
84
        
 
85
        for(i=0; i < categories1.length; i++)
 
86
        {
 
87
            headingInfo.add( "<td class=\"TableHeadingCellStyles\" align=\"center\" style=\"border-style: dotted; border-width: 1\">"+categories1[i]+"</td>" );
 
88
        }
 
89
        headingInfo.add( "</tr>" );
 
90
        
 
91
        for(i=0; i<data1.length; i++)
 
92
        {
 
93
            headingInfo.add( "<tr><td class=\"TableHeadingCellStyles\" style=\"border-style: dotted; border-width: 1\">"+series1[i]+"</td>" );
 
94
            for(int j=0; j<data1[i].length; j++)
 
95
            {
 
96
                headingInfo.add( "<td class=\"TableDataCellStyles\" align=\"center\" style=\"border-style: dotted; border-width: 1\">"+data1[i][j]+"</td>" );                                         
 
97
            }
 
98
            headingInfo.add( "</tr>" );
 
99
        }
 
100
        
 
101
        headingInfo.add( "</table>" );
 
102
    }
 
103
    
 
104
    public void initialzeAllLists(String[]series1S, String[] series2S, String[] categories1S, String[] categories2S)
 
105
    {
 
106
        int i;
 
107
        series1 = new String[series1S.length];
 
108
        series2 = new String[series2S.length];
 
109
        categories1 = new String[categories1S.length];
 
110
        categories2 = new String[categories2S.length];
 
111
        
 
112
        for(i = 0; i < series1S.length; i++)
 
113
        {
 
114
                series1[i] = series1S[i];
 
115
        }
 
116
 
 
117
        for(i = 0; i < series2S.length; i++)
 
118
        {
 
119
                series2[i] = series2S[i];
 
120
        }
 
121
        
 
122
        for(i = 0; i < categories1S.length; i++)
 
123
        {
 
124
                categories1[i] = categories1S[i];
 
125
        }
 
126
        
 
127
        for(i = 0; i < categories2S.length; i++)
 
128
        {
 
129
                categories2[i] = categories2S[i];
 
130
        }
 
131
        
 
132
    }
 
133
    
 
134
    public double[][] convertDoubleTodouble( Double[][] objData )
 
135
    {
 
136
        System.out.println("Before Sorting : ");
 
137
        double[][] data = new double[series1.length][categories1.length];
 
138
        for ( int i = 0; i < objData.length; i++ )
 
139
        {
 
140
            for ( int j = 0; j < objData[0].length; j++ )
 
141
            {
 
142
                data[i][j] = objData[i][j].doubleValue();
 
143
                System.out.print(categories1[j]+": "+data[i][j]+", ");                
 
144
            }
 
145
            System.out.println("");
 
146
        }
 
147
 
 
148
        return data;
 
149
    }// convertDoubleTodouble end
 
150
 
 
151
    public void sortByAscending()
 
152
    {
 
153
        for(int i=0; i < categories1.length-1 ; i++)
 
154
        {
 
155
                for(int j=0; j < categories1.length-1-i; j++)
 
156
                {
 
157
                        if(data1[0][j] > data1[0][j+1])
 
158
                        {
 
159
                                for(int k=0; k<series1.length; k++)
 
160
                                {
 
161
                                        double temp1 = data1[k][j];
 
162
                                        data1[k][j] = data1[k][j+1];
 
163
                                        data1[k][j+1] = temp1;                                          
 
164
                                }
 
165
                                
 
166
                                String temp2 = categories1[j];
 
167
                                categories1[j] = categories1[j+1];
 
168
                                categories1[j+1] = temp2;
 
169
                        }
 
170
                }
 
171
        }
 
172
        
 
173
        for(int i=0; i < categories2.length-1 ; i++)
 
174
        {
 
175
                for(int j=0; j < categories2.length-1-i; j++)
 
176
                {
 
177
                        if(data2[0][j] > data2[0][j+1])
 
178
                        {
 
179
                                for(int k=0; k<series2.length; k++)
 
180
                                {
 
181
                                        double temp1 = data2[k][j];
 
182
                                        data2[k][j] = data2[k][j+1];
 
183
                                        data2[k][j+1] = temp1;                                          
 
184
                                }
 
185
                                
 
186
                                String temp2 = categories2[j];
 
187
                                categories2[j] = categories2[j+1];
 
188
                                categories2[j+1] = temp2;
 
189
                        }
 
190
                }
 
191
        }
 
192
        
 
193
    }
 
194
 
 
195
    public void sortByDesscending()
 
196
    {
 
197
        for(int i=0; i < categories1.length-1 ; i++)
 
198
        {
 
199
                for(int j=0; j < categories1.length-1-i; j++)
 
200
                {
 
201
                        if(data1[0][j] < data1[0][j+1])
 
202
                        {
 
203
                                for(int k=0; k<series1.length; k++)
 
204
                                {
 
205
                                        double temp1 = data1[k][j];
 
206
                                        data1[k][j] = data1[k][j+1];
 
207
                                        data1[k][j+1] = temp1;                                          
 
208
                                }
 
209
                                
 
210
                                String temp2 = categories1[j];
 
211
                                categories1[j] = categories1[j+1];
 
212
                                categories1[j+1] = temp2;
 
213
                        }
 
214
                }
 
215
        }
 
216
        
 
217
        for(int i=0; i < categories2.length-1 ; i++)
 
218
        {
 
219
                for(int j=0; j < categories2.length-1-i; j++)
 
220
                {
 
221
                        if(data2[0][j] < data2[0][j+1])
 
222
                        {
 
223
                                for(int k=0; k<series2.length; k++)
 
224
                                {
 
225
                                        double temp1 = data2[k][j];
 
226
                                        data2[k][j] = data2[k][j+1];
 
227
                                        data2[k][j+1] = temp1;                                          
 
228
                                }
 
229
                                
 
230
                                String temp2 = categories2[j];
 
231
                                categories2[j] = categories2[j+1];
 
232
                                categories2[j+1] = temp2;
 
233
                        }
 
234
                }
 
235
        }
 
236
 
 
237
    }   
 
238
    
 
239
    public void sortByAlphabet()
 
240
        {
 
241
                for(int i=0; i < categories1.length-1 ; i++)
 
242
                {
 
243
                        for(int j=0; j < categories1.length-1-i; j++)
 
244
                        {
 
245
                                if(categories1[j].compareToIgnoreCase(categories1[j+1]) > 0)
 
246
                                {
 
247
                                        for(int k=0; k<series1.length; k++)
 
248
                                        {
 
249
                                        double temp1 = data1[k][j];
 
250
                                        data1[k][j] = data1[k][j+1];
 
251
                                        data1[k][j+1] = temp1;                                          
 
252
                                        }
 
253
                                        
 
254
                                        String temp2 = categories1[j];
 
255
                                        categories1[j] = categories1[j+1];
 
256
                                        categories1[j+1] = temp2;
 
257
                                }
 
258
                        }
 
259
                }
 
260
                
 
261
                for(int i=0; i < categories2.length-1 ; i++)
 
262
                {
 
263
                        for(int j=0; j < categories2.length-1-i; j++)
 
264
                        {
 
265
                                if(categories2[j].compareToIgnoreCase(categories2[j+1]) > 0)
 
266
                                {
 
267
                                        for(int k=0; k<series2.length; k++)
 
268
                                        {
 
269
                                        double temp1 = data2[k][j];
 
270
                                        data2[k][j] = data2[k][j+1];
 
271
                                        data2[k][j+1] = temp1;                                          
 
272
                                        }
 
273
                                        
 
274
                                        String temp2 = categories2[j];
 
275
                                        categories2[j] = categories2[j+1];
 
276
                                        categories2[j+1] = temp2;
 
277
                                }
 
278
                        }
 
279
                }
 
280
        
 
281
    }
 
282
    
 
283
}// class end