internal void SetParent(RequestModel model) { _parent = model; }
internal async Task ReloadData(Action <ExpandoObject> setParams, String json, TextWriter writer) { ExpandoObject dataToSave = JsonConvert.DeserializeObject <ExpandoObject>(json, new ExpandoObjectConverter()); String baseUrl = dataToSave.Get <String>("baseUrl"); // initial = [query, controller] ExpandoObject initialParams = new ExpandoObject(); if (baseUrl.Contains("?")) { var parts = baseUrl.Split('?'); baseUrl = parts[0]; // parts[1] contains query parameters var qryParamsColl = HttpUtility.ParseQueryString(parts[1]); initialParams.Append(CheckPeriod(qryParamsColl), toPascalCase: true); } setParams?.Invoke(initialParams); if (NormalizeBaseUrl != null) { baseUrl = NormalizeBaseUrl(baseUrl); } if (baseUrl == null) { throw new RequestModelException("There are not base url for command 'reload'"); } var rm = await RequestModel.CreateFromBaseUrl(_host, Admin, baseUrl); RequestView rw = rm.GetCurrentAction(); String loadProc = rw.LoadProcedure; if (loadProc == null) { throw new RequestModelException("The data model is empty"); } // realParams = [model.json, id, initial] var loadPrms = new ExpandoObject(); loadPrms.Append(rw.parameters); // model.json loadPrms.Set("Id", rw.Id); // id loadPrms.Append(initialParams); // initial ExpandoObject prms2 = loadPrms; if (rw.indirect) { // for indirect action - @UserId and @Id only prms2 = new ExpandoObject(); setParams?.Invoke(prms2); prms2.Set("Id", rw.Id); } IDataModel model = await _dbContext.LoadModelAsync(rw.CurrentSource, loadProc, prms2); if (rw.HasMerge) { var mergeModel = await _dbContext.LoadModelAsync(rw.MergeSource, rw.MergeLoadProcedure, prms2); model.Merge(mergeModel); } rw = await LoadIndirect(rw, model, loadPrms); model.AddRuntimeProperties(); WriteDataModel(model, writer); }
async Task <RequestView> LoadIndirect(RequestView rw, IDataModel innerModel, ExpandoObject loadPrms) { if (!rw.indirect) { return(rw); } if (!String.IsNullOrEmpty(rw.target)) { String targetUrl = innerModel.Root.Resolve(rw.target); if (String.IsNullOrEmpty(rw.targetId)) { throw new RequestModelException("targetId must be specified for indirect action"); } targetUrl += "/" + innerModel.Root.Resolve(rw.targetId); var rm = await RequestModel.CreateFromUrl(_host, rw.CurrentKind, targetUrl); rw = rm.GetCurrentAction(); String loadProc = rw.LoadProcedure; if (loadProc != null) { loadPrms.Set("Id", rw.Id); if (rw.parameters != null) { loadPrms.AppendIfNotExists(rw.parameters); } var newModel = await _dbContext.LoadModelAsync(rw.CurrentSource, loadProc, loadPrms, rw.commandTimeout); innerModel.Merge(newModel); innerModel.System.Set("__indirectUrl__", rm.BaseUrl); } } else { // simple view/model redirect if (rw.targetModel == null) { throw new RequestModelException("'targetModel' must be specified for indirect action without 'target' property"); } rw.model = innerModel.Root.Resolve(rw.targetModel.model); rw.view = innerModel.Root.Resolve(rw.targetModel.view); rw.viewMobile = innerModel.Root.Resolve(rw.targetModel.viewMobile); rw.schema = innerModel.Root.Resolve(rw.targetModel.schema); if (String.IsNullOrEmpty(rw.schema)) { rw.schema = null; } rw.template = innerModel.Root.Resolve(rw.targetModel.template); if (String.IsNullOrEmpty(rw.template)) { rw.template = null; } String loadProc = rw.LoadProcedure; if (loadProc != null) { loadPrms.Set("Id", rw.Id); var newModel = await _dbContext.LoadModelAsync(rw.CurrentSource, loadProc, loadPrms, rw.commandTimeout); innerModel.Merge(newModel); } } return(rw); }
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); _host.CheckTypes(action.Path, action.checkTypes, dm); 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 if (!String.IsNullOrEmpty(export.template)) { 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; case RequestExportFormat.dbf: case RequestExportFormat.csv: { var fmt = export.format.ToString().ToLowerInvariant(); var extDataProvider = _externalDataProvider.GetWriter(dm, fmt, export.GetEncoding()); if (extDataProvider == null) { throw new RequestModelException($"There is no data provider for '{fmt}' files"); } extDataProvider.Write(response.OutputStream); SetResponseInfo(response, export); } break; } }
public async Task SaveFiles(String pathInfo, HttpFileCollectionBase files, Action <ExpandoObject> setParams, TextWriter writer) { var rm = await RequestModel.CreateFromBaseUrl(_host, pathInfo); ExpandoObject prms = new ExpandoObject(); var ru = rm.GetFile(); ExpandoObject savePrms = new ExpandoObject(); setParams?.Invoke(savePrms); savePrms.AppendIfNotExists(ru.parameters); ru.CheckPermissions(_userStateManager?.GetUserPermissions(), _host.IsDebugConfiguration); switch (ru.type) { case RequestFileType.clr: { if (String.IsNullOrEmpty(ru.clrType)) { throw new RequestModelException($"'clrType' is required for '{rm.ModelFile}' file"); } savePrms.Set("Id", ru.Id); savePrms.Set("Stream", files[0].InputStream); savePrms.Set("FileName", files[0].FileName); var result = await DoUploadClr(ru, savePrms); writer.Write(JsonConvert.SerializeObject(result, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration))); } break; case RequestFileType.parse: { savePrms.Set("Id", ru.Id); ExpandoObject dm = null; var fileName = Path.GetFileName(files[0].FileName); switch (ru.parse) { case RequestFileParseType.auto: var ext = Path.GetExtension(files[0].FileName).ToLowerInvariant(); switch (ext) { case ".xlsx": dm = await SaveExcel(ru, files[0].InputStream, savePrms, fileName); break; case ".csv": dm = await SaveFlat("csv", ru, files[0].InputStream, savePrms); break; case ".dbf": dm = await SaveFlat("dbf", ru, files[0].InputStream, savePrms); break; case ".xml": dm = await SaveFlat("xml", ru, files[0].InputStream, savePrms); break; case ".json": dm = await SaveJson(ru, files[0].InputStream, savePrms); break; default: throw new RequestModelException($"'{ext}' file not yet supported"); } break; case RequestFileParseType.excel: case RequestFileParseType.xlsx: dm = await SaveExcel(ru, files[0].InputStream, savePrms, fileName); break; case RequestFileParseType.csv: dm = await SaveFlat("csv", ru, files[0].InputStream, savePrms); break; case RequestFileParseType.dbf: dm = await SaveFlat("dbf", ru, files[0].InputStream, savePrms); break; case RequestFileParseType.xml: dm = await SaveFlat("xml", ru, files[0].InputStream, savePrms); break; case RequestFileParseType.json: dm = await SaveJson(ru, files[0].InputStream, savePrms); break; } if (dm != null) { WriteExpandoObject(dm, writer); } } break; case RequestFileType.sql: { savePrms.Set("Id", ru.Id); var result = await SaveFilesSql(ru, savePrms, files); writer.Write(JsonConvert.SerializeObject(result, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration))); } break; case RequestFileType.azureBlob: { savePrms.Set("Id", ru.Id); var result = await SaveFilesAzureStorage(ru, savePrms, files); writer.Write(JsonConvert.SerializeObject(result, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration))); } break; } }