private XElement AddEntityElement(Entity entity, SchemabasedEntityActionEnum entityAction, string uniqueFieldType) { XElement element = new XElement(targetNamespace + entity.EntityType.Id); element.Add(new XAttribute("EntityId", entity.Id)); if (!string.IsNullOrEmpty(entity.FieldSetId) && entity.EntityType.FieldSets.Any(et => et.Id == entity.FieldSetId)) { element.Add(new XAttribute("FieldSet", entity.FieldSetId)); } switch (entityAction) { case SchemabasedEntityActionEnum.New: element.Add(new XAttribute("Action", "New")); break; case SchemabasedEntityActionEnum.Updated: element.Add(new XAttribute("Action", "Updated")); break; case SchemabasedEntityActionEnum.Deleted: element.Add(new XAttribute("Action", "Deleted")); break; } if (!string.IsNullOrEmpty(uniqueFieldType)) { element.Add(new XAttribute("ExternalUniqueIdField", uniqueFieldType)); } return(element); }
public XDocument GenerateXml(Entity entity, SchemabasedEntityActionEnum entityAction, Dictionary <string, string> uniqueFieldTypes, int channelId = 0, Link linkToBeDeleted = null) { XDocument doc = null; if (entity == null) { context.Log(LogLevel.Error, "Entity argument is null in Generate Xml in SchemabasedOutput. Aborting."); return(doc); } try { this.currentChannelId = channelId; this.deletedLink = linkToBeDeleted; ReGenerateSchema(entity.EntityType.Id, false); doc = this.GetDocumentBase(entity.EntityType.Id); XElement root = doc.Element(targetNamespace + entity.EntityType.Id + "s"); if (root != null) { string uniqueFieldType = string.Empty; if (uniqueFieldTypes.ContainsKey(entity.EntityType.Id)) { uniqueFieldType = uniqueFieldTypes[entity.EntityType.Id]; } root.Add(this.CreateEntity(entity, entityAction, uniqueFieldType, uniqueFieldTypes)); } } catch (Exception exception) { context.Log(LogLevel.Error, $"Error during generating xml for entity id {entity.Id}:", exception); doc = null; } return(doc); }
private XElement CreateEntity(int entityId, SchemabasedEntityActionEnum entityAction, string uniqueFieldType, Dictionary <string, string> uniqueFieldTypes) { Entity entity = context.ExtensionManager.DataService.GetEntity(entityId, LoadLevel.DataAndLinks); if (entity == null) { return(null); } return(this.CreateEntity(entity, entityAction, uniqueFieldType, uniqueFieldTypes)); }
public string GenerateXml(int entityId, SchemabasedEntityActionEnum entityAction, Dictionary <string, string> uniqueFieldTypes, bool generateNewSchema, int channelId = 0, Link linkToBeDeleted = null) { if (entityId == 1401 || entityId == 906) { Console.WriteLine("Test"); } Entity entity = context.ExtensionManager.DataService.GetEntity(entityId, LoadLevel.Shallow); return(GenerateXml(entity, entityAction, uniqueFieldTypes, generateNewSchema, channelId, linkToBeDeleted)); }
public string GenerateXml(KeyValuePair <string, List <int> > entityTypePair, SchemabasedEntityActionEnum entityAction, Dictionary <string, string> uniqueFieldTypes, bool generateNewSchema, int channelId = 0) { try { string result = string.Empty; this.currentChannelId = channelId; List <int> entityIds = entityTypePair.Value; if (entityIds == null || !entityIds.Any()) { context.Log(LogLevel.Error, $"Selected entities with entity type id {entityTypePair.Key} was not found when Generate Xml in SchemabasedOutput."); return(result); } ReGenerateSchema(entityTypePair.Key, generateNewSchema); var doc = this.GetDocumentBase(entityTypePair.Key); XElement root = doc.Element(targetNamespace + entityTypePair.Key + "s"); if (root != null) { foreach (int entityId in entityIds) { string uniqueFieldType = string.Empty; if (uniqueFieldTypes.ContainsKey(entityTypePair.Key)) { uniqueFieldType = uniqueFieldTypes[entityTypePair.Key]; } XElement xElement = this.CreateEntity(entityId, entityAction, uniqueFieldType, uniqueFieldTypes); if (xElement != null) { root.Add(xElement); } } } // Validate xml this.ValidateXml(xmlSchemas[entityTypePair.Key], doc, entityTypePair.Key); result = doc.ToString(); return(result); } catch (Exception exception) { context.Log(LogLevel.Error, $"Error during generating xml for entity type {entityTypePair.Key}:", exception); throw; } }
public string GenerateXml(Entity entity, SchemabasedEntityActionEnum entityAction, Dictionary <string, string> uniqueFieldTypes, bool generateNewSchema, int channelId = 0, Link linkToBeDeleted = null) { string result = string.Empty; if (entity == null) { context.Log(LogLevel.Error, "Entity argument is null in Generate Xml in SchemabasedOutput. Aborting."); return(result); } try { this.currentChannelId = channelId; this.deletedLink = linkToBeDeleted; ReGenerateSchema(entity.EntityType.Id, generateNewSchema); XDocument doc = this.GetDocumentBase(entity.EntityType.Id); XElement root = doc.Element(targetNamespace + entity.EntityType.Id + "s"); if (root != null) { string uniqueFieldType = string.Empty; if (uniqueFieldTypes.ContainsKey(entity.EntityType.Id)) { uniqueFieldType = uniqueFieldTypes[entity.EntityType.Id]; } XElement xElement = this.CreateEntity(entity.Id, entityAction, uniqueFieldType, uniqueFieldTypes); if (xElement != null) { root.Add(xElement); } } // Validate xml this.ValidateXml(xmlSchemas[entity.EntityType.Id], doc, entity.EntityType.Id); result = doc.ToString(); return(result); } catch (Exception exception) { context.Log(LogLevel.Error, $"Error during generating xml for entity id {entity.Id}:", exception); throw; } }
private bool WriteEntityToFile(int channelId, Entity entity, SchemabasedEntityActionEnum action, Link linkToBeDeleted = null) { if (entity == null) { return(false); } if (!common.ShouldEntityBeExported(entity, adapterSettings)) { Context.Log(LogLevel.Information, $"Entity type {entity.EntityType.Id} are not included in the list that shall be exported."); return(false); } XDocument entityXml = schemaBasedOutput.GenerateXml(entity, action, ExternalUniqueFileTypes, channelId, linkToBeDeleted); if (!schemaBasedOutput.IsValid(entity.EntityType.Id, entityXml)) { return(false); } string uniqueId = common.GetUniqueId(ExternalUniqueFileTypes, entity.Id, entity.EntityType.Id, entityXml); return(azureListenerHelper.WriteEntityXmlToBlobFile(entity.EntityType.Id, uniqueId, entityXml, this.adapterSettings[ConnectorSettings.PublishFolder])); }
private bool WriteEntityToFile(int channelId, int entityId, SchemabasedEntityActionEnum action, Link linkToBeDeleted = null) { var entity = Context.ExtensionManager.DataService.GetEntity(entityId, LoadLevel.DataOnly); return(WriteEntityToFile(channelId, entity, action, linkToBeDeleted)); }
public void Publish(int channelId) { if (!initializeListener()) { return; } if (!isForConfiguredChannel(channelId)) { return; } bool shouldGenerateSchema = true; const SchemabasedEntityActionEnum SchemabasedEntityAction = SchemabasedEntityActionEnum.New; int percentage = 0; //const ConnectorEventType CurrentEventType = ConnectorEventType.Publish; //Guid sessionId = EventHelper.FireEvent(channelId, this.Id, CurrentEventType, 0, string.Format("Publish started for channel id {0}", channelId)); Context.Log(LogLevel.Information, $"Publish started for channel id {channelId}"); try { Dictionary <string, List <int> > entitiesInStructure = channelHelper.GetEntityIdsInChannelByEntityType(channelId); int totalEntities = entitiesInStructure.Aggregate(0, (current, pair) => current + pair.Value.Count); int totalFiles = entitiesInStructure.Where(pair => pair.Key == "Resource") .Sum(pair => pair.Value.Select(resource => Context.ExtensionManager.DataService.GetEntity(resource, LoadLevel.DataOnly)) .Select(entityWithField => entityWithField.GetField("ResourceFileId")) .Count(identityField => identityField != null && identityField.Data != null)); int totalAmount = totalEntities + totalFiles; int totalCount = totalAmount; List <string> entityTypesToExport = mappingHelper.GetEntityTypeIdToExportFromXml(adapterSettings[ConnectorSettings.MappingXml]); foreach (KeyValuePair <string, List <int> > valuePair in entitiesInStructure) { percentage = common.GetPercentage(totalAmount, totalCount); Context.Log(LogLevel.Verbose, $"Start working with {valuePair.Key}"); totalCount = totalCount - valuePair.Value.Count; if (!entityTypesToExport.Contains(valuePair.Key)) { Context.Log(LogLevel.Information, $"Entity type {valuePair.Key} is not included in the list that shall be exported."); continue; } if (SingleFile) { if (MaxEntitiesInFile == 0) { // One big file per entity type string entityTypeXml; try { entityTypeXml = schemaBasedOutput.GenerateXml(valuePair, SchemabasedEntityAction, ExternalUniqueFileTypes, shouldGenerateSchema, channelId); } catch (Exception exception) { Context.Log(LogLevel.Error, $"Error while generating XML for {valuePair.Key}s:", exception); continue; } XDocument doc = XDocument.Parse(entityTypeXml); azureListenerHelper.PublishEntityTypeXml(valuePair.Key, doc, adapterSettings[ConnectorSettings.PublishFolder]); azureListenerHelper.WriteCvlForEntityType(Context.ExtensionManager.ModelService.GetEntityType(valuePair.Key), adapterSettings[ConnectorSettings.CvlFolder]); } else { // Split it up into several files. int refCount = 0; List <int> entities = valuePair.Value; int maxRange = entities.Count; int fileNumber = 0; while (refCount < maxRange) { int numberOfEntities = MaxEntitiesInFile; if (entities.Count < refCount + numberOfEntities) { numberOfEntities = entities.Count - refCount; } List <int> result = entities.GetRange(refCount, numberOfEntities); if (result.Count == 0) { break; } refCount += result.Count; KeyValuePair <string, List <int> > entityTypePair = new KeyValuePair <string, List <int> >(valuePair.Key, result); string entityTypeXml; try { entityTypeXml = schemaBasedOutput.GenerateXml(entityTypePair, SchemabasedEntityAction, ExternalUniqueFileTypes, shouldGenerateSchema, channelId); shouldGenerateSchema = false; } catch (Exception exception) { Context.Log(LogLevel.Error, $"Error while generating XML for {valuePair.Key}s:", exception); continue; } XDocument doc = XDocument.Parse(entityTypeXml); fileNumber++; azureListenerHelper.PublishEntityTypeXml(valuePair.Key, doc, adapterSettings[ConnectorSettings.PublishFolder], fileNumber); } azureListenerHelper.WriteCvlForEntityType(Context.ExtensionManager.ModelService.GetEntityType(valuePair.Key), adapterSettings[ConnectorSettings.CvlFolder]); } } else { // One file per entity foreach (int entityId in valuePair.Value) { // string entityXml; try { WriteEntityToFile(channelId, entityId, SchemabasedEntityAction); shouldGenerateSchema = false; } catch (Exception exception) { Context.Log(LogLevel.Error, $"Error while generating XML for Entity with id {entityId}", exception); } } EntityType entityType = Context.ExtensionManager.ModelService.GetEntityType(valuePair.Key); azureListenerHelper.WriteCvlForEntityType(entityType, adapterSettings[ConnectorSettings.CvlFolder]); } if (valuePair.Key == "Resource") { percentage = common.GetPercentage(totalAmount, totalCount); Context.Log(LogLevel.Verbose, "Start working with resource files"); totalCount = totalCount - azureListenerHelper.StoreResourceFiles(valuePair.Value, adapterSettings); } } Context.Log(LogLevel.Information, "Publish complete."); } catch (Exception exception) { Context.Log(LogLevel.Error, $"Error when publishing channel {channelId} ", exception); } }
private XElement CreateEntity(Entity entity, SchemabasedEntityActionEnum entityAction, string uniqueFieldType, Dictionary <string, string> uniqueFieldTypes) { XElement entityElement = this.AddEntityElement(entity, entityAction, uniqueFieldType); // Fields XElement fieldsElement = new XElement(targetNamespace + $"{entity.EntityType.Id}Fields"); entityElement.Add(fieldsElement); foreach (Field field in entity.Fields) { if (ExcludedFieldTypes.Contains(field.FieldType.Id)) { // Do not include this field. continue; } XElement fieldElement = new XElement(targetNamespace + field.FieldType.Id); if (field.Data == null) { continue; } switch (field.FieldType.DataType) { case "LocaleString": fieldElement.Add(this.AddLocaleStringFieldData(field)); break; case "CVL": if (field.FieldType.Multivalue) { string fieldData = field.Data.ToString(); var multivalues = fieldData.Split(';'); if (string.IsNullOrEmpty(fieldData)) { fieldElement.Add(new XElement(this.targetNamespace + "Data", string.Empty)); } else { foreach (string value in multivalues) { fieldElement.Add(new XElement(this.targetNamespace + "Data", value)); } } } else { fieldElement.Add(new XElement(targetNamespace + "Data", string.Format("{0}", field.Data))); } fieldElement.Add(new XAttribute("Cvl", field.FieldType.CVLId)); break; default: fieldElement.Add(new XElement(targetNamespace + "Data", string.Format("{0}", field.Data))); break; } fieldsElement.Add(fieldElement); } // Links XElement linksElement = new XElement(targetNamespace + string.Format("{0}Links", entity.EntityType.Id)); XElement parentLinksElement = new XElement(targetNamespace + string.Format("{0}ParentLinks", entity.EntityType.Id)); XElement childLinksElement = new XElement(targetNamespace + string.Format("{0}ChildLinks", entity.EntityType.Id)); foreach (Link inboundLink in context.ExtensionManager.ChannelService.GetInboundLinksForEntity(currentChannelId, entity.Id)) { Entity otherEntity = context.ExtensionManager.DataService.GetEntity(inboundLink.Source.Id, LoadLevel.DataOnly); XElement linkElement = this.AddLinkElement(inboundLink, otherEntity, uniqueFieldTypes); parentLinksElement.Add(linkElement); } foreach (Link outboundLink in context.ExtensionManager.ChannelService.GetOutboundLinksForEntity(currentChannelId, entity.Id)) { Entity otherEntity = context.ExtensionManager.DataService.GetEntity(outboundLink.Target.Id, LoadLevel.DataOnly); XElement linkElement = this.AddLinkElement(outboundLink, otherEntity, uniqueFieldTypes); childLinksElement.Add(linkElement); } if (deletedLink != null) { Entity otherEntity = context.ExtensionManager.DataService.GetEntity(deletedLink.Target.Id, LoadLevel.DataOnly); XElement linkElement = this.AddLinkElement(deletedLink, otherEntity, uniqueFieldTypes); linkElement.Add(new XAttribute("Action", "Deleted")); childLinksElement.Add(linkElement); } linksElement.Add(parentLinksElement); linksElement.Add(childLinksElement); entityElement.Add(linksElement); // Others XElement additionalsElement = new XElement(targetNamespace + string.Format("{0}Additionals", entity.EntityType.Id)); if (entity.EntityType.Id == "Specification") { additionalsElement.Add(this.AddSpecificationTemplateElement(entity)); } else if (HasSpecification(entity)) { additionalsElement.Add(AddSpecificationElement(entity)); } entityElement.Add(additionalsElement); return(entityElement); }