public virtual ActionResult Post(InstitutionalAgreementForm model)
        {
            if (model != null)
            {
                // do not process empty Guid
                if (model.EntityId == Guid.Empty)
                {
                    return(HttpNotFound());
                }

                if (model.RevisionId != 0)
                {
                    // find agreement
                    var agreement = _queryProcessor.Execute(
                        new GetMyInstitutionalAgreementByGuidQuery(User, model.EntityId));
                    if (agreement == null)
                    {
                        return(HttpNotFound());
                    }
                }
                else
                {
                    // find person's default affiliation
                    var person = _queryProcessor.Execute(new GetMyPersonQuery(User));
                    if (person == null || person.DefaultAffiliation == null)
                    {
                        return(HttpNotFound());
                    }
                }

                // always upload files
                UploadFiles(model);

                if (ModelState.IsValid)
                {
                    var command = new CreateOrUpdateInstitutionalAgreementCommand(User);
                    Mapper.Map(model, command);
                    _insertOrUpdateHandler.Handle(command);
                    SetFeedbackMessage(command.ChangeCount > 0
                        ? "Institutional agreement was saved successfully."
                        : "No changes were saved.");
                    return(RedirectToAction(MVC.InstitutionalAgreements.PublicSearch.Info(command.EntityId)));
                    //var hex = agreement.Files.ToList()[0].File.Content.ToHexString();
                }

                // set umbrella options
                model.Umbrella.Options = GetUmbrellaOptions(model.RevisionId);
                return(View(model));
            }
            return(HttpNotFound());
        }
        public virtual ActionResult Post(Guid?entityId)
        {
            // do not process empty Guid
            if (entityId.HasValue && entityId.Value == Guid.Empty)
            {
                return(HttpNotFound());
            }

            InstitutionalAgreementForm model;

            if (!entityId.HasValue)
            {
                // find person's default affiliation
                var person = _queryProcessor.Execute(new GetMyPersonQuery(User));
                if (person == null || person.DefaultAffiliation == null)
                {
                    return(HttpNotFound());
                }

                model = new InstitutionalAgreementForm
                {
                    EntityId = Guid.NewGuid(),
                };
                model.Participants.Add(new InstitutionalAgreementParticipantForm
                {
                    EstablishmentOfficialName = person.DefaultAffiliation.Establishment.OfficialName,
                    EstablishmentEntityId     = person.DefaultAffiliation.Establishment.EntityId,
                    IsOwner = true,
                });
            }
            else
            {
                // find agreement
                var agreement = _queryProcessor.Execute(
                    new GetMyInstitutionalAgreementByGuidQuery(User, entityId.Value)
                    );
                if (agreement == null)
                {
                    return(HttpNotFound());
                }

                model = Mapper.Map <InstitutionalAgreementForm>(agreement);
            }

            model.Umbrella.Options = GetUmbrellaOptions(model.RevisionId);
            return(View(model));
        }
        private void UploadFiles(InstitutionalAgreementForm form)
        {
            foreach (var formFile in form.Files)
            {
                if (formFile.IsDeleted && formFile.EntityId != Guid.Empty && formFile.RevisionId == 0)
                {
                    _purgeFileHandler.Handle(new PurgeLooseFileCommand(formFile.EntityId));
                    formFile.EntityId = Guid.Empty;
                }
                else if (!formFile.IsDeleted && formFile.PostedFile != null && formFile.IsValidPostedFile)
                {
                    var command = Mapper.Map <CreateLooseFileCommand>(formFile.PostedFile);
                    _createFileHandler.Handle(command);
                    var looseFile = command.CreatedLooseFile;
                    Mapper.Map(looseFile, formFile);
                }
            }
            var fileChooser = form.Files.SingleOrDefault(m => m.PostedFile == null && !m.IsDeleted && m.EntityId == Guid.Empty);

            if (fileChooser != null)
            {
                form.Files.Remove(fileChooser);
            }
        }