/// <summary> /// Retrieve an enumerable type /// </summary> /// <param name="enumerable"></param> /// <returns></returns> private JObject GetJSONEnumerableType(Type enumerable) { JObject values = new JObject(); if (!enumerable.IsEnum) { return(values); } string[] valueString = enumerable.GetEnumNames(); values["Name"] = enumerable.Name; string area = DSDatabase.GetArea(enumerable.Namespace); if (area != null) { values["Area"] = area; } int i = 0; foreach (int value in enumerable.GetEnumValues()) { JObject enumValue = new JObject { ["Label"] = Field + "_" + valueString[i].ToUpper(), ["Name"] = valueString[i] }; values[value.ToString()] = enumValue; i++; } return(values); }
/// <summary> /// Load the content of the table and returns a list of columns matching within the area and the profile of the user /// </summary> /// <param name="customerId"></param> /// <param name="userId"></param> /// <param name="profile"></param> /// <param name="area"></param> /// <param name="table"></param> /// <param name="existingRecords">Define it to replace the loading into the database</param> /// <returns>The table records: /// Table = table name /// Records = List of tuple containing all data /// LastSequenceId = Last sequence id of the user in this table /// </returns> public IEnumerable <JArray> LoadTable(int customerId, int userId, UserProfile.EUserProfile profile, string area, string table, List <DSRecord> existingRecords) { List <JArray> records = new List <JArray>(); Info($"Loading content of the table '{table}' ..."); // Retrieve the database schema DSSchema.DSDatabase schema = ConfigurationManager.Schemas[area]; if (schema == null) { Error("No schema available!"); throw new ExceptionDefinitionRecord("ERR_SCHEMA"); } // Read the content of the given table System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); long lotSize = ConfigurationManager.ConnectionLotSize; long sizeTotal = 0; long sizeRecord = 0; long nbRecords = 0; foreach (object[] record in schema.ReadTable(Database, table, customerId, userId, profile, area, null, existingRecords)) { records.Add(new JArray(record)); using (System.IO.Stream s = new System.IO.MemoryStream()) { formatter.Serialize(s, record); sizeRecord = s.Length; } sizeTotal += sizeRecord; nbRecords++; if (sizeTotal >= lotSize) { yield return(new JArray(records)); records.Clear(); sizeTotal = 0; } } if (sizeTotal > 0) { yield return(new JArray(records)); } Info($"{nbRecords} records read from the table '{table}'"); }
/// <summary> /// Refer to the directory containing the picture of the enunmeration value /// </summary> /// <param name="enumeration"></param> /// <returns></returns> public static string GetDirectory(Type enumeration) => DSDatabase.GetArea(enumeration.Namespace) + "/" + enumeration.Name + "/";
/// <summary> /// Build a column description /// </summary> /// <param name="databaseSchema"></param> /// <param name="tableName"></param> /// <param name="property"></param> /// <param name="defaultInstance"></param> public DSColumn(DSDatabase databaseSchema, string tableName, PropertyInfo property, object defaultInstance) { Field = tableName.ToUpper() + "_" + property.Name.ToUpper(); Property = property; ColumnName = property.Name; DefaultValue = property.GetValue(defaultInstance); Formats = new List <DSFormatAttribute>(); Controls = new List <DSControlAttribute>(); Constraints = new List <DSConstraintAttribute>(); Restriction = new List <DSRestrictedAttribute>(); foreach (object annotation in property.GetCustomAttributes(true)) { if (typeof(DSNameAttribute).IsInstanceOfType(annotation)) { Field = (annotation as DSNameAttribute).Name; } else if (typeof(DSControlAttribute).IsInstanceOfType(annotation)) { Controls.Add(annotation as DSControlAttribute); } else if (typeof(DSConstraintAttribute).IsInstanceOfType(annotation)) { Constraints.Add(annotation as DSConstraintAttribute); } else if (typeof(DSSequenceAttribute).IsInstanceOfType(annotation)) { Sequence = annotation as DSSequenceAttribute; Formats.Add(annotation as DSFormatAttribute); } else if (typeof(DSFormatAttribute).IsInstanceOfType(annotation)) { Formats.Add(annotation as DSFormatAttribute); } else if (typeof(DSRestrictedAttribute).IsInstanceOfType(annotation)) { Restriction.Add(annotation as DSRestrictedAttribute); } else if (typeof(System.ComponentModel.DataAnnotations.Schema.ColumnAttribute).IsInstanceOfType(annotation)) { ColumnName = (annotation as System.ComponentModel.DataAnnotations.Schema.ColumnAttribute).Name; } else if (typeof(System.ComponentModel.DataAnnotations.KeyAttribute).IsInstanceOfType(annotation)) { Controls.Add(new DSKeyAttribute()); } else if (typeof(System.ComponentModel.DataAnnotations.RequiredAttribute).IsInstanceOfType(annotation)) { Controls.Add(new DSRequiredAttribute()); } } if (this.Type == typeof(DateTime)) { bool existFormatDate = false; foreach (DSFormatAttribute format in Formats) { if (!format.Type.Equals("DateTime")) { continue; } existFormatDate = true; break; } if (!existFormatDate) { Formats.Add(new DSDateTimeAttribute()); } } // In case of history, retrieve the source field if (tableName.StartsWith("History") && databaseSchema.Schema.GetProperty(tableName.Substring(7)) != null) { PropertyInfo sourceTableProperty = databaseSchema.Schema.GetProperty(tableName.Substring(7)); // Only DbSet<X> contains a table // Ignore private, protected tables or properties started with "_" if (sourceTableProperty.PropertyType.IsGenericType && sourceTableProperty.PropertyType.GetGenericTypeDefinition() == typeof(DbSet <>) && !sourceTableProperty.Name.StartsWith("_") && !sourceTableProperty.PropertyType.IsNotPublic && sourceTableProperty.PropertyType.GetGenericArguments().First().IsSubclassOf(typeof(DSRecord))) { // Ignore record not inheritence of DSRecord Type sourceTable = sourceTableProperty.PropertyType.GetGenericArguments().First(); SourceProperty = sourceTable.GetProperty(property.Name); } } }
/// <summary> /// The client starts the initialization process and has to declare its area /// </summary> /// <param name="area"></param> /// <param name="moduleId"></param> /// <returns>the database schema correspnding to the user's profile and its area: /// Version = database version /// Schema = database schema /// DefaultLanguage = default language (user's language or default language of the application) /// User = user's profile /// LastRequestId = last request id of the user /// </returns> public JObject Initialize(string area, int moduleId) { string defaultLanguage = "EN"; int versionDatabase = 0; int lastRequestId = 0; IUser user = null; IModule module = null; Info($"Initializing connection within the area '{area}' ({moduleId}) ..."); if (_userManager == null) { Error("Unable to open the connection. UserManager is undefined!"); throw new ExceptionDefinitionRecord("ERR_CONNECTION"); } // Retrieve database information defaultLanguage = ConfigurationManager.DefaultLanguage; ParameterRecord parameterVersion = Database._Parameter.FirstOrDefault(p => p.Key.Equals("Database.Version")); if (parameterVersion != null) { versionDatabase = int.Parse(parameterVersion.Value); } // Retrieve the current user and its language user = _userManager.GetById(_userId); if (user == null) { Warn("The user doesn't exist!"); throw new ExceptionDefinitionRecord("ERR_CONNECTION"); } module = _userManager.GetModule(user, moduleId); if (module == null) { Warn("The module doesn't exist!"); throw new ExceptionDefinitionRecord("ERR_CONNECTION"); } // Retrieve the connection ConnectionRecord currentConnection = Database._Connection.FirstOrDefault(c => c.ConnectionId.Equals(_connectionId) && c.Machine.Equals(Environment.MachineName)); if (currentConnection == null) { Warn("The connection doesn't exist. Open the new connection!"); throw new ExceptionDefinitionRecord("ERR_CONNECTION"); } // Retrieve the database schema DSSchema.DSDatabase schema = ConfigurationManager.Schemas[area]; if (schema == null) { Error("No schema available!"); throw new ExceptionDefinitionRecord("ERR_SCHEMA"); } // Is this connection authorized to start the dialog ? if (!currentConnection.Allow) { Error("Not allowed!"); throw new ExceptionDefinitionRecord("ERR_UNAUTHORIZED"); } // Retrieve the last requestId of this user RequestIdRecord requestId = Database._RequestId.FirstOrDefault(r => r.UserId == _userId); if (requestId != null) { lastRequestId = requestId.RequestId; } // Set the area of the connection and notify that the connection is initialized currentConnection.Area = area; currentConnection.ModuleId = module.Id; currentConnection.Profile = module.Profile; currentConnection.Status = true; currentConnection.ConnectionLast = DateTime.Now; Database.SaveChanges(); Info($"The connection '{currentConnection}' is linked to the area '{area}'"); // define the response JObject result = new JObject { ["Version"] = versionDatabase, ["Schema"] = schema.ToJSON(area, currentConnection.Profile, Database.GetCache(schema)), ["DefaultLanguage"] = defaultLanguage, ["CurrentUserId"] = user == null ? -1 : user.Id, ["CurrentModuleId"] = moduleId, ["LastRequestId"] = lastRequestId }; JObject settings = new JObject(); foreach (KeyValuePair <string, string> setting in ConfigurationManager.Settings) { if (!setting.Key.StartsWith("Syncytium.Client.")) { continue; } settings[setting.Key.Substring("Syncytium.Client.".Length)] = setting.Value; } result["Parameters"] = settings; return(result); }