internal static bool FormatExists(dynamic dto) { bool exists = false; ADO Ado = new ADO("defaultconnection"); try { Format_ADO adoFormat = new Format_ADO(); Format_DTO_Read dtoFormat = new Format_DTO_Read(); dtoFormat.FrmType = dto.Format.FrmType; dtoFormat.FrmVersion = dto.Format.FrmVersion; var result = adoFormat.Read(Ado, dtoFormat); exists = result.hasData; } catch { exists = false; } finally { Ado.Dispose(); } return(exists); }
private bool ValidateFormat(dynamic frm) { Format_DTO_Read format = new Format_DTO_Read() { FrmType = frm.Type, FrmVersion = frm.Version, FrmDirection = "DOWNLOAD" }; return(Build.CustomValidations.FormatForReadDataset(format)); }
private bool ValidateFormat(Format ex) { Format_DTO_Read format = new Format_DTO_Read() { FrmType = ex.Type, FrmVersion = ex.Version }; return(Build.CustomValidations.FormatForReadDataset(format)); }
internal static bool FormatForBuildCreate(Format_DTO_Read dto) { if (dto.FrmDirection != Format_DTO_Read.FormatDirection.UPLOAD.ToString()) { return(false); } if (dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.JSONstat) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.PX)) { return(false); } return(true); }
internal static bool FormatForReadPreDataset(Format_DTO_Read dto) { if (dto.FrmDirection != Format_DTO_Read.FormatDirection.DOWNLOAD.ToString()) { return(false); } if (dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.JSONstat) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.PX) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.CSV) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.XLSX)) { return(false); } return(true); }
internal static bool FormatForReadMetadata(Format_DTO_Read dto) { if (dto.FrmDirection != Format_DTO_Read.FormatDirection.DOWNLOAD.ToString()) { return(false); } if (dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.JSONstat)) { return(false); } return(true); }
internal static bool FormatExistsReadDataset(RESTful_API api) { if (api.parameters.Count < Convert.ToInt32(Utility.GetCustomConfig("APP_REST_READ_DATASET_PARAMETER_COUNT"))) { return(false); } Format_DTO_Read dto = new Format_DTO_Read() { FrmType = api.parameters[2], FrmVersion = api.parameters[3] == "" ? "none" : api.parameters[3] }; return(CustomValidations.FormatExists(dto)); }
internal static bool FormatForReadDataset(Format_DTO_Read dto) { if (dto.FrmDirection != Format_DTO_Read.FormatDirection.DOWNLOAD.ToString()) { return(false); } if (dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.JSONstat) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.PX) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.CSV) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.XLSX) && dto.FrmType != EnumInfo.GetEnumDescription(Format_DTO_Read.FormatType.SDMX)) { return(false); } bool exists; using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection"))) { exists = bso.Exists(dto); } return(exists); }
/// <summary> /// Map RESTful parameters to JsonRpc parameters /// </summary> /// <param name="restfulParameters"></param> /// <returns></returns> internal dynamic ReadMetadata_MapParameters(dynamic restfulParameters) { Format_DTO_Read format; using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection"))) { format = bso.Read(new Format_DTO_Read() { FrmType = Constants.C_SYSTEM_JSON_STAT_NAME, FrmDirection = Format_DTO_Read.FormatDirection.DOWNLOAD.ToString() }).FirstOrDefault(); } //Create a null format if one is not found. This will be caught in validation if (format == null) { format = new Format_DTO_Read(); } var prm = JObject.FromObject(new { matrix = restfulParameters.Count >= 2 ? restfulParameters[1] : null, language = restfulParameters.Count >= 3 ? restfulParameters[2] : null, format = new { type = format.FrmType, version = format.FrmVersion } }); // To make the parameters agnostic of trailing slashes: if (restfulParameters.Count >= 2) { if (restfulParameters[1].ToString().Length == 0) { prm["matrix"] = null; } } if (restfulParameters.Count >= 3) { if (restfulParameters[2].ToString().Length == 0) { prm["language"] = null; } } return(prm); }
/// <summary> /// Get a JSON-stat metadata based on a database read of collections /// </summary> /// <param name="collection"></param> /// <param name="statistics"></param> /// <param name="classifications"></param> /// <returns></returns> private Item GetJsonStatRelease(List <dynamic> collection, List <dynamic> statistics, List <dynamic> classifications, List <dynamic> periods, List <Format_DTO_Read> formats) { var jsStat = new Item(); // jsStat.Class = Class.Dataset; //jsStat.Version = Version.The20; jsStat.Id = new List <string>(); jsStat.Size = new List <long>(); var thisItem = collection.FirstOrDefault(); jsStat.Extension = new Dictionary <string, object>(); var Frequency = new { name = thisItem.FrqValue, code = thisItem.FrqCode }; jsStat.Extension.Add("copyright", new { name = thisItem.CprValue, code = thisItem.CprCode, href = thisItem.CprUrl }); jsStat.Extension.Add("exceptional", thisItem.ExceptionalFlag); jsStat.Extension.Add("language", new { code = thisItem.LngIsoCode, name = thisItem.LngIsoName }); jsStat.Extension.Add("matrix", thisItem.MtrCode); Format_DTO_Read fDtoMain = formats.Where(x => x.FrmType == Constants.C_SYSTEM_JSON_STAT_NAME).FirstOrDefault();// new Format_DTO_Read() { FrmDirection = Utility.GetCustomConfig("APP_FORMAT_DOWNLOAD_NAME"), FrmType = Constants.C_SYSTEM_JSON_STAT_NAME }; jsStat.Href = new Uri(Configuration_BSO.GetCustomConfig("url.restful") + string.Format(Utility.GetCustomConfig("APP_RESTFUL_DATASET"), Utility.GetCustomConfig("APP_READ_DATASET_API"), thisItem.MtrCode, fDtoMain.FrmType, fDtoMain.FrmVersion, thisItem.LngIsoCode)); jsStat.Label = thisItem.MtrTitle; jsStat.Updated = DataAdaptor.ConvertToString(thisItem.RlsLiveDatetimeFrom); jsStat.Dimension = new Dictionary <string, Dimension>(); formats.Remove(fDtoMain); var link = new DimensionLink { Alternate = new List <Alternate>() }; foreach (var f in formats) { link.Alternate.Add(new Alternate() { Href = Configuration_BSO.GetCustomConfig("url.restful") + string.Format(Utility.GetCustomConfig("APP_RESTFUL_DATASET"), Utility.GetCustomConfig("APP_READ_DATASET_API"), thisItem.MtrCode, f.FrmType, f.FrmVersion, thisItem.LngIsoCode) }); } jsStat.Link = link; formats.Add(fDtoMain); var statDimension = new Dimension() { Label = Utility.GetCustomConfig("APP_CSV_STATISTIC"), Category = new Category() { } }; jsStat.Dimension.Add(Utility.GetCustomConfig("APP_CSV_STATISTIC"), statDimension); jsStat.Id.Add(Frequency.code); List <PeriodRecordDTO_Create> plist = new List <PeriodRecordDTO_Create>(); foreach (var per in periods) { plist.Add(new PeriodRecordDTO_Create() { Code = per.PrdCode, Value = per.PrdValue }); } var timeDimension = new Dimension() { Label = Frequency.name, Category = new Category() { Index = plist.Select(v => v.Code).ToList(), Label = plist.ToDictionary(v => v.Code, v => v.Value) } }; jsStat.Dimension.Add(Frequency.code, timeDimension); foreach (var s in classifications) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add(s.ClsCode, s.ClsValue); var clsDimension = new Dimension() { Label = s.ClsValue, Category = new Category() { } }; jsStat.Dimension.Add(s.ClsCode, clsDimension); } jsStat.Role = new Role(); jsStat.Role.Metric = new List <string> { Utility.GetCustomConfig("APP_CSV_STATISTIC") }; jsStat.Role.Time = new List <string> { thisItem.FrqCode }; //Values will be blank in this case, all we want is metadata //jsStat.Value = new JsonStatValue() { AnythingArray = new List<ValueElement>() }; return(jsStat); }
/// <summary> /// /// </summary> /// <param name="parameters"></param> public BuildUpdate_DTO(dynamic parameters) { if (parameters.MtrInput != null) { this.MtrInput = Utility.DecodeBase64ToUTF8((string)parameters["MtrInput"]); } if (parameters.Data != null) { PxData = new PxData_DTO(parameters); } if (parameters.LngIsoCode != null) { LngIsoCode = parameters.LngIsoCode; } else { LngIsoCode = Configuration_BSO.GetCustomConfig(ConfigType.global, "language.iso.code"); } Periods = new List <PeriodRecordDTO_Create>(); if (parameters.MtrCode != null) { MtrCode = parameters.MtrCode; } if (parameters.CprCode != null) { CprCode = parameters.CprCode; } if (parameters.FrqCodeTimeval != null) { FrqCodeTimeval = parameters.FrqCodeTimeval; } if (parameters.FrqValueTimeval != null) { FrqValueTimeval = parameters.FrqValueTimeval; } if (parameters.MtrOfficialFlag != null) { MtrOfficialFlag = parameters.MtrOfficialFlag; } Format = new Format_DTO_Read(); if (parameters.FrmType != null && parameters.FrmVersion != null) { Format.FrmType = parameters.FrmType; Format.FrmVersion = parameters.FrmVersion; Format.FrmDirection = Format_DTO_Read.FormatDirection.UPLOAD.ToString(); } if (parameters.Signature != null) { this.Signature = parameters.Signature; } if (parameters.Labels != null) { this.Labels = parameters.Labels; } else { this.Labels = true; } if (parameters.Elimination != null) { Elimination = JsonConvert.DeserializeObject <Dictionary <string, string> >(parameters.Elimination.ToString()); } if (parameters.Dimension != null) { Dimension = new List <Dimension_DTO>(); foreach (var dim in parameters.Dimension) { Dimension_DTO dimension = new Dimension_DTO(); if (dim.Frequency != null) { dimension.Frequency = new FrequencyRecordDTO_Create(); if (dim.Frequency.Period != null) { dimension.Frequency.Period = new List <Data.PeriodRecordDTO_Create>(); foreach (var per in dim.Frequency.Period) { PeriodRecordDTO_Create period = new PeriodRecordDTO_Create(); if (per.PrdCode != null) { period.Code = per.PrdCode; } if (per.PrdValue != null) { period.Value = per.PrdValue; } if (Periods.Where(x => x.Code.Equals(period.Code)).FirstOrDefault() == null) { Periods.Add(period); } dimension.Frequency.Period.Add(period); } } if (dim.Frequency.FrqValue != null) { dimension.Frequency.Value = dim.Frequency.FrqValue; } } if (dim.Classification != null) { dimension.Classifications = new List <ClassificationRecordDTO_Create>(); foreach (var cls in dim.Classification) { ClassificationRecordDTO_Create classification = new ClassificationRecordDTO_Create(); if (cls.ClsCode != null) { classification.Code = cls.ClsCode; } dimension.Classifications.Add(classification); } } if (dim.LngIsoCode != null) { dimension.LngIsoCode = dim.LngIsoCode; } if (dim.MtrTitle != null) { dimension.MtrTitle = dim.Dimension; } if (dim.FrqValue != null) { dimension.FrqValue = dim.FrqValue; } if (dim.MtrNote != null) { dimension.MtrNote = dim.MtrNote; } if (dim.StatisticLabel != null) { dimension.StatisticLabel = dim.StatisticLabel; } if (dim.MtrTitle != null) { dimension.MtrTitle = dim.MtrTitle; } Dimension.Add(dimension); } } if (parameters.Map != null) { Map = JsonConvert.DeserializeObject <Dictionary <string, string> >(parameters.Map.ToString()); } }
/// <summary> /// /// </summary> /// <param name="parameters"></param> public Build_DTO(dynamic parameters) { if (parameters.MtrInput != null) { this.MtrInput = Utility.DecodeBase64ToUTF8((string)parameters["MtrInput"]); } this.LngIsoCode = Configuration_BSO.GetCustomConfig(ConfigType.global, "language.iso.code"); if (parameters.MtrOfficialFlag != null) { this.MtrOfficialFlag = parameters.MtrOfficialFlag; } else { this.MtrOfficialFlag = Configuration_BSO.GetCustomConfig(ConfigType.global, "dataset.officialStatistics"); } if (parameters.MtrCode != null) { this.MtrCode = parameters.MtrCode; } if (parameters.CprCode != null) { this.CprCode = parameters.CprCode; } if (parameters.FrqCode != null) { this.FrqCode = parameters.FrqCode; } if (parameters.FrqValue != null) { this.FrqValue = parameters.FrqValue; } Format = new Format_DTO_Read(); if (parameters.FrmType != null && parameters.FrmVersion != null) { Format = new Format_DTO_Read { FrmVersion = parameters.FrmVersion, FrmType = parameters.FrmType, FrmDirection = Format_DTO_Read.FormatDirection.UPLOAD.ToString() }; } if (parameters.Elimination != null) { Elimination = JsonConvert.DeserializeObject <Dictionary <string, string> >(parameters.Elimination.ToString()); } if (parameters.Dimension != null) { matrixDto = new Matrix_DTO(); if (parameters.LngIsoCode != null) { matrixDto.LngIsoCode = parameters.LngIsoCode; } else { matrixDto.LngIsoCode = Configuration_BSO.GetCustomConfig(ConfigType.global, "language.iso.code"); } if (parameters.MtrCode != null) { matrixDto.MtrCode = parameters.MtrCode; } if (parameters.MtrOfficialFlag != null) { matrixDto.MtrOfficialFlag = parameters.MtrOfficialFlag; } DimensionList = new List <Dimension_DTO>(); foreach (var dim in parameters.Dimension) { Dimension_DTO mlDto = new Dimension_DTO(dim); if (dim.MtrTitle != null) { mlDto.Contents = dim.MtrTitle; if (mlDto.Classifications != null) { mlDto.MtrTitle = mlDto.Contents + " (" + String.Join(", ", ((mlDto.Classifications.Select(c => c.Value.ToString())).ToList <string>()).ToArray()) + ')'; } else { mlDto.MtrTitle = dim.MtrTitle; } if (mlDto.MtrTitle.Length > 256) { mlDto.MtrTitle = mlDto.MtrTitle.Substring(0, 256); } } if (this.CprCode != null) { mlDto.CprCode = this.CprCode; Copyright_BSO cBso = new Copyright_BSO(); Copyright_DTO_Create cDTO = cBso.Read(this.CprCode); mlDto.CprValue = cDTO.CprValue; } if (dim.CprValue != null) { mlDto.CprValue = dim.CprValue; } if (dim.Frequency != null) { mlDto.Frequency = new FrequencyRecordDTO_Create(); if (parameters.FrqCode != null) { mlDto.Frequency.Code = parameters.FrqCode; } if (dim.Frequency.FrqValue != null) { mlDto.Frequency.Value = dim.Frequency.FrqValue; } if (dim.Frequency.Period != null) { mlDto.Frequency.Period = new List <PeriodRecordDTO_Create>(); foreach (var per in dim.Frequency.Period) { PeriodRecordDTO_Create period = new Data.PeriodRecordDTO_Create(); if (per.PrdCode != null) { period.Code = per.PrdCode; } if (per.PrdValue != null) { period.Value = per.PrdValue; } mlDto.Frequency.Period.Add(period); } } } if (this.FrqCode != null) { mlDto.FrqCode = this.FrqCode; //Get Frequency value Frequency_BSO bso = new Frequency_BSO(); Frequency_DTO dto = new Frequency_DTO(); dto = bso.Read(this.FrqCode); mlDto.FrqValue = dto.FrqValue; } if (dim.FrqCode != null) { mlDto.FrqCode = dim.FrqCode; } if (dim.FrqValue != null) { mlDto.FrqValue = dim.FrqValue; } if (dim.LngIsoCode != null) { mlDto.LngIsoCode = dim.LngIsoCode; } if (dim.MtrTitle != null) { mlDto.MtrTitle = dim.MtrTitle; } if (dim.MtrNote != null) { mlDto.MtrNote = dim.MtrNote; } if (dim.Statistic != null) { mlDto.Statistics = new List <StatisticalRecordDTO_Create>(); foreach (var stat in dim.Statistic) { StatisticalRecordDTO_Create src = new StatisticalRecordDTO_Create(stat); mlDto.Statistics.Add(src); } } DimensionList.Add(mlDto); } } if (parameters.Map != null) { Map = JsonConvert.DeserializeObject <Dictionary <string, string> >(parameters.Map.ToString()); } }
/// <summary> /// Constructor /// </summary> /// <param name="parameters"></param> public Cube_DTO_Read(dynamic parameters) { if (parameters.matrix != null) { this.matrix = parameters.matrix; } if (parameters.release != null) { this.release = parameters.release; } if (parameters.language != null) { this.language = parameters.language; } else { this.language = Configuration_BSO.GetCustomConfig(ConfigType.global, "language.iso.code"); } Format = new Format_DTO_Read(); if (parameters.format != null) { if (parameters.format.type != null) { Format.FrmType = parameters.format.type; } Format.FrmDirection = API.Utility.GetCustomConfig("APP_FORMAT_DOWNLOAD_NAME"); if (parameters.format.version != null) { Format.FrmVersion = parameters.format.version; } } if (parameters.role != null) { this.role = GetJsonObject <Role>(parameters.role, Converter.Settings); } if (parameters.dimension != null) { this.dimension = this.GetJsonObject <IList <Dimension> >(parameters.dimension, Converter.Settings); } // Default language if (string.IsNullOrEmpty(this.language)) { this.language = Configuration_BSO.GetCustomConfig(ConfigType.global, "language.iso.code"); } // Default format if (string.IsNullOrEmpty(this.Format.FrmType)) { this.Format.FrmType = Utility.GetCustomConfig("APP_DEFAULT_DATASET_FORMAT"); } }
internal static void Create(HttpRequest hRequest, string method, string userAgent, string ipaddress, string matrixCode, bool m2m, Format_DTO_Read format) { ADO Ado = new ADO("defaultConnection"); try { Analytic_DTO aDto = new Analytic_DTO() { NltMaskedIp = ipaddress, matrix = matrixCode, NltM2m = m2m, NltDate = DateTime.Now, FrmType = format.FrmType, FrmVersion = format.FrmVersion }; // Get the Referer aDto.NltReferer = hRequest.UrlReferrer == null || String.IsNullOrEmpty(hRequest.UrlReferrer.Host) ? Configuration_BSO.GetCustomConfig("analytic.referrer-not-applicable") : hRequest.UrlReferrer.Host; //Get the device detector and populate the dto attributes DeviceDetector deviceDetector = GetDeviceDetector(hRequest.UserAgent); aDto.NltBotFlag = deviceDetector.IsBot(); if (deviceDetector.GetBrowserClient().Match != null) { aDto.NltBrowser = deviceDetector.GetBrowserClient().Match.Name; } if (deviceDetector.GetOs().Match != null) { aDto.NltOs = deviceDetector.GetOs().Match.Name; } var valids = new Analytic_VLD().Validate(aDto); //validate whatever has been returned if (!valids.IsValid) { foreach (var fail in valids.Errors) { Log.Instance.Debug("Analytic method failed validation:" + method + " :" + fail.ErrorMessage); } return; } //Create the analytic entry Analytic_ADO ado = new Analytic_ADO(Ado); if (ado.Create(aDto) == 0) { Log.Instance.Debug("Failed to create Analytic:" + method); return; } return; } catch (Exception ex) { throw ex; } finally { Ado.Dispose(); } }
public static dynamic ReadDataset(RESTful_API restfulRequest) { try { //A pre-validation. If a validation problem is found then an output containing the error will be created and returned immediately var vldOutput = ValidateRest <Cube_VLD_REST_ReadDataset>(restfulRequest, new Cube_VLD_REST_ReadDataset()); if (vldOutput != null) { return(vldOutput); } //Map the RESTful request to an equivalent Json Rpc request JSONRPC_API jsonRpcRequest = Map.RESTful2JSONRPC_API(restfulRequest); //Map the parameters - this is specific to the function Cube_MAP map = new Cube_MAP(); //jsonRpcRequest.parameters = map.ReadDataset_MapParameters(jsonRpcRequest.parameters); JsonStatQuery jq = map.ReadMetadata_MapParametersToQuery(restfulRequest.parameters); jq.Extension["codes"] = true; jsonRpcRequest.parameters = Utility.JsonSerialize_IgnoreLoopingReference(jq); //Run the request as a Json Rpc call JSONRPC_Output rsp = new Cube_BSO_ReadDataset(jsonRpcRequest).Read().Response; if (rsp.error != null) { return(Map.JSONRPC2RESTful_Output(rsp, null, HttpStatusCode.NoContent, HttpStatusCode.InternalServerError)); } Format_DTO_Read format = new Format_DTO_Read() { FrmType = restfulRequest.parameters[Constants.C_DATA_RESTFUL_FORMAT_TYPE], FrmVersion = restfulRequest.parameters[Constants.C_DATA_RESTFUL_FORMAT_VERSION] }; string mtype = null; using (Format_BSO fbso = new Format_BSO(new ADO("defaultConnection"))) { mtype = fbso.GetMimetypeForFormat(format); }; //Convert the JsonRpc output to RESTful output var response = Map.JSONRPC2RESTful_Output(rsp, mtype, rsp.data == null ? HttpStatusCode.NotFound : HttpStatusCode.NoContent); string suffix; using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection"))) { suffix = bso.GetFileSuffixForFormat(format); }; response.fileName = restfulRequest.parameters[Constants.C_DATA_RESTFUL_MATRIX] + "." + DateTime.Now.ToString("yyyyMMddHHmmss") + suffix; return(response); } catch (Exception ex) { RESTful_Output error = new RESTful_Output { statusCode = HttpStatusCode.InternalServerError }; Log.Instance.Debug(ex.Message); return(error); } }
public static dynamic PxAPIv1(RESTful_API restfulRequest) { try { var parameters = ((List <string>)restfulRequest.parameters).Where(x => !string.IsNullOrWhiteSpace(x)); int pcount = parameters.Count() - 1; JSONRPC_API rpc = Map.RESTful2JSONRPC_API(restfulRequest); Cube_MAP map = new Cube_MAP(); JObject rpcParams = new JObject(); PxApiMetadata meta = new PxApiMetadata(); var context = HttpContext.Current.Request; switch (pcount) { case Constants.C_DATA_PXAPIV1_SUBJECT_QUERY: //Get a list of all subjects rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpc.parameters = rpcParams; var rspSbj = new Subject_BSO_Read(rpc).Read().Response; if (rspSbj.data == null) { return new RESTful_Output() { statusCode = HttpStatusCode.NotFound } } ; rspSbj.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadSubjectsAsObjectList(rspSbj)); var responseSbj = Map.JSONRPC2RESTful_Output(rspSbj, null, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError); return(responseSbj); case Constants.C_DATA_PXAPIV1_PRODUCT_QUERY: //Get a list of products for a subject rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpcParams.Add("SbjCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY)); rpc.parameters = rpcParams; var rspPrd = new Product_BSO_Read(rpc).Read().Response; if (rspPrd.data == null) { return new RESTful_Output() { statusCode = HttpStatusCode.NotFound } } ; meta = new PxApiMetadata(); if (rspPrd.data.Count == 0) { rspPrd = new JSONRPC_Output(); rspPrd.data = null; } else { rspPrd.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadProductsAsObjectList(rspPrd)); } var responsePrd = Map.JSONRPC2RESTful_Output(rspPrd, null, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError); return(responsePrd); case Constants.C_DATA_PXAPIV1_COLLECTION_QUERY: //Get a list of live tables for a product rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpcParams.Add("product", parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); if (!Int32.TryParse(parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), out int sbjInt)) { rpcParams.Add("SbjCode", "0"); } else { rpcParams.Add("SbjCode", sbjInt.ToString()); } rpc.parameters = rpcParams; List <dynamic> collection = new List <dynamic>(); RESTful_Output output = new RESTful_Output(); DateTime nextReleaseDateForProduct = default; using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { nextReleaseDateForProduct = cBso.GetNextReleaseDate(parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); } //See if this request has cached data MemCachedD_Value cache = MemCacheD.Get_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams); if (cache.hasData && (nextReleaseDateForProduct == default || nextReleaseDateForProduct > DateTime.Now)) { output.response = cache.data; output.statusCode = HttpStatusCode.OK; } else { using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { collection = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); } meta = new PxApiMetadata(); if (collection != null) { if (collection.Count > 0) { output.response = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadCollectionAsObjectList(new JSONRPC_Output() { data = collection })); output.statusCode = HttpStatusCode.OK; } else { output.statusCode = HttpStatusCode.NotFound; } } else { output.statusCode = HttpStatusCode.NotFound; } } MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams, output.response, nextReleaseDateForProduct, Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI); return(output); default: if (pcount < Constants.C_DATA_PXAPIV1_METADATA_QUERY) { throw new Exception(Label.Get("error.validation")); } //Either get the metadata for the table or, if a query has been supplied, get the table data based on the query string request = string.IsNullOrWhiteSpace(restfulRequest.httpPOST) ? restfulRequest.httpGET[Constants.C_JSON_STAT_QUERY_CLASS] : restfulRequest.httpPOST; //For conventional RESTful patterns, don't return metadata unless the requested MtrCode is contained in the collection //for the subject and product parameters List <dynamic> collectionPrdSbjmeta = new List <dynamic>(); using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { collectionPrdSbjmeta = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); } if (collectionPrdSbjmeta == null) { var empty = new RESTful_Output(); empty.statusCode = HttpStatusCode.NotFound; return(empty); } if (collectionPrdSbjmeta.Where(x => x.MtrCode == parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)).ToList().Count == 0) { var empty = new RESTful_Output(); empty.statusCode = HttpStatusCode.NotFound; return(empty); } if (request == null) { rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpcParams.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)); rpcParams.Add("format", JToken.FromObject(new { type = Constants.C_SYSTEM_JSON_STAT_NAME, version = Constants.C_SYSTEM_JSON_STAT_2X_VERSION })); Matrix theMatrix; using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { theMatrix = cBso.GetMetadataMatrix(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)); } var rspMeta = new RESTful_Output(); // JSONRPC_Output(); if (theMatrix != null) { var json = theMatrix.GetApiMetadata(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpc.parameters = rpcParams; rspMeta.response = Utility.JsonSerialize_IgnoreLoopingReference(json); rspMeta.statusCode = HttpStatusCode.OK; } else { rspMeta.statusCode = HttpStatusCode.NotFound; } return(rspMeta); } else { //This is a request for data - we need to use the query //For conventional RESTful patterns, don't return metadata unless the requested MtrCode is contained in the collection //for the subject and product parameters List <dynamic> collectionPrdSbjdata = new List <dynamic>(); using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { collectionPrdSbjdata = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); } if (collectionPrdSbjmeta == null) { var empty = new RESTful_Output(); empty.statusCode = HttpStatusCode.NotFound; return(empty); } if (collectionPrdSbjdata.Where(x => x.MtrCode == parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)).ToList().Count == 0) { var empty = new RESTful_Output(); empty.statusCode = HttpStatusCode.NotFound; return(empty); } string queryString = string.IsNullOrWhiteSpace(restfulRequest.httpPOST) ? restfulRequest.httpGET[Constants.C_JSON_STAT_QUERY_CLASS] : restfulRequest.httpPOST; PxApiV1Query pxapiQuery = Utility.JsonDeserialize_IgnoreLoopingReference <PxApiV1Query>(queryString); Format_DTO_Read format = new Format_DTO_Read(pxapiQuery.Response.Format); string pivot = pxapiQuery.Response.Pivot; JsonStatQuery jsQuery = new JsonStatQuery(); jsQuery.Class = Constants.C_JSON_STAT_QUERY_CLASS; jsQuery.Id = new List <string>(); jsQuery.Dimensions = new Dictionary <string, JsonQuery.Dimension>(); foreach (var q in pxapiQuery.Query) { jsQuery.Id.Add(q.Code); jsQuery.Dimensions.Add(q.Code, new JsonQuery.Dimension() { Category = new JsonQuery.Category() { Index = q.Selection.Values.ToList <string>() } }); } jsQuery.Extension = new Dictionary <string, object>(); jsQuery.Extension.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)); jsQuery.Extension.Add("language", new Language() { Code = parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), Culture = null }); jsQuery.Extension.Add("codes", false); jsQuery.Extension.Add("format", new JsonQuery.Format() { Type = format.FrmType, Version = format.FrmVersion }); if (pivot != null) { jsQuery.Extension.Add("pivot", pivot); } jsQuery.Version = Constants.C_JSON_STAT_QUERY_VERSION; JSONRPC_API jsonRpcRequest = Map.RESTful2JSONRPC_API(restfulRequest); jsonRpcRequest.parameters = Utility.JsonSerialize_IgnoreLoopingReference(jsQuery); //Run the request as a Json Rpc call JSONRPC_Output rsp = new Cube_BSO_ReadDataset(jsonRpcRequest, true).Read().Response; if (rsp.error != null) { return(Map.JSONRPC2RESTful_Output(rsp, null, HttpStatusCode.Forbidden)); } // Convert the JsonRpc output to RESTful output var response = Map.JSONRPC2RESTful_Output(rsp, format.FrmMimetype, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError); string suffix; using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection"))) { suffix = bso.GetFileSuffixForFormat(format); }; response.fileName = parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY) + "." + DateTime.Now.ToString("yyyyMMddHHmmss") + suffix; return(response); } } } catch (Exception ex) { RESTful_Output error = new RESTful_Output { statusCode = HttpStatusCode.InternalServerError }; Log.Instance.Debug(ex.Message); return(error); } }
public static dynamic PxAPIv1(RESTful_API restfulRequest) { try { var parameters = ((List <string>)restfulRequest.parameters).Where(x => !string.IsNullOrWhiteSpace(x)); int pcount = parameters.Count() - 1; JSONRPC_API rpc = Map.RESTful2JSONRPC_API(restfulRequest); Cube_MAP map = new Cube_MAP(); JObject rpcParams = new JObject(); PxApiMetadata meta = new PxApiMetadata(); var context = HttpContext.Current.Request; switch (pcount) { case Constants.C_DATA_PXAPIV1_SUBJECT_QUERY: //Get a list of all subjects rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpc.parameters = rpcParams; var rspSbj = new Subject_BSO_Read(rpc).Read().Response; if (rspSbj.data == null) { return new RESTful_Output() { statusCode = HttpStatusCode.NotFound } } ; rspSbj.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadSubjectsAsObjectList(rspSbj)); var responseSbj = Map.JSONRPC2RESTful_Output(rspSbj, null, rspSbj.data == null ? HttpStatusCode.NotFound : HttpStatusCode.OK); return(responseSbj); case Constants.C_DATA_PXAPIV1_PRODUCT_QUERY: //Get a list of products for a subject rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpcParams.Add("SbjCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY)); rpc.parameters = rpcParams; var rspPrd = new Product_BSO_Read(rpc).Read().Response; if (rspPrd.data == null) { return new RESTful_Output() { statusCode = HttpStatusCode.NotFound } } ; meta = new PxApiMetadata(); if (rspPrd.data.Count == 0) { rspPrd = new JSONRPC_Output(); rspPrd.data = null; } else { rspPrd.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadProductsAsObjectList(rspPrd)); } var responsePrd = Map.JSONRPC2RESTful_Output(rspPrd, null, rspPrd.data == null ? HttpStatusCode.NotFound : HttpStatusCode.OK); return(responsePrd); case Constants.C_DATA_PXAPIV1_COLLECTION_QUERY: //Get a list of live tables for a product rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpcParams.Add("product", parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); rpc.parameters = rpcParams; List <dynamic> collection = new List <dynamic>(); RESTful_Output output = new RESTful_Output(); DateTime nextReleaseDateForProduct = default; using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { nextReleaseDateForProduct = cBso.GetNextReleaseDate(parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); } //See if this request has cached data MemCachedD_Value cache = MemCacheD.Get_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams); if (cache.hasData && (nextReleaseDateForProduct == default || nextReleaseDateForProduct > DateTime.Now)) { output.response = cache.data; output.statusCode = HttpStatusCode.OK; } else { using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { collection = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); } meta = new PxApiMetadata(); if (collection.Count > 0) { output.response = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadCollectionAsObjectList(new JSONRPC_Output() { data = collection })); output.statusCode = HttpStatusCode.OK; } else { output.statusCode = HttpStatusCode.NotFound; } } MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams, output.response, nextReleaseDateForProduct, Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI + parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); //MemCacheD.CasRepositoryFlush(Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI + parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY)); return(output); case Constants.C_DATA_PXAPIV1_METADATA_QUERY: //Either get the metadata for the table or, if a query has been supplied, get the table data based on the query string request = string.IsNullOrWhiteSpace(Utility.HttpPOST()) ? Utility.HttpGET()["query"] : Utility.HttpPOST(); if (request == null) { rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpcParams.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)); rpcParams.Add("format", JToken.FromObject(new { type = "JSON-stat", version = "2.0" })); Matrix theMatrix; using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection"))) { theMatrix = cBso.GetMetadataMatrix(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)); } var rspMeta = new RESTful_Output(); // JSONRPC_Output(); if (theMatrix != null) { var json = theMatrix.GetApiMetadata(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)); rpc.parameters = rpcParams; rspMeta.response = Utility.JsonSerialize_IgnoreLoopingReference(json); rspMeta.statusCode = HttpStatusCode.OK; } else { rspMeta.statusCode = HttpStatusCode.NotFound; } return(rspMeta); } else { //This is a request for data - we need to use the query string queryString = string.IsNullOrWhiteSpace(Utility.HttpPOST()) ? Utility.HttpGET()["query"] : Utility.HttpPOST(); PxApiV1Query pxapiQuery = Utility.JsonDeserialize_IgnoreLoopingReference <PxApiV1Query>(queryString); Format_DTO_Read format = new Format_DTO_Read(pxapiQuery.Response.Format); Analytic_BSO_Create.Create(HttpContext.Current.Request, "PxStat.Data.PxAPIv1", HttpContext.Current.Request.UserAgent, restfulRequest.ipAddress, parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY), false, format); JsonStatQuery jsQuery = new JsonStatQuery(); jsQuery.Class = "query"; jsQuery.Id = new List <string>(); jsQuery.Dimensions = new Dictionary <string, JsonQuery.Dimension>(); foreach (var q in pxapiQuery.Query) { jsQuery.Id.Add(q.Code); jsQuery.Dimensions.Add(q.Code, new JsonQuery.Dimension() { Category = new JsonQuery.Category() { Index = q.Selection.Values.ToList <string>() } }); } jsQuery.Extension = new Dictionary <string, object>(); jsQuery.Extension.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)); jsQuery.Extension.Add("language", new Language() { Code = parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY) }); jsQuery.Extension.Add("format", new JsonQuery.Format() { Type = format.FrmType, Version = format.FrmVersion }); jsQuery.Version = Utility.GetCustomConfig("APP_JSON_STAT_QUERY_VERSION"); JSONRPC_API jsonRpcRequest = Map.RESTful2JSONRPC_API(restfulRequest); jsonRpcRequest.parameters = Utility.JsonSerialize_IgnoreLoopingReference(jsQuery); //Run the request as a Json Rpc call JSONRPC_Output rsp = new Cube_BSO_ReadDataset(jsonRpcRequest).Read().Response; // Convert the JsonRpc output to RESTful output var response = Map.JSONRPC2RESTful_Output(rsp, format.FrmMimetype, rsp.data == null ? HttpStatusCode.NotFound : HttpStatusCode.OK); string suffix; using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection"))) { suffix = bso.GetFileSuffixForFormat(format); }; response.fileName = parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY) + suffix; return(response); } default: throw new Exception(Label.Get("error.validation")); } } catch (Exception ex) { RESTful_Output error = new RESTful_Output { statusCode = HttpStatusCode.InternalServerError }; Log.Instance.Error(ex.Message); return(error); } }