Subversion Repositories bacoAlunos

Rev

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

package pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils;

import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import org.dom4j.DocumentException;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import pt.estgp.estgweb.domain.utils.JsonView;
import pt.estgp.estgweb.utils.Dom4jUtil;

import javax.xml.transform.TransformerException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jorgemachado on 11/11/16.
 */

public class DataTable implements 1.5.0/docs/api/java/io/Serializable.html">Serializable, JsonView
{



    public DataTable() {

        setRows(new ArrayList<Row>());
    }


    private ArrayList<Row> rows;

    public Row addRow()
    {
        Row row = new Row();
        rows.add(row);
        return row;
    }
    public Row addRowHeader()
    {
        Row row = new Row();
        row.changeType2Header();
        rows.add(row);
        return row;
    }
    public Row addRowNormal()
    {
        Row row = new Row();
        row.changeType2Normal();
        rows.add(row);
        return row;
    }
    public Row addRowFooter()
    {
        Row row = new Row();
        row.changeType2Footer();
        rows.add(row);
        return row;
    }

    public ArrayList<Row> getRows() { return rows; }

    public void setRows(ArrayList<Row> rows) {
        this.rows = rows;
    }

    public static class Row implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
    {
        //header, footer, normal
        1.5.0/docs/api/java/lang/String.html">String type = null;
        boolean invisible = false;
        private ArrayList<Col> cols;


        public Row() {
            setCols(new ArrayList<Col>());
        }

        public boolean isInvisible() {
            return invisible;
        }
        public void setInvisible(boolean invisible) {
            this.invisible = invisible;
        }

        public void setCols(ArrayList<Col> cols) {
            this.cols = cols;
        }
        public ArrayList<Col> getCols() { return cols; }

        public 1.5.0/docs/api/java/lang/String.html">String getType() { return type; }
        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
            this.type = type;
        }


        private void changeType2Normal() { type = "normal"; }
        private void changeType2Header() { type = "header"; }
        private void changeType2Footer() { type = "footer"; }



        public Col addCol()
        {
            Col col = new Col();
            cols.add(col);
            return col;
        }

        public Col addColNumberLeft(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeNumber();
            col.changeAlignLeft();
            cols.add(col);
            return col;
        }
        public Col addColNumberRight(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeNumber();
            col.changeAlignRight();
            cols.add(col);
            return col;
        }
        public Col addColNumberCenter(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeNumber();
            col.changeAlignCenter();
            cols.add(col);
            return col;
        }

        public Col addColTextLeft(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeText();
            col.changeAlignLeft();
            cols.add(col);
            return col;
        }
        public Col addColTextRight(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeText();
            col.changeAlignRight();
            cols.add(col);
            return col;
        }

        public Col addColTextCenter(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeText();
            col.changeAlignCenter();
            cols.add(col);
            return col;
        }

