Subversion Repositories bacoAlunos

Rev

Rev 1838 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1574 jmachado 1
package pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils;
1553 jmachado 2
 
1830 jmachado 3
import com.owlike.genson.Genson;
4
import com.owlike.genson.GensonBuilder;
1553 jmachado 5
import org.dom4j.DocumentException;
1830 jmachado 6
import org.json.JSONException;
7
import org.json.JSONObject;
1553 jmachado 8
import org.w3c.dom.Document;
1830 jmachado 9
import pt.estgp.estgweb.domain.utils.JsonView;
1553 jmachado 10
import pt.estgp.estgweb.utils.Dom4jUtil;
11
 
12
import javax.xml.transform.TransformerException;
13
import java.io.ByteArrayOutputStream;
14
import java.io.IOException;
15
import java.io.Serializable;
16
import java.util.ArrayList;
1830 jmachado 17
import java.util.List;
1553 jmachado 18
 
19
/**
20
 * Created by jorgemachado on 11/11/16.
21
 */
1830 jmachado 22
public class DataTable implements 1.5.0/docs/api/java/io/Serializable.html">Serializable, JsonView
1553 jmachado 23
{
24
 
25
 
26
 
27
    public DataTable() {
28
 
29
        setRows(new ArrayList<Row>());
30
    }
31
 
32
 
33
    private ArrayList<Row> rows;
34
 
35
    public Row addRow()
36
    {
37
        Row row = new Row();
38
        rows.add(row);
39
        return row;
40
    }
41
    public Row addRowHeader()
42
    {
43
        Row row = new Row();
1830 jmachado 44
        row.changeType2Header();
1553 jmachado 45
        rows.add(row);
46
        return row;
47
    }
48
    public Row addRowNormal()
49
    {
50
        Row row = new Row();
1830 jmachado 51
        row.changeType2Normal();
1553 jmachado 52
        rows.add(row);
53
        return row;
54
    }
55
    public Row addRowFooter()
56
    {
57
        Row row = new Row();
1830 jmachado 58
        row.changeType2Footer();
1553 jmachado 59
        rows.add(row);
60
        return row;
61
    }
62
 
63
    public ArrayList<Row> getRows() { return rows; }
64
 
65
    public void setRows(ArrayList<Row> rows) {
66
        this.rows = rows;
67
    }
68
 
69
    public static class Row implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
70
    {
71
        //header, footer, normal
72
        1.5.0/docs/api/java/lang/String.html">String type = null;
1569 jmachado 73
        boolean invisible = false;
1553 jmachado 74
        private ArrayList<Col> cols;
75
 
1567 jmachado 76
 
1553 jmachado 77
        public Row() {
78
            setCols(new ArrayList<Col>());
79
        }
80
 
1569 jmachado 81
        public boolean isInvisible() {
82
            return invisible;
83
        }
84
        public void setInvisible(boolean invisible) {
85
            this.invisible = invisible;
86
        }
87
 
1553 jmachado 88
        public void setCols(ArrayList<Col> cols) {
89
            this.cols = cols;
90
        }
1830 jmachado 91
        public ArrayList<Col> getCols() { return cols; }
1553 jmachado 92
 
1830 jmachado 93
        public 1.5.0/docs/api/java/lang/String.html">String getType() { return type; }
94
        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
95
            this.type = type;
96
        }
1553 jmachado 97
 
1567 jmachado 98
 
1830 jmachado 99
        private void changeType2Normal() { type = "normal"; }
100
        private void changeType2Header() { type = "header"; }
101
        private void changeType2Footer() { type = "footer"; }
1567 jmachado 102
 
1830 jmachado 103
 
104
 
1553 jmachado 105
        public Col addCol()
106
        {
107
            Col col = new Col();
108
            cols.add(col);
109
            return col;
110
        }
111
 
112
        public Col addColNumberLeft(1.5.0/docs/api/java/lang/String.html">String value)
113
        {   Col col = new Col(value);
1830 jmachado 114
            col.changeTypeNumber();
115
            col.changeAlignLeft();
1553 jmachado 116
            cols.add(col);
117
            return col;
118
        }
119
        public Col addColNumberRight(1.5.0/docs/api/java/lang/String.html">String value)
1567 jmachado 120
        {   Col col = new Col(value);
1830 jmachado 121
            col.changeTypeNumber();
122
            col.changeAlignRight();
1553 jmachado 123
            cols.add(col);
124
            return col;
125
        }
126
        public Col addColNumberCenter(1.5.0/docs/api/java/lang/String.html">String value)
127
        {   Col col = new Col(value);
1830 jmachado 128
            col.changeTypeNumber();
129
            col.changeAlignCenter();
1553 jmachado 130
            cols.add(col);
131
            return col;
132
        }
133
 
134
        public Col addColTextLeft(1.5.0/docs/api/java/lang/String.html">String value)
135
        {   Col col = new Col(value);
1830 jmachado 136
            col.changeTypeText();
137
            col.changeAlignLeft();
1553 jmachado 138
            cols.add(col);
139
            return col;
140
        }
141
        public Col addColTextRight(1.5.0/docs/api/java/lang/String.html">String value)
142
        {   Col col = new Col(value);
1830 jmachado 143
            col.changeTypeText();
144
            col.changeAlignRight();
1553 jmachado 145
            cols.add(col);
146
            return col;
147
        }
1571 jmachado 148
 
1553 jmachado 149
        public Col addColTextCenter(1.5.0/docs/api/java/lang/String.html">String value)
150
        {   Col col = new Col(value);
1830 jmachado 151
            col.changeTypeText();
152
            col.changeAlignCenter();
1553 jmachado 153
            cols.add(col);
154
            return col;
155
        }
156
 
1571 jmachado 157
        public Col addColInvisible()
158
        {   Col col = new Col();
1830 jmachado 159
            col.changeTypeInvisible();
1571 jmachado 160
            cols.add(col);
161
            return col;
162
        }
163
 
1553 jmachado 164
        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value)
165
        {   Col col = new Col(value);
1830 jmachado 166
            col.changeTypePercentage();
167
            col.changeAlignLeft();
1553 jmachado 168
            cols.add(col);
169
            return col;
170
        }
171
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value)
172
        {   Col col = new Col(value);
1830 jmachado 173
            col.changeTypePercentage();
174
            col.changeAlignRight();
1553 jmachado 175
            cols.add(col);
176
            return col;
177
        }
178
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value)
179
        {   Col col = new Col(value);
1830 jmachado 180
            col.changeTypePercentage();
181
            col.changeAlignCenter();
1553 jmachado 182
            cols.add(col);
183
            return col;
184
        }
1554 jmachado 185
 
186
        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
187
        {   Col col = new Col(value);
1830 jmachado 188
            col.changeTypePercentage();
189
            col.changeAlignLeft();
1554 jmachado 190
            col.setUsePercentageColor(usePercentageColors);
191
            cols.add(col);
192
            return col;
193
        }
194
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
195
        {   Col col = new Col(value);
1830 jmachado 196
            col.changeTypePercentage();
197
            col.changeAlignRight();
1554 jmachado 198
            col.setUsePercentageColor(usePercentageColors);
199
            cols.add(col);
200
            return col;
201
        }
202
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
203
        {   Col col = new Col(value);
1830 jmachado 204
            col.changeTypePercentage();
205
            col.changeAlignCenter();
1554 jmachado 206
            col.setUsePercentageColor(usePercentageColors);
207
            cols.add(col);
208
            return col;
209
        }
210
 
211
 
212
        public Col addColPercentageDefinedLeft(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent,boolean usePercentageColors)
213
        {   Col col = new Col(value);
214
            col.setPercentDefined(percent);
1830 jmachado 215
            col.changeTypePercentageDefined();
216
            col.changeAlignLeft();
1554 jmachado 217
            col.setUsePercentageColor(usePercentageColors);
218
            cols.add(col);
219
            return col;
220
        }
221
        public Col addColPercentageDefinedRight(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent,boolean usePercentageColors)
222
        {   Col col = new Col(value);
223
            col.setPercentDefined(percent);
1830 jmachado 224
            col.changeTypePercentageDefined();
225
            col.changeAlignRight();
1554 jmachado 226
            col.setUsePercentageColor(usePercentageColors);
227
            cols.add(col);
228
            return col;
229
        }
230
        public Col addColPercentageDefinedCenter(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent,boolean usePercentageColors)
231
        {   Col col = new Col(value);
232
            col.setPercentDefined(percent);
1830 jmachado 233
            col.changeTypePercentageDefined();
234
            col.changeAlignCenter();
1554 jmachado 235
            col.setUsePercentageColor(usePercentageColors);
236
            cols.add(col);
237
            return col;
238
        }
239
 
1570 jmachado 240
        public Col addColPercentageDefinedProgressCenter(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent)
241
        {   Col col = new Col(value);
242
            col.setPercentDefined(percent);
1830 jmachado 243
            col.changeTypePercentageDefinedProgress();
244
            col.changeAlignCenter();
1570 jmachado 245
            cols.add(col);
246
            return col;
247
        }
248
        public Col addColPercentageProgressCenter(1.5.0/docs/api/java/lang/String.html">String value)
249
        {   Col col = new Col(value);
1830 jmachado 250
            col.changeTypePercentageProgress();
251
            col.changeAlignCenter();
1570 jmachado 252
            cols.add(col);
253
            return col;
254
        }
1554 jmachado 255
 
1570 jmachado 256
 
1553 jmachado 257
        public Col addColLabelLeft(1.5.0/docs/api/java/lang/String.html">String value)
258
        {   Col col = new Col(value);
1830 jmachado 259
            col.changeTypeLabel();
260
            col.changeAlignLeft();
1553 jmachado 261
            cols.add(col);
262
            return col;
263
        }
264
        public Col addColLabelRight(1.5.0/docs/api/java/lang/String.html">String value)
265
        {   Col col = new Col(value);
1830 jmachado 266
            col.changeTypeLabel();
267
            col.changeAlignRight();
1553 jmachado 268
            cols.add(col);
269
            return col;
270
        }
271
        public Col addColLabelCenter(1.5.0/docs/api/java/lang/String.html">String value)
272
        {   Col col = new Col(value);
1830 jmachado 273
            col.changeTypeLabel();
274
            col.changeAlignCenter();
1553 jmachado 275
            cols.add(col);
276
            return col;
277
        }
278
 
279
 
280
 
281
        public static class Col implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
282
        {
283
            public Col(1.5.0/docs/api/java/lang/String.html">String value) {
284
                this.value = value;
285
            }
286
            public Col() {
287
            }
288
 
1569 jmachado 289
 
1553 jmachado 290
            //center, left, right
1568 jmachado 291
            1.5.0/docs/api/java/lang/String.html">String width = "";
1553 jmachado 292
            1.5.0/docs/api/java/lang/String.html">String align = null;
293
            1.5.0/docs/api/java/lang/String.html">String value = null;
1554 jmachado 294
            1.5.0/docs/api/java/lang/String.html">String percentDefined = null;
295
            //number, percentage, percentageDefined, text, label
1553 jmachado 296
            1.5.0/docs/api/java/lang/String.html">String type = null;
1554 jmachado 297
            boolean usePercentageColor = false;
298
            1.5.0/docs/api/java/lang/String.html">String backgroundColor = "";
1565 jmachado 299
            1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage = "";
1567 jmachado 300
            1.5.0/docs/api/java/lang/String.html">String fontWeight = "";
301
            int colspan = 0;
1553 jmachado 302
 
1568 jmachado 303
            public 1.5.0/docs/api/java/lang/String.html">String getWidth() {
304
                return width;
305
            }
306
            public void setWidth(1.5.0/docs/api/java/lang/String.html">String width) {
307
                this.width = width;
308
            }
309
 
1567 jmachado 310
            public 1.5.0/docs/api/java/lang/String.html">String getFontWeight() {
311
                return fontWeight;
312
            }
313
            public void setFontWeight(1.5.0/docs/api/java/lang/String.html">String fontWeight) {
314
                this.fontWeight = fontWeight;
315
            }
316
 
317
            public int getColspan() {
318
                return colspan;
319
            }
320
            public void setColspan(int colspan) {
321
                this.colspan = colspan;
322
            }
323
 
1554 jmachado 324
            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColor() {
325
                return backgroundColor;
326
            }
327
            public void setBackgroundColor(1.5.0/docs/api/java/lang/String.html">String backgroundColor) {
328
                this.backgroundColor = backgroundColor;
329
            }
330
 
1565 jmachado 331
            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColorPercentage() {
332
                return backgroundColorPercentage;
333
            }
334
            public void setBackgroundColorPercentage(1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage) {
335
                this.backgroundColorPercentage = backgroundColorPercentage;
336
            }
337
 
1830 jmachado 338
            public 1.5.0/docs/api/java/lang/String.html">String getValue() { return value; }
339
            public void setValue(1.5.0/docs/api/java/lang/String.html">String value) { this.value = value; }
1553 jmachado 340
 
1830 jmachado 341
            public 1.5.0/docs/api/java/lang/String.html">String getAlign() { return align; }
342
            public void setAlign(1.5.0/docs/api/java/lang/String.html">String align) { this.align = align; }
343
 
344
            public 1.5.0/docs/api/java/lang/String.html">String getType() { return type; }
345
            public void setType(1.5.0/docs/api/java/lang/String.html">String type) { this.type = type; }
346
 
347
            public void changeAlignLeft() { align = "left"; }
348
            public void changeAlignRight() { align = "right"; }
349
            public void changeAlignCenter() { align = "center"; }
350
            public void changeTypeNumber() { type = "number"; }
351
            public void changeTypeText() { type = "text"; }
352
            public void changeTypeInvisible() { type = "invisible"; }
353
            public void changeTypePercentage() { type = "percentage"; }
354
            public void changeTypePercentageDefined() { type = "percentageDefined"; }
355
            public void changeTypePercentageDefinedProgress() { type = "percentageDefinedProgress"; }
356
            public void changeTypePercentageProgress() { type = "percentageProgress"; }
357
            public void changeTypeLabel() { type = "label"; }
358
 
1554 jmachado 359
            public boolean isUsePercentageColor() {
360
                return usePercentageColor;
361
            }
1565 jmachado 362
            public void setUsePercentageColor(boolean usePercentageColor)
363
            {
1554 jmachado 364
                this.usePercentageColor = usePercentageColor;
1565 jmachado 365
                //new Approach for percentage color using degrade
1838 jmachado 366
                if(usePercentageColor && type != null)
1565 jmachado 367
                {
1838 jmachado 368
                    double percentage = 0;
369
                    if(type.equals("percentageDefined"))
370
                    {
371
                        if(percentDefined != null)
372
                            percentage = 1.5.0/docs/api/java/lang/Double.html">Double.parseDouble(percentDefined.replace(",","."));
373
                    }
374
                    else
375
                    {
376
                        if(value != null)
1839 jmachado 377
                            percentage = 1.5.0/docs/api/java/lang/Double.html">Double.parseDouble(value.replace(",","."));
1838 jmachado 378
                    }
1565 jmachado 379
                    percentage = percentage / 100.0;
1567 jmachado 380
                    setBackgroundColorPercentage(getColorGradientForPercentage(percentage));
1565 jmachado 381
                }
1554 jmachado 382
            }
383
 
384
 
385
            public 1.5.0/docs/api/java/lang/String.html">String getPercentDefined() {
386
                return percentDefined;
387
            }
1830 jmachado 388
            public void setPercentDefined(1.5.0/docs/api/java/lang/String.html">String percentDefined)
389
            {
390
                if(percentDefined == null)
391
                    return;
1565 jmachado 392
                if(percentDefined.contains("."))
393
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf("."));
394
                else if(percentDefined.contains(","))
395
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf(","));
1554 jmachado 396
                this.percentDefined = percentDefined;
397
            }
1553 jmachado 398
        }
399
    }
400
 
1567 jmachado 401
    /**
402
     *
403
     * @param percentageDecimal between 0 and 1
404
     * @return
405
     */
406
    public static 1.5.0/docs/api/java/lang/String.html">String getColorGradientForPercentage(double percentageDecimal)
407
    {
408
        if(percentageDecimal < 0.5)
409
        {
410
            int green = ChartBuilderUtil.getGreenComponentGradient(percentageDecimal);
411
            return "rgb(255," + green + ",0)";
412
        }
413
        else
414
        {
415
 
416
            int red = ChartBuilderUtil.getRedComponentGradient(percentageDecimal);
417
            //int red = (int) (255.0*((1.0-percentageDecimal)*2));
418
            return "rgb(" + red + ",255,0)";
419
        }
420
    }
421
 
422
 
423
 
1553 jmachado 424
    public 5+0%2Fdocs%2Fapi+Document">Document serialize() throws DocumentException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException, 1.5.0/docs/api/java/io/IOException.html">IOException {
425
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
426
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
427
        java.beans.1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder xe1 = new java.beans.1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder(out);
428
 
429
 
430
        xe1.writeObject(this);
431
        xe1.flush();
432
        xe1.close();
433
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
434
        return Dom4jUtil.toW3c(Dom4jUtil.parse(out.toString()));
435
 
436
    }
437
 
1830 jmachado 438
    public 1.5.0/docs/api/java/lang/String.html">String serializeString() throws DocumentException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException, 1.5.0/docs/api/java/io/IOException.html">IOException {
439
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
440
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
441
        java.beans.1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder xe1 = new java.beans.1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder(out);
442
 
443
 
444
        xe1.writeObject(this);
445
        xe1.flush();
446
        xe1.close();
447
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
448
        1.5.0/docs/api/java/lang/String.html">String xml = out.toString();
449
        out.close();
450
        return xml;
451
    }
452
 
453
 
454
 
455
 
456
 
457
 
458
 
459
 
460
 
461
    private static Genson genson;
462
    static{
463
        genson = new GensonBuilder()
464
                .useMethods(true)
465
                .useFields(true)
466
                .useRuntimeType(true)
467
 
468
                .create();
469
    }
470
 
471
    public static DataTable fromJson(1.5.0/docs/api/java/lang/String.html">String json)
472
    {
473
        return genson.deserialize(json,DataTable.class);
474
    }
475
 
476
 
477
 
478
    public List<String> getJsonExcludedProperties() {
479
        return null;
480
    }
481
 
482
    /**
483
     * Generic Json Object only for local class methods
484
     * @return
485
     * @throws org.json.JSONException
486
     */
487
    public JSONObject toJsonObject() throws JSONException
488
    {
489
        return new JSONObject(toJson());
490
    }
491
 
492
    /**
493
     * * Generic Json Object only for local class methods
494
     * @return
495
     * @throws java.io.IOException
496
     */
497
    public 1.5.0/docs/api/java/lang/String.html">String toJson()
498
    {
499
        return genson.serialize(this);
500
    }
501
 
1553 jmachado 502
}