예제 #1
0
        public ActionResult Index(HttpPostedFileBase[] files)
        {
            return(CreateTask(() =>
            {
                var importResults = new List <MatterImportResult>();

                using (var db = new LibiadaWebEntities())
                {
                    Matter[] matters = Cache.GetInstance().Matters.Where(m => m.Nature == Nature.Music).ToArray();

                    for (int i = 0; i < Request.Files.Count; i++)
                    {
                        string sequenceName = Request.Files[i].FileName.Substring(0, Request.Files[i].FileName.LastIndexOf('.'));

                        var importResult = new MatterImportResult()
                        {
                            MatterName = sequenceName
                        };

                        try
                        {
                            var sequence = new CommonSequence();

                            if (matters.Any(m => m.Name == sequenceName))
                            {
                                sequence.MatterId = matters.Single(m => m.Name == sequenceName).Id;
                                importResult.Result = "Successfully imported music for existing matter";
                            }
                            else
                            {
                                sequence.Matter = new Matter
                                {
                                    Name = sequenceName,
                                    Group = Group.ClassicalMusic,
                                    Nature = Nature.Music,
                                    SequenceType = SequenceType.CompleteMusicalComposition
                                };

                                importResult.Result = "Successfully imported music and created matter";
                            }

                            var repository = new MusicSequenceRepository(db);

                            repository.Create(sequence, Request.Files[i].InputStream);
                            importResult.Status = "Success";
                            importResults.Add(importResult);
                        }
                        catch (Exception exception)
                        {
                            importResult.Result = $"Failed to import music: {exception.Message}";
                            while (exception.InnerException != null)
                            {
                                importResult.Result += $" {exception.InnerException.Message}";

                                exception = exception.InnerException;
                            }

                            importResult.Status = "Error";
                            importResults.Add(importResult);
                        }
                    }

                    var result = new Dictionary <string, object> {
                        { "result", importResults }
                    };

                    return new Dictionary <string, string> {
                        { "data", JsonConvert.SerializeObject(result) }
                    };
                }
            }));
        }
예제 #2
0
        public ActionResult Index(long matterId,
                                  Notation notation,
                                  Language?language,
                                  Translator?translator,
                                  PauseTreatment?pauseTreatment,
                                  bool?sequentialTransfer,
                                  int scrambling)
        {
            Matter matter = Cache.GetInstance().Matters.Single(m => m.Id == matterId);
            long   sequenceId;

            switch (matter.Nature)
            {
            case Nature.Literature:
                sequenceId = db.LiteratureSequence.Single(l => l.MatterId == matterId &&
                                                          l.Notation == notation &&
                                                          l.Language == language &&
                                                          l.Translator == translator).Id;
                break;

            case Nature.Music:
                sequenceId = db.MusicSequence.Single(m => m.MatterId == matterId &&
                                                     m.Notation == notation &&
                                                     m.PauseTreatment == pauseTreatment &&
                                                     m.SequentialTransfer == sequentialTransfer).Id;
                break;

            default:
                sequenceId = db.CommonSequence.Single(c => c.MatterId == matterId && c.Notation == notation).Id;
                break;
            }

            BaseChain chain = sequenceRepository.GetLibiadaBaseChain(sequenceId);

            for (int i = 0; i < scrambling; i++)
            {
                int firstIndex  = randomGenerator.Next(chain.Length);
                int secondIndex = randomGenerator.Next(chain.Length);

                IBaseObject firstElement  = chain[firstIndex];
                IBaseObject secondElement = chain[secondIndex];
                chain[firstIndex]  = secondElement;
                chain[secondIndex] = firstElement;
            }

            var resultMatter = new Matter
            {
                Nature = matter.Nature,
                Name   = $"{matter.Name} {scrambling} mixes"
            };
            MatterRepository matterRepository = new MatterRepository(db);

            matterRepository.SaveToDatabase(resultMatter);

            var result = new CommonSequence
            {
                Notation = notation,
                MatterId = resultMatter.Id
            };

            long[] alphabet = elementRepository.ToDbElements(chain.Alphabet, notation, false);

            switch (matter.Nature)
            {
            case Nature.Genetic:
                DnaSequence dnaSequence = db.DnaSequence.Single(c => c.Id == sequenceId);

                dnaSequenceRepository.Create(result, dnaSequence.Partial, alphabet, chain.Building);
                break;

            case Nature.Music:
                musicSequenceRepository.Create(result, alphabet, chain.Building);
                break;

            case Nature.Literature:
                LiteratureSequence sequence = db.LiteratureSequence.Single(c => c.Id == sequenceId);

                literatureSequenceRepository.Create(result, sequence.Original, sequence.Language, sequence.Translator, alphabet, chain.Building);
                break;

            case Nature.MeasurementData:
                dataSequenceRepository.Create(result, alphabet, chain.Building);
                break;

            default:
                throw new InvalidEnumArgumentException(nameof(matter.Nature), (int)matter.Nature, typeof(Nature));
            }

            return(RedirectToAction("Index", "Matters"));
        }
