Пример #1
0
        public static SMSMessageEntity CreateSMSMessage(this ISMSModel smsModel, CultureInfo?forceCultureInfo = null)
        {
            if (smsModel.UntypedEntity == null)
            {
                throw new InvalidOperationException("Entity property not set on SMSModel");
            }

            using (IsolationEntity.Override((smsModel.UntypedEntity as Entity)?.TryIsolation()))
            {
                var smsModelEntity = ToSMSModelEntity(smsModel.GetType());
                var template       = GetDefaultTemplate(smsModelEntity);

                return(SMSLogic.CreateSMSMessage(template.ToLite(), smsModel.UntypedEntity, smsModel, forceCultureInfo));
            }
        }
Пример #2
0
        public static void Register()
        {
            GetState = m => m.State;

            new ConstructFrom <SMSTemplateEntity>(SMSMessageOperation.CreateSMSFromTemplate)
            {
                CanConstruct = t => !t.IsActive ? SMSCharactersMessage.TheTemplateMustBeActiveToConstructSMSMessages.NiceToString() : null,
                ToStates     = { SMSMessageState.Created },
                Construct    = (t, args) =>
                {
                    return(SMSLogic.CreateSMSMessage(t.ToLite(),
                                                     args.GetArg <Lite <Entity> >().RetrieveAndRemember(),
                                                     args.TryGetArgC <ISMSModel>(),
                                                     args.TryGetArgC <CultureInfo>()));
                }
            }.Register();

            new Execute(SMSMessageOperation.Send)
            {
                CanBeNew      = true,
                CanBeModified = true,
                FromStates    = { SMSMessageState.Created },
                ToStates      = { SMSMessageState.Sent },
                Execute       = (m, _) =>
                {
                    try
                    {
                        SMSLogic.SendSMS(m);
                    }
                    catch (Exception e)
                    {
                        var ex = e.LogException();
                        using (Transaction tr = Transaction.ForceNew())
                        {
                            m.Exception = ex.ToLite();
                            m.Save();
                            tr.Commit();
                        }
                        throw;
                    }
                }
            }.Register();


            new Graph <SMSMessageEntity> .Execute(SMSMessageOperation.UpdateStatus)
            {
                CanExecute = m => m.State != SMSMessageState.Created ? null : SMSCharactersMessage.StatusCanNotBeUpdatedForNonSentMessages.NiceToString(),
                Execute    = (sms, args) =>
                {
                    var func = args.TryGetArgC <Func <SMSMessageEntity, SMSMessageState> >();
                    if (func == null)
                    {
                        func = SMSLogic.GetProvider().SMSUpdateStatusAction;
                    }

                    sms.State = func(sms);

                    if (sms.UpdatePackage != null)
                    {
                        sms.UpdatePackageProcessed = true;
                    }
                }
            }

            .Register();
        }