Example #1
0
 public override double CountSalary()
 {
     if (Designation.Equals("HR", StringComparison.InvariantCultureIgnoreCase))
     {
         return((basicSalary * 12) + (1000 * Experience));
     }
     else if (Designation.Equals("Developer", StringComparison.InvariantCultureIgnoreCase))
     {
         return((basicSalary * 12) + (2000 * Experience));
     }
     else
     {
         return(basicSalary * 12);
     }
 }
            /// <summary>
            /// Validates a person object.
            /// </summary>
            public PersonValidator()
            {
                RuleFor(x => x.Id).NotEmpty();
                RuleFor(x => x.LastName).NotEmpty().Length(1, 40)
                .WithMessage("The last name must not be left blank and must not exceed 40 characters.");
                RuleFor(x => x.FirstName).Length(0, 40)
                .WithMessage("The first name must not exceed 40 characters.");
                RuleFor(x => x.MiddleName).Length(0, 40)
                .WithMessage("The middle name must not exceed 40 characters.");
                RuleFor(x => x.Suffix).Length(0, 40)
                .WithMessage("The suffix must not exceed 40 characters.");
                RuleFor(x => x.SSN).NotEmpty().Must(x => System.Text.RegularExpressions.Regex.IsMatch(x, @"^(?!\b(\d)\1+-(\d)\1+-(\d)\1+\b)(?!123-45-6789|219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4}$"))
                .WithMessage("The SSN must be valid and contain only numbers.");
                RuleFor(x => x.DateOfBirth).NotEmpty()
                .WithMessage("The DOB must not be left blank.");
                RuleFor(x => x.PRD).NotEmpty()
                .WithMessage("The DOB must not be left blank.");
                RuleFor(x => x.Sex).NotNull()
                .WithMessage("The sex must not be left blank.");
                RuleFor(x => x.Remarks).Length(0, 150)
                .WithMessage("Remarks must not exceed 150 characters.");
                RuleFor(x => x.Command).NotEmpty().WithMessage("A person must have a command.  If you are trying to indicate this person left the command, please set his or her duty status to 'LOSS'.");
                RuleFor(x => x.Department).NotEmpty().WithMessage("A person must have a department.  If you are trying to indicate this person left the command, please set his or her duty status to 'LOSS'.");
                RuleFor(x => x.Division).NotEmpty().WithMessage("A person must have a division.  If you are trying to indicate this person left the command, please set his or her duty status to 'LOSS'.");
                RuleFor(x => x.Ethnicity).Must(x =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    Ethnicity ethnicity = DataProvider.CurrentSession.Get <Ethnicity>(x.Id);

                    if (ethnicity == null)
                    {
                        return(false);
                    }

                    return(ethnicity.Equals(x));
                })
                .WithMessage("The ethnicity wasn't valid.  It must match exactly a list item in the database.");
                RuleFor(x => x.ReligiousPreference).Must(x =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    ReligiousPreference pref = DataProvider.CurrentSession.Get <ReligiousPreference>(x.Id);

                    if (pref == null)
                    {
                        return(false);
                    }

                    return(pref.Equals(x));
                })
                .WithMessage("The religious preference wasn't valid.  It must match exactly a list item in the database.");
                RuleFor(x => x.Designation).Must(x =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    Designation designation = DataProvider.CurrentSession.Get <Designation>(x.Id);

                    if (designation == null)
                    {
                        return(false);
                    }

                    return(designation.Equals(x));
                })
                .WithMessage("The designation wasn't valid.  It must match exactly a list item in the database.");
                RuleFor(x => x.Division).Must((person, x) =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    Division division = DataProvider.CurrentSession.Get <Division>(x.Id);

                    if (division == null)
                    {
                        return(false);
                    }

                    return(division.Equals(x));
                })
                .WithMessage("The division wasn't a valid division.  It must match exactly.");
                RuleFor(x => x.Department).Must(x =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    Department department = DataProvider.CurrentSession.Get <Department>(x.Id);

                    if (department == null)
                    {
                        return(false);
                    }

                    return(department.Equals(x));
                })
                .WithMessage("The department was invalid.");
                RuleFor(x => x.Command).Must(x =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    Command command = DataProvider.CurrentSession.Get <Command>(x.Id);

                    if (command == null)
                    {
                        return(false);
                    }

                    return(command.Equals(x));
                })
                .WithMessage("The command was invalid.");
                RuleFor(x => x.PrimaryNEC).Must((person, x) =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    NEC nec = DataProvider.CurrentSession.Get <NEC>(x.Id);

                    if (nec == null)
                    {
                        return(false);
                    }

                    if (!nec.Equals(x))
                    {
                        return(false);
                    }

                    //Now let's also make sure this isn't in the secondary NECs.
                    if (person.SecondaryNECs.Any(y => y.Id == x.Id))
                    {
                        return(false);
                    }

                    return(true);
                })
                .WithMessage("The primary NEC must not exist in the secondary NECs list.");
                RuleFor(x => x.Supervisor).Length(0, 40)
                .WithMessage("The supervisor field may not be longer than 40 characters.");
                RuleFor(x => x.WorkCenter).Length(0, 40)
                .WithMessage("The work center field may not be longer than 40 characters.");
                RuleFor(x => x.WorkRoom).Length(0, 40)
                .WithMessage("The work room field may not be longer than 40 characters.");
                RuleFor(x => x.Shift).Length(0, 40)
                .WithMessage("The shift field may not be longer than 40 characters.");
                RuleFor(x => x.WorkRemarks).Length(0, 150)
                .WithMessage("The work remarks field may not be longer than 150 characters.");
                RuleFor(x => x.UIC).Must(x =>
                {
                    if (x == null)
                    {
                        return(true);
                    }

                    UIC uic = DataProvider.CurrentSession.Get <UIC>(x.Id);

                    if (uic == null)
                    {
                        return(false);
                    }

                    return(uic.Equals(x));
                })
                .WithMessage("The UIC was invalid.");
                RuleFor(x => x.JobTitle).Length(0, 40)
                .WithMessage("The job title may not be longer than 40 characters.");
                RuleFor(x => x.UserPreferences).Must((person, x) =>
                {
                    return(x.Keys.Count <= 20);
                })
                .WithMessage("You may not submit more than 20 preference keys.");
                RuleForEach(x => x.UserPreferences).Must((person, x) =>
                {
                    return(x.Value.Length <= 1000);
                })
                .WithMessage("No preference value may be more than 1000 characters.");

                When(x => x.IsClaimed, () =>
                {
                    RuleFor(x => x.EmailAddresses).Must((person, x) =>
                    {
                        return(x.Any(y => y.IsDodEmailAddress));
                    }).WithMessage("You must have at least one mail.mil address.");
                });

                RuleForEach(x => x.SubscribedEvents).Must((person, subEvent) =>
                {
                    if (person.SubscribedEvents.Count(x => x.Key == subEvent.Key) != 1)
                    {
                        return(false);
                    }

                    var changeEvent = ChangeEvents.ChangeEventHelper.AllChangeEvents.FirstOrDefault(x => x.Id == subEvent.Key);

                    if (changeEvent == null)
                    {
                        return(false);
                    }

                    if (!changeEvent.ValidLevels.Contains(subEvent.Value))
                    {
                        return(false);
                    }

                    return(true);
                })
                .WithMessage("One or more of your subscription events were not valid.");

                //Set validations
                RuleFor(x => x.EmailAddresses)
                .SetCollectionValidator(new EmailAddress.EmailAddressValidator());
                RuleFor(x => x.PhoneNumbers)
                .SetCollectionValidator(new PhoneNumber.PhoneNumberValidator());
                RuleFor(x => x.PhysicalAddresses)
                .SetCollectionValidator(new PhysicalAddress.PhysicalAddressValidator());
            }