예제 #3
0
        public ActionResult Create(
            [Bind(Include = "Id,Notation,RemoteDb,RemoteId,Description,Matter,MatterId")] CommonSequence commonSequence,
            bool localFile,
            Language?language,
            bool?original,
            Translator?translator,
            bool?partial,
            int?precision)
        {
            return(CreateTask(() =>
            {
                var db = new LibiadaWebEntities();
                try
                {
                    if (!ModelState.IsValid)
                    {
                        throw new Exception("Model state is invalid");
                    }

                    Stream sequenceStream;
                    Nature nature = commonSequence.Notation.GetNature();
                    if (nature == Nature.Genetic && !localFile)
                    {
                        sequenceStream = NcbiHelper.GetFastaFileStream(commonSequence.RemoteId);
                    }
                    else
                    {
                        sequenceStream = FileHelper.GetFileStream(Request.Files[0]);
                    }

                    switch (nature)
                    {
                    case Nature.Genetic:
                        ISequence bioSequence = NcbiHelper.GetFastaSequence(sequenceStream);
                        var dnaSequenceRepository = new GeneticSequenceRepository(db);
                        dnaSequenceRepository.Create(commonSequence, bioSequence, partial ?? false);
                        break;

                    case Nature.Music:
                        var musicSequenceRepository = new MusicSequenceRepository(db);
                        musicSequenceRepository.Create(commonSequence, sequenceStream);
                        break;

                    case Nature.Literature:
                        var literatureSequenceRepository = new LiteratureSequenceRepository(db);
                        literatureSequenceRepository.Create(commonSequence, sequenceStream, language ?? Language.Russian, original ?? true, translator ?? Translator.NoneOrManual);
                        break;

                    case Nature.MeasurementData:
                        var dataSequenceRepository = new DataSequenceRepository(db);
                        dataSequenceRepository.Create(commonSequence, sequenceStream, precision ?? 0);
                        break;

                    case Nature.Image:
                        var matterRepository = new MatterRepository(db);
                        int fileSize = Request.Files[0].ContentLength;
                        var file = new byte[fileSize];
                        Request.Files[0].InputStream.Read(file, 0, fileSize);
                        var matter = new Matter
                        {
                            Nature = Nature.Image,
                            SequenceType = commonSequence.Matter.SequenceType,
                            Name = commonSequence.Matter.Name,
                            Source = file,
                            Group = commonSequence.Matter.Group
                        };
                        matterRepository.SaveToDatabase(matter);
                        break;

                    default:
                        throw new InvalidEnumArgumentException(nameof(nature), (int)nature, typeof(Nature));
                    }
                    string multisequenceName = db.Multisequence.SingleOrDefault(ms => ms.Id == commonSequence.Matter.MultisequenceId).Name;
                    var result = new ImportResult(commonSequence, language, original, translator, partial, precision, multisequenceName);

                    return new Dictionary <string, string> {
                        { "data", JsonConvert.SerializeObject(result) }
                    };
                }
                catch (Exception)
                {
                    long matterId = commonSequence.MatterId;
                    if (matterId != 0)
                    {
                        List <Matter> orphanMatter = db.Matter
                                                     .Include(m => m.Sequence)
                                                     .Where(m => m.Id == matterId && m.Sequence.Count == 0)
                                                     .ToList();

                        if (orphanMatter.Count > 0)
                        {
                            db.Matter.Remove(orphanMatter[0]);
                            db.SaveChanges();
                        }
                    }

                    throw;
                }
                finally
                {
                    Dispose(true);
                }
            }));
        }