Subversion Repositories bacoAlunos

Rev

Rev 1574 | 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
1830 jmachado 366
                if(usePercentageColor && type != null && percentDefined != null && value != null)
1565 jmachado 367
                {
368
                    double percentage = type.equals("percentageDefined") ? 1.5.0/docs/api/java/lang/Double.html">Double.parseDouble(percentDefined.replace(",",".")) : 1.5.0/docs/api/java/lang/Double.html">Double.parseDouble(value.replace(",","."));
369
                    percentage = percentage / 100.0;
1567 jmachado 370
                    setBackgroundColorPercentage(getColorGradientForPercentage(percentage));
1565 jmachado 371
                }
1554 jmachado 372
            }
373
 
374
 
375
            public 1.5.0/docs/api/java/lang/String.html">String getPercentDefined() {
376
                return percentDefined;
377
            }
1830 jmachado 378
            public void setPercentDefined(1.5.0/docs/api/java/lang/String.html">String percentDefined)
379
            {
380
                if(percentDefined == null)
381
                    return;
1565 jmachado 382
                if(percentDefined.contains("."))
383
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf("."));
384
                else if(percentDefined.contains(","))
385
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf(","));
1554 jmachado 386
                this.percentDefined = percentDefined;
387
            }
1553 jmachado 388
        }
389
    }
390
 
1567 jmachado 391
    /**
392
     *
393
     * @param percentageDecimal between 0 and 1
394
     * @return
395
     */
396
    public static 1.5.0/docs/api/java/lang/String.html">String getColorGradientForPercentage(double percentageDecimal)
397
    {
398
        if(percentageDecimal < 0.5)
399
        {
400
            int green = ChartBuilderUtil.getGreenComponentGradient(percentageDecimal);
401
            return "rgb(255," + green + ",0)";
402
        }
403
        else
404
        {
405
 
406
            int red = ChartBuilderUtil.getRedComponentGradient(percentageDecimal);
407
            //int red = (int) (255.0*((1.0-percentageDecimal)*2));
408
            return "rgb(" + red + ",255,0)";
409
        }
410
    }
411
 
412
 
413
 
1553 jmachado 414
    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 {
415
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
416
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
417
        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);
418
 
419
 
420
        xe1.writeObject(this);
421
        xe1.flush();
422
        xe1.close();
423
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
424
        return Dom4jUtil.toW3c(Dom4jUtil.parse(out.toString()));
425
 
426
    }
427
 
1830 jmachado 428
    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 {
429
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
430
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
431
        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);
432
 
433
 
434
        xe1.writeObject(this);
435
        xe1.flush();
436
        xe1.close();
437
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
438
        1.5.0/docs/api/java/lang/String.html">String xml = out.toString();
439
        out.close();
440
        return xml;
441
    }
442
 
443
 
444
 
445
 
446
 
447
 
448
 
449
 
450
 
451
    private static Genson genson;
452
    static{
453
        genson = new GensonBuilder()
454
                .useMethods(true)
455
                .useFields(true)
456
                .useRuntimeType(true)
457
 
458
                .create();
459
    }
460
 
461
    public static DataTable fromJson(1.5.0/docs/api/java/lang/String.html">String json)
462
    {
463
        return genson.deserialize(json,DataTable.class);
464
    }
465
 
466
 
467
 
468
    public List<String> getJsonExcludedProperties() {
469
        return null;
470
    }
471
 
472
    /**
473
     * Generic Json Object only for local class methods
474
     * @return
475
     * @throws org.json.JSONException
476
     */
477
    public JSONObject toJsonObject() throws JSONException
478
    {
479
        return new JSONObject(toJson());
480
    }
481
 
482
    /**
483
     * * Generic Json Object only for local class methods
484
     * @return
485
     * @throws java.io.IOException
486
     */
487
    public 1.5.0/docs/api/java/lang/String.html">String toJson()
488
    {
489
        return genson.serialize(this);
490
    }
491
 
1553 jmachado 492
}