/// <summary>
        /// Repopulates the model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="identifiers">The identifiers.</param>
        /// <returns>EntityIdentifierModel.</returns>
        private EntityIdentifierModel RepopulateModel(EntityIdentifierModel model, List <EntityIdentifier> identifiers)
        {
            model.ExistingIdentifiers = identifiers.Select(i => new EntityIdentifierViewModel(i)).OrderBy(i => i.Name).ToList();

            if (!string.IsNullOrEmpty(model.Type) && !string.IsNullOrWhiteSpace(model.Type))
            {
                model.Types = RemoveExistingIdentifiers(this.GetAssigningAuthorities().ToSelectList("Name", "Key", a => a.Key == Guid.Parse(model.Type)), identifiers);
            }
            else
            {
                model.Types = RemoveExistingIdentifiers(this.GetAssigningAuthorities().ToSelectList("Name", "Key"), identifiers);
            }

            return(model);
        }
        public ActionResult Edit(Guid id, Guid entityId, string type)
        {
            var model = new EntityIdentifierModel(id, entityId);

            try
            {
                var modelType = this.GetModelType(type);
                var entity    = this.GetEntity(model.EntityId, modelType);

                model.ExistingIdentifiers = entity.Identifiers.Select(i => new EntityIdentifierViewModel(i)).ToList();

                if (entity.Identifiers.Any(i => i.Key == id))
                {
                    var entityIdentifier = entity.Identifiers.First(i => i.Key == id);
                    model.Type  = entityIdentifier.AuthorityKey?.ToString();
                    model.Value = entityIdentifier.Value;
                }

                model.ModelType = type;

                Guid modelTypeKey;

                if (!string.IsNullOrEmpty(model.Type) && !string.IsNullOrWhiteSpace(model.Type) && Guid.TryParse(model.Type, out modelTypeKey))
                {
                    model.Types = RemoveExistingIdentifiers(this.GetAssigningAuthorities().ToSelectList("Name", "Key", a => a.Key == modelTypeKey), entity.Identifiers);

                    // set the selected option to the current model type
                    model.Types = model.Types.Select(i => new SelectListItem {
                        Selected = i.Value == model.Type, Text = i.Text, Value = i.Value
                    }).ToList();
                }
                else
                {
                    model.Types = RemoveExistingIdentifiers(this.GetAssigningAuthorities().ToSelectList("Name", "Key"), entity.Identifiers);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError($"Unable to retrieve entity identifier: { e }");

                this.TempData["error"] = Locale.UnexpectedErrorMessage;
                return(this.RedirectToRequestOrHome());
            }

            return(View(model));
        }
        public ActionResult Create(Guid id, string type)
        {
            var model = new EntityIdentifierModel(id);

            try
            {
                var modelType = this.GetModelType(type);
                var entity    = this.GetEntity(model.EntityId, modelType);

                model.ExistingIdentifiers = entity.Identifiers.Select(i => new EntityIdentifierViewModel(i)).ToList();
                model.ModelType           = type;
                model.Types = RemoveExistingIdentifiers(this.GetAssigningAuthorities().ToSelectList("Name", "Key").ToList(), entity.Identifiers);
            }
            catch (Exception e)
            {
                Trace.TraceError($"Unable to retrieve entity: { e }");
            }

            return(View(model));
        }
        public ActionResult Create(EntityIdentifierModel model)
        {
            var identifiers = new List <EntityIdentifier>();

            try
            {
                if (this.ModelState.IsValid)
                {
                    var returnUrl = this.Request.UrlReferrer ?? new Uri(this.Request.Url.GetLeftPart(UriPartial.Authority));

                    var modelType = this.GetModelType(model.ModelType);
                    var entity    = this.GetEntity(model.EntityId, modelType);

                    if (entity == null)
                    {
                        this.TempData["error"] = Locale.EntityNotFound;

                        return(Redirect(returnUrl.ToString()));
                    }

                    identifiers = entity.Identifiers;

                    var authority = this.ImsiClient.Get <AssigningAuthority>(Guid.Parse(model.Type), null) as AssigningAuthority;

                    if (authority == null)
                    {
                        this.TempData["error"] = Locale.AssigningAuthorityNotFound;
                        return(Redirect(returnUrl.ToString()));
                    }

                    Bundle bundle = null;
                    string name   = null;

                    if (modelType == typeof(Material))
                    {
                        name   = Locale.Material;
                        bundle = this.ImsiClient.Query <Material>(e => e.Identifiers.Any(i => i.AuthorityKey == authority.Key && i.Value == model.Value && i.ObsoleteVersionSequenceId == null), 0, 0, false);
                    }
                    else if (modelType == typeof(Place))
                    {
                        name   = Locale.Place;
                        bundle = this.ImsiClient.Query <Place>(e => e.Identifiers.Any(i => i.AuthorityKey == authority.Key && i.Value == model.Value && i.ObsoleteVersionSequenceId == null), 0, 0, false);
                    }
                    else if (modelType == typeof(Organization))
                    {
                        name   = Locale.Organization;
                        bundle = this.ImsiClient.Query <Organization>(e => e.Identifiers.Any(i => i.AuthorityKey == authority.Key && i.Value == model.Value && i.ObsoleteVersionSequenceId == null), 0, 0, false);
                    }

                    bundle?.Reconstitute();

                    if (!this.IsValidIdentifier(authority, model.Value))
                    {
                        this.ModelState.AddModelError(nameof(model.Value), Locale.IdentifierFormatInvalid);

                        model = this.RepopulateModel(model, identifiers);

                        this.TempData["error"] = Locale.IdentifierFormatInvalid;
                        return(View(model));
                    }

                    if (bundle?.TotalResults > 0)
                    {
                        this.ModelState.AddModelError(nameof(model.Value), string.Format(Locale.DuplicateIdentifierValue, name));

                        model = this.RepopulateModel(model, identifiers);

                        this.TempData["error"] = string.Format(Locale.DuplicateIdentifierValue, name);
                        return(View(model));
                    }

                    entity.Identifiers.Add(new EntityIdentifier(authority, model.Value));

                    var updatedEntity = this.UpdateEntity(entity, modelType);

                    this.TempData["success"] = Locale.IdentifierCreatedSuccessfully;

                    return(RedirectToAction("Edit", model.ModelType, new { id = updatedEntity.Key.Value, versionId = updatedEntity.VersionKey }));
                }
            }
            catch (Exception e)
            {
                Trace.TraceError($"Unable to create entity identifier: { e }");
            }

            model.Types = RemoveExistingIdentifiers(this.GetAssigningAuthorities().ToSelectList("Name", "Key").ToList(), identifiers);

            this.TempData["error"] = Locale.UnableToCreateIdentifier;

            return(View(model));
        }