public virtual ActionResult Edit()
        {
            // even if there is no configuration, return default affiliation establishment info to view
            var model = new InstitutionalAgreementConfigurationForm();

            // find a configuration for the currently signed in user's default affiliation establishment
            var configuration = _queryProcessor.Execute(new GetMyInstitutionalAgreementConfigurationQuery(User));

            // when configuration exists, return corresponding viewmodel
            if (configuration != null)
            {
                model = Mapper.Map <InstitutionalAgreementConfigurationForm>(configuration);
                AddEmptyAllowedOptions(model);
            }
            else
            {
                // when configuration does not exist, get the default affiliation for the currently signed in user
                var person = GetConfigurationSupervisor();
                if (person == null)
                {
                    return(HttpNotFound());
                }

                // allow the viewmodel to disclose the establishment official name
                model.ForEstablishmentOfficialName = person.DefaultAffiliation.Establishment.OfficialName;
            }
            return(View(model));
        }
 private static void AddEmptyAllowedOptions(InstitutionalAgreementConfigurationForm model)
 {
     model.AllowedTypeValues.Insert(0, new InstitutionalAgreementTypeValueForm {
         ConfigurationId = model.RevisionId
     });
     model.AllowedStatusValues.Insert(0, new InstitutionalAgreementStatusValueForm {
         ConfigurationId = model.RevisionId
     });
     model.AllowedContactTypeValues.Insert(0, new InstitutionalAgreementContactTypeValueForm {
         ConfigurationId = model.RevisionId
     });
 }
        public virtual ActionResult Edit(InstitutionalAgreementConfigurationForm model)
        {
            if (model != null)
            {
                // look for current entity
                var existingEntity = _queryProcessor.Execute(
                    new GetInstitutionalAgreementConfigurationByGuidQuery(model.EntityId)
                {
                    EagerLoad = new Expression <Func <InstitutionalAgreementConfiguration, object> >[]
                    {
                        c => c.ForEstablishment.Affiliates.Select(a => a.Person.User),
                        c => c.AllowedTypeValues,
                        c => c.AllowedStatusValues,
                        c => c.AllowedContactTypeValues,
                    },
                });

                if (existingEntity == null)
                {
                    return(HttpNotFound());
                }

                var compareConfiguration =
                    _queryProcessor.Execute(new GetMyInstitutionalAgreementConfigurationQuery(User));

                if (existingEntity.RevisionId != compareConfiguration.RevisionId || existingEntity.EntityId != compareConfiguration.EntityId)
                {
                    ModelState.AddModelError(string.Empty, string.Format(
                                                 "You are not authorized to configure the Institutional Agreements module for {0}.", existingEntity.ForEstablishment.OfficialName));
                }

                if (ModelState.IsValid)
                {
                    var command = new CreateOrUpdateConfigurationCommand(User, model.RevisionId)
                    {
                        IsCustomTypeAllowed        = model.IsCustomTypeAllowed,
                        IsCustomStatusAllowed      = model.IsCustomStatusAllowed,
                        IsCustomContactTypeAllowed = model.IsCustomTypeAllowed,
                        AllowedTypeValues          = model.AllowedTypeValues.Select(x => x.Text),
                        AllowedStatusValues        = model.AllowedStatusValues.Select(x => x.Text),
                        AllowedContactTypeValues   = model.AllowedContactTypeValues.Select(x => x.Text),
                    };
                    _commandHandler.Handle(command);
                    SetFeedbackMessage("Module configuration was saved successfully.");
                    return(RedirectToAction(MVC.InstitutionalAgreements.ConfigurationForms.Edit()));
                }

                AddEmptyAllowedOptions(model);
                return(View(model));
            }
            return(HttpNotFound());
        }
        public virtual ActionResult Add()
        {
            var model = new InstitutionalAgreementConfigurationForm();

            // find the configuration for the currently signed in user's default affiliation
            var configuration = _queryProcessor.Execute(new GetMyInstitutionalAgreementConfigurationQuery(User));

            // if configuration exists, cannot add
            if (configuration != null)
            {
                model = Mapper.Map <InstitutionalAgreementConfigurationForm>(configuration);
            }
            else
            {
                // when configuration does not exist, get the default affiliation for the currently signed in user
                var person = GetConfigurationSupervisor();
                if (person == null)
                {
                    return(HttpNotFound());
                }

                // allow the viewmodel to disclose the establishment official name
                model.ForEstablishmentOfficialName = person.DefaultAffiliation.Establishment.OfficialName;

                // add default options
                DefaultTypes.ForEach(option => model.AllowedTypeValues.Add(
                                         new InstitutionalAgreementTypeValueForm {
                    IsAdded = true, Text = option
                }));
                DefaultStatuses.ForEach(option => model.AllowedStatusValues.Add(
                                            new InstitutionalAgreementStatusValueForm {
                    IsAdded = true, Text = option
                }));
                DefaultContactTypes.ForEach(option => model.AllowedContactTypeValues.Add(
                                                new InstitutionalAgreementContactTypeValueForm {
                    IsAdded = true, Text = option
                }));

                // add a default empty allowed options
                AddEmptyAllowedOptions(model);
            }
            return(View(model));
        }
        public virtual ActionResult Add(InstitutionalAgreementConfigurationForm model)
        {
            // do nothing without a viewmodel
            if (model != null)
            {
                if (ModelState.IsValid)
                {
                    // make sure configuration does not already exist
                    var configuration = _queryProcessor.Execute(new GetMyInstitutionalAgreementConfigurationQuery(User));
                    if (configuration != null)
                    {
                        SetFeedbackMessage("Your configuration has already been set up.");
                        return(RedirectToAction(MVC.InstitutionalAgreements.ConfigurationForms.Add()));
                    }

                    // configuration must have a ForEstablishmentId, and all items should be added
                    var person        = _queryProcessor.Execute(new GetMyPersonQuery(User));
                    var establishment = person.DefaultAffiliation.Establishment;
                    model.ForEstablishmentId = establishment.RevisionId;

                    var command = new CreateOrUpdateConfigurationCommand(User)
                    {
                        IsCustomTypeAllowed        = model.IsCustomTypeAllowed,
                        IsCustomStatusAllowed      = model.IsCustomStatusAllowed,
                        IsCustomContactTypeAllowed = model.IsCustomTypeAllowed,
                        AllowedTypeValues          = model.AllowedTypeValues.Select(x => x.Text),
                        AllowedStatusValues        = model.AllowedStatusValues.Select(x => x.Text),
                        AllowedContactTypeValues   = model.AllowedContactTypeValues.Select(x => x.Text),
                    };
                    _commandHandler.Handle(command);

                    SetFeedbackMessage("Module configuration was set up successfully.");
                    return(RedirectToAction(MVC.InstitutionalAgreements.ManagementForms.Browse()));
                }

                // add a default empty allowed options
                AddEmptyAllowedOptions(model);
                return(View(model));
            }
            return(HttpNotFound());
        }
        public virtual JsonResult ValidateDuplicateOption(string type, List <string> values)
        {
            var model             = new InstitutionalAgreementConfigurationForm();
            var validationResults = new List <ValidationResult>();
            var validationContext = new ValidationContext(model, null, null);
            var isValid           = false;

            if (string.Compare(type, typeof(InstitutionalAgreementTypeValueForm).FullName, false, CultureInfo.InvariantCulture) == 0)
            {
                values.ForEach(t => model.AllowedTypeValues.Add(new InstitutionalAgreementTypeValueForm {
                    Text = t
                }));
                validationContext.MemberName = "AllowedTypeValues";
                isValid = Validator.TryValidateProperty(model.AllowedTypeValues, validationContext, validationResults);
            }
            else if (string.Compare(type, typeof(InstitutionalAgreementStatusValueForm).FullName, false, CultureInfo.InvariantCulture) == 0)
            {
                values.ForEach(t => model.AllowedStatusValues.Add(new InstitutionalAgreementStatusValueForm {
                    Text = t
                }));
                validationContext.MemberName = "AllowedStatusValues";
                isValid = Validator.TryValidateProperty(model.AllowedStatusValues, validationContext, validationResults);
            }
            else if (string.Compare(type, typeof(InstitutionalAgreementContactTypeValueForm).FullName, false, CultureInfo.InvariantCulture) == 0)
            {
                values.ForEach(t => model.AllowedContactTypeValues.Add(new InstitutionalAgreementContactTypeValueForm {
                    Text = t
                }));
                validationContext.MemberName = "AllowedContactTypeValues";
                isValid = Validator.TryValidateProperty(model.AllowedContactTypeValues, validationContext, validationResults);
            }

            var errorMessage = (isValid)
                ? null
                : validationResults[0].ErrorMessage;

            return(Json(new { IsValid = isValid, ErrorMessage = errorMessage, }, JsonRequestBehavior.AllowGet));
        }