Exemplo n.º 1
0
        public String ResolveEnvironment(String text)
        {
            if (text.IndexOf("((") == -1)
            {
                return(text);
            }
            var r  = new Regex("\\(\\((.+?)\\)\\)");
            var ms = r.Matches(text);

            if (ms.Count == 0)
            {
                return(text);
            }
            var sb  = new StringBuilder(text);
            var env = _msgModel.Eval <List <ExpandoObject> >("Message.Environment")
                      .ToDictionary(k => k.Get <String>("Name"), v => v.Get <String>("Value"));

            foreach (Match m in ms)
            {
                String key = m.Groups[1].Value;
                if (env.TryGetValue(key, out String val))
                {
                    sb.Replace(m.Value, val);
                }
                else
                {
                    if (key == "Host")
                    {
                        throw new MessagingException("Configuration parameter 'appSettings/appHost' not defined");
                    }
                    throw new MessagingException($"Invalid environment key. '{key}'");
                }
            }
            return(sb.ToString());
        }
Exemplo n.º 2
0
        Task <IMessageForSend> ResolveMessageAsync(IDataModel dm)
        {
            var templateName = dm.Eval <String>("Message.Template");
            var key          = dm.Eval <String>("Message.Key");
            var templatePath = _host.ApplicationReader.MakeFullPath(templateName, String.Empty);
            var fullPath     = Path.ChangeExtension(templatePath, "xaml");

            var env     = dm.Eval <List <ExpandoObject> >("Message.Environment");
            var hostObj = new ExpandoObject();

            hostObj.Set("Name", "Host");
            hostObj.Set("Value", _host.AppHost);
            env.Add(hostObj);

            using (var stream = _host.ApplicationReader.FileStreamFullPath(fullPath)) {
                var tml             = XamlServices.Load(stream) as Template;
                TemplatedMessage tm = tml.Get(key);
                if (tm == null)
                {
                    throw new MessagingException($"Message not found. Key = '{key}'");
                }
                var resolver = new MessageResolver(_host, _dbContext, dm);
                return(tm.ResolveAndSendAsync(resolver));
            }
        }
Exemplo n.º 3
0
        public MessageResolver(IApplicationHost host, IDbContext dbContext, IDataModel msgModel)
        {
            _host      = host;
            _dbContext = dbContext;
            _msgModel  = msgModel;
            if (_msgModel == null)
            {
                return;
            }
            _msgParams = new ExpandoObject();
            var trgId = msgModel.Eval <Int64>("Message.TargetId");

            if (trgId != 0)
            {
                _msgParams.Set("TargetId", msgModel.Eval <Int64>("Message.TargetId"));
            }
            var env = msgModel.Eval <List <ExpandoObject> >("Message.Environment");

            foreach (var e in env)
            {
                var key = e.Get <String>("Name");
                if (key == "Host")
                {
                    return;
                }
                var val = e.Get <String>("Value");
                if (!String.IsNullOrEmpty(val))
                {
                    _msgParams.Set(key, val);
                }
            }
        }
Exemplo n.º 4
0
        public async Task Export(String path, Int32 TenantId, Int64 UserId, ExpandoObject prms, HttpResponseBase response)
        {
            var rm = await RequestModel.CreateFromBaseUrl(_host, Admin, path);

            var action = rm.CurrentAction;
            var export = action.Export;

            if (export == null)
            {
                throw new RequestModelException($"There is no export in '{rm.ModelAction}' action");
            }
            if (prms != null)
            {
                prms.Append(action.parameters);
                prms.SetIfNotExists("Id", action.Id);
            }
            IDataModel dm = await _dbContext.LoadModelAsync(action.CurrentSource, action.ExportProcedure, prms);

            Stream stream    = null;
            var    templExpr = export.GetTemplateExpression();

            if (!String.IsNullOrEmpty(templExpr))
            {
                var bytes = dm.Eval <Byte[]>(templExpr);
                if (bytes == null)
                {
                    throw new RequestModelException($"Template stream not found or its format is invalid. ({templExpr})");
                }
                stream = new MemoryStream(dm.Eval <Byte[]>(templExpr));
            }
            else
            {
                var fileName  = export.template.AddExtension(export.format.ToString());
                var appReader = _host.ApplicationReader;
                var filePath  = appReader.MakeFullPath(action.Path, fileName.RemoveHeadSlash());
                if (!appReader.FileExists(filePath))
                {
                    throw new FileNotFoundException($"Template file not found. ({fileName})");
                }
                stream = appReader.FileStreamFullPathRO(filePath);
            }

            switch (export.format)
            {
            case RequestExportFormat.xlsx:
                using (var rep = new ExcelReportGenerator(stream))
                {
                    rep.GenerateReport(dm);
                    Byte[] bytes = File.ReadAllBytes(rep.ResultFile);
                    if (bytes == null || bytes.Length == 0)
                    {
                        throw new RequestModelException("There are no bytes to send");
                    }
                    SetResponseInfo(response, export);
                    response.BinaryWrite(bytes);
                    stream.Close();
                }
                break;
            }
        }
