public ActionResult ShowAll(UserLanguages userLanguages, GroupForUser group, CrossReferenceType crossReferenceType) { if (WebSettingsConfig.Instance.IsSectionForbidden(SectionId)) { return(RedirectToAction("Index", RouteConfig.MAIN_CONTROLLER_NAME)); } if (UserLanguages.IsInvalid(userLanguages) || group == null) { return(GetRedirectToGroups()); } GroupModel model = GetModel(userLanguages, group); if (model.IsInvalid()) { return(GetRedirectToGroups()); } long languageId = WebSettingsConfig.Instance.GetLanguageFromId(); var crossReferencesQuery = new CrossReferencesQuery(languageId); List <CrossReference> crossReferences = crossReferencesQuery.GetReferences(group.Id, crossReferenceType); model.CrossReferencesModel = new CrossReferencesModel(group.Name, crossReferenceType, crossReferences); return(View("All", model)); }
public List <ExternalCrossReference> GetReferences(long sourceId, CrossReferenceType sourceType) { var parsedSourceType = (int)sourceType; List <CrossReference> crossReferences = Adapter.ReadByContext( c => c.CrossReference.Where(e => e.SourceId == sourceId && e.SourceType == parsedSourceType).ToList()); var result = new List <ExternalCrossReference>(); foreach (CrossReference crossReference in crossReferences) { var crossReferenceType = (CrossReferenceType)crossReference.DestinationType; long id = crossReference.DestinationId; string name = GetNameById(id, crossReferenceType); if (!string.IsNullOrEmpty(name)) { result.Add(new ExternalCrossReference(id, name, crossReferenceType)); } else { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "CrossReferencesQuery.GetReferences не удалось найти перекрестную ссылку с идентификатором {0} и типом {1} для источника с идентификатором {2}, типом {3}!", id, crossReferenceType, sourceId, sourceType); } } return(result); }
private void AddIdNameCacheByCrossType(CrossReferenceType type) { Func <IEnumerable <Tuple <long, string> > > dataGetter; switch (type) { case CrossReferenceType.VisualDictionary: dataGetter = () => { List <RepresentationForUser> areas = _representationsQuery.GetVisibleWithoutAreas(); return(areas.Select(e => new Tuple <long, string>(e.Id, e.Title))); }; break; case CrossReferenceType.GroupWord: dataGetter = () => GetGroupsByType(GroupType.ByWord); break; case CrossReferenceType.GroupSentence: dataGetter = () => GetGroupsByType(GroupType.BySentence); break; default: throw new ArgumentException("Неизвестный тип " + type); } var idNameCache = new IdNameCache(dataGetter); _cachesByCrossReferenceTypes.Add(type, idNameCache); }
public CrossReferencesModel(string current, CrossReferenceType currentType, IEnumerable <CrossReference> crossReferences) { if (crossReferences == null) { return; } _current = current; foreach (CrossReference crossReference in crossReferences) { CrossReferenceType type = crossReference.Type; List <CrossReference> crossReferencesByType; if (!_crossReferencesByType.TryGetValue(type, out crossReferencesByType)) { crossReferencesByType = new List <CrossReference>(); _crossReferencesByType.Add(type, crossReferencesByType); if (type != currentType) { _sortedTitlesBySortedTitlesByTypes.Add(GetTitleByType(type)); } } crossReferencesByType.Add(crossReference); HasCrossReferences = true; } //если есть рекомендуемые данные для текущей тематики, то поместить их в начало if (_crossReferencesByType.ContainsKey(currentType)) { _sortedTitlesBySortedTitlesByTypes.Insert(0, GetTitleByType(currentType)); } }
public CrossReferenceTablePart(IReadOnlyDictionary <IndirectReference, long> objectOffsets, long offset, long previous, DictionaryToken dictionary, CrossReferenceType type) { ObjectOffsets = objectOffsets; Offset = offset; Previous = previous; Dictionary = dictionary; Type = type; }
public CrossReferenceTablePart(IReadOnlyDictionary <CosObjectKey, long> objectOffsets, long offset, long previous, PdfDictionary dictionary, CrossReferenceType type) { ObjectOffsets = objectOffsets; Offset = offset; Previous = previous; Dictionary = dictionary; Type = type; }
public List <CrossReference> GetReferencesByType(CrossReferenceType type) { List <CrossReference> result; if (!_crossReferencesByType.TryGetValue(type, out result)) { result = new List <CrossReference>(0); } return(result.OrderBy(e => e.ReferenceName).ToList()); }
private IdNameCache GetCacheByCrossType(CrossReferenceType type) { IdNameCache idNameCache; if (_cachesByCrossReferenceTypes.TryGetValue(type, out idNameCache)) { return(idNameCache); } LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "CrossReferencesQuery.GetCacheByCrossType не удалось найти кэш для типа {0}!", type); return(null); }
public long Add(string source, CrossReferenceType sourceType, string destination, CrossReferenceType destinationType) { long sourceId = GetIdByName(source, sourceType); long destinationId = GetIdByName(destination, destinationType); if (IdValidator.IsInvalid(sourceId) || IdValidator.IsInvalid(destinationId)) { return(IdValidator.INVALID_ID); } if (sourceId == destinationId && string.Equals(source, destination, StringComparison.InvariantCultureIgnoreCase)) { //саму на себя ссылку нельзя добавлять return(IdValidator.INVALID_ID); } long result = IdValidator.INVALID_ID; var parsedSourceType = (int)sourceType; var parsedDestinationType = (int)destinationType; Adapter.ActionByContext(c => { CrossReference crossReference = c.CrossReference.FirstOrDefault( e => e.SourceId == sourceId && e.SourceType == parsedSourceType && e.DestinationId == destinationId && e.DestinationType == parsedDestinationType); if (crossReference != null) { result = crossReference.Id; return; } crossReference = new CrossReference { SourceId = sourceId, SourceType = parsedSourceType, DestinationId = destinationId, DestinationType = parsedDestinationType }; c.CrossReference.Add(crossReference); c.SaveChanges(); if (IdValidator.IsInvalid(crossReference.Id)) { return; } result = crossReference.Id; }); return(result); }
public CrossReferenceTable(CrossReferenceType type, IReadOnlyDictionary <IndirectReference, long> objectOffsets, DictionaryToken dictionary) { if (objectOffsets == null) { throw new ArgumentNullException(nameof(objectOffsets)); } Type = type; Dictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary)); var result = new Dictionary <IndirectReference, long>(); foreach (var objectOffset in objectOffsets) { result[objectOffset.Key] = objectOffset.Value; } this.objectOffsets = result; }
internal CrossReferenceTable(CrossReferenceType type, IReadOnlyDictionary <IndirectReference, long> objectOffsets, TrailerDictionary trailer, IReadOnlyList <CrossReferenceOffset> crossReferenceOffsets) { if (objectOffsets == null) { throw new ArgumentNullException(nameof(objectOffsets)); } Type = type; Trailer = trailer ?? throw new ArgumentNullException(nameof(trailer)); CrossReferenceOffsets = crossReferenceOffsets ?? throw new ArgumentNullException(nameof(crossReferenceOffsets)); var result = new Dictionary <IndirectReference, long>(); foreach (var objectOffset in objectOffsets) { result[objectOffset.Key] = objectOffset.Value; } this.objectOffsets = result; }
public CrossReferenceTable Build(long firstCrossReferenceOffset, ILog log) { CrossReferenceType type = CrossReferenceType.Table; DictionaryToken trailerDictionary = new DictionaryToken(new Dictionary <NameToken, IToken>()); Dictionary <IndirectReference, long> objectOffsets = new Dictionary <IndirectReference, long>(); List <long> xrefSeqBytePos = new List <long>(); var currentPart = parts.FirstOrDefault(x => x.Offset == firstCrossReferenceOffset); if (currentPart == null) { // no XRef at given position log.Warn("Did not found XRef object at specified startxref position " + firstCrossReferenceOffset); // use all objects in byte position order (last entries overwrite previous ones) xrefSeqBytePos.AddRange(parts.Select(x => x.Offset)); xrefSeqBytePos.Sort(); } else { // copy xref type type = currentPart.Type; // found starting Xref object // add this and follow chain defined by 'Prev' keys xrefSeqBytePos.Add(firstCrossReferenceOffset); while (currentPart.Dictionary != null) { long prevBytePos = currentPart.GetPreviousOffset(); if (prevBytePos == -1) { break; } currentPart = parts.FirstOrDefault(x => x.Offset == prevBytePos); if (currentPart == null) { log.Warn("Did not found XRef object pointed to by 'Prev' key at position " + prevBytePos); break; } xrefSeqBytePos.Add(prevBytePos); // sanity check to prevent infinite loops if (xrefSeqBytePos.Count >= parts.Count) { break; } } // have to reverse order so that later XRefs will overwrite previous ones xrefSeqBytePos.Reverse(); } // merge used and sorted XRef/trailer foreach (long bPos in xrefSeqBytePos) { var currentObject = parts.First(x => x.Offset == bPos); if (currentObject.Dictionary != null) { foreach (var entry in currentObject.Dictionary.Data) { /* * If we're at a second trailer, we have a linearized pdf file, meaning that the first Size entry represents * all of the objects so we don't need to grab the second. */ if (!entry.Key.Equals("Size", StringComparison.OrdinalIgnoreCase) || !trailerDictionary.ContainsKey(NameToken.Size)) { trailerDictionary = trailerDictionary.With(entry.Key, entry.Value); } } } foreach (var item in currentObject.ObjectOffsets) { objectOffsets[item.Key] = item.Value; } } return(new CrossReferenceTable(type, objectOffsets, new TrailerDictionary(trailerDictionary), parts.Select(x => { var prev = x.GetPreviousOffset(); return new CrossReferenceTable.CrossReferenceOffset(x.Offset, prev >= 0 ? prev : default(long?)); }).ToList())); }
public void Fill(LanguageShortName languageShortName, string fileName) { long languageId = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown).GetByShortName(languageShortName).Id; var crossReferencesQuery = new CrossReferencesQuery(languageId); bool hasErrors = false; var ids = new HashSet <long>(); var csvReader = new CsvReader(fileName); int i = 0; do { i++; string[] line = csvReader.ReadLine(); if (line == null) { break; } if (line.Length != 4) { ShowError( "Некорректное кол-во полей в строке {0}: Ожидаем 4, а полей {1}. Для продолжения нажмите Enter...", i, line.Length); continue; } string source = GetTrimmed(line[0]); CrossReferenceType sourceType = GetCrossType(line[1]); string destination = GetTrimmed(line[2]); CrossReferenceType destinationType = GetCrossType(line[3]); if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(destination) || sourceType == CrossReferenceType.Unknown || destinationType == CrossReferenceType.Unknown) { ShowError("Некорректные данные в строке {0}: {1}. Для продолжения нажмите Enter...", i, string.Join(";", line)); continue; } long id = crossReferencesQuery.Add(source, sourceType, destination, destinationType); if (IdValidator.IsValid(id)) { ids.Add(id); Console.WriteLine("Обработана строка под номером {0}", i); } else { hasErrors = true; ShowError( "Не удалось сохранить данные из строки {0}. Источник {1}, тип источника {2}, приемник {3}, тип источника {4}. Для продолжения нажмите Enter...", i, source, sourceType, destination, destinationType); } } while (true); if (!hasErrors) { crossReferencesQuery.RemoveAllExceptIds(ids); } Console.WriteLine("С ссылками все"); Console.ReadLine(); }
private Tuple <CrossReferenceType, string> GetTitleByType(CrossReferenceType type) { string title = "Смотрите " + _titlesByTypes[type]; return(new Tuple <CrossReferenceType, string>(type, title)); }
public CrossReference(long id, string referenceName, CrossReferenceType type) { Id = id; ReferenceName = referenceName; Type = type; }
public CrossReferenceTable Build(long startXrefOffset, ILog log) { CrossReferenceType type = CrossReferenceType.Table; PdfDictionary trailerDictionary = new PdfDictionary(); Dictionary <CosObjectKey, long> objectOffsets = new Dictionary <CosObjectKey, long>(); List <long> xrefSeqBytePos = new List <long>(); var currentPart = parts.FirstOrDefault(x => x.Offset == startXrefOffset); if (currentPart == null) { // no XRef at given position log.Warn("Did not found XRef object at specified startxref position " + startXrefOffset); // use all objects in byte position order (last entries overwrite previous ones) xrefSeqBytePos.AddRange(parts.Select(x => x.Offset)); xrefSeqBytePos.Sort(); } else { // copy xref type type = currentPart.Type; // found starting Xref object // add this and follow chain defined by 'Prev' keys xrefSeqBytePos.Add(startXrefOffset); while (currentPart.Dictionary != null) { long prevBytePos = currentPart.Dictionary.GetLongOrDefault(CosName.PREV, -1L); if (prevBytePos == -1) { break; } currentPart = parts.FirstOrDefault(x => x.Offset == prevBytePos); if (currentPart == null) { log.Warn("Did not found XRef object pointed to by 'Prev' key at position " + prevBytePos); break; } xrefSeqBytePos.Add(prevBytePos); // sanity check to prevent infinite loops if (xrefSeqBytePos.Count >= parts.Count) { break; } } // have to reverse order so that later XRefs will overwrite previous ones xrefSeqBytePos.Reverse(); } // merge used and sorted XRef/trailer foreach (long bPos in xrefSeqBytePos) { var currentObject = parts.First(x => x.Offset == bPos); if (currentObject.Dictionary != null) { foreach (var entry in currentObject.Dictionary) { /* * If we're at a second trailer, we have a linearized pdf file, meaning that the first Size entry represents * all of the objects so we don't need to grab the second. */ if (!entry.Key.Name.Equals("Size") || !trailerDictionary.ContainsKey(CosName.Create("Size"))) { trailerDictionary.Set(entry.Key, entry.Value); } } } foreach (var item in currentObject.ObjectOffsets) { objectOffsets[item.Key] = item.Value; } } return(new CrossReferenceTable(type, objectOffsets, trailerDictionary)); }
private string GetNameById(long id, CrossReferenceType type) { IdNameCache idNameCache = GetCacheByCrossType(type); return(idNameCache != null?idNameCache.GetName(id) : null); }
private long GetIdByName(string name, CrossReferenceType type) { IdNameCache idNameCache = GetCacheByCrossType(type); return(idNameCache != null?idNameCache.GetId(name) : IdValidator.INVALID_ID); }