public void CreateProfile(int agent, int activity, StateProfileDocument doc) { using (SqlConnection connection = new SqlConnection(DbUtils.GetConnectionString())) { // get activity SqlCommand command = new SqlCommand(CreateStateProfileQuery, connection); command.Parameters.AddRange(new[] { new SqlParameter("@v1", doc.StateId), new SqlParameter("@v2", activity), new SqlParameter("@v3", agent), new SqlParameter("@v4", doc.Registration ?? (object)DBNull.Value), new SqlParameter("@v5", doc.ContentType), new SqlParameter("@v6", doc.Content), new SqlParameter("@v7", doc.Checksum), new SqlParameter("@v8", doc.LastModified), new SqlParameter("@v9", doc.CreateDate), }); try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { } } }
public StateProfileDocument GetProfile(StateProfileDocument profile) { StateProfileDocument result = null; int agentId = _agentProfileRepository.GetAgentId(profile.Agent); int activityId = _activityProfileRepository.GetActivityId(profile.Activity)[0]; if (agentId != -1 && activityId != -1) { result = GetStateProfileDocument(agentId, activityId, profile.StateId, profile.Registration); } return(result); }
public void SaveProfile(StateProfileDocument document) { int agentIndex = _agentProfileRepository.GetAgentId(document.Agent); //If agent doesn't exist we have to create it if (agentIndex == -1) { agentIndex = _agentProfileRepository.CreateAgent(document.Agent); } int activityIndex = _activityProfileRepository.GetActivityId(document.Activity)[0]; if (agentIndex == -1) { activityIndex = _activityProfileRepository.CreateActivity(document.Activity); } CreateProfile(agentIndex, activityIndex, document); }
public IHttpActionResult GetState( Iri activityId = null, Agent agent = null, [FromUri] string stateId = null, Guid?registration = null, DateTimeOffset?since = null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (agent == null) { return(BadRequest("Agent object needs to be provided.")); } if (activityId == null) { return(BadRequest("ActivityId parameter is missing.")); } StateProfileDocument mock = new StateProfileDocument { Activity = activityId, Agent = agent, StateId = stateId, Registration = registration }; StateProfileDocument profile = null; if (stateId != null) { profile = _stateProfileRepository.GetProfile(mock); if (profile == null) { return(NotFound()); } return(new DocumentResult(profile)); } Object[] profiles = _stateProfileRepository.GetProfiles(mock, since); if (profiles == null) { return(Ok(new string[0])); } return(new DocumentsResult(profiles)); }
public void DeleteProfile(StateProfileDocument profile) { if (profile == null) { return; } using (SqlConnection connection = new SqlConnection(DbUtils.GetConnectionString())) { SqlCommand command = null; command = new SqlCommand(DeleteSingleProfileQuery, connection); command.Parameters.AddWithValue("@id", profile.Id); try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { } } }
public IHttpActionResult DeleteProfile( Iri activityId = null, Agent agent = null, [FromUri] string stateId = null, [FromUri] Guid?registration = null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (agent == null) { return(BadRequest("Agent parameter needs to be provided.")); } if (activityId == null) { return(BadRequest("ProfileId parameter needs to be provided.")); } var mock = new StateProfileDocument() { Activity = activityId, Agent = agent, StateId = stateId, Registration = registration }; StateProfileDocument profile = null; //delete single document if (stateId != null) { profile = _stateProfileRepository.GetProfile(mock); _stateProfileRepository.DeleteProfile(profile); return(StatusCode(HttpStatusCode.NoContent)); } //delete multiple documents _stateProfileRepository.DeleteProfiles(mock); return(StatusCode(HttpStatusCode.NoContent)); }
public void DeleteProfiles(StateProfileDocument profile) { var agentIndex = _agentProfileRepository.GetAgentId(profile.Agent); if (agentIndex == -1) { return; } var activityIndex = _activityProfileRepository.GetActivityId(profile.Activity)[0]; if (activityIndex == -1) { return; } using (SqlConnection connection = new SqlConnection(DbUtils.GetConnectionString())) { SqlCommand command = null; if (profile.Registration != null) { command = new SqlCommand(String.Format(DeleteMultipleProfilesQuery, RegistrationQuery), connection); command.Parameters.AddWithValue("@regId", profile.Registration); } else { command = new SqlCommand(String.Format(DeleteMultipleProfilesQuery, ""), connection); } command.Parameters.AddWithValue("@actId", activityIndex); command.Parameters.AddWithValue("@agentId", agentIndex); try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { } } }
public void OverwriteProfile(StateProfileDocument document) { using (SqlConnection connection = new SqlConnection(DbUtils.GetConnectionString())) { try { connection.Open(); // Create the activity SqlCommand command = new SqlCommand(UpdateStateProfileQuery, connection); command.Parameters.AddWithValue("@ctt", document.ContentType); command.Parameters.AddWithValue("@ct", document.Content); command.Parameters.AddWithValue("@cks", document.Checksum); command.Parameters.AddWithValue("@lm", document.LastModified); command.Parameters.AddWithValue("@id", document.Id); command.ExecuteNonQuery(); } catch (Exception ex) { } finally { } } }
private StateProfileDocument GetStateProfileDocument(int agentId, int activityId, string stateId, Guid?registration) { StateProfileDocument profileDoc = null; using (SqlConnection connection = new SqlConnection(DbUtils.GetConnectionString())) { SqlDataReader reader = null; try { connection.Open(); SqlCommand command = null; //dynamic query if registration is not null if (registration != null) { command = new SqlCommand(String.Format(GetStateProfileQuery, RegistrationQuery), connection); command.Parameters.AddWithValue("@regId", registration); } else { command = new SqlCommand(String.Format(GetStateProfileQuery, ""), connection); } command.Parameters.AddWithValue("@stateId", stateId); command.Parameters.AddWithValue("@agentId", agentId); command.Parameters.AddWithValue("@activityId", activityId); reader = command.ExecuteReader(); if (reader.Read()) { profileDoc = new StateProfileDocument(); if (!reader.IsDBNull(0)) { profileDoc.Id = (int)reader[0]; } profileDoc.StateId = stateId; profileDoc.Registration = registration; if (!reader.IsDBNull(5)) { profileDoc.ContentType = (string)reader[3]; } profileDoc.Content = DbUtils.GetBytes(reader, 6); if (!reader.IsDBNull(7)) { profileDoc.Checksum = reader.GetString(7); } if (!reader.IsDBNull(8)) { profileDoc.LastModified = reader.GetDateTimeOffset(8); } return(profileDoc); } } catch (Exception ex) { } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } } return(profileDoc); }
public object[] GetProfiles(StateProfileDocument state, DateTimeOffset?since) { var result = new Object[2]; var listResult = new List <StateProfileDocument>(); var actId = _activityProfileRepository.GetActivityId(state.Activity)[0]; if (actId == -1) { return(null); } var agentId = _agentProfileRepository.GetAgentId(state.Agent); if (agentId == -1) { return(null); } using (SqlConnection connection = new SqlConnection(DbUtils.GetConnectionString())) { // get profiles SqlCommand command = null; if (state.Registration != null) { command = new SqlCommand(String.Format(GetStateProfilesQuery, RegistrationQuery), connection); command.Parameters.AddWithValue("@regId", state.Registration); } else { command = new SqlCommand(String.Format(GetStateProfilesQuery, ""), connection); } command.Parameters.AddWithValue("@agentId", agentId); command.Parameters.AddWithValue("@activityId", actId); SqlDataReader reader = null; try { connection.Open(); reader = command.ExecuteReader(); if (since == null) { since = DateTimeOffset.MinValue; } if (reader.HasRows) { while (reader.Read()) { StateProfileDocument curr = new StateProfileDocument(); curr.StateId = reader.GetString(0); curr.LastModified = reader.GetDateTimeOffset(1); if (curr.LastModified > since) { listResult.Add(curr); } } } else { throw new Exception("State profiles not found"); } } catch (Exception ex) { return(null); } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } } listResult.Sort((x, y) => Nullable.Compare(y.LastModified, x.LastModified)); result[1] = listResult.First().LastModified; result[0] = listResult.Select(i => i.StateId).ToList(); return(result); }
public IHttpActionResult SaveProfile( [RawBody] byte[] body, Iri activityId = null, Agent agent = null, [FromUri] string stateId = null, [FromUri] Guid?registration = null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (agent == null) { return(BadRequest("Missing parameter agent")); } if (stateId == null) { return(BadRequest("Missing parameter stateId")); } if (activityId == null) { return(BadRequest("Missing parameter activityId")); } string contenttype = null; if (this.Request.Content.Headers.Contains(RequiredContentTypeHeaderAttribute.CONTENT_TYPE)) { contenttype = this.Request.Content.Headers.GetValues(RequiredContentTypeHeaderAttribute.CONTENT_TYPE).First(); } // connect to db and check if the profile already exists. Create it or if the profile exists try to merge. StateProfileDocument newDocument = new StateProfileDocument(body, contenttype) { Agent = agent, StateId = stateId, Registration = registration, Activity = activityId }; var oldDocument = _stateProfileRepository.GetProfile(newDocument); if (oldDocument != null) { /* If method is Post -> merge documents: * 1. check if both have the content type app/json * 2. check for valid ETag * 3. Try to merge * 4. Return No Content */ if (this.Request.Method.Equals(HttpMethod.Post)) { if (!(oldDocument.ContentType.Equals(Constants.CONTENT_JSON) && oldDocument.ContentType.Equals(newDocument.ContentType))) { return(BadRequest("Couldn't merge. The content-type needs to be application/json for both documents to be merged")); } oldDocument.MergeDocument(newDocument); _stateProfileRepository.OverwriteProfile(oldDocument); return(StatusCode(HttpStatusCode.NoContent)); } else if (this.Request.Method.Equals(HttpMethod.Put)) { oldDocument.UpdateDocument(newDocument.Content, newDocument.ContentType); _stateProfileRepository.OverwriteProfile(oldDocument); return(StatusCode(HttpStatusCode.NoContent)); } } //create _stateProfileRepository.SaveProfile(newDocument); return(StatusCode(HttpStatusCode.NoContent)); }