/// <summary> /// Associated a set of Event Clusters Ids to a response /// </summary> /// <param name="responseObj">ResponseEventClustersUpdateModel</param> /// <returns>ResponseModel</returns> public async Task <ResponseModel> AddEventClusterIdsToResponse(ResponseEventClustersUpdateModel responseUpdate) { ResponseDAO response = await _repoResponses.GetItemAsync(responseUpdate.ResponseId); if (response == null) { throw new Exception($"No response found that matches responseid: {responseUpdate.ResponseId}"); } string etag = response.ETag; if (response.EventClusterIds == null) { response.EventClusterIds = new List <Guid>(); } response.EventClusterIds = response.EventClusterIds.Concat(responseUpdate.EventClusterIds); response.ETag = etag; try { await _repoResponses.UpdateItemAsync(response); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await AddEventClusterIdsToResponse(responseUpdate)); } throw e; } return(_mapper.Map <ResponseModel>(response)); }
/// <summary> /// Delete a response. This call is for debugging purposes only /// </summary> /// <param name="responseId">Response Id</param> /// <param name="responseExists">True if the response exist, will skip the call</param> /// <returns>true if the call succeeded</returns> public async Task <bool> DeleteResponse(Guid responseId, bool responseExists = false) { if (!responseExists) { ResponseDAO response = await _repoResponses.GetItemAsync(responseId); if (response == null) { throw new Exception($"No response found that matches responseid: {responseId}"); } } try { await _repoResponses.DeleteItemAsync(responseId); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await DeleteResponse(responseId, true)); } throw e; } return(true); }
/// <summary> /// Create a new response /// </summary> /// <param name="responseObj">ResponseCreationModel</param> /// <returns>ResponseModel</returns> public async Task <ResponseModel> CreateResponse(ResponseCreationModel responseObj) { //Instantiate the actions InstantiateResponseActions(responseObj.ActionPlan.OpenActions); InstantiateResponseActions(responseObj.ActionPlan.CloseActions); ResponseDAO response = new ResponseDAO() { ActionPlan = _mapper.Map <ResponseActionPlanDAOObject>(responseObj.ActionPlan), ResponderUserId = responseObj.ResponderUserId, ResponseState = RESPONSE_STATE_ACTIVE, PrimaryEventClusterId = responseObj.PrimaryEventClusterId, Geolocation = _mapper.Map <GeolocationDAOObject>(responseObj.Geolocation) }; response.Id = await _repoResponses.CreateItemAsync(response); if (_repoResponses.IsDocumentKeyNull(response)) { throw new Exception($"An error occured when creating a new response"); } ResponseModel output = _mapper.Map <ResponseModel>(response); return(output); }
/// <summary> /// Close a response by adding a end date. /// </summary> /// <param name="responseObj">EventSagaReceiveResponseClosed</param> /// <returns>ResponseModel</returns> public async Task <ResponseModel> CloseResponse(ResponseCloseModel responseObj) { ResponseDAO response = await _repoResponses.GetItemAsync(responseObj.ResponseId); if (response == null) { throw new Exception($"No response found that matches responseid: {responseObj.ResponseId}"); } string etag = response.ETag; response.ResponseState = RESPONSE_STATE_INACTIVE; //responseObj.State; Add for later alt states of completion response.EndDate = DateTime.UtcNow; response.ETag = etag; try { await _repoResponses.UpdateItemAsync(response); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await CloseResponse(responseObj)); } throw e; } var output = _mapper.Map <ResponseModel>(response); return(output); }
/// <summary> /// Complete an action and update the action object /// </summary> /// <param name="actionCompletionObj">ActionCompletionModel</param> /// <returns>true if the call succeeded</returns> public async Task <bool> CompleteAction(ActionCompletionModel actionCompletionObj) { ResponseDAO response = await _repoResponses.GetItemAsync(actionCompletionObj.ResponseId); if (response == null) { throw new Exception($"No response found that matches responseid: {actionCompletionObj.ResponseId}"); } var action = response.ActionPlan.OpenActions.FirstOrDefault(p => p.ActionId == actionCompletionObj.ActionId); if (action == null) { action = response.ActionPlan.CloseActions.FirstOrDefault(p => p.ActionId == actionCompletionObj.ActionId); } if (action != null) { action.StartDate = actionCompletionObj.StartDate; action.EndDate = actionCompletionObj.EndDate; action.Status = actionCompletionObj.Status.ToString(); action.ErrorMessage = actionCompletionObj.ErrorMessage; } else { return(false); } try { await _repoResponses.UpdateItemAsync(response); return(true); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await CompleteAction(actionCompletionObj)); } throw e; } }
/// <summary> /// Set the safe status of a user /// </summary> /// <param name="response">ResponseDAO</param> /// <param name="userId">User Id</param> /// <param name="isSafe">True if the user is safe</param> /// <returns>true if the call succeeded</returns> public async Task <bool> SetSafeStatus(ResponseDAO response, string userId, bool isSafe) { try { if (response.SafeUsers == null) { response.SafeUsers = new List <string>(); } if (isSafe && !response.SafeUsers.Contains(userId)) { response.SafeUsers.Add(userId); } else if (!isSafe && response.SafeUsers.Contains(userId)) { response.SafeUsers.Remove(userId); } else { return(true); //no reason to update } await _repoResponses.UpdateItemAsync(response); return(true); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { response = await _repoResponses.GetItemAsync(response.Id); } return(await SetSafeStatus(response, userId, isSafe)); throw e; } }
/// <summary> /// Map a header key to its actual value /// </summary> /// <param name="key">Key</param> /// <param name="response">Response object</param> /// <returns>Value</returns> private object GetResponseHeaderValue(string key, ResponseDAO response) { switch (key) { case "{RESPONSETYPE}": return(response.ActionPlan.Name); case "{RESPONSEDESCRIPTION}": return(response.ActionPlan.Description); case "{PRIMARYRADIUS}": return(response.ActionPlan.PrimaryRadius.ToString()); case "{SECONDARYRADIUS}": return(response.ActionPlan.SecondaryRadius.ToString()); case "{PRIMARYEVENTCLUSTERID}": return(response.PrimaryEventClusterId.ToString()); case "{LONGITUDE}": return(response.Geolocation?.Longitude.ToString()); case "{LATITUDE}": return(response.Geolocation?.Latitude.ToString()); case "{CREATIONDATE}": return(response.CreationDate.ToOADate()); case "{ENDDATE}": if (response.EndDate == null) { return("ONGOING"); } return(response.EndDate.Value.ToOADate()); } return(key); }
/// <summary> /// Locate a response by adding a geolocation. The call will fail if a geolocation already exist /// </summary> /// <param name="responseObj">ResponseUpdateModel</param> /// <returns>ResponseModel</returns> public async Task <ResponseModel> LocateResponse(ResponseUpdateModel responseObj) { ResponseDAO response = await _repoResponses.GetItemAsync(responseObj.ResponseId); if (response == null) { throw new Exception($"No response found that matches responseid: {responseObj.ResponseId}"); } if (response.Geolocation != null) { throw new Exception($"The response already had a geolocation: {responseObj.ResponseId}"); } string etag = response.ETag; response.Geolocation = _mapper.Map <GeolocationDAOObject>(responseObj.Geolocation); response.ETag = etag; try { await _repoResponses.UpdateItemAsync(response); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await LocateResponse(responseObj)); } throw e; } var output = _mapper.Map <ResponseModel>(response); return(output); }
private ResponseDAO UpdateActionsOnResponseModel(ResponseDAO response, ResponseChangeActionPlanModel responseChangeAction) { var openAddActions = responseChangeAction.Actions.Where(x => !x.IsCloseAction && x.ActionChangedString == "add"); var openEditActions = responseChangeAction.Actions.Where(x => !x.IsCloseAction && x.ActionChangedString == "edit"); var openDeleteActions = responseChangeAction.Actions.Where(x => !x.IsCloseAction && x.ActionChangedString == "delete"); var closeAddActions = responseChangeAction.Actions.Where(x => x.IsCloseAction && x.ActionChangedString == "add"); var closeEditActions = responseChangeAction.Actions.Where(x => x.IsCloseAction && x.ActionChangedString == "edit"); var closeDeleteActions = responseChangeAction.Actions.Where(x => x.IsCloseAction && x.ActionChangedString == "delete"); var openList = response.ActionPlan.OpenActions.ToList(); var closeList = response.ActionPlan.CloseActions.ToList(); if (openEditActions.Any() || openDeleteActions.Any()) { for (int i = 0; i < openList.Count(); i++) { //Edit Open if (openEditActions.Select(a => a.Action.ActionId).Contains(openList[i].ActionId)) { openList[i] = _mapper.Map <ResponseActionDAOObject>(openEditActions.First(a => a.Action.ActionId == openList[i].ActionId).Action); } //Delete Open else if (openDeleteActions.Select(a => a.Action.ActionId).Contains(openList[i].ActionId)) { openList.RemoveAt(i); } } } if (closeEditActions.Any() || closeDeleteActions.Any()) { for (int i = 0; i < closeList.Count(); i++) { //Edit Close if (closeEditActions.Select(a => a.Action.ActionId).Contains(closeList[i].ActionId)) { closeList[i] = _mapper.Map <ResponseActionDAOObject>(closeEditActions.First(a => a.Action.ActionId == closeList[i].ActionId).Action); } //Delete Close else if (closeDeleteActions.Select(a => a.Action.ActionId).Contains(closeList[i].ActionId)) { closeList.RemoveAt(i); } } } //Add Close if (closeAddActions.Any()) { closeList.AddRange(_mapper.Map <IEnumerable <ResponseActionDAOObject> >(closeAddActions.Select(a => { a.Action.ActionId = Guid.NewGuid(); return(a.Action); }))); } //Add Open if (openAddActions.Any()) { openList.AddRange(_mapper.Map <IEnumerable <ResponseActionDAOObject> >(openAddActions.Select(a => { a.Action.ActionId = Guid.NewGuid(); return(a.Action); }))); } response.ActionPlan.OpenActions = openList.AsEnumerable(); response.ActionPlan.CloseActions = closeList.AsEnumerable(); return(response); }
/// <summary> /// Get a response full object by Id /// </summary> /// <param name="responseId">Response Id</param> /// <returns>ResponseModel</returns> public async Task <ResponseModel> GetResponse(Guid responseId) { ResponseDAO response = await _repoResponses.GetItemAsync(responseId); return(_mapper.Map <ResponseModel>(response)); }
/// <summary> Retrieves Entity rows in a datatable which match the specified filter. It will always create a new connection to the database.</summary> /// <param name="selectFilter">A predicate or predicate expression which should be used as filter for the entities to retrieve.</param> /// <param name="maxNumberOfItemsToReturn"> The maximum number of items to return with this retrieval query.</param> /// <param name="sortClauses">The order by specifications for the sorting of the resultset. When not specified, no sorting is applied.</param> /// <param name="relations">The set of relations to walk to construct to total query.</param> /// <param name="pageNumber">The page number to retrieve.</param> /// <param name="pageSize">The page size of the page to retrieve.</param> /// <returns>DataTable with the rows requested.</returns> public static DataTable GetMultiAsDataTable(IPredicate selectFilter, long maxNumberOfItemsToReturn, ISortExpression sortClauses, IRelationCollection relations, int pageNumber, int pageSize) { ResponseDAO dao = DAOFactory.CreateResponseDAO(); return(dao.GetMultiAsDataTable(maxNumberOfItemsToReturn, sortClauses, selectFilter, relations, pageNumber, pageSize)); }
/// <summary> /// Generate the header of a response sheet /// </summary> /// <param name="workbook">Workbook</param> /// <param name="sheet">Sheet</param> /// <param name="rowStartIndex">Starting row</param> /// <param name="response">Response object</param> private void GenerateResponseHeaderReport(IWorkbook workbook, ISheet sheet, int rowStartIndex, ResponseDAO response) { List <ReportResponseHeaderRowOptions> responseHeaders = _config.ReportConfiguration.ResponseHeader; if (responseHeaders == null) { return; } foreach (var responseHeaderRow in responseHeaders) { IRow row = sheet.CreateRow(rowStartIndex + responseHeaderRow.RowIndex); foreach (var responseHeaderColumn in responseHeaderRow.Columns) { var value = GetResponseHeaderValue(responseHeaderColumn.Value, response); if (value is double) { SetCellValue(row, responseHeaderColumn.ColumnIndex, value, GetStyle(workbook, responseHeaderColumn.Style), ReportDataType.Double); } else { SetCellValue(row, responseHeaderColumn.ColumnIndex, value, GetStyle(workbook, responseHeaderColumn.Style), ReportDataType.Text); } } } }