/// <summary> /// Returns all resource items for a given resource ID in all locales. /// Returned as full ResourceItem objects /// </summary> /// <param name="resourceId">The resource Id to return for</param> /// <param name="resourceSet">Resourceset to look in</param> /// <param name="forAllResourceSetLocales">When true returns empty entries for missing resources of locales in this resource set</param> /// <returns>List of resource items or null</returns> public virtual IEnumerable<ResourceItem> GetResourceItems(string resourceId, string resourceSet, bool forAllResourceSetLocales = false) { ErrorMessage = string.Empty; if (resourceSet == null) resourceSet = string.Empty; List<ResourceItem> items = null; using (var data = GetDb()) { using (IDataReader reader = data.ExecuteReader( "select * from " + Configuration.ResourceTableName + " where ResourceId=@ResourceId and ResourceSet=@ResourceSet " + " order by LocaleId", data.CreateParameter("@ResourceId", resourceId), data.CreateParameter("@ResourceSet", resourceSet))) { if (reader == null) { SetError(data.ErrorMessage); return null; } items = new List<ResourceItem>(); while (reader.Read()) { var item = new ResourceItem(); item.FromDataReader(reader); items.Add(item); } reader.Close(); } if (forAllResourceSetLocales) { var locales = GetAllLocalesForResourceSet(resourceSet); if (locales != null) { var usedLocales = items.Select(i => i.LocaleId); var emptyLocales = locales.Where(s => !usedLocales.Contains(s)); foreach (var locale in emptyLocales) { items.Add(new ResourceItem(){ LocaleId = locale, Value = "", ResourceSet = resourceSet }); } } } } return items; }
/// <summary> /// Updates a resource if it exists, if it doesn't one is created /// </summary> /// <param name="resource">Resource to update</param> public virtual int UpdateOrAddResource(ResourceItem resource) { if (!IsValidCulture(resource.LocaleId)) { ErrorMessage = string.Format(Resources.Can_t_save_resource__Invalid_culture_id_passed, resource.LocaleId); return -1; } int result = 0; result = UpdateResource(resource); // We either failed or we updated if (result != 0) return result; // We have no records matched in the Update - Add instead result = AddResource(resource); if (result == -1) return -1; return 1; }
public static ResourceItem SetFileDataOnResourceItem(ResourceItem item, byte[] data, string fileName) { if (data == null || item == null || string.IsNullOrEmpty(fileName)) throw new ArgumentException(Resources.ResourceItemMissingFileUploadData); string ext = Path.GetExtension(fileName).TrimStart('.').ToLower(); const string filter = ",bmp,ico,gif,jpg,jpeg,png,css,js,txt,html,htm,xml,wav,mp3,"; if (!filter.Contains("," + ext + ",")) throw new ArgumentException(Resources.InvalidFileExtensionForFileResource); string type; if ("jpg,jpeg,png,gif,bmp".Contains(ext)) type = typeof (Bitmap).AssemblyQualifiedName; else if("ico" == ext) type = typeof(Icon).AssemblyQualifiedName; else if ("txt,css,htm,html,xml,js".Contains(ext)) type = typeof (string).AssemblyQualifiedName; else type = typeof (byte[]).AssemblyQualifiedName; using (var ms = new MemoryStream()) { item.Value = fileName + ";" + type; item.BinFile = data; item.Type = "FileResource"; item.FileName = fileName; } return item; }
/// <summary> /// Returns a resource item that returns both the Value and Comment to the /// fields to the client. /// </summary> /// <param name="resourceId">The ID of the resource to retrieve</param> /// <param name="resourceSet">Name of the ResourceSet to return</param> /// <param name="cultureName">required. Null or Empty returns invariant</param> /// <returns></returns> public virtual ResourceItem GetResourceItem(string resourceId, string resourceSet, string cultureName) { ErrorMessage = string.Empty; if (cultureName == null) cultureName = string.Empty; ResourceItem item; using (var data = GetDb()) { using (IDataReader reader = data.ExecuteReader( "select * from " + Configuration.ResourceTableName + " where ResourceId=@ResourceId and ResourceSet=@ResourceSet and LocaleId=@LocaleId", data.CreateParameter("@ResourceId", resourceId), data.CreateParameter("@ResourceSet", resourceSet), data.CreateParameter("@LocaleId", cultureName))) { if (reader == null || !reader.Read()) return null; item = new ResourceItem(); item.FromDataReader(reader); reader.Close(); } } return item; }
/// <summary> /// Updates a resource if it exists, if it doesn't one is created /// </summary> /// <param name="resource">Resource to update</param> public virtual int UpdateResource(ResourceItem resource) { if (resource == null) { SetError("Resource passed cannot be null."); return -1; } string type = null; if (resource.LocaleId == null) resource.LocaleId = string.Empty; if (resource.Value != null && !(resource.Value is string)) { type = resource.Value.GetType().AssemblyQualifiedName; try { resource.Value = SerializeValue(resource.Value); } catch (Exception ex) { ErrorMessage = ex.Message; return -1; } } else if (resource.BinFile != null && string.IsNullOrEmpty(resource.Type)) type = "FileResource"; else { type = string.Empty; if (resource.Value == null) resource.Value = string.Empty; } if (resource.Value == null) resource.Value = string.Empty; int result; using (var data = GetDb()) { if (Transaction != null) data.Transaction = Transaction; // Set up Binfile and TextFile parameters which are set only for // file values - otherwise they'll pass as Null values. var binFileParm = data.CreateParameter("@BinFile", resource.BinFile, DbType.Binary); var textFileParm = data.CreateParameter("@TextFile", resource.TextFile); string sql = "update " + Configuration.ResourceTableName + " set Value=@Value, Type=@Type, BinFile=@BinFile,TextFile=@TextFile,FileName=@FileName, Comment=@Comment, ValueType=@ValueType, updated=@Updated " + "where LocaleId=@LocaleId AND ResourceSet=@ResourceSet and ResourceId=@ResourceId"; result = data.ExecuteNonQuery(sql, data.CreateParameter("@ResourceId", resource.ResourceId), data.CreateParameter("@Value", resource.Value), data.CreateParameter("@Type", resource.Type), data.CreateParameter("@LocaleId", resource.LocaleId), data.CreateParameter("@ResourceSet", resource.ResourceSet), binFileParm, textFileParm, data.CreateParameter("@FileName", resource.FileName), data.CreateParameter("@Comment", resource.Comment), data.CreateParameter("@ValueType", resource.ValueType), data.CreateParameter("@Updated", DateTime.UtcNow) ); if (result == -1) { ErrorMessage = data.ErrorMessage; return -1; } } return result; }
/// <summary> /// Adds a resource to the Localization Table /// </summary> /// <param name="resource">Resource to update</param> public virtual int AddResource(ResourceItem resource) { string Type = string.Empty; if (resource.LocaleId == null) resource.LocaleId = string.Empty; if (string.IsNullOrEmpty(resource.ResourceId)) { ErrorMessage = Resources.NoResourceIdSpecifiedCantAddResource; return -1; } if (resource.Value != null && !(resource.Value is string)) { Type = resource.Value.GetType().AssemblyQualifiedName; try { SerializeValue(resource.Value); } catch (Exception ex) { ErrorMessage = ex.Message; return -1; } } else Type = string.Empty; if (resource.Value == null) resource.Value = string.Empty; using (var data = GetDb()) { if (Transaction != null) data.Transaction = Transaction; DbParameter BinFileParm = data.CreateParameter("@BinFile", resource.BinFile, DbType.Binary); DbParameter TextFileParm = data.CreateParameter("@TextFile", resource.TextFile); string Sql = "insert into " + Configuration.ResourceTableName + " (ResourceId,Value,LocaleId,Type,Resourceset,BinFile,TextFile,Filename,Comment,ValueType,Updated) Values (@ResourceID,@Value,@LocaleId,@Type,@ResourceSet,@BinFile,@TextFile,@FileName,@Comment,@ValueType,@Updated)"; if (data.ExecuteNonQuery(Sql, data.CreateParameter("@ResourceId", resource.ResourceId), data.CreateParameter("@Value", resource.Value), data.CreateParameter("@LocaleId", resource.LocaleId), data.CreateParameter("@Type", resource.Type), data.CreateParameter("@ResourceSet", resource.ResourceSet), BinFileParm, TextFileParm, data.CreateParameter("@FileName", resource.FileName), data.CreateParameter("@Comment", resource.Comment), data.CreateParameter("@ValueType", resource.ValueType), data.CreateParameter("@Updated", DateTime.UtcNow)) == -1) { ErrorMessage = data.ErrorMessage; return -1; } } return 1; }
/// <summary> /// Returns a list of all the resources for all locales. The result is in a /// table called TResources that contains all fields of the table. The table is /// ordered by LocaleId. /// /// This version returns either local or global resources in a Web app /// /// Fields: /// ResourceId,Value,LocaleId,ResourceSet,Type /// </summary> /// <param name="localResources">return local resources if true</param> /// <returns></returns> public override List <ResourceItem> GetAllResources(bool localResources = false, bool applyValueConverters = false, string resourceSet = null) { List <ResourceItem> items; using (var data = GetDb()) { string resourceSetFilter = ""; if (!string.IsNullOrEmpty(resourceSet)) { resourceSetFilter = " AND resourceset = @ResourceSet2 "; } string sql = "select ResourceId,Value,LocaleId,ResourceSet,Type,TextFile,BinFile,Filename,Comment,ValueType,Updated " + "from " + Configuration.ResourceTableName + " " + "where ResourceSet Is Not Null " + resourceSetFilter + " ORDER BY ResourceSet,LocaleId, ResourceId"; //var parms = new List<IDbDataParameter> { data.CreateParameter("@ResourceSet", "%.%") }; var parms = new List <IDbDataParameter>(); if (!string.IsNullOrEmpty(resourceSetFilter)) { parms.Add(data.CreateParameter("@ResourceSet2", resourceSet)); } using (var reader = data.ExecuteReader(sql, parms.ToArray())) { if (reader == null) { SetError(data.ErrorMessage); return(null); } items = new List <ResourceItem>(); while (reader.Read()) { var item = new ResourceItem(); item.ResourceId = reader["ResourceId"] as string; item.ResourceSet = reader["ResourceSet"] as string; item.Value = reader["Value"]; item.LocaleId = reader["LocaleId"] as string; item.Type = reader["Type"] as string; item.TextFile = reader["TextFile"] as string; item.BinFile = reader["BinFile"] as byte[]; item.Comment = reader["Comment"] as string; var number = reader["ValueType"]; // int64 returned from Microsoft.Data.SqLite if (number is int) { item.ValueType = (int)number; } else { item.ValueType = Convert.ToInt32(number); } var time = reader["Updated"]; // string return from Microsoft.Data.SqLite if (time == null) { item.Updated = DateTime.MinValue; } if (time is DateTime) { item.Updated = (DateTime)time; } else { item.Updated = Convert.ToDateTime(time); } items.Add(item); } } if (applyValueConverters && DbResourceConfiguration.Current.ResourceSetValueConverters.Count > 0) { foreach (var resourceItem in items) { foreach (var convert in DbResourceConfiguration.Current.ResourceSetValueConverters) { if (resourceItem.ValueType == convert.ValueType) { resourceItem.Value = convert.Convert(resourceItem.Value, resourceItem.ResourceId); } } } } return(items); } }
/// <summary> /// Returns a resource item that returns both the Value and Comment to the /// fields to the client. /// </summary> /// <param name="resourceId"></param> /// <param name="resourceSet"></param> /// <param name="cultureName"></param> /// <returns></returns> public ResourceItem GetResourceItem(string resourceId, string resourceSet, string cultureName) { ErrorMessage = string.Empty; if (cultureName == null) cultureName = string.Empty; using (SqlDataAccess data = new SqlDataAccess(DbResourceConfiguration.Current.ConnectionString)) { using (IDataReader reader = data.ExecuteReader("select ResourceId, Value,Comment from " + DbResourceConfiguration.Current.ResourceTableName + " where ResourceId=@ResourceId and ResourceSet=@ResourceSet and LocaleId=@LocaleId", data.CreateParameter("@ResourceId", resourceId), data.CreateParameter("@ResourceSet", resourceSet), data.CreateParameter("@LocaleId", cultureName))) { if (reader == null || !reader.Read()) return null; ResourceItem item = new ResourceItem() { ResourceId = reader["ResourceId"] as string, Value = reader["Value"] as string, Comment = reader["Comment"] as string }; reader.Close(); return item; } } }
/// <summary> /// Adds a resource to the Localization Table /// </summary> /// <param name="resource">Resource to update</param> public virtual int AddResource(ResourceItem resource) { if (resource == null) { SetError("Resource passed cannot be null."); return -1; } return AddResource(resource.ResourceId, resource.Value, resource.LocaleId, resource.ResourceSet, resource.Comment, false); }