public ActionResult Edit(string scoreId)
        {
            theLogger.Info(String.Format("GET method Edit, parameter scoreId {0}", scoreId));
            FoireMusesConnection connection = GetConnection();
            Score score = null;
            SearchResult <SourceSearchItem> sourceList = null;

            try
            {
                if (!String.IsNullOrWhiteSpace(scoreId))                //get the score matching the id
                {
                    score = connection.GetScore(scoreId, new Result <Score>()).Wait();
                    if (score == null)
                    {
                        return(RedirectToAction("Missing", "Error", null));
                    }
                    if (score.TextualSource != null && score.TextualSource.PieceId != null)
                    {
                        SearchResult <Play> searchResultPlay = null;
                        searchResultPlay = connection.GetPlaysFromSource(score.TextualSource.SourceId, 0, 0, new Result <SearchResult <Play> >()).Wait();
                        if (searchResultPlay == null)
                        {
                            return(RedirectToAction("Problem", "Error", null));
                        }
                        ViewBag.Pieces = searchResultPlay.Rows;
                    }
                    if (score.MasterId != null)
                    {
                        Score maitre = connection.GetScore(score.MasterId, new Result <Score>()).Wait();
                        if (maitre == null)
                        {
                            score.MasterId = null;
                        }
                        else
                        {
                            ViewBag.MasterIdTitle = maitre.Title;
                        }
                    }
                    ViewBag.HeadTitle = "Edition";
                }
                else
                {
                    score             = new Score();
                    ViewBag.HeadTitle = "Création";
                }
                sourceList = connection.GetSources(0, 0, new Result <SearchResult <SourceSearchItem> >()).Wait();
            }
            catch (Exception e)
            {
                return(RedirectToAction("Problem", "Error", null));
            }
            if (score == null || sourceList == null)
            {
                return(RedirectToAction("Missing", "Error", null));
            }
            ViewBag.Sources = sourceList.Rows.OrderBy(x => x.Name);
            return(View("Edit", score));
        }
        public ActionResult Edit(Score model, FormCollection collection)
        {
            theLogger.Info("POST method Edit");
            if (model == null)
            {
                return(RedirectToAction("Missing", "Error", null));
            }
            FoireMusesConnection connection = GetConnection();

            try
            {
                if (model.Id == null)
                {
                    if (model.TextualSource.SourceId == null)
                    {
                        model.TextualSource = null;
                    }
                    if (model.MusicalSource.SourceId == null)
                    {
                        model.MusicalSource = null;
                    }
                    model = connection.CreateScore(model, new Result <Score>()).Wait();
                }
                else
                {
                    Score current = connection.GetScore(model.Id, new Result <Score>()).Wait();
                    if (current == null)
                    {
                        return(RedirectToAction("Missing", "Error", null));
                    }
                    TryUpdateModel(current);
                    if (current.TextualSource.SourceId == null)
                    {
                        current.TextualSource = null;
                    }
                    if (current.MusicalSource.SourceId == null)
                    {
                        current.MusicalSource = null;
                    }
                    model = connection.EditScore(current, new Result <Score>()).Wait();
                }
            }
            catch (Exception e)
            {
                theLogger.Error("Stacktrace:\n" + e.StackTrace);
                return(RedirectToAction("Problem", "Error", null));
            }
            if (model == null)
            {
                theLogger.Error("Reason: model is null");
                return(RedirectToAction("Problem", "Error", null));
            }
            return(RedirectToAction("Details", new { scoreId = model.Id }));
        }
        public ActionResult Publish(string scoreId, bool overwrite, HttpPostedFileBase file)
        {
            theLogger.Info("POST Publish");
            if (file == null || file.ContentType != "text/xml" || file.ContentLength == 0)
            {
                ViewBag.Error = "Error during the upload, please be sure to choose a valid xml file from your computer";
                return(View("Publish"));
            }
            FoireMusesConnection connection = GetConnection();
            Score score = null;

            try
            {
                if (scoreId == null)
                {
                    XDoc theDoc = XDocFactory.From(file.InputStream, MimeType.XML);
                    score = connection.CreateScoreWithXml(theDoc, new Result <Score>()).Wait();
                }
                else
                {
                    Score current = connection.GetScore(scoreId, new Result <Score>()).Wait();
                    if (current == null)
                    {
                        return(RedirectToAction("Missing", "Error", null));
                    }
                    score = connection.UpdateScoreWithXml(current.Id, current.Rev, XDocFactory.From(file.InputStream, MimeType.XML), overwrite, new Result <Score>()).Wait();
                    if (score == null)
                    {
                        return(RedirectToAction("Problem", "Error", null));
                    }
                    return(RedirectToAction("Details", new { scoreId = score.Id }));
                }
            }
            catch (Exception e)
            {
                theLogger.Error("Stacktrace:\n" + e.StackTrace);
                return(RedirectToAction("Problem", "Error", null));
            }
            return(RedirectToAction("Edit", new { scoreId = score.Id }));
        }
        //
        // GET: /Scores/Details
        public ActionResult Details(string scoreId)
        {
            if (String.IsNullOrWhiteSpace(scoreId))
            {
                return(RedirectToAction("Missing", "Error", null));
            }

            FoireMusesConnection connection = GetConnection();
            Score  score        = null;
            Score  genericScore = null;
            Source sTextuelle   = null;
            Source sMusicale    = null;
            Play   assPlay      = null;
            IList <ScoreSearchItem> otherTitlesScore = null;
            IEnumerable <string>    attachedFiles    = null;
            IEnumerable <string>    documents        = null;

            try
            {
                score = connection.GetScore(scoreId, new Result <Score>()).Wait();
                if (score == null)
                {
                    return(RedirectToAction("Missing", "Error", null));
                }
                if (score.TextualSource != null && !String.IsNullOrWhiteSpace(score.TextualSource.SourceId))
                {
                    sTextuelle = connection.GetSource(score.TextualSource.SourceId, new Result <Source>()).Wait();
                    if (sTextuelle == null)
                    {
                        return(RedirectToAction("Problem", "Error", null));
                    }
                    if (!String.IsNullOrWhiteSpace(score.TextualSource.PieceId))
                    {
                        assPlay = connection.GetPlay(score.TextualSource.PieceId, new Result <Play>()).Wait();
                        if (assPlay == null)
                        {
                            return(RedirectToAction("Problem", "Error", null));
                        }
                    }
                }
                if (score.MusicalSource != null && !String.IsNullOrWhiteSpace(score.MusicalSource.SourceId))
                {
                    sMusicale = connection.GetSource(score.MusicalSource.SourceId, new Result <Source>()).Wait();
                    if (sMusicale == null)
                    {
                        return(RedirectToAction("Problem", "Error", null));
                    }
                }
                if (score.HasAttachement)
                {
                    attachedFiles = score.GetAttachmentNames().Where(x => !x.StartsWith("$"));
                    documents     = score.GetAttachmentNames().Where(x => x.StartsWith("$"));
                }
                if (score.IsMaster)
                {
                    SearchResult <ScoreSearchItem> results = connection.SearchScore(0, 0, new Dictionary <string, object>()
                    {
                        { "masterId", score.Id }
                    }, new Result <SearchResult <ScoreSearchItem> >()).Wait();
                    if (results == null)
                    {
                        return(RedirectToAction("Problem", "Error", null));
                    }
                    otherTitlesScore = results.Rows;
                }
                else if (score.MasterId != null)
                {
                    genericScore = connection.GetScore(score.MasterId, new Result <Score>()).Wait();
                    if (genericScore == null)
                    {
                        return(RedirectToAction("Problem", "Error", null));
                    }
                    SearchResult <ScoreSearchItem> results = connection.SearchScore(0, 0, new Dictionary <string, object>()
                    {
                        { "masterId", score.MasterId }
                    }, new Result <SearchResult <ScoreSearchItem> >()).Wait();
                    if (results == null)
                    {
                        return(RedirectToAction("Problem", "Error", null));
                    }
                    otherTitlesScore = results.Rows;
                }
            }
            catch (Exception e)
            {
                return(RedirectToAction("Problem", "Error", null));
            }
            ViewBag.TextualSource    = sTextuelle;
            ViewBag.AssociatedPlay   = assPlay;
            ViewBag.MusicalSource    = sMusicale;
            ViewBag.AttachedFiles    = attachedFiles;
            ViewBag.Documents        = documents;
            ViewBag.OtherTitlesScore = otherTitlesScore;
            ViewBag.GenericScore     = genericScore;
            return(View(score));
        }