public static Tuple <string, bool> GetAuthor(ObjectVersionWrapper obj, CountriesClient countries) { var author = obj.Author; var cauthor = obj.CorporateAuthor; var player = obj.Player; if (!string.IsNullOrWhiteSpace(author)) { return(new Tuple <string, bool>(author, false)); } if (!string.IsNullOrEmpty(cauthor)) { return(new Tuple <string, bool>(cauthor, true)); } /*if (!string.IsNullOrEmpty(player)) * { * var country = countries.GetCountryIsoCode2(obj.Player); * if (country != null) * { * return new Tuple<string, bool>(player, true); * } * }*/ return(null); }
private static void ProcessTags(DocumentsContext ctx, Document masterDoc, ObjectVersionWrapper sourceDoc) { masterDoc.Tags.Clear(); var propertyType = ctx.ValueTypes.First(x => x.Name == ListPropertyTypesNames.Tag); foreach (var source in sourceDoc.Tags) { var values = (source.IsStringProperty ? source.Value.Split(',').Select(x => x.Trim().ToLower()).Where(x => !String.IsNullOrEmpty(x)).ToArray() : new[] { source.Value.ToLower() }); foreach (var value in values) { var target = ctx.Values.FirstOrDefault(t => t.Value == value); if (target == null) { var listPropertyId = Guid.NewGuid(); if (!source.IsStringProperty && source.Guid.HasValue) { listPropertyId = source.Guid.Value; } target = new Documents.ListProperty { ListPropertyId = listPropertyId, Value = value.Trim() }; ctx.Values.Add(target); } target.Types.Add(propertyType); masterDoc.Tags.Add(target); } } }
public void ProcessObject(ObjectVersionWrapper obj) { ClassLogger.Trace($"Process {obj.Title} from {obj.VaultName} with UN-Number {obj.UnNumber}"); _parent.ProcessedMFilesGuids[obj.Guid] = true; var doc = Logic.FindDocument(_ctx, obj.Guid); var master = Logic.FindMaster(_ctx, string.IsNullOrEmpty(obj.UnNumber) ? obj.Name : obj.UnNumber); var masterByGuid = Logic.FindMasterById(_ctx, obj.Guid); if (doc != null && (master == null || masterByGuid == null || master.Guid != masterByGuid.Guid)) // Changed UnNumber { Logic.Delete(_ctx, doc); doc = null; } // Special case for Basel convention if (master != null && obj.VaultName != "Basel" && master.Convention == "basel") { Logic.Delete(_ctx, doc); return; } if (doc == null) { if (master == null) { ClassLogger.Info($"Create master document '{obj.File.Name}.{obj.File.Extension}' {obj.ModifiedDate}"); master = Logic.CreateMaster(_ctx, obj, _parent.VaultDetails, _parent.Countries); } if (master != null) { if (master.Guid != obj.Guid) { ClassLogger.Info( $"Create slave document '{obj.File.Name}.{obj.File.Extension}' {obj.ModifiedDate}"); } Logic.CreateSlave(_ctx, master, obj, _parent.VaultDetails, _parent.ThumbnailsUrlPattern); } } else if (doc.ModifiedDate != obj.ModifiedDate) { if (master.Guid == doc.Guid) { ClassLogger.Info($"Update master document '{obj.File.Name}.{obj.File.Extension}' {obj.ModifiedDate}"); Logic.UpdateMaster(_ctx, master, obj, _parent.VaultDetails, _parent.Countries); } else { ClassLogger.Info($"Update slave document '{obj.File.Name}.{obj.File.Extension}' {obj.ModifiedDate}"); } Logic.UpdateSlave(_ctx, master, doc, obj, _parent.VaultDetails, _parent.ThumbnailsUrlPattern); } }
public static MFilesDocument CreateMaster(DocumentsContext ctx, ObjectVersionWrapper sourceDoc, IDictionary <string, VaultDetails> vaultDetails, CountriesClient coutries) { var targetDoc = ctx.MFilesDocuments.Create(); ctx.MFilesDocuments.Add(targetDoc); var masterDoc = new Document { MFilesDocument = targetDoc }; ctx.Documents.Add(masterDoc); UpdateMaster(ctx, targetDoc, sourceDoc, vaultDetails, coutries); return(targetDoc); }
public static bool UpdateMaster(DocumentsContext ctx, MFilesDocument targetDoc, ObjectVersionWrapper sourceDoc, IDictionary <string, VaultDetails> vaultDetails, CountriesClient countries) { targetDoc.Guid = sourceDoc.Guid; targetDoc.CreatedDate = sourceDoc.CreatedDate; targetDoc.ModifiedDate = sourceDoc.ModifiedDate; var masterDoc = targetDoc.Document; masterDoc.MFilesDocument = targetDoc; masterDoc.UnNumber = string.IsNullOrEmpty(sourceDoc.UnNumber)?sourceDoc.Name:sourceDoc.UnNumber; masterDoc.Convention = vaultDetails[sourceDoc.VaultName].NameInDb ?? sourceDoc.VaultName.ToLower(); var authorAndType = GetAuthor(sourceDoc, countries); if (authorAndType != null) { masterDoc.Author = authorAndType.Item1; masterDoc.AuthorType = authorAndType.Item2 ? "organization" : "person"; } masterDoc.CountryFull = countries.GetCountryIsoCode2(sourceDoc.Country) != null ? sourceDoc.Country: null; masterDoc.Country = countries.GetCountryIsoCode2(masterDoc.CountryFull); masterDoc.Copyright = sourceDoc.Copyright; var period = sourceDoc.GetPeriod(); if (period != null) { masterDoc.PeriodStartDate = period.Item1; masterDoc.PeriodEndDate = period.Item2; } masterDoc.PublicationDate = sourceDoc.PublicationDate; lock ("Process") { ProcessDocumentTypes(ctx, masterDoc, sourceDoc); ProcessMeetings(ctx, masterDoc, sourceDoc); ProcessMeetingTypes(ctx, masterDoc, sourceDoc); ProcessChemicals(ctx, masterDoc, sourceDoc); ProcessPrograms(ctx, masterDoc, sourceDoc); ProcessTerms(ctx, masterDoc, sourceDoc); ProcessTags(ctx, masterDoc, sourceDoc); using (var trans = ctx.Database.BeginTransaction()) { try { ctx.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); ClassLogger.Error("SQL exception " + ex); throw; } } } return(true); }
public static MFilesDocument UpdateSlave(DocumentsContext ctx, MFilesDocument masterDoc, MFilesDocument targetDoc, ObjectVersionWrapper sourceDoc, IDictionary <string, VaultDetails> vaultDetails, string thumbnailsUrlPattern) { if (sourceDoc.Guid != masterDoc.Guid) { targetDoc.Guid = sourceDoc.Guid; targetDoc.ModifiedDate = sourceDoc.ModifiedDate; targetDoc.CreatedDate = sourceDoc.CreatedDate; } else { targetDoc = masterDoc; } var doc = masterDoc.Document; Debug.Assert(doc != null); string languageCode; // TODO: should be in configuration or don't use CultureUtils // or we need to update language in M-Files if (sourceDoc.Language == "Portugese") { languageCode = "pt"; } else if (sourceDoc.Language == "Azeri") { languageCode = "az"; } else { languageCode = CultureUtils.GetLangTwoLetterCode(sourceDoc.Language); } if (languageCode == null) { ClassLogger.Warn( $"Could not find language code for {sourceDoc.Language} (Document {sourceDoc.UnNumber})"); return(null); } var title = doc.Titles.FirstOrDefault(t => t.Language == languageCode && t.Document == doc); if (title == null || title.MFilesDocument == targetDoc) { if (title == null) { title = new Title { MFilesDocument = targetDoc }; doc.Titles.Add(title); } title.Document = doc; title.Language = languageCode; title.LanguageFull = sourceDoc.Language; title.MFilesDocument = targetDoc; title.Value = sourceDoc.Title; } var descirpiton = doc.Descriptions.FirstOrDefault(t => t.Language == languageCode && t.Document == doc); if (descirpiton == null || descirpiton.MFilesDocument == targetDoc) { if (descirpiton == null) { descirpiton = new Description(); doc.Descriptions.Add(descirpiton); } descirpiton.Document = doc; descirpiton.Language = languageCode; descirpiton.LanguageFull = sourceDoc.Language; descirpiton.MFilesDocument = targetDoc; descirpiton.Value = sourceDoc.Description; } var file = sourceDoc.File; var repositoryUrl = vaultDetails[sourceDoc.VaultName].Url ?? ""; var targetFile = ctx.Files.FirstOrDefault(f => f.FileId == targetDoc.Guid); if (targetFile == null) { targetFile = new File(); doc.Files.Add(targetFile); } targetFile.Document = doc; targetFile.MFilesDocument = targetDoc; targetFile.Language = languageCode; targetFile.LanguageFull = sourceDoc.Language; targetFile.Name = file.Name; targetFile.Extension = file.Extension.ToLower(); targetFile.Size = file.Size; targetFile.MimeType = Mime.Lookup(file.Name + "." + file.Extension.ToLower()); targetFile.Url = file.GetUrl(repositoryUrl); targetFile.ThumbnailUrl = thumbnailsUrlPattern.Replace("{vault}", sourceDoc.VaultName) .Replace("{file}", $"{targetFile.Name}.{targetFile.Extension}"); using (var trans = ctx.Database.BeginTransaction()) { try { ctx.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); ClassLogger.Error(ex); throw; } } return(targetDoc); }
public static MFilesDocument CreateSlave(DocumentsContext ctx, MFilesDocument master, ObjectVersionWrapper sourceDoc, IDictionary <string, VaultDetails> vaultDetails, string thumbnailsUrlPattern) { MFilesDocument targetDoc = null; if (sourceDoc.Guid != master.Guid) { targetDoc = ctx.MFilesDocuments.Create(); } else { targetDoc = master; } return(UpdateSlave(ctx, master, targetDoc, sourceDoc, vaultDetails, thumbnailsUrlPattern)); }
private static void ProcessMeetingTypes(DocumentsContext ctx, Document masterDoc, ObjectVersionWrapper sourceDoc) { masterDoc.MeetingsTypes.Clear(); var propertyType = ctx.ValueTypes.First(x => x.Name == ListPropertyTypesNames.MeetingType); foreach (var source in sourceDoc.MeetingsTypes) { var target = ctx.Values.FirstOrDefault(t => t.Value == source.Value); if (target == null) { target = new Documents.ListProperty { ListPropertyId = source.Guid ?? Guid.NewGuid(), Value = source.Value }; ctx.Values.Add(target); } target.Types.Add(propertyType); masterDoc.MeetingsTypes.Add(target); } }