Exemplo n.º 5
0
 public DataTester(IDataModel dataModel, String expression)
 {
     _dataModel     = dataModel;
     _instance      = dataModel.Eval <ExpandoObject>(expression);
     _instanceArray = dataModel.Eval <IList <ExpandoObject> >(expression);
     Assert.IsTrue(_instance != null || _instanceArray != null, $"Could not evaluate expression '{expression}'");
 }
Exemplo n.º 6
0
 void ProcessDataSets()
 {
     foreach (var dataSet in _dataSetRows)
     {
         IList <ExpandoObject> list = _dataModel.Eval <List <ExpandoObject> >(dataSet.Key);
         if (list == null)
         {
             throw new InteropException($"The data model does not have a '{dataSet.Key}' property ");
         }
         RowSetDef def = dataSet.Value;
         if (list.Count == 0)
         {
             // no records - delete range
             for (Int32 i = 0; i < def.RowCount; i++)
             {
                 var row = _sheetData.Elements <Row>().First <Row>(r => r.RowIndex == def.FirstRow + i);
                 row.Remove();
             }
             _wrkshtModified = true;
         }
         else
         {
             UInt32 count = 0;
             Row    lr    = null;
             for (Int32 i = 0; i < list.Count; i++)
             {
                 lr = InsertRowFromTemplate(def, ref count);
                 _wrkshtModified = true;
                 SetRecordData(def, list[i]);
             }
         }
     }
 }
Exemplo n.º 7
0
        protected Stream CreateStream(IDataModel model, String report)
        {
            String templExpr = report.TemplateExpression();

            if (templExpr != null)
            {
                var bytes = model.Eval <Byte[]>(templExpr);
                if (bytes == null)
                {
                    throw new InvalidDataException($"Expression '{report}'  is null");
                }
                return(new MemoryStream(bytes));
            }
            else
            {
                String path = _host.ApplicationReader.MakeFullPath(report, String.Empty).ToLowerInvariant();
                if (!path.EndsWith(FileExtension))
                {
                    path += FileExtension;
                }
                if (!_host.ApplicationReader.FileExists(path))
                {
                    throw new FileNotFoundException(path);
                }
                return(_host.ApplicationReader.FileStreamFullPathRO(path));
            }
        }
Exemplo n.º 8
0
 void SetUserStatePermission(IDataModel model)
 {
     if (_userStateManager == null)
     {
         return;
     }
     _userStateManager.SetReadOnly(model.Eval <Boolean>("UserState.ReadOnly"));
 }
Exemplo n.º 9
0
 void ProcessDataSets()
 {
     foreach (var dataSet in _dataSetRows)
     {
         IList <ExpandoObject> list = _dataModel.Eval <List <ExpandoObject> >(dataSet.Key);
         if (list == null)
         {
             throw new InteropException($"The data model does not have a '{dataSet.Key}' property ");
         }
         RowSetDef def   = dataSet.Value;
         UInt32    count = 0;
         Row       lr    = null;
         for (Int32 i = 0; i < list.Count; i++)
         {
             lr = InsertRowFromTemplate(def, ref count);
             _wrkshtModified = true;
             SetRecordData(def, list[i]);
         }
     }
 }
Exemplo n.º 10
0
        public Int64 QueueMessage(IQueuedMessage message)
        {
            ExpandoObject msg   = PrepareMessage(message);
            IDataModel    md    = _dbContext.SaveModel(String.Empty, "a2messaging.[Message.Queue.Update]", msg);
            Int64         msgId = md.Eval <Int64>("Result.Id");

            if (message.Immediately)
            {
                FireAndForget(SendMessageAsync(msgId));
            }
            return(msgId);
        }
Exemplo n.º 11
0
        ApiResponse ModelResponse(IDataModel dm)
        {
            Object model = dm.Root;

            if (!String.IsNullOrEmpty(_command.Returns))
            {
                model = dm.Eval <Object>(_command.Returns);
            }

            return(new ApiResponse()
            {
                ContentType = MimeTypes.Application.Json,
                Body = JsonConvert.SerializeObject(Wrap(model), JsonHelpers.CompactSerializerSettings)
            });
        }
Exemplo n.º 12
0
        public async Task <Int64> QueueMessageAsync(IQueuedMessage message, Boolean immediately)
        {
            ExpandoObject msg = PrepareMessage(message);
            IDataModel    md  = await _dbContext.SaveModelAsync(String.Empty, "a2messaging.[Message.Queue.Update]", msg);

            Int64 msgId = md.Eval <Int64>("Result.Id");

            if (message.Immediately)
            {
                /*
                 * Task.Run(() => {
                 *      SendMessageAsync(msgId);
                 * });
                 */
            }
            return(msgId);
        }
Exemplo n.º 13
0
        String ResolveDataModel(String source)
        {
            if (_dataModel == null)
            {
                return(source);
            }
            if (source.IndexOf("{{") == -1)
            {
                return(source);
            }
            if (_datRegEx == null)
            {
                _datRegEx = new Regex("\\{\\{(.+?)\\}\\}", RegexOptions.Compiled);
            }
            var ms = _datRegEx.Matches(source);

            if (ms.Count == 0)
            {
                return(source);
            }
            var sb = new StringBuilder(source);

            foreach (Match m in ms)
            {
                String key    = m.Groups[1].Value;
                var    valObj = _dataModel.Eval <Object>(key);
                if (ms.Count == 1 && m.Groups[0].Value == source)
                {
                    return(valObj?.ToString());                    // single element
                }
                if (valObj is String valStr)
                {
                    sb.Replace(m.Value, valStr);
                }
                else if (valObj is ExpandoObject valEo)
                {
                    sb.Replace(m.Value, JsonConvert.SerializeObject(valEo));
                }
                else
                {
                    sb.Replace(m.Value, valObj.ToString());
                }
            }
            return(sb.ToString());
        }
Exemplo n.º 14
0
        public static String ResolveDataModel(this IDataModel model, String source)
        {
            if (model == null)
            {
                return(source);
            }
            if (String.IsNullOrEmpty(source))
            {
                return(source);
            }
            if (source.IndexOf("{{") == -1)
            {
                return(source);
            }
            var ms = Regex.Matches(source, "\\{\\{(.+?)\\}\\}");

            if (ms.Count == 0)
            {
                return(source);
            }
            var sb = new StringBuilder(source);

            foreach (Match m in ms)
            {
                String key    = m.Groups[1].Value;
                var    valObj = model.Eval <Object>(key);
                if (ms.Count == 1 && m.Groups[0].Value == source)
                {
                    return(valObj?.ToString());                    // single element
                }
                if (valObj is String valStr)
                {
                    sb.Replace(m.Value, valStr);
                }
                else if (valObj is ExpandoObject valEo)
                {
                    sb.Replace(m.Value, JsonConvert.SerializeObject(valEo));
                }
                else
                {
                    sb.Replace(m.Value, valObj.ToString());
                }
            }
            return(sb.ToString());
        }
Exemplo n.º 15
0
        UserCompany SetUserCompany(Int32 TenantId, Int64 UserId)
        {
            // TenantId is null
            Object     TenantToCall = TenantId == 0 ? null : (Object)TenantId;
            var        userCompany  = new UserCompany();
            IDataModel dm           = null;

            if (!_host.IsMultiTenant && _host.IsMultiCompany)
            {
                dm = _dbContext.LoadModel(null, "[a2security].[User.Company.Load]", new { UserId });
            }
            else if (_host.IsMultiTenant)
            {
                dm = _dbContext.LoadModel(null, "[a2security_tenant].[UserCompany.Load]", new { TenantId = TenantToCall, UserId });
            }
            userCompany.CompanyId = dm.Eval <Int64>("UserCompany.Company");
            if (userCompany.CompanyId == 0)
            {
                throw new InvalidOperationException("Procedure 'UserCompany.Load' returned '0'.");
            }
            HttpContext.Current.Session[_userCompanyKey] = userCompany;
            return(userCompany);
        }