/// <summary> /// Execute a request on the database schema (Create, Update or Delete a record for an existing table) /// on depends on the restriction view for the area and the profile /// </summary> /// <param name="database"></param> /// <param name="transaction"></param> /// <param name="lot"></param> /// <returns></returns> public List <Tuple <DSRecord, InformationRecord> > ExecuteRequest(DatabaseContext database, DSTransaction transaction, List <DSRequest> lot) { if (lot.Count == 0) { return(new List <Tuple <DSRecord, InformationRecord> >()); } DSRequest reference = lot[0]; if (!Tables.ContainsKey(reference.Table)) { throw new ExceptionDefinitionRecord("ERR_REQUEST_UNKNOWN"); } return(Tables[reference.Table].ExecuteRequest(database, transaction, reference.Action, lot)); }
/// <summary> /// Build a dynamic record containing properties depending on the area and the profile /// </summary> /// <param name="record"></param> /// <param name="area"></param> /// <param name="userId"></param> /// <param name="profile"></param> /// <param name="request"></param> /// <returns>true if the record is filtered</returns> public bool FilterRecord(DSRecord record, string area, int userId, UserProfile.EUserProfile profile, DSRequest request) { foreach (KeyValuePair <string, DSTable> table in Tables) { if (table.Value.Table != record.GetType()) { continue; } return(table.Value.FilterRecord(record, area, userId, profile, request)); } return(false); }
/// <summary> /// Convert a list of requests from the client into a transaction /// </summary> /// <param name="eventId"></param> /// <param name="connectionId"></param> /// <param name="customerId"></param> /// <param name="userId"></param> /// <param name="profile"></param> /// <param name="area"></param> /// <param name="moduleId"></param> /// <param name="requestId"></param> /// <param name="label"></param> /// <param name="requests">List of requests from the client</param> /// <param name="transaction"></param> /// <param name="notify"></param> public DSTransaction(int eventId, string connectionId, int customerId, int userId, UserProfile.EUserProfile profile, string area, int moduleId, int requestId, JObject label, JObject[] requests, bool transaction, bool notify) { EventId = eventId; ConnectionId = connectionId; CustomerId = customerId; UserId = userId; Profile = profile; Area = area; ModuleId = moduleId; RequestId = requestId; Label = label; CompressedRequests = requests; Transaction = transaction; Notify = notify; // Uncompress transaction int index = 0; Requests = new List <DSRequest>(); LotRequests = new List <List <DSRequest> >(); foreach (JObject request in requests) { // Retrieve the current request string table = null; if (request["table"] != null && request["table"].Type == JTokenType.String) { table = request["table"].ToObject <string>(); } string action = null; if (request["action"] != null && request["action"].Type == JTokenType.String) { action = request["action"].ToObject <string>(); } if (table == null || action == null || !(request["identity"] is JObject identity) || !(request["record"] is JObject record)) { Logger.LoggerManager.Instance.Error("DSTransaction", $"The request[{index}] isn't correctly formatted!"); throw new ExceptionDefinitionRecord("ERR_UNAUTHORIZED"); } // Build a request or a list of requests JToken tick = request["tick"]; int? tickValue = null; List <DSRequest> currentLot = new List <DSRequest>(); LotRequests.Add(currentLot); if (tick == null || tick.Type != JTokenType.Array) { if (tick != null && tick.Type != JTokenType.Undefined && tick.Type != JTokenType.Null) { tickValue = tick.ToObject <int>(); } DSRequest newRequest = new DSRequest(index, table, action, record, identity, tickValue, request, -1); Requests.Add(newRequest); currentLot.Add(newRequest); index++; continue; } JArray ticks = tick as JArray; for (int i = 0; i < ticks.Count; i++) { JObject transactionRecord; JObject transactionIdentity; if (ticks[i].Type != JTokenType.Undefined && ticks[i].Type != JTokenType.Null) { tickValue = ticks[i].ToObject <int>(); } if (action == "Update") { transactionRecord = new JObject(); transactionIdentity = new JObject(); transactionRecord["New"] = GetArrayIndex(record["New"] as JObject, i); transactionRecord["Old"] = GetArrayIndex(record["Old"] as JObject, i); transactionIdentity["New"] = GetArrayIndex(identity["New"] as JObject, i); transactionIdentity["Old"] = GetArrayIndex(identity["Old"] as JObject, i); } else { transactionRecord = GetArrayIndex(record, i); transactionIdentity = GetArrayIndex(identity, i); } DSRequest newRequest = new DSRequest(index, table, action, transactionRecord, transactionIdentity, tickValue, request, i); Requests.Add(newRequest); currentLot.Add(newRequest); index++; } } }