KpiEvaluation.cs 4.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using iText.Kernel.Colors;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using PDFGenerator.Models;
using PDFGenerator.Utilities;
using System.Collections.Generic;
using KPIEvaluation = PDFGenerator.Models.KpiEvaluation;

namespace PDFGenerator.EvaluationReport
{
    public class KpiEvaluation : BaseSection
    {
        private KPIEvaluation Evaluation { get; }
        private Table Table { get; set; }
        private Dictionary<string, byte[]> CustomUploads { get; set; }


        public KpiEvaluation(KPIEvaluation kpiEvaluation, Dictionary<string, byte[]> customUploads, Document document,
            Bookmarks bookmarks, TableOfContents tableOfContents) : base(document, bookmarks, tableOfContents)
        {
            Evaluation = kpiEvaluation;
            CustomUploads = customUploads;
        }

        /// <summary>
Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
27
        /// Renders the content regarding a single kpi and iterates through its reports.
28
29
30
31
32
33
34
35
36
37
        /// </summary>
        public override void Render()
        {
            Style style = Styles.H4;

            if (Evaluation.KpiMandatoryTypeE)
            {
                style.SetFontColor(new DeviceRgb(156, 158, 159));
            }

38
39
            Table = new Table(1, false)
            .UseAllAvailableWidth()
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            .SetKeepTogether(false);

            AddParagraphWithLeadingToTable(Table, Evaluation.TableHeader, true, style);

            if (!Evaluation.KpiMandatoryTypeE)
            {
                if (Evaluation.EvaluatedKpis != null)
                {
                    AddKpiIsReachedSection(Evaluation.EvaluatedKpis);
                    AddKpiCommentSection(Evaluation.EvaluatedKpis);
                }

                Document.Add(Table);

                if (Evaluation.KpiEvaluationReports != null)
                {
                    new KpiEvaluationReports(Evaluation.KpiEvaluationReports, CustomUploads, Document, Bookmarks, TableOfContents)
                    .Render();
                }

                return;
            }

            Document.Add(Table);
        }

        /// <summary>
        /// Adds content about if the kpi is reached or not.
        /// </summary>
Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
69
        /// <param name="evaluatedKpis">Containing information for each available course.</param>
70
71
72
73
74
75
        private void AddKpiIsReachedSection(List<EvaluatedKpi> evaluatedKpis)
        {
            evaluatedKpis.ForEach(item =>
            {
                if (!string.IsNullOrEmpty(item.Paragraph))
                {
76
77
                    Cell cell = new Cell().SetBorder(Border.NO_BORDER);
                    DeviceRgb color;
78
79
                    if (item.IsReached)
                    {
80
                        color = new DeviceRgb(221, 235, 206);
81
82
83
                    }
                    else
                    {
84
                        color = new DeviceRgb(249, 210, 218);
85
                    }
86

Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
87
                    // avoid that existent multiple whitespaces after a line break are ignored by iText7
88
89
90
                    Table panel = Panel.GetPanel(new Paragraph(new Text(PreserveLeadingSpaces(item.Paragraph)).AddStyle(Styles.Paragraph)), color);
                    cell.Add(panel);
                    Table.AddCell(cell);
91
92
93
94
95
96
97
                }
            });
        }

        /// <summary>
        /// Adds content about the kpi comment.
        /// </summary>
Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
98
        /// <param name="evaluatedKpis">Contains information for each available course.</param>
99
100
101
102
103
104
105
106
107
108
109
110
111
        private void AddKpiCommentSection(List<EvaluatedKpi> evaluatedKpis)
        {
            evaluatedKpis.ForEach(item =>
            {
                if (!string.IsNullOrEmpty(item.Comment))
                {
                    Cell cell = new Cell().SetBorder(Border.NO_BORDER).SetPaddingTop(5f);

                    if (!string.IsNullOrEmpty(item.CourseName))
                    {
                        cell.Add(ParagraphFactory.GetCustomParagraph(item.CourseName, Styles.H5, 0, 5, 1.15f));
                    }

112
                    cell.Add(ParagraphFactory.GetStandardParagraph(PreserveLeadingSpaces(item.Comment)));
113
114
115
116
117
118
                    Table.AddCell(cell);
                }
            });
        }
    }
}