/// <summary> /// Update the existing ExpressionXml with the QuotaRemaining values and insert the updated one in the column QuotaExpressionsXML /// </summary> /// <param name="steamStudyObject"></param> /// <param name="gmiSampleQuotasObject"></param> public void UpdateQuotaExpression(SteamStudyObject steamStudyObject, GMISampleQuotasObject gmiSampleQuotasObject) { string xml = steamStudyObject.ExpressionsXML; int count = -1; int quotaIndex = -1; var quotaIdVar = "<quotaId>"; var quotaIdVarEnd = "</quotaId>"; var quotaRemainingVar = "<quotaRemaining>"; var quotaRemainingVarEnd = "</quotaRemaining>"; var quotas = gmiSampleQuotasObject.GMIQuotasList.Where(p => p.SampleId == steamStudyObject.SampleId).ToList(); foreach (GMIQuotaObject gmiQuotaObject in quotas) { count = (quotaIdVar + gmiQuotaObject.QuotaId + quotaIdVarEnd).Count(); quotaIndex = xml.IndexOf(quotaIdVar + gmiQuotaObject.QuotaId + quotaIdVarEnd) + count; if (quotaIndex < count) return; xml = xml.Insert(quotaIndex, quotaRemainingVar + gmiQuotaObject.QuotaRemaining + quotaRemainingVarEnd); } steamStudyObject.QuotaExpressionsXML = xml; }
/// <summary> /// Update the quota remaining values in the GMISampleQuotasObject /// </summary> /// <param name="GMISampleQuotasObject"></param> /// <param name="QuotasLiveObject"></param> public void UpdateQuotaRemaingValues(GMISampleQuotasObject gmiSampleQuotasObject, QuotasLiveObject quotasLiveObject) { if (quotasLiveObject.QuotasLiveList == null) return; foreach (QuotaLiveObject quotaLiveObject in quotasLiveObject.QuotasLiveList) { var quota = gmiSampleQuotasObject.GMIQuotasList.Where(p => p.InternalQuotaId == quotaLiveObject.InternalQuotaId).ToList(); if (quota.Count > 0) quota[0].QuotaRemaining = quotaLiveObject.QuotaRemaining; } }
/// Fetches the samples and their quotas from GMI service for a specific study /// </summary> /// <param name="studyId"></param> public GMISampleQuotasObject GetGMISamples(int studyId, int sampleId) { var parameters = new Hashtable { { "data[study_id]", studyId } }; GMISampleQuotasObject gmiSampleQuotasObject = new GMISampleQuotasObject(); var service = "GMI getSamples"; LogUtil.CallingService(service, LogUtil.getHashtableString(parameters)); try { var result = CallGMIStudyService("getSamples", parameters); LogUtil.CallSuccess(service, result.ToString()); gmiSampleQuotasObject = ProcessGMIStudyResponse(result, sampleId); } catch(Exception e) { LogUtil.CallFail(service, e); throw; } return gmiSampleQuotasObject; }
/// <summary> /// Get the externals/internals Quotas and samples from the GMI service response /// </summary> /// <param name="result"></param> /// <returns>GMISampleQuotasObject</returns> private GMISampleQuotasObject ProcessGMIStudyResponse(XDocument result, int sampleId) { GMISampleQuotasObject gmiSampleQuotasObject = new GMISampleQuotasObject(); List<GMISampleObject> gmiSampleQuotasList = new List<GMISampleObject>(); List<GMISampleObject> gmiSamplesList = new List<GMISampleObject>(); List<GMIQuotaObject> gmiQuotasList = new List<GMIQuotaObject>(); try { gmiSampleQuotasObject = (from openSampleNode in result.Descendants("Samples") select new GMISampleQuotasObject() { GMISampleQuotasList = (from QuotaAttributes in openSampleNode.Descendants("Item") where 0 != XmlUtil.GetSafeIntegerNodeValue(QuotaAttributes.Element("Id")) && 0 != XmlUtil.GetSafeIntegerNodeValue(QuotaAttributes.Element("ExternalId")) select new GMISampleObject() { InternalSampleId = XmlUtil.GetSafeIntegerNodeValue(QuotaAttributes.Element("Id")), SampleId = XmlUtil.GetSafeIntegerNodeValue(QuotaAttributes.Element("ExternalId")) }).ToList(), GMIQuotasList = (from QuotaAttributes in openSampleNode.Descendants("QuotaCells") from QuotaAttribute in QuotaAttributes.Descendants("Item") where 0 != XmlUtil.GetSafeIntegerNodeValue(QuotaAttribute.Element("Id")) && 0 != XmlUtil.GetSafeIntegerNodeValue(QuotaAttribute.Element("ExternalId")) select new GMIQuotaObject() { InternalQuotaId = XmlUtil.GetSafeIntegerNodeValue(QuotaAttribute.Element("Id")), QuotaId = XmlUtil.GetSafeIntegerNodeValue(QuotaAttribute.Element("ExternalId")) }).ToList() }).FirstOrDefault(); if (gmiSampleQuotasObject == null) return new GMISampleQuotasObject(); gmiQuotasList = gmiSampleQuotasObject.GMIQuotasList; gmiSampleQuotasList = gmiSampleQuotasObject.GMISampleQuotasList; int i = -1; foreach (GMISampleObject gmiSampleObject in gmiSampleQuotasList) { var quota = gmiQuotasList.Where(p => p.QuotaId == gmiSampleObject.SampleId).ToList(); if (quota.Count > 0) { if (gmiSamplesList.Count > 0) quota[0].SampleId = gmiSamplesList[i].SampleId; } else { i++; gmiSamplesList.Add(new GMISampleObject(gmiSampleObject.SampleId, gmiSampleObject.InternalSampleId)); } } gmiSampleQuotasObject.GMISampleQuotasList = gmiSamplesList.Where(s => s.SampleId == sampleId).ToList(); gmiSampleQuotasObject.GMIQuotasList = gmiQuotasList.Where(p => p.SampleId == sampleId).ToList(); } catch { throw; } return gmiSampleQuotasObject; }
private bool UpdateQuotaCell(OfferObject offerObject, SteamStudyObject steamStudyObject, QuotasLiveObject quotasLiveObject) { var sampleId = offerObject.SampleId.Value; LoggingUtility log = LoggerFactory.GetLogger(); try { QuotaMappingRepository quotaMappingRepository = new QuotaMappingRepository(); GMISampleObject sampleObject = new SampleMappingRepository().SelectByID(steamStudyObject.SampleId); List<GMISampleObject> GMISampleObjectList = new List<GMISampleObject>(); GMISampleObjectList.Add((sampleObject != null) ? sampleObject : new GMISampleObject()); List<GMIQuotaObject> GMIQuotasList = quotaMappingRepository.SelectBySampleID(steamStudyObject.SampleId).ToList(); bool needUpdate = false; foreach (GMIQuotaObject gmiQuotaObject in GMIQuotasList) { var quota = quotasLiveObject.QuotasLiveList.Where(p => p.InternalQuotaId == gmiQuotaObject.InternalQuotaId).Where(p => p.InternalSampleId == sampleObject.InternalSampleId).ToList(); if (quota.Count() > 0 && gmiQuotaObject.QuotaRemaining != quota[0].QuotaRemaining) { gmiQuotaObject.QuotaRemaining = quota[0].QuotaRemaining; quotaMappingRepository.Update(gmiQuotaObject); needUpdate = true; } } if (needUpdate) { // There was a change in quota remaining, update the quota expressions XML to reflect the new values log.Debug("Update the quota remaining for sample: " + sampleId); GMISampleQuotasObject gmiSampleQuotasObject = new GMISampleQuotasObject(); gmiSampleQuotasObject.GMIQuotasList = GMIQuotasList; gmiSampleQuotasObject.GMISampleQuotasList = GMISampleObjectList; new SteamStudy().UpdateQuotaExpression(steamStudyObject, gmiSampleQuotasObject); new QuotaExpressionRepository().updateQuotaExpressionsXML(steamStudyObject); } } catch (Exception e) { log.Error("An error occurred while trying to update the quota cells for offerId " + offerObject.Id.ToString(), e); throw; } return true; }