using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Web.Core; using Web.Core.DB; using Web.Core.DB.conditions; using FLocal.Common.actions; namespace FLocal.Common.dataobjects { public class AvatarsSettings : SqlObject { public class TableSpec : ISqlObjectTableSpec { public const string TABLE = "Accounts_AvatarsSettings"; public const string FIELD_ID = "Id"; public const string FIELD_ACCOUNTID = "AccountId"; public const string FIELD_AVATARS = "Avatars"; public static readonly TableSpec instance = new TableSpec(); public string name { get { return TABLE; } } public string idName { get { return FIELD_ID; } } public void refreshSqlObject(int id) { Refresh(id); } } protected override ISqlObjectTableSpec table { get { return TableSpec.instance; } } private int _accountId; public int accountId { get { this.LoadIfNotLoaded(); return this._accountId; } } public Account account { get { return Account.LoadById(this._accountId); } } private HashSet _avatarsIds; public HashSet avatarsIds { get { this.LoadIfNotLoaded(); return new HashSet(this._avatarsIds); } } public IEnumerable avatars { get { return from avatarId in this.avatarsIds select Upload.LoadById(avatarId); } } protected override void doFromHash(Dictionary data) { this._accountId = int.Parse(data[TableSpec.FIELD_ACCOUNTID]); this._avatarsIds = new HashSet(from stringId in data[TableSpec.FIELD_AVATARS].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) select int.Parse(stringId)); } private static readonly Dictionary accountid2id = new Dictionary(); public static AvatarsSettings LoadByAccount(Account account) { if(!accountid2id.ContainsKey(account.id)) { lock(accountid2id) { if(!accountid2id.ContainsKey(account.id)) { accountid2id[account.id] = int.Parse( Config.instance.mainConnection.LoadIdByField( TableSpec.instance.getColumnSpec(TableSpec.FIELD_ACCOUNTID), account.id.ToString() ) ); } } } return AvatarsSettings.LoadById(accountid2id[account.id]); } private static void Save(Account account, HashSet avatars) { Dictionary dataToUpdate = new Dictionary { { TableSpec.FIELD_AVATARS, new ScalarFieldValue(avatars.Join(",")) }, }; Dictionary dataToInsert = new Dictionary(dataToUpdate) { { TableSpec.FIELD_ACCOUNTID, new ScalarFieldValue(account.id.ToString()) }, }; ChangeSetUtil.ApplyChanges( new InsertOrUpdateChange( TableSpec.instance, dataToInsert, dataToUpdate, new ComparisonCondition( TableSpec.instance.getColumnSpec(TableSpec.FIELD_ACCOUNTID), ComparisonType.EQUAL, account.id.ToString() ) ) ); } private static HashSet SafeGetAvatars(Account account) { try { return AvatarsSettings.LoadByAccount(account).avatarsIds; } catch(NotFoundInDBException) { return new HashSet(); } } public static void AddAvatar(Account account, Upload avatar) { if(avatar.size > Upload.AVATAR_MAX_FILESIZE) throw new FLocalException("Avatar is too big (max. 80KB allowed)"); HashSet avatars = SafeGetAvatars(account); avatars.Add(avatar.id); Save(account, avatars); } public static void RemoveAvatar(Account account, Upload avatar) { HashSet avatars = SafeGetAvatars(account); avatars.Remove(avatar.id); Save(account, avatars); } } }