示例#1
0
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        Organization org        = Organization.PPSE;
        Geography    geo        = this.DropGeographies.SelectedGeography;
        Election     election   = Election.September2010;
        string       ballotName = this.TextName.Text;
        MeetingElectionCandidates candidates =
            MeetingElection.FromIdentity(Int32.Parse(this.DropPolls.SelectedValue)).Candidates;

        Ballot ballot = Ballot.Create(election, org, geo, ballotName, 0, string.Empty);

        Dictionary <string, int> nameLookup = new Dictionary <string, int>();

        foreach (MeetingElectionCandidate candidate in candidates)
        {
            nameLookup[candidate.Person.Name.ToLowerInvariant()] = candidate.PersonId;
        }

        string[] candidateNames = this.TextCandidates.Text.Split('\r', '\n');

        foreach (string candidateNameSource in candidateNames)
        {
            int personId = 0;

            int idStartIndex = candidateNameSource.LastIndexOf("(#");

            if (idStartIndex > 0)
            {
                string identityString = candidateNameSource.Substring(idStartIndex + 2);
                int    idEndIndex     = identityString.IndexOf(")");

                identityString = identityString.Substring(0, idEndIndex);

                personId = Int32.Parse(identityString);
            }

            if (personId == 0)
            {
                // Derive from name

                string candidateName = candidateNameSource;
                int    parenIndex    = candidateName.IndexOfAny(new char[] { '(', ',' });
                if (parenIndex >= 0)
                {
                    candidateName = candidateName.Substring(0, parenIndex);
                }
                candidateName = candidateName.ToLowerInvariant().Trim();

                if (candidateName.Length < 1)
                {
                    continue;
                }

                personId = nameLookup[candidateName];
            }

            ballot.AddCandidate(Person.FromIdentity(personId));
        }

        Page.ClientScript.RegisterStartupScript(typeof(Page), "OkMessage", @"alert ('Valsedel #" + ballot.Identity + " registrerades.');", true);
    }