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); } })); }
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")); }
public ActionResult Index(HttpPostedFileBase[] files) { return(CreateTask(() => { var importResults = new List <MatterImportResult>(); using (var db = new LibiadaWebEntities()) { Matter[] matters = db.Matter.Where(m => m.Nature == Nature.Image).ToArray(); var matterRepository = new MatterRepository(db); for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; string sequenceName = file?.FileName.Substring(0, file.FileName.LastIndexOf('.')); var importResult = new MatterImportResult() { MatterName = sequenceName }; try { if (file == null) { throw new FileNotFoundException($"No image file is provided. Iteration: {i}"); } if (matters.Any(m => m.Name == sequenceName)) { importResult.Result = "Image already exists"; continue; } int fileSize = file.ContentLength; var fileBytes = new byte[fileSize]; file.InputStream.Read(fileBytes, 0, fileSize); var matter = new Matter { Name = sequenceName, Group = Group.Picture, Nature = Nature.Image, Source = fileBytes, SequenceType = SequenceType.CompleteImage }; matterRepository.SaveToDatabase(matter); importResult.Result = "Successfully imported image and created matter"; importResult.Status = "Success"; importResults.Add(importResult); } catch (Exception exception) { importResult.Result = $"Failed to import image: {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) } }; } })); }