private bool SaveLciaMethod(DbContextWrapper ilcdDb) { bool isSaved = false; string lookupName; string refUUID; string uuid = GetCommonUUID(); if (!ilcdDb.IlcdEntityAlreadyExists<LCIAMethod>(uuid)) { LCIAMethod lciaMethod = new LCIAMethod(); SaveIlcdEntity(ilcdDb, lciaMethod, DataTypeEnum.LCIAMethod); lciaMethod.Name = GetCommonName(); lciaMethod.Methodology = GetElementValue(ElementName("methodology")); lookupName = GetElementValue(ElementName("impactCategory")); if (lookupName != null) { lciaMethod.ImpactCategoryID = (int)ilcdDb.LookupEntityID<ImpactCategory>(lookupName); } lciaMethod.ImpactIndicator = GetElementValue(ElementName("impactIndicator")); lookupName = GetElementValue(ElementName("typeOfDataSet")); if (lookupName != null) { lciaMethod.IndicatorTypeID = ilcdDb.LookupEntityID<IndicatorType>(lookupName); } lciaMethod.ReferenceYear = GetElementValue(ElementName("referenceYear")); lciaMethod.Duration = GetElementValue(ElementName("duration")); lciaMethod.ImpactLocation = GetElementValue(ElementName("impactLocation")); lciaMethod.Normalization = Convert.ToBoolean(GetElementValue(ElementName("normalisation"))); lciaMethod.Weighting = Convert.ToBoolean(GetElementValue(ElementName("weighting"))); lciaMethod.UseAdvice = GetElementValue(ElementName("useAdviceForDataSet")); refUUID = GetElementAttributeValue(ElementName("referenceQuantity"), "refObjectId"); Debug.Assert(refUUID != null); int refID; if (ilcdDb.FindRefIlcdEntityID<FlowProperty>(refUUID, out refID)) { lciaMethod.ReferenceFlowPropertyID = refID; } if (ilcdDb.AddIlcdEntity(lciaMethod, uuid)) { List<LCIA> lciaList = LoadedDocument.Root.Descendants(ElementName("characterisationFactors")).Elements(ElementName("factor")).Select(f => CreateLCIA(ilcdDb, f, lciaMethod.ID, uuid)).ToList(); ilcdDb.AddEntities<LCIA>(lciaList); isSaved = true; } } return isSaved; }
/// <summary> /// Import data from loaded flowproperty file to new FlowProperty entity /// </summary> /// <param name="ilcdDb">Database context wrapper object</param> /// <returns>true iff data was imported</returns> private bool SaveFlowProperty(DbContextWrapper ilcdDb) { bool isSaved = false; string ugUUID; string uuid = GetCommonUUID(); if (!ilcdDb.IlcdEntityAlreadyExists<FlowProperty>(uuid)) { FlowProperty flowProperty = new FlowProperty(); SaveIlcdEntity(ilcdDb, flowProperty, DataTypeEnum.FlowProperty); flowProperty.Name = GetCommonName(); ugUUID = GetElementAttributeValue(ElementName("referenceToReferenceUnitGroup"), "refObjectId"); if (ugUUID == null) { Program.Logger.WarnFormat("Unable to find referenceToReferenceUnitGroup in flow property {0}", flowProperty.ILCDEntity.UUID); } else { int ugID; if (ilcdDb.FindRefIlcdEntityID<UnitGroup>(ugUUID, out ugID)) { flowProperty.UnitGroupID = ugID; } } isSaved = ilcdDb.AddIlcdEntity(flowProperty, uuid); } return isSaved; }
/// <summary> /// Extract UUID from referenceToFlowPropertyDataSet and transform it to entity ID (FlowPropertyID). /// This depends on the referenced flow property having been previously imported. /// </summary> /// <param name="ilcdDb">Database context wrapper object</param> /// <param name="fpElement">Element containing referenceToFlowPropertyDataSet</param> /// <returns>Entity ID, if the UUID was extracted and a loaded entity ID was found, otherwise null</returns> private int GetFlowPropertyID(DbContextWrapper ilcdDb, XElement fpElement) { string fpUUID = fpElement.Element(ElementName("referenceToFlowPropertyDataSet")).Attribute("refObjectId").Value; int fpID; if (ilcdDb.FindRefIlcdEntityID<FlowProperty>( fpUUID, out fpID)) { return fpID; } else { throw new ArgumentNullException("fpID","FlowProperty UUID not found."); } }
/// <summary> /// Import data from loaded flow file to new Flow entity /// </summary> /// <param name="ilcdDb">Database context wrapper object</param> /// <returns>true iff data was imported</returns> private bool SaveFlow(DbContextWrapper ilcdDb) { bool isSaved = false; int? fpID; string dataSetInternalID = "0"; string uuid = GetCommonUUID(); int flowId = 0; if (ilcdDb.IlcdEntityAlreadyExists<Flow>(uuid)) ilcdDb.FindRefIlcdEntityID<Flow>(uuid, out flowId); else { XElement fpElement; Flow flow = new Flow(); SaveIlcdEntity(ilcdDb, flow, DataTypeEnum.Flow); // TODO : generate name from classification/category flow.Name = GetElementValue(ElementName("baseName")); flow.CASNumber = GetElementValue(ElementName("CASNumber")); flow.FlowTypeID = ilcdDb.GetFlowTypeID(GetElementValue(ElementName("typeOfDataSet"))); // Get Reference Flow Property dataSetInternalID = GetElementValue(ElementName("referenceToReferenceFlowProperty")); fpElement = GetElementWithInternalId(ElementName("flowProperty"), dataSetInternalID); fpID = GetFlowPropertyID(ilcdDb, fpElement); flow.ReferenceFlowProperty = (int)fpID; if (ilcdDb.AddIlcdEntity(flow, uuid)) { isSaved = true; flowId = flow.FlowID; } } if (flowId != 0) // successful import or existing object { // add flow properties that don't already exist var ffp = CreateFFPList(ilcdDb, flowId); ilcdDb.AddEntities<FlowFlowProperty>(ffp); // add classification data if it is not already present var cs = CreateClassificationList(ilcdDb, uuid); ilcdDb.AddEntities<Classification>(cs); Program.Logger.InfoFormat("Added {0} FlowFlowProperty entries and {1} Classification entries", ffp.Count(), cs.Count()); } return isSaved; }
/// <summary> /// Create a process flow entity from an Process exchange. /// </summary> /// <param name="ilcdDb">Database context wrapper object</param> /// <param name="el">Process exchange element</param> /// <param name="processID">Process parent entity ID</param> private ProcessFlow CreateProcessFlow(DbContextWrapper ilcdDb, XElement el, int processID) { ProcessFlow pf = null; string varName = (string)el.Element(ElementName("referenceToVariable")); string type; if (el.Element(_CommonNamespace + "other") == null ) { type = "none"; } else { type=(string)el.Element(_CommonNamespace + "other").Element(_GabiNamespace + "GaBi").Attribute("IOType"); } double magnitude = (double)el.Element(ElementName("meanAmount")); double result = (double)el.Element(ElementName("resultingAmount")); double stdev = 0; if ( el.Element("relativeStandardDeviation95In") != null) { stdev = (double)el.Elements(ElementName("relativeStandardDeviation95In")).FirstOrDefault(); } string uuid = el.Element(ElementName("referenceToFlowDataSet")).Attribute("refObjectId").Value; int flowID; if (ilcdDb.FindRefIlcdEntityID<Flow>(uuid, out flowID)) { string direction = (string)el.Element(ElementName(("exchangeDirection"))); int? dirID = ilcdDb.LookupEntityID<Direction>(direction); if (dirID == null) { Program.Logger.WarnFormat("Unable to find ID for exchangeDirection = {0}", direction); } string location = (string)el.Element(ElementName(("location"))); pf = new ProcessFlow { DirectionID = (int)dirID, FlowID = flowID, Geography = location, Magnitude = magnitude, ProcessID = processID, Result = result, STDev = stdev, Type = type, VarName = varName }; } return pf; }