        public Col addColInvisible()
        {   Col col = new Col();
            col.changeTypeInvisible();
            cols.add(col);
            return col;
        }

        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypePercentage();
            col.changeAlignLeft();
            cols.add(col);
            return col;
        }
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypePercentage();
            col.changeAlignRight();
            cols.add(col);
            return col;
        }
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypePercentage();
            col.changeAlignCenter();
            cols.add(col);
            return col;
        }

        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
        {   Col col = new Col(value);
            col.changeTypePercentage();
            col.changeAlignLeft();
            col.setUsePercentageColor(usePercentageColors);
            cols.add(col);
            return col;
        }
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
        {   Col col = new Col(value);
            col.changeTypePercentage();
            col.changeAlignRight();
            col.setUsePercentageColor(usePercentageColors);
            cols.add(col);
            return col;
        }
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
        {   Col col = new Col(value);
            col.changeTypePercentage();
            col.changeAlignCenter();
            col.setUsePercentageColor(usePercentageColors);
            cols.add(col);
            return col;
        }


        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)
        {   Col col = new Col(value);
            col.setPercentDefined(percent);
            col.changeTypePercentageDefined();
            col.changeAlignLeft();
            col.setUsePercentageColor(usePercentageColors);
            cols.add(col);
            return col;
        }
        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)
        {   Col col = new Col(value);
            col.setPercentDefined(percent);
            col.changeTypePercentageDefined();
            col.changeAlignRight();
            col.setUsePercentageColor(usePercentageColors);
            cols.add(col);
            return col;
        }
        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)
        {   Col col = new Col(value);
            col.setPercentDefined(percent);
            col.changeTypePercentageDefined();
            col.changeAlignCenter();
            col.setUsePercentageColor(usePercentageColors);
            cols.add(col);
            return col;
        }

        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)
        {   Col col = new Col(value);
            col.setPercentDefined(percent);
            col.changeTypePercentageDefinedProgress();
            col.changeAlignCenter();
            cols.add(col);
            return col;
        }
        public Col addColPercentageProgressCenter(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypePercentageProgress();
            col.changeAlignCenter();
            cols.add(col);
            return col;
        }


        public Col addColLabelLeft(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeLabel();
            col.changeAlignLeft();
            cols.add(col);
            return col;
        }
        public Col addColLabelRight(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeLabel();
            col.changeAlignRight();
            cols.add(col);
            return col;
        }
        public Col addColLabelCenter(1.5.0/docs/api/java/lang/String.html">String value)
        {   Col col = new Col(value);
            col.changeTypeLabel();
            col.changeAlignCenter();
            cols.add(col);
            return col;
        }



        public static class Col implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
        {
            public Col(1.5.0/docs/api/java/lang/String.html">String value) {
                this.value = value;
            }
            public Col() {
            }


            //center, left, right
            1.5.0/docs/api/java/lang/String.html">String width = "";
            1.5.0/docs/api/java/lang/String.html">String align = null;
            1.5.0/docs/api/java/lang/String.html">String value = null;
            1.5.0/docs/api/java/lang/String.html">String percentDefined = null;
            //number, percentage, percentageDefined, text, label
            1.5.0/docs/api/java/lang/String.html">String type = null;
            boolean usePercentageColor = false;
            1.5.0/docs/api/java/lang/String.html">String backgroundColor = "";
            1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage = "";
            1.5.0/docs/api/java/lang/String.html">String fontWeight = "";
            int colspan = 0;

            public 1.5.0/docs/api/java/lang/String.html">String getWidth() {
                return width;
            }
            public void setWidth(1.5.0/docs/api/java/lang/String.html">String width) {
                this.width = width;
            }

            public 1.5.0/docs/api/java/lang/String.html">String getFontWeight() {
                return fontWeight;
            }
            public void setFontWeight(1.5.0/docs/api/java/lang/String.html">String fontWeight) {
                this.fontWeight = fontWeight;
            }

            public int getColspan() {
                return colspan;
            }
            public void setColspan(int colspan) {
                this.colspan = colspan;
            }

            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColor() {
                return backgroundColor;
            }
            public void setBackgroundColor(1.5.0/docs/api/java/lang/String.html">String backgroundColor) {
                this.backgroundColor = backgroundColor;
            }

            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColorPercentage() {
                return backgroundColorPercentage;
            }
            public void setBackgroundColorPercentage(1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage) {
                this.backgroundColorPercentage = backgroundColorPercentage;
            }

            public 1.5.0/docs/api/java/lang/String.html">String getValue() { return value; }
            public void setValue(1.5.0/docs/api/java/lang/String.html">String value) { this.value = value; }

            public 1.5.0/docs/api/java/lang/String.html">String getAlign() { return align; }
            public void setAlign(1.5.0/docs/api/java/lang/String.html">String align) { this.align = align; }

            public 1.5.0/docs/api/java/lang/String.html">String getType() { return type; }
            public void setType(1.5.0/docs/api/java/lang/String.html">String type) { this.type = type; }

            public void changeAlignLeft() { align = "left"; }
            public void changeAlignRight() { align = "right"; }
            public void changeAlignCenter() { align = "center"; }
            public void changeTypeNumber() { type = "number"; }
            public void changeTypeText() { type = "text"; }
            public void changeTypeInvisible() { type = "invisible"; }
            public void changeTypePercentage() { type = "percentage"; }
            public void changeTypePercentageDefined() { type = "percentageDefined"; }
            public void changeTypePercentageDefinedProgress() { type = "percentageDefinedProgress"; }
            public void changeTypePercentageProgress() { type = "percentageProgress"; }
            public void changeTypeLabel() { type = "label"; }

            public boolean isUsePercentageColor() {
                return usePercentageColor;
            }
            public void setUsePercentageColor(boolean usePercentageColor)
            {
                this.usePercentageColor = usePercentageColor;
                //new Approach for percentage color using degrade
                if(usePercentageColor && type != null && percentDefined != null && value != null)
                {
                    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(",","."));
                    percentage = percentage / 100.0;
                    setBackgroundColorPercentage(getColorGradientForPercentage(percentage));
                }
            }


            public 1.5.0/docs/api/java/lang/String.html">String getPercentDefined() {
                return percentDefined;
            }
            public void setPercentDefined(1.5.0/docs/api/java/lang/String.html">String percentDefined)
            {
                if(percentDefined == null)
                    return;
                if(percentDefined.contains("."))
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf("."));
                else if(percentDefined.contains(","))
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf(","));
                this.percentDefined = percentDefined;
            }
        }
    }

    /**
     *
     * @param percentageDecimal between 0 and 1
     * @return
     */

    public static 1.5.0/docs/api/java/lang/String.html">String getColorGradientForPercentage(double percentageDecimal)
    {
        if(percentageDecimal < 0.5)
        {
            int green = ChartBuilderUtil.getGreenComponentGradient(percentageDecimal);
            return "rgb(255," + green + ",0)";
        }
        else
        {

            int red = ChartBuilderUtil.getRedComponentGradient(percentageDecimal);
            //int red = (int) (255.0*((1.0-percentageDecimal)*2));
            return "rgb(" + red + ",255,0)";
        }
    }



    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 {
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
        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);


        xe1.writeObject(this);
        xe1.flush();
        xe1.close();
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
        return Dom4jUtil.toW3c(Dom4jUtil.parse(out.toString()));

    }

    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 {
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
        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);


        xe1.writeObject(this);
        xe1.flush();
        xe1.close();
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
        1.5.0/docs/api/java/lang/String.html">String xml = out.toString();
        out.close();
        return xml;
    }









    private static Genson genson;
    static{
        genson = new GensonBuilder()
                .useMethods(true)
                .useFields(true)
                .useRuntimeType(true)

                .create();
    }

    public static DataTable fromJson(1.5.0/docs/api/java/lang/String.html">String json)
    {
        return genson.deserialize(json,DataTable.class);
    }



    public List<String> getJsonExcludedProperties() {
        return null;
    }

    /**
     * Generic Json Object only for local class methods
     * @return
     * @throws org.json.JSONException
     */

    public JSONObject toJsonObject() throws JSONException
    {
        return new JSONObject(toJson());
    }

    /**
     * * Generic Json Object only for local class methods
     * @return
     * @throws java.io.IOException
     */

    public 1.5.0/docs/api/java/lang/String.html">String toJson()
    {
        return genson.serialize(this);
    }

}