private bool SaveProcess(DbContextWrapper ilcdDb) { bool isSaved = false; string lookupName; string uuid = GetCommonUUID(); string version = GetCommonVersion(); if (!ilcdDb.IlcdEntityAlreadyExists<LcaDataModel.Process>(uuid, version)) { Program.Logger.InfoFormat("Importing process with uuid {0}", uuid); LcaDataModel.Process process = new LcaDataModel.Process(); SaveIlcdEntity(ilcdDb, process, DataTypeEnum.Process); process.Name = GetElementValue(ElementName("baseName")); process.ReferenceYear = GetElementValue(_CommonNamespace + "referenceYear"); string geog = GetElementAttributeValue(ElementName("locationOfOperationSupplyOrProduction"), "location"); if (geog.Length > 15) geog = geog.Substring(0, 15); process.Geography = geog; lookupName = GetElementAttributeValue(ElementName("quantitativeReference"), "type"); if (lookupName != null) { process.ReferenceTypeID = ilcdDb.LookupEntityID<ReferenceType>(lookupName); } lookupName = GetElementValue(ElementName("typeOfDataSet")); if (lookupName != null) { process.ProcessTypeID = ilcdDb.LookupEntityID<ProcessType>(lookupName); } if (ilcdDb.AddIlcdEntity(process, uuid, version)) { List<ProcessFlow> pfList = LoadedDocument.Root.Descendants(ElementName("exchanges")).Elements(ElementName("exchange")).Select(f => CreateProcessFlow(ilcdDb, f, process.ID)).ToList(); ilcdDb.AddEntities<ProcessFlow>(pfList); isSaved = true; } } 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; }
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> /// Create an LCIA entity from an LCIAMethod characterization factor. /// </summary> /// <param name="ilcdDb">Database context wrapper object</param> /// <param name="el">LCIAMethod characterization factor element</param> /// <param name="lciaMethodID">LCIAMethod parent entity ID</param> private LCIA CreateLCIA(DbContextWrapper ilcdDb, XElement el, int lciaMethodID, string lciaMethodUUID) { LCIA lcia = null; XElement refEl = el.Element(ElementName("referenceToFlowDataSet")); string uuid = refEl.Attribute("refObjectId").Value; double meanValue = (double)el.Element(ElementName(("meanValue"))); string direction = (string)el.Element(ElementName(("exchangeDirection"))); string location = (string)el.Element(ElementName(("location"))); string name = (string)refEl.Element(_CommonNamespace + "shortDescription"); // Most of the referenced flows will not be found, so don't bother searching for them now. // The program will update LCIA flow references before exiting. //Flow flow = ilcdDb.GetIlcdEntity<Flow>(uuid); //int? id = null; //if (flow == null) { // Program.Logger.WarnFormat("Unable to find flow matching LCIA refObjectId = {0}", uuid); //} //else { // id = flow.FlowID; //} int? dirID = ilcdDb.LookupEntityID<Direction>(direction); if (dirID == null) { Program.Logger.ErrorFormat("Invalid LCIA exchangeDirection : {0}, refObjectId = {1}. LCIA Method UUID = {2}. Skipping record.", direction, uuid, lciaMethodUUID); } else { int reqDirID = Convert.ToInt32(dirID); lcia = new LCIA { //FlowID = id, FlowUUID = uuid, FlowName = name, DirectionID = reqDirID, Factor = meanValue, Geography = location, LCIAMethodID = lciaMethodID }; } return lcia; }