Ejemplo n.º 1
0
Archivo: Item.cs Proyecto: ucdavis/CRP
 public virtual void AddEditor(Editor editor)
 {
     editor.Item = this;
     Editors.Add(editor);
 }
Ejemplo n.º 2
0
Archivo: Item.cs Proyecto: ucdavis/CRP
 public virtual void RemoveEditor(Editor editor)
 {
     Editors.Remove(editor);
 }
Ejemplo n.º 3
0
        public ActionResult Create(Item item, ExtendedPropertyParameter[] extendedProperties, string[] tags, string mapLink)
        {
            ModelState.Clear();
            // get the file and add it into the item
            if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
            {
                var file = Request.Files[0];
                var reader = new BinaryReader(file.InputStream);
                item.Image = reader.ReadBytes(file.ContentLength);
            }
            if (item.Image == null || item.Image.Length <= 0)
            {
                ModelState.AddModelError("Image", @"An image is required.");
            }
            // process the extended properties and tags
            //item = PopulateObject.Item(Repository, item, extendedProperties, tags);
            item = Copiers.PopulateItem(Repository, item, extendedProperties, tags, mapLink);

            if(item.ExtendedPropertyAnswers != null && item.ExtendedPropertyAnswers.Count > 0)
            {
                foreach (var extendedPropertyAnswer in item.ExtendedPropertyAnswers)
                {
                    if(string.IsNullOrWhiteSpace(extendedPropertyAnswer.Answer))
                    {
                        ModelState.AddModelError("Extended Properties", "All Extended Properties are required");
                        break;
                    }
                }
            }

            var user = Repository.OfType<User>().Queryable.Where(a => a.LoginID == CurrentUser.Identity.Name).FirstOrDefault();

            // add permissions
            var editor = new Editor(item, user);
            editor.Owner = true;
            item.AddEditor(editor);

            // set the unit
            //item.Unit = user.Units.FirstOrDefault();

            // add the required question set
            var questionSet =
                Repository.OfType<QuestionSet>().Queryable.Where(
                    a => a.Name == StaticValues.QuestionSet_ContactInformation).FirstOrDefault();

            // this should really not be null at any point.
            if (questionSet != null)
            {
                item.AddTransactionQuestionSet(questionSet);
            }

            if (item.ItemType != null)
            {
                // add the default question sets
                foreach (var qs in item.ItemType.QuestionSets)
                {
                    if (qs.TransactionLevel)
                    {
                        item.AddTransactionQuestionSet(qs.QuestionSet);
                    }
                    else
                    {
                        item.AddQuantityQuestionSet(qs.QuestionSet);
                    }
                }
            }

            if(item.Template == null)
            {
                var tempTemplate = Repository.OfType<Template>().Queryable.Where(a => a.Default).FirstOrDefault();
                if (tempTemplate != null && !string.IsNullOrEmpty(tempTemplate.Text))
                {
                    var template = new Template(tempTemplate.Text);
                    item.Template = template;
                }
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ModelState, item.ValidationResults());

            if (ModelState.IsValid)
            {
                Repository.OfType<Item>().EnsurePersistent(item);
                Message = NotificationMessages.STR_ObjectCreated.Replace(NotificationMessages.ObjectType, "Item");
                return this.RedirectToAction(a => a.List(null));
            }
            else
            {
                var viewModel = ItemViewModel.Create(Repository, CurrentUser, item);
                viewModel.IsNew = true;
                return View(viewModel);
            }
        }