コード例 #1
0
ファイル: ReportBuilder.cs プロジェクト: xoposhiy/KOIB
        private List <BasePlainElement> CreateProtocolBodyTable(
            ProtocolText protocolTemplate,
            Election election,
            bool final,
            bool printResults)
        {
            int    lineNumber   = election.Protocol.GetLatestLineNumber(final);
            string disabled     = election.Protocol.DisabledString;
            bool   showDisabled = !string.IsNullOrEmpty(disabled);
            bool   delimiter    = false;
            var    table        = new List <BasePlainElement>();

            foreach (var voteLine in protocolTemplate.VoteLines)
            {
                var mask = new VoteKey {
                    ElectionNum = election.ElectionId
                };
                switch (voteLine.Type)
                {
                case VoteLineType.Vote:
                    foreach (Candidate currentCand in election.Candidates)
                    {
                        if ((currentCand.Id == voteLine.ID) && (!currentCand.Disabled || showDisabled))
                        {
                            lineNumber++;
                            mask.CandidateId = currentCand.Id;
                            int votesCount = _votingResultManager.VotingResults.VotesCount(mask);
                            table.Add(new LineClause(
                                          new[]
                            {
                                lineNumber.ToString(),
                                currentCand.GetInitials(!currentCand.NoneAbove),
                                currentCand.Disabled ? disabled : (printResults ? votesCount.ToString() : "0"),
                                currentCand.Disabled ? "" : (printResults
                                        ? "(" + CustomRusNumber.Str(votesCount, true).Trim() + ")"
                                        : CustomRusNumber.Str(0, true).Trim())
                            },
                                          voteLine.FontSize, voteLine.Bold, voteLine.Italic, delimiter));
                            delimiter = false;
                            break;
                        }
                    }
                    break;

                case VoteLineType.Line:
                    if (string.CompareOrdinal(voteLine.ID, VoteTextLine.TOTAL_RECEIVED_VOTETEXTLINE_ID) == 0)
                    {
                        var value = _votingResultManager.VotingResults.VotesCount(
                            new VoteKey(
                                BlankType.AllButBad,
                                null, null, null, null,
                                _electionManager.SourceData.GetBlankIdByElectionNumber(election.ElectionId)));
                        var text = string.IsNullOrEmpty(voteLine.Text)
                                           ? VoteTextLine.TOTAL_RECEIVED_VOTETEXTLINE_DEFAULT_TEXT
                                           : voteLine.Text;
                        table.Add(new LineClause(
                                      new[]
                        {
                            "",
                            text,
                            printResults ? value.ToString() : "0",
                            printResults
                                                      ? "(" + CustomRusNumber.Str(value, true).Trim() + ")"
                                                      : CustomRusNumber.Str(0, true).Trim()
                        },
                                      voteLine.FontSize, voteLine.Bold, voteLine.Italic, delimiter));
                    }
                    else
                    {
                        foreach (Line currentLine in election.Protocol.Lines)
                        {
                            if (currentLine.Id == voteLine.ID)
                            {
                                int value = final && currentLine.Value.HasValue ? currentLine.Value.Value : 0;
                                table.Add(new LineClause(
                                              new[]
                                {
                                    currentLine.Num + currentLine.AdditionalNum,
                                    currentLine.Name,
                                    printResults ? value.ToString() : "0",
                                    printResults
                                                              ? "(" + CustomRusNumber.Str(value, true).Trim() + ")"
                                                              : CustomRusNumber.Str(0, true).Trim()
                                },
                                              voteLine.FontSize, voteLine.Bold, voteLine.Italic, delimiter));
                                break;
                            }
                        }
                    }
                    delimiter = false;
                    break;

                case VoteLineType.Delimiter:
                    delimiter = true;
                    break;
                }
            }
            return(table);
        }
コード例 #2
0
        private string FormatObject(object obj, string[] objComponents)
        {
            var fmtString = objComponents.Length > 1 ? objComponents[1] : "";

            if (obj == null)
            {
                throw new ArgumentNullException(
                          "obj",
                          string.Format("Объект '{0}' не определен. Строка формата = '{1}'", objComponents[0], fmtString));
            }
            if (obj.GetType().IsEnum)
            {
                foreach (var attribute in obj.GetType().GetField(obj.ToString()).GetCustomAttributes(true))
                {
                    if (attribute is PresentationForReportAttribute)
                    {
                        return((attribute as PresentationForReportAttribute).DisplayName);
                    }
                }
                return(obj.ToString());
            }
            if (obj is bool)
            {
                return((bool)obj ? "Да" : "Нет");
            }
            string objectPresentation;

            if (fmtString.Length > 0)
            {
                var fmtParts = fmtString.Split('#');
                if (fmtParts[0].Length > 0)
                {
                    var methodInfo = obj.GetType().GetMethod("ToString", new[] { typeof(string) });
                    if (methodInfo == null)
                    {
                        objectPresentation = obj.ToString();
                    }
                    else
                    {
                        if (obj is DateTime && PlatformDetector.IsUnix && fmtParts[0].Contains("MMMM"))
                        {
                            objectPresentation =
                                ((DateTime)obj).ToString(fmtParts[0], new System.Globalization.CultureInfo("ru-RU"));
                            objectPresentation = DataConvert(objectPresentation);
                        }
                        else
                        {
                            var components = _reFmtVariable.Match(fmtParts[0]);
                            while (components.Success)
                            {
                                fmtParts[0] = fmtParts[0].Replace(
                                    components.Groups[0].Value,
                                    GetVariable(components.Groups[1].Value).ToString());
                                components = components.NextMatch();
                            }
                            objectPresentation = methodInfo.Invoke(obj, new object[] { fmtParts[0] }).ToString();
                        }
                    }
                }
                else
                {
                    objectPresentation = obj.ToString();
                }
                if (fmtParts.Length > 1 && fmtParts[1].Length > 0)
                {
                    switch (fmtParts[1][0])
                    {
                    case 'u':
                        return(objectPresentation.ToUpper());

                    case 'l':
                        return(objectPresentation.ToLower());

                    case 'w':
                        int value;
                        if (Int32.TryParse(objectPresentation, out value))
                        {
                            return(CustomRusNumber.Str(value, true).Trim());
                        }
                        break;
                    }
                }
            }
            else
            {
                objectPresentation = obj.ToString();
            }
            return(objectPresentation);
        }