using System; using Kreta.Core.ConnectionType; using Kreta.DataAccessManual.Interfaces; using Kreta.Framework; namespace Kreta.DataAccessManual.Util { internal abstract class ConnectionHandlerBase { internal void ActionWithHandler(Action actions, IDalHandler handler = null) { try { var isNotNestedConnection = handler == null; handler = handler ?? new DalHandler(); actions(handler); if (isNotNestedConnection && handler.CommitOnEnd) { UserContext.Instance.CommitTransaction(); } } catch { UserContext.Instance.RollbackTransaction(); throw; } } internal T FunctionWithHandler(Func actions, IDalHandler handler = null) { try { var isNotNestedConnection = handler == null; handler = handler ?? new DalHandler(); var result = actions(handler); if (isNotNestedConnection && handler.CommitOnEnd) { UserContext.Instance.CommitTransaction(); } return result; } catch { UserContext.Instance.RollbackTransaction(); throw; } } } internal class ConnectionHandler : ConnectionHandlerBase, IConnectionHandler { public void Run(string sessionId, Action actions) { using (new Connection(sessionId)) { ActionWithHandler(actions); } } public T Run(string sessionId, Func actions) { using (new Connection(sessionId)) { return FunctionWithHandler(actions); } } } internal class OrganizationConnectionHandler : ConnectionHandlerBase, IOrganizationConnectionHandler { public void Run(string intezmenyAzonosito, Action actions) { using (new OrganizationConnection(intezmenyAzonosito)) { ActionWithHandler(actions); } } public T Run(string intezmenyAzonosito, Func actions) { using (new OrganizationConnection(intezmenyAzonosito)) { return FunctionWithHandler(actions); } } } internal class MobileConnectionHandler : ConnectionHandlerBase, IMobileConnectionHandler { public void Run(string intezmenyAzonosito, int? tanevId, Action actions, int? felhasznaloId = null) { using (new MobileConnection(intezmenyAzonosito, tanevId, felhasznaloId)) { ActionWithHandler(actions); } } public T Run(string intezmenyAzonosito, int? tanevId, Func actions, int? felhasznaloId = null) { using (new MobileConnection(intezmenyAzonosito, tanevId, felhasznaloId)) { return FunctionWithHandler(actions); } } } internal class SystemConnectionHandler : ConnectionHandlerBase, ISystemConnectionHandler { public void Run(Action actions) { using (new SystemConnection()) { ActionWithHandler(actions); } } public T Run(Func actions) { using (new SystemConnection()) { return FunctionWithHandler(actions); } } } internal class ServiceSystemConnectionHandler : ConnectionHandlerBase, IServiceSystemConnectionHandler { public void Run(string connectionString, Action actions) { using (new ServiceSystemConnection(connectionString)) { ActionWithHandler(actions); } } public T Run(string connectionString, Func actions) { using (new ServiceSystemConnection(connectionString)) { return FunctionWithHandler(actions); } } } internal class CustomConnectionHandler : ConnectionHandlerBase, ICustomConnectionHandler { public void Run(IConnectionType connectionType, Action actions) { switch (connectionType) { case DalHandlerConnectionType dalHandlerConnectionType: ActionWithHandler(actions, dalHandlerConnectionType.DalHandler); break; case MobileConnectionType mobileConnectionType: new MobileConnectionHandler().Run(mobileConnectionType.IntezmenyAzonosito, mobileConnectionType.TanevId, actions, mobileConnectionType.FelhasznaloId); break; case OrganizationConnectionType organizationConnectionType: new OrganizationConnectionHandler().Run(organizationConnectionType.IntezmenyAzonosito, actions); break; case SessionConnectionType sessionConnectionType: new ConnectionHandler().Run(sessionConnectionType.SessionId, actions); break; case SystemConnectionType systemConnectionType: new SystemConnectionHandler().Run(actions); break; default: throw new ArgumentException("Ismeretlen ConnectionTpye", nameof(connectionType)); } } public T Run(IConnectionType connectionType, Func actions) { switch (connectionType) { case DalHandlerConnectionType dalHandlerConnectionType: return FunctionWithHandler(actions, dalHandlerConnectionType.DalHandler); case MobileConnectionType mobileConnectionType: return new MobileConnectionHandler().Run(mobileConnectionType.IntezmenyAzonosito, mobileConnectionType.TanevId, actions, mobileConnectionType.FelhasznaloId); case OrganizationConnectionType organizationConnectionType: return new OrganizationConnectionHandler().Run(organizationConnectionType.IntezmenyAzonosito, actions); case SessionConnectionType sessionConnectionType: return new ConnectionHandler().Run(sessionConnectionType.SessionId, actions); case SystemConnectionType systemConnectionType: return new SystemConnectionHandler().Run(actions); default: throw new ArgumentException("Ismeretlen ConnectionTpye", nameof(connectionType)); } } } }