Ejemplo n.º 1
0
 public static string StreetOrEmpty(this PostalAddress address)
 {
     return((address == null) ? string.Empty : address.Street.Value);
 }
Ejemplo n.º 2
0
 public static string CareOfOrEmpty(this PostalAddress address)
 {
     return((address == null) ? string.Empty : address.CareOf.Value);
 }
Ejemplo n.º 3
0
 public static string PlaceOrEmpty(this PostalAddress address)
 {
     return((address == null) ? string.Empty : address.Place.Value);
 }
Ejemplo n.º 4
0
 public static string PostOfficeBoxOrEmpty(this PostalAddress address)
 {
     return((address == null) ? string.Empty : address.PostOfficeBox.Value);
 }
Ejemplo n.º 5
0
        public static string StateOrEmpty(this PostalAddress address, Translator translator)
        {
            var state = (address == null) ? null : address.State.Value;

            return((state == null) ? string.Empty : state.Name.Value[translator.Language]);
        }
Ejemplo n.º 6
0
 public static string CountryOrEmpty(this PostalAddress address, Translator translator)
 {
     return((address == null) ? string.Empty : address.Country.Value.Name.Value[translator.Language]);
 }
        public PostalAddressEdit()
        {
            this.RequiresAuthentication();

            Get["/postaladdress/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    address  = Database.Query <PostalAddress>(idString);

                if (address != null)
                {
                    if (HasAccess(address.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        return(View["View/postaladdressedit.sshtml",
                                    new PostalAddressEditViewModel(Translator, Database, address)]);
                    }
                }

                return(null);
            };
            Post["/postaladdress/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <PostalAddressEditViewModel>(ReadBody());
                var    address  = Database.Query <PostalAddress>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(address))
                {
                    if (status.HasAccess(address.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        status.AssignStringFree("CareOf", address.CareOf, model.CareOf);
                        status.AssignStringFree("Street", address.Street, model.Street);
                        status.AssignStringFree("PostOfficeBox", address.PostOfficeBox, model.PostOfficeBox);
                        status.AssignStringFree("CareOf", address.PostalCode, model.PostalCode);
                        status.AssignStringFree("PostalCode", address.Place, model.Place);
                        status.AssignObjectIdString("State", address.State, model.State);
                        status.AssignObjectIdString("Country", address.Country, model.Country);

                        if (status.IsSuccess)
                        {
                            Database.Save(address);
                            Journal(address.Contact,
                                    "PostalAddress.Journal.Edit",
                                    "Journal entry changed postal address",
                                    "Changed postal address {0}",
                                    t => address.GetText(t));
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/postaladdress/add/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    contact  = Database.Query <Contact>(idString);

                if (contact != null)
                {
                    if (HasAccess(contact, PartAccess.Contact, AccessRight.Write))
                    {
                        return(View["View/postaladdressedit.sshtml",
                                    new PostalAddressEditViewModel(Translator, Database, contact)]);
                    }
                }

                return(null);
            };
            Post["/postaladdress/add/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <PostalAddressEditViewModel>(ReadBody());
                var    contact  = Database.Query <Contact>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(contact))
                {
                    if (status.HasAccess(contact, PartAccess.Contact, AccessRight.Write))
                    {
                        var address = new PostalAddress(Guid.NewGuid());
                        status.AssignStringFree("CareOf", address.CareOf, model.CareOf);
                        status.AssignStringFree("Street", address.Street, model.Street);
                        status.AssignStringFree("PostOfficeBox", address.PostOfficeBox, model.PostOfficeBox);
                        status.AssignStringFree("CareOf", address.PostalCode, model.PostalCode);
                        status.AssignStringFree("PostalCode", address.Place, model.Place);
                        status.AssignObjectIdString("State", address.State, model.State);
                        status.AssignObjectIdString("Country", address.Country, model.Country);
                        address.Precedence.Value = contact.PostalAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
                        address.Contact.Value    = contact;

                        if (status.IsSuccess)
                        {
                            Database.Save(address);
                            Journal(address.Contact,
                                    "PostalAddress.Journal.Add",
                                    "Journal entry adding postal address",
                                    "Added postal address {0}",
                                    t => address.GetText(t));
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/postaladdress/delete/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    address  = Database.Query <PostalAddress>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(address))
                {
                    if (status.HasAccess(address.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        address.Delete(Database);
                        Journal(address.Contact,
                                "PostalAddress.Journal.Delete",
                                "Journal entry deleting postal addresses",
                                "Deleted postal address {0}",
                                t => address.GetText(t));
                    }
                }

                return(status.CreateJsonData());
            };
            Post["/postaladdress/switch"] = parameters =>
            {
                var model  = JsonConvert.DeserializeObject <SwitchViewModel>(ReadBody());
                var source = Database.Query <PostalAddress>(model.SourceId);
                var status = CreateStatus();

                if (status.ObjectNotNull(source))
                {
                    if (Guid.TryParse(model.TargetId, out Guid targetId))
                    {
                        if (status.HasAccess(source.Contact.Value, PartAccess.Contact, AccessRight.Write))
                        {
                            var target = source.Contact.Value.PostalAddresses
                                         .FirstOrDefault(a => a.Id.Equals(targetId));

                            if (status.ObjectNotNull(target))
                            {
                                var sourcePrecedence = source.Precedence.Value;
                                var targetPrecedence = target.Precedence.Value;
                                source.Precedence.Value = targetPrecedence;
                                target.Precedence.Value = sourcePrecedence;

                                if (source.Dirty || target.Dirty)
                                {
                                    Database.Save(source.Contact);
                                    Journal(source.Contact,
                                            "PostalAddress.Journal.Switch",
                                            "Journal entry switching two postal addresses",
                                            "Changed precedence between postal addresses {0} and {1}",
                                            t => source.GetText(t),
                                            t => target.GetText(t));
                                }
                            }
                        }
                    }
                    else
                    {
                        status.SetErrorNotFound();
                    }
                }

                return(status.CreateJsonData());
            };
        }
Ejemplo n.º 8
0
        private Contact CreateContact(int number)
        {
            var female          = _rnd.Next(2) == 1;
            int middleNameCount = _rnd.Next(5);

            if (middleNameCount > 2)
            {
                middleNameCount = 0;
            }

            var contact = new Contact(Guid.NewGuid());

            contact.Language.Value = SelectLanguage();
            contact.LastName.Value = LastNames.Skip(_rnd.Next(LastNames.Count())).First();

            if (female)
            {
                contact.FirstName.Value = FemaleFirstNames.Skip(_rnd.Next(FemaleFirstNames.Count())).First();
                var middleNames = new List <string>();
                for (int i = 0; i < middleNameCount; i++)
                {
                    middleNames.Add(FemaleFirstNames.Skip(_rnd.Next(FemaleFirstNames.Count())).First());
                }
                contact.MiddleNames.Value = string.Join(" ", middleNames);
            }
            else
            {
                contact.FirstName.Value = MaleFirstNames.Skip(_rnd.Next(MaleFirstNames.Count())).First();
                var middleNames = new List <string>();
                for (int i = 0; i < middleNameCount; i++)
                {
                    middleNames.Add(MaleFirstNames.Skip(_rnd.Next(MaleFirstNames.Count())).First());
                }
                contact.MiddleNames.Value = string.Join(" ", middleNames);
            }

            contact.BirthDate.Value    = new DateTime(1960, 1, 1).AddDays(_rnd.NextDouble() * 40d * 365d);
            contact.Organization.Value = "New Organization";

            var homeMail = new ServiceAddress(Guid.NewGuid());

            homeMail.Service.Value    = ServiceType.EMail;
            homeMail.Category.Value   = AddressCategory.Home;
            homeMail.Precedence.Value = contact.ServiceAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
            homeMail.Address.Value    =
                "stefan+" +
                contact.FirstName.Value.ToLowerInvariant() + "." +
                contact.LastName.Value.ToLowerInvariant() + "@savvy.ch";
            homeMail.Contact.Value = contact;

            if (_rnd.Next(3) != 0)
            {
                var mobile = new ServiceAddress(Guid.NewGuid());
                mobile.Service.Value    = ServiceType.Phone;
                mobile.Category.Value   = AddressCategory.Mobile;
                mobile.Precedence.Value = contact.ServiceAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
                mobile.Address.Value    =
                    MobilePrefix.Skip(_rnd.Next(MobilePrefix.Count())).First() +
                    ComposeNumber();
                mobile.Contact.Value = contact;
            }

            if (_rnd.Next(3) == 0)
            {
                var phone = new ServiceAddress(Guid.NewGuid());
                phone.Service.Value    = ServiceType.Phone;
                phone.Category.Value   = AddressCategory.Mobile;
                phone.Precedence.Value = contact.ServiceAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
                phone.Address.Value    =
                    PhonePrefix.Skip(_rnd.Next(PhonePrefix.Count())).First() +
                    ComposeNumber();
                phone.Contact.Value = contact;
            }

            var place         = Places.Skip(_rnd.Next(Places.Count())).First();
            var postalAddress = new PostalAddress(Guid.NewGuid());

            postalAddress.Country.Value    = GetCountry("Schweiz");
            postalAddress.State.Value      = GetState(place.Item3);
            postalAddress.PostalCode.Value = place.Item1.ToString();
            postalAddress.Place.Value      = place.Item2;
            postalAddress.Street.Value     =
                Streets.Skip(_rnd.Next(Streets.Count())).First() +
                " " + (_rnd.Next(23) + 1).ToString();
            postalAddress.Precedence.Value = contact.PostalAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
            postalAddress.Contact.Value    = contact;

            var subscription = new Subscription(Guid.NewGuid());

            subscription.Contact.Value   = contact;
            subscription.Feed.Value      = GetFeed("Piratenpartei Zentralschweiz", null);
            subscription.StartDate.Value = new DateTime(2018, 12, 18);

            switch (_rnd.Next(4))
            {
            case 0:
                var subscription2 = new Subscription(Guid.NewGuid());
                subscription2.Contact.Value   = contact;
                subscription2.Feed.Value      = GetFeed("Piratenpartei Zug", "Piratenpartei Zentralschweiz");
                subscription2.StartDate.Value = new DateTime(2018, 12, 18);
                break;

            case 1:
                var subscription3 = new Subscription(Guid.NewGuid());
                subscription3.Contact.Value   = contact;
                subscription3.Feed.Value      = GetFeed("Piratenpartei Luzern", "Piratenpartei Zentralschweiz");
                subscription3.StartDate.Value = new DateTime(2018, 12, 18);
                break;
            }

            var tagAssignment1 = new TagAssignment(Guid.NewGuid());

            tagAssignment1.Tag.Value     = GetTag("Partizipationsmails", TagUsage.Mailing, TagMode.Default | TagMode.Manual);
            tagAssignment1.Contact.Value = contact;

            if (_rnd.Next(5) < 4)
            {
                var tagAssignment2 = new TagAssignment(Guid.NewGuid());
                tagAssignment2.Tag.Value     = GetTag("Verantstaltungsmails", TagUsage.Mailing, TagMode.Default | TagMode.Manual | TagMode.Self);
                tagAssignment2.Contact.Value = contact;
            }

            if (_rnd.Next(5) < 4)
            {
                var tagAssignment3 = new TagAssignment(Guid.NewGuid());
                tagAssignment3.Tag.Value     = GetTag("Aktionsmails", TagUsage.Mailing, TagMode.Default | TagMode.Manual | TagMode.Self);
                tagAssignment3.Contact.Value = contact;
            }

            if (_rnd.Next(5) < 3)
            {
                var tagAssignment4 = new TagAssignment(Guid.NewGuid());
                tagAssignment4.Tag.Value     = GetTag("Aktivist", TagUsage.None, TagMode.Manual);
                tagAssignment4.Contact.Value = contact;
            }

            _db.Save(contact);

            return(contact);
        }
Ejemplo n.º 9
0
 public PostalAddressViewModel(Translator translator, PostalAddress address)
 {
     Id   = address.Id.ToString();
     Text = string.Join("<br/>", address.Elements.Select(e => e.EscapeHtml()));
     PhraseDeleteConfirmationQuestion = translator.Get("Contact.Detail.Master.PostalAddresses.Delete.Confirm.Question", "Delete postal address confirmation question", "Do you really wish to delete postal address {0}?", address.GetText(translator)).EscapeHtml();
 }