/// <summary> /// This method gets the data or current status for a request /// </summary> /// <param name="dataRequestLogObj"></param> /// <returns></returns> public static ZillowPropertyDetails ReRun(DataRequestLog dataRequestLogObj) { ZillowPropertyDetails zPropertyDetails = new ZillowPropertyDetails(); zPropertyDetails.BBL = dataRequestLogObj.BBL; zPropertyDetails.requestId = dataRequestLogObj.RequestId; zPropertyDetails.externalReferenceId = dataRequestLogObj.ExternalReferenceId; zPropertyDetails.status = ((RequestStatus)dataRequestLogObj.RequestStatusTypeId).ToString();; zPropertyDetails.zEstimate = null; try { using (WebDataEntities webDBEntities = new WebDataEntities()) { if (dataRequestLogObj.RequestStatusTypeId == (int)RequestStatus.Success) { Parameters parameters = JSONToParameters(dataRequestLogObj.RequestParameters); //check if data available WebDataDB.Zillow zillowObj = webDBEntities.Zillows.FirstOrDefault(i => i.BBL == parameters.BBL); if (zillowObj != null && DateTime.UtcNow.Subtract(zillowObj.LastUpdated).Days <= 30) { zPropertyDetails.zEstimate = zillowObj.zEstimate; } else { zPropertyDetails.status = RequestStatus.Error.ToString(); } } } return(zPropertyDetails); } catch (Exception e) { Common.Logs.log().Error(string.Format("Exception encountered processing request log for {0} with externalRefId {1}{2}", dataRequestLogObj.BBL, dataRequestLogObj.ExternalReferenceId, Common.Logs.FormatException(e))); return(null); } }
/// <summary> /// This method updates the Zillow table based on the information received from the Request Object /// </summary> public static bool UpdateData(Common.Context appContext, Request requestObj) { using (WebDataEntities webDBEntities = new WebDataEntities()) { using (var webDBEntitiestransaction = webDBEntities.Database.BeginTransaction()) { try { List <DataRequestLog> logs = null; decimal?zEstimate = null; switch (requestObj.RequestStatusTypeId) { case (int)RequestStatus.Error: logs = DAL.DataRequestLog.SetAsError(webDBEntities, requestObj.RequestId); break; case (int)RequestStatus.Success: { DataRequestLog dataRequestLogObj = DAL.DataRequestLog.GetFirst(webDBEntities, requestObj.RequestId); if (dataRequestLogObj != null) { RequestResponseBuilder.ResponseObjects.ZillowZEstimate resultObj = null; try { resultObj = (ResponseData.ParseZillowZEstimate(requestObj.ResponseData))[0]; zEstimate = resultObj.Zestimate; } catch (Exception e) { Common.Logs.log().Error(string.Format("Cannot parse the response data {0}{1}", requestObj.RequestId, Common.Logs.FormatException(e))); } Parameters parameters = JSONToParameters(dataRequestLogObj.RequestParameters); //check if old data in the DB WebDataDB.Zillow zillowObj = webDBEntities.Zillows.FirstOrDefault(i => i.BBL == parameters.BBL); if (zillowObj != null) { //Update data with new results zillowObj.zEstimate = zEstimate; zillowObj.LastUpdated = requestObj.DateTimeEnded.GetValueOrDefault(); } else { // add an entry into cache or DB zillowObj = new WebDataDB.Zillow(); zillowObj.BBL = parameters.BBL; zillowObj.zEstimate = zEstimate; zillowObj.LastUpdated = requestObj.DateTimeEnded.GetValueOrDefault(); webDBEntities.Zillows.Add(zillowObj); } webDBEntities.SaveChanges(); logs = DAL.DataRequestLog.SetAsSuccess(webDBEntities, requestObj.RequestId); } else { throw (new Exception("Cannot locate Request Log Record(s)")); } break; } default: Common.Logs.log().Warn(String.Format("Update called for a Request Object Id {0} with incorrect Status Id {1}", requestObj.RequestId, requestObj.RequestStatusTypeId)); break; } webDBEntitiestransaction.Commit(); if (logs != null) { MakeCallBacks(appContext, logs, zEstimate); } return(true); } catch (Exception e) { webDBEntitiestransaction.Rollback(); Common.Logs.log().Error(string.Format("Exception encountered updating request with id {0}{1}", requestObj.RequestId, Common.Logs.FormatException(e))); return(false); } } } }
/// <summary> /// This method deals with all the details associated with either returning the Zillow details or creating the /// request for getting it scrapped from the web /// </summary> public static ZillowPropertyDetails Get(string propertyBBL, string address, string externalReferenceId, int priority, string jobId) { ZillowPropertyDetails zPropertyDetails = new ZillowPropertyDetails(); zPropertyDetails.BBL = propertyBBL; zPropertyDetails.externalReferenceId = externalReferenceId; zPropertyDetails.status = RequestStatus.Pending.ToString(); zPropertyDetails.zEstimate = null; string parameters = ParametersToJSON(propertyBBL, address); using (WebDataEntities webDBEntities = new WebDataEntities()) { using (var webDBEntitiestransaction = webDBEntities.Database.BeginTransaction()) { try { string jsonBillParams = ParametersToJSON(propertyBBL, address); //check if data available WebDataDB.Zillow zillowObj = webDBEntities.Zillows.FirstOrDefault(i => i.BBL == propertyBBL); // record in database and data is not stale if (zillowObj != null && DateTime.UtcNow.Subtract(zillowObj.LastUpdated).Days <= 30) { zPropertyDetails.zEstimate = zillowObj.zEstimate; zPropertyDetails.status = RequestStatus.Success.ToString(); DAL.DataRequestLog.InsertForCacheAccess(webDBEntities, propertyBBL, RequestTypeId, externalReferenceId, jobId, jsonBillParams); } else { //check if pending request in queue DataRequestLog dataRequestLogObj = DAL.DataRequestLog.GetPendingRequest(webDBEntities, propertyBBL, RequestTypeId, jsonBillParams); if (dataRequestLogObj == null) //No Pending Request Create New Request { string requestStr = RequestResponseBuilder.RequestObjects.RequestData.ZillowZEstimate(address); Request requestObj = DAL.Request.Insert(webDBEntities, requestStr, RequestTypeId, priority, jobId); dataRequestLogObj = DAL.DataRequestLog.InsertForWebDataRequest(webDBEntities, propertyBBL, RequestTypeId, requestObj.RequestId, externalReferenceId, jobId, jsonBillParams); zPropertyDetails.status = RequestStatus.Pending.ToString(); zPropertyDetails.requestId = requestObj.RequestId; } else //Pending request in queue { zPropertyDetails.status = RequestStatus.Pending.ToString(); //Send the RequestId for the pending request back zPropertyDetails.requestId = dataRequestLogObj.RequestId; dataRequestLogObj = DAL.DataRequestLog.InsertForWebDataRequest(webDBEntities, propertyBBL, RequestTypeId, dataRequestLogObj.RequestId.GetValueOrDefault(), externalReferenceId, jobId, jsonBillParams); } } webDBEntitiestransaction.Commit(); } catch (Exception e) { webDBEntitiestransaction.Rollback(); zPropertyDetails.status = RequestStatus.Error.ToString(); DAL.DataRequestLog.InsertForFailure(propertyBBL, RequestTypeId, externalReferenceId, jobId, parameters); Common.Logs.log().Error(string.Format("Exception encountered processing {0} with externalRefId {1}{2}", propertyBBL, externalReferenceId, Common.Logs.FormatException(e))); } } } return(zPropertyDetails); }