public IContentConstraintMutableObject buildContentConstraint(string id, string agency, string version) { IContentConstraintMutableObject ccs = new ContentConstraintMutableCore(); ccs.Id = id; ccs.AgencyId = agency; ccs.Version = version; return ccs; }
/// <summary> /// Get the maximum number of observations that can be retrieved given the specified criteria /// </summary> /// <param name="dataflow"> /// The dataflow /// </param> /// <param name="criteria"> /// The criteria includes a set of Member and MemberValue(s) for each dimension. Each member should have member values else they shouldn't be included. It can be null /// </param> /// <returns> /// The maximum number of observations or -1 if it can't be parsed or it is not available /// </returns> /// <exception cref="NsiClientException"> /// NSI WS communication error or parsing error /// </exception> public int GetDataflowDataCount(IDataflowObject dataflow, IContentConstraintMutableObject criteria) { int count; List <IContentConstraintMutableObject> criterias = new List <IContentConstraintMutableObject>(); if (criteria == null) { criteria = new ContentConstraintMutableCore(); } criteria.Id = CustomCodelistConstants.CountCodeList; criteria.AddName("en", "name"); criteria.AgencyId = "agency"; criterias.Add(criteria); var codelistRef = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CodeList)) { MaintainableId = CustomCodelistConstants.CountCodeList, AgencyId = CustomCodelistConstants.Agency, Version = CustomCodelistConstants.Version }; string info = string.Format(CultureInfo.InvariantCulture, Resources.InfoCountFormat2, Utils.MakeKey(dataflow), Utils.MakeKey(codelistRef)); try { ICodelistObject countCodelist = this.GetCodelist(dataflow, codelistRef, criterias, info, true); if (!CustomCodelistConstants.IsCountCodeList(countCodelist) || !int.TryParse(countCodelist.Items[0].Id, out count)) { Logger.WarnFormat(CultureInfo.InvariantCulture, Resources.ExceptionParsingCountCodelistFormat0, info); throw new NsiClientException("Error parsing the count codelist for " + info); } } catch (NsiClientException ex) { Logger.Warn(ex.Message, ex); count = -1; } return(count); }
private ICodelistObject GetCodeListCostraint( IDataflowObject df, IDataStructureObject kf, IComponent component) { // ICodelistObject codes = GetCodeList(query, component); ICodelistObject codes = null; bool Contrained = component.StructureType.EnumType != Org.Sdmxsource.Sdmx.Api.Constants.SdmxStructureEnumType.DataAttribute; #region Criteria List<IContentConstraintMutableObject> criterias = new List<IContentConstraintMutableObject>(); if (Contrained) { string currentComponent = component.ConceptRef.ChildReference.Id; IContentConstraintMutableObject criteria = new ContentConstraintMutableCore(); criteria.Id = currentComponent ?? "SPECIAL"; criteria.AddName("en", "english"); criteria.AgencyId = "agency"; ICubeRegionMutableObject region = new CubeRegionMutableCore(); if (currentComponent != null) { IKeyValuesMutable keyValue = new KeyValuesMutableImpl(); keyValue.Id = currentComponent; keyValue.AddValue(SpecialValues.DummyMemberValue); region.AddKeyValue(keyValue); if (CodemapObj.PreviusCostraint != null) { foreach (string costreintKey in CodemapObj.PreviusCostraint.Keys) { if (costreintKey == currentComponent) continue; if (costreintKey == kf.TimeDimension.Id) { // Qui considerare il caso in qui in CodemapObj.PreviusCostraint[costreintKey][0] ci sia solo un valore, ke equivale alla data da. if (CodemapObj.PreviusCostraint[costreintKey].Count > 1) //if (!string.IsNullOrEmpty(CodemapObj.PreviusCostraint[costreintKey][1])) { DateTime MinDate = GetDateTimeFromSDMXTimePeriod(CodemapObj.PreviusCostraint[costreintKey][0].ToString(), 'M'); //DateTime.ParseExact(CodemapObj.PreviusCostraint[costreintKey][0].ToString(), "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None); DateTime MaxDate = GetDateTimeFromSDMXTimePeriod(CodemapObj.PreviusCostraint[costreintKey][1].ToString(), 'M'); //DateTime.ParseExact(CodemapObj.PreviusCostraint[costreintKey][1].ToString(), "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None); if (MinDate.CompareTo(MaxDate) > 0) { criteria.StartDate = MaxDate; criteria.EndDate = MinDate; } else { criteria.StartDate = MinDate; criteria.EndDate = MaxDate; } } } else { foreach (var code in CodemapObj.PreviusCostraint[costreintKey]) { IKeyValuesMutable _keyValue = new KeyValuesMutableImpl(); _keyValue.Id = costreintKey; _keyValue.AddValue(code); region.AddKeyValue(_keyValue); } } } } } criteria.IncludedCubeRegion = region; criterias.Add(criteria); } #endregion codes = GetSDMXObject.GetCodelist(df, kf, component, criterias, Contrained); return codes; }
/*FINE NUOVO SESSION QUERY*/ private ICodelistObject GetCodeList(IDataflowObject df, IDataStructureObject kf, IComponent component) { ICodelistObject codes = null; if (this.SessionObj.CodelistConstrained != null && this.SessionObj.CodelistConstrained.ContainsKey(Utils.MakeKey(df))) { if (this.SessionObj.CodelistConstrained[Utils.MakeKey(df)].ContainsKey(component.Id)) { codes = this.SessionObj.CodelistConstrained[Utils.MakeKey(df)][component.Id]; } } if (codes == null) { bool Contrained = component.StructureType.EnumType != Org.Sdmxsource.Sdmx.Api.Constants.SdmxStructureEnumType.DataAttribute; #region Criteria List<IContentConstraintMutableObject> criterias = new List<IContentConstraintMutableObject>(); if (Contrained) { IContentConstraintMutableObject criteria = new ContentConstraintMutableCore(); string currentComponent = component.ConceptRef.ChildReference.Id; criteria.Id = currentComponent ?? "SPECIAL"; criteria.AddName("en", "english"); criteria.AgencyId = "agency"; ICubeRegionMutableObject region = new CubeRegionMutableCore(); if (currentComponent != null) { IKeyValuesMutable keyValue = new KeyValuesMutableImpl(); keyValue.Id = currentComponent; keyValue.AddValue(SpecialValues.DummyMemberValue); region.AddKeyValue(keyValue); } criteria.IncludedCubeRegion = region; criterias.Add(criteria); } #endregion codes = GetSDMXObject.GetCodelist(df, kf, component, criterias, Contrained); } return codes; }
public int GetCountObservation(SessionQuery query) { ISdmxObjects structure = query.Structure; IDataflowObject df = structure.Dataflows.First(); IDataStructureObject kf = structure.DataStructures.First(); if (kf == null) throw new InvalidOperationException("DataStructure is not set"); if (df == null) throw new InvalidOperationException("Dataflow is not set"); var currentComponent = "CL_COUNT"; IContentConstraintMutableObject criteria = new ContentConstraintMutableCore(); criteria.Id = currentComponent; criteria.AddName("en", "english"); criteria.AgencyId = "agency"; ICubeRegionMutableObject region = new CubeRegionMutableCore(); if (currentComponent != null) { IKeyValuesMutable keyValue = new KeyValuesMutableImpl(); keyValue.Id = currentComponent; keyValue.AddValue(SpecialValues.DummyMemberValue); region.AddKeyValue(keyValue); if (query.Criteria != null) { foreach (string costreintKey in query.Criteria.Keys) { if (costreintKey == currentComponent) continue; if (costreintKey == kf.TimeDimension.Id) { // Qui considerare il caso in qui in CodemapObj.PreviusCostraint[costreintKey][0] ci sia solo un valore, ke equivale alla data da. if (query.Criteria[costreintKey].Count > 1) { DateTime MinDate = GetDateTimeFromSDMXTimePeriod(query.Criteria[costreintKey][0].ToString(), 'M'); //DateTime.ParseExact(CodemapObj.PreviusCostraint[costreintKey][0].ToString(), "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None); DateTime MaxDate = GetDateTimeFromSDMXTimePeriod(query.Criteria[costreintKey][1].ToString(), 'M'); //DateTime.ParseExact(CodemapObj.PreviusCostraint[costreintKey][1].ToString(), "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None); //baco fabio if (MinDate.CompareTo(MaxDate) > 0) { criteria.StartDate = MaxDate; criteria.EndDate = MinDate; } else { criteria.StartDate = MinDate; criteria.EndDate = MaxDate; } } } else { foreach (var code in query.Criteria[costreintKey]) { IKeyValuesMutable _keyValue = new KeyValuesMutableImpl(); _keyValue.Id = costreintKey; _keyValue.AddValue(code); region.AddKeyValue(_keyValue); } } } } } criteria.IncludedCubeRegion = region; GetSDMXObject = WebServiceSelector.GetSdmxImplementation(this.CodemapObj.Configuration); int count = GetSDMXObject.GetDataflowDataCount(df, criteria); return count; }
/// <summary> /// Build constraint. /// </summary> /// <param name="dataReaderEngine"> /// The data reader engine. /// </param> /// <param name="attachment"> /// The attachment. /// </param> /// <param name="dataSourceAttachment"> /// The data source attachment. /// </param> /// <param name="indexAttributes"> /// The index attributes. /// </param> /// <param name="indexDataset"> /// The index dataset. /// </param> /// <param name="indexReportingPeriod"> /// The index reporting period. /// </param> /// <param name="indexTimeSeries"> /// The index time series. /// </param> /// <param name="definingDataPresent"> /// The defining data present. /// </param> /// <param name="refParameters"> /// The ref parameters. /// </param> /// <returns> /// The <see cref="IContentConstraintObject"/>. /// </returns> public virtual IContentConstraintObject BuildConstraint( IDataReaderEngine dataReaderEngine, IStructureReference attachment, IDataSource dataSourceAttachment, bool indexAttributes, bool indexDataset, bool indexReportingPeriod, bool indexTimeSeries, bool definingDataPresent, IMaintainableRefObject refParameters) { dataReaderEngine.Reset(); // TODO Should it check if there is more then one dataset in the datasource if (!dataReaderEngine.MoveNextDataset()) { throw new SdmxSemmanticException( "Can not index time series for registered datasource, the data retrieved from the datasource does not contain a dataset"); } if (!indexAttributes && !indexDataset && !indexReportingPeriod && !indexTimeSeries) { return null; } IDatasetHeader header = dataReaderEngine.CurrentDatasetHeader; if (indexTimeSeries && !header.Timeseries) { throw new SdmxSemmanticException( "Can not index time series for registered datasource, the data retrieved from the datasource is not time series"); } // Create mutable Maintainable IContentConstraintMutableObject mutableBean = new ContentConstraintMutableCore(); mutableBean.AgencyId = refParameters.AgencyId; mutableBean.Id = refParameters.MaintainableId; mutableBean.Version = refParameters.Version; mutableBean.AddName("en", "Generated Constraint"); mutableBean.AddDescription("en", "Constraint built from dataset"); mutableBean.IsDefiningActualDataPresent = true; mutableBean.ConstraintAttachment = BuildAttachement(attachment); IConstraintDataKeySetMutableObject dataKeySet = null; IDictionary<string, ISet<string>> cubeRegionMap = new Dictionary<string, ISet<string>>(); IDictionary<string, ISet<string>> attributeMap = new Dictionary<string, ISet<string>>(); DateTime? reportFrom = null; DateTime? reportTo = null; ISet<string> processedDates = new HashSet<string>(); while (dataReaderEngine.MoveNextKeyable()) { IKeyable key = dataReaderEngine.CurrentKey; if (key.Series) { // TODO Check if Cross Sectional and put out exception if it is // 1. If indexing the time series store the time Series Key on the Constraint if (indexTimeSeries) { if (dataKeySet == null) { dataKeySet = new ConstraintDataKeySetMutableCore(); mutableBean.IncludedSeriesKeys = dataKeySet; } IConstrainedDataKeyMutableObject dataKey = new ConstrainedDataKeyMutableCore(); dataKey.KeyValues.AddAll(key.Key); dataKeySet.AddConstrainedDataKey(dataKey); } // 2. If indexing the dataset, store the individual code values if (indexDataset) { StoreKeyValuesOnMap(key.Key, cubeRegionMap); } // 3. If indexing attributes, store the individual code values for each attribute if (indexAttributes) { StoreKeyValuesOnMap(key.Attributes, attributeMap); } } if (indexAttributes || indexReportingPeriod) { while (dataReaderEngine.MoveNextObservation()) { IObservation obs = dataReaderEngine.CurrentObservation; // If indexing the dates, determine the data start and end dates from the obs dates // To save time, do not process the same date twice if (indexReportingPeriod) { if (!processedDates.Contains(obs.ObsTime)) { DateTime obsDate = obs.ObsAsTimeDate.GetValueOrDefault(); if (reportFrom == null || (reportFrom.Value.Ticks / 10000) > (obsDate.Ticks / 10000)) { reportFrom = obsDate; } if (reportTo == null || (reportTo.Value.Ticks / 10000) < (obsDate.Ticks / 10000)) { reportTo = obsDate; } processedDates.Add(obs.ObsTime); } } if (indexAttributes) { StoreKeyValuesOnMap(obs.Attributes, attributeMap); } } } } if (indexAttributes || indexDataset) { ICubeRegionMutableObject cubeRegionMutableBean = new CubeRegionMutableCore(); mutableBean.IncludedCubeRegion = cubeRegionMutableBean; if (indexAttributes) { CreateKeyValues(attributeMap, cubeRegionMutableBean.AttributeValues); } if (indexDataset) { CreateKeyValues(cubeRegionMap, cubeRegionMutableBean.KeyValues); } } if (indexReportingPeriod && reportFrom != null && reportTo.HasValue) { IReferencePeriodMutableObject refPeriodMutable = new ReferencePeriodMutableCore(); refPeriodMutable.EndTime = reportTo.Value; refPeriodMutable.StartTime = reportFrom.Value; mutableBean.ReferencePeriod = refPeriodMutable; } return mutableBean.ImmutableInstance; }
/// <summary> /// The build constraint object. /// </summary> /// <param name="dataflowRefType"> /// The dataflow ref type. /// </param> /// <returns> /// The <see cref="IContentConstraintObject"/>. /// </returns> private static IContentConstraintObject BuildConstraintObject(DataflowRefType dataflowRefType) { IContentConstraintObject immutableInstance = null; if (dataflowRefType.Constraint != null) { string id = dataflowRefType.Constraint.ConstraintID; IContentConstraintMutableObject mutable = new ContentConstraintMutableCore(); AddDummyValuesForValidity(dataflowRefType, mutable); mutable.Id = id; foreach (CubeRegionType cubeRegionType in dataflowRefType.Constraint.CubeRegion) { ICubeRegionMutableObject cubeRegion = BuildCubeRegion(cubeRegionType); if (cubeRegionType.isIncluded && mutable.IncludedCubeRegion == null) { mutable.IncludedCubeRegion = cubeRegion; } else if (mutable.ExcludedCubeRegion == null) { mutable.ExcludedCubeRegion = cubeRegion; } } if (dataflowRefType.Constraint.ReferencePeriod != null) { mutable.ReferencePeriod = new ReferencePeriodMutableCore { StartTime = dataflowRefType.Constraint.ReferencePeriod.startTime, EndTime = dataflowRefType.Constraint.ReferencePeriod.endTime }; } immutableInstance = mutable.ImmutableInstance; } return immutableInstance; }
/// <summary> /// Get the maximum number of observations that can be retrieved given the specified criteria /// </summary> /// <param name="dataflow"> /// The dataflow /// </param> /// <param name="criteria"> /// The criteria includes a set of Member and MemberValue(s) for each dimension. Each member should have member values else they shouldn't be included. It can be null /// </param> /// <returns> /// The maximum number of observations or -1 if it can't be parsed or it is not available /// </returns> /// <exception cref="NsiClientException"> /// NSI WS communication error or parsing error /// </exception> public int GetDataflowDataCount(IDataflowObject dataflow, IContentConstraintMutableObject criteria) { int count; if (criteria == null) { criteria = new ContentConstraintMutableCore(); criteria.Id = CustomCodelistConstants.CountCodeList; criteria.AddName("en", "name"); criteria.AgencyId = "agency"; } var codelistRef = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CodeList)) { MaintainableId = CustomCodelistConstants.CountCodeList, AgencyId = CustomCodelistConstants.Agency, Version = CustomCodelistConstants.Version }; string info = string.Format( CultureInfo.InvariantCulture, Resources.InfoCountFormat2, Utils.MakeKey(dataflow), Utils.MakeKey(codelistRef)); try { ICodelistObject countCodelist = this.GetCodelist(dataflow, codelistRef, criteria, info); if (!CustomCodelistConstants.IsCountCodeList(countCodelist) || !int.TryParse(countCodelist.Items[0].Id, out count)) { Logger.WarnFormat(CultureInfo.InvariantCulture, Resources.ExceptionParsingCountCodelistFormat0, info); // throw new NsiClientException("Error parsing the count codelist for " + info); count = -1; } } catch (NsiClientException ex) { Logger.Warn(ex.Message, ex); count = -1; } return count; }
/// <summary> /// Create a new Constrain from the <see cref="SessionQuery._queryComponentIndex"/> and optionally for the specified component /// </summary> /// <param name="currentComponent"> /// The current component. Normally this component should be belong to the AvailableComponents set. Set to null if there isn't any current component /// </param> /// <returns> /// A constrain bean /// </returns> public IContentConstraintMutableObject CreateConstraintBean(string currentComponent) { if (!this._sessionQuery.IsDataflowSet) { throw new InvalidOperationException("Dataflow is not set"); } IContentConstraintMutableObject criteria = new ContentConstraintMutableCore(); criteria.Id = currentComponent ?? "SPECIAL"; criteria.AddName("en", "english"); criteria.AgencyId = "agency"; ICubeRegionMutableObject region = new CubeRegionMutableCore(); if (currentComponent != null) { IKeyValuesMutable keyValue = new KeyValuesMutableImpl(); keyValue.Id = currentComponent; keyValue.AddValue(SpecialValues.DummyMemberValue); region.AddKeyValue(keyValue); } foreach (var queryComponent in this._sessionQuery.GetQueryComponents()) { if (queryComponent != null && !queryComponent.GetKeyFamilyComponent().ConceptRef.ChildReference.Id.Equals(currentComponent)) { var setComponents = new KeyValuesMutableImpl(); region.AddKeyValue(setComponents); setComponents.Id = queryComponent.GetKeyFamilyComponent().ConceptRef.ChildReference.Id; var dimension = queryComponent.GetKeyFamilyComponent() as IDimension; if (dimension != null && dimension.TimeDimension) { // there is another way to handle this with ReferencePeriod but // ReferencePeriod accepts {xs:Date,xs:DateTime}. Using this way, MemberValue, we can use anything we want. // must be first setComponents.KeyValues.Add(queryComponent.StartDate); if (!string.IsNullOrEmpty(queryComponent.EndDate)) { setComponents.KeyValues.Add(queryComponent.EndDate); } } else if (queryComponent.GetKeyFamilyComponent().Representation.Representation.MaintainableReference.MaintainableId != null) { // java has different API for MemberValue foreach (ICode code in queryComponent.RetrieveCodes()) { setComponents.KeyValues.Add(code.Id); } if (setComponents.KeyValues.Count == 0) { setComponents.AddValue(SpecialValues.DummyMemberValue); } } else { setComponents.KeyValues.Add(queryComponent.TextValue); } } } if (region.KeyValues.Count > 0) { criteria.IncludedCubeRegion = region; } return criteria; }
/// <summary> /// The build constrainable structure reference. /// </summary> /// <param name="id"> /// The id. /// </param> /// <param name="agencyId"> /// The agency id. /// </param> /// <param name="version"> /// The version. /// </param> /// <param name="name"> /// The name. /// </param> /// <param name="previousMembers"> /// The previous members. /// </param> /// <param name="currentDataflowReference"> /// The current dataflow reference. /// </param> /// <returns> /// The <see cref="IConstrainableStructureReference"/>. /// </returns> private static IConstrainableStructureReference BuildConstrainableStructureReference( string id, string agencyId, string version, string name, IEnumerable<IKeyValuesMutable> previousMembers, IStructureReference currentDataflowReference) { IContentConstraintMutableObject mutableConstraint = new ContentConstraintMutableCore { Id = id, AgencyId = agencyId, Version = version }; mutableConstraint.AddName("en", name); var cubeRegion = new CubeRegionMutableCore(); mutableConstraint.IncludedCubeRegion = cubeRegion; IKeyValuesMutable requestedDimension = new KeyValuesMutableImpl(); requestedDimension.Id = id; requestedDimension.AddValue(SpecialValues.DummyMemberValue); cubeRegion.AddKeyValue(requestedDimension); if (previousMembers != null) { cubeRegion.KeyValues.AddAll(previousMembers); } IContentConstraintObject constraint = new ContentConstraintObjectCore(mutableConstraint); IConstrainableStructureReference specialRequest = new ConstrainableStructureReference(currentDataflowReference, constraint); return specialRequest; }
/// <summary> /// Create a new Constrain from the <see cref="SessionQuery._queryComponentIndex"/> and optionally for the specified component /// </summary> /// <param name="currentComponent"> /// The current component. Normally this component should be belong to the AvailableComponents set. Set to null if there isn't any current component /// </param> /// <returns> /// A constrain bean /// </returns> public IContentConstraintMutableObject CreateConstraintBean(string currentComponent) { if (!this._sessionQuery.IsDataflowSet) { throw new InvalidOperationException("Dataflow is not set"); } IContentConstraintMutableObject criteria = new ContentConstraintMutableCore(); criteria.Id = currentComponent ?? "SPECIAL"; criteria.AddName("en", "english"); criteria.AgencyId = "agency"; ICubeRegionMutableObject region = new CubeRegionMutableCore(); if (currentComponent != null) { IKeyValuesMutable keyValue = new KeyValuesMutableImpl(); keyValue.Id = currentComponent; keyValue.AddValue(SpecialValues.DummyMemberValue); region.AddKeyValue(keyValue); } foreach (var queryComponent in this._sessionQuery.GetQueryComponents()) { if (queryComponent != null && !queryComponent.GetKeyFamilyComponent().ConceptRef.ChildReference.Id.Equals(currentComponent)) { var setComponents = new KeyValuesMutableImpl(); region.AddKeyValue(setComponents); setComponents.Id = queryComponent.GetKeyFamilyComponent().ConceptRef.ChildReference.Id; var dimension = queryComponent.GetKeyFamilyComponent() as IDimension; if (dimension != null && dimension.TimeDimension) { // there is another way to handle this with ReferencePeriod but // ReferencePeriod accepts {xs:Date,xs:DateTime}. Using this way, MemberValue, we can use anything we want. // must be first setComponents.KeyValues.Add(queryComponent.StartDate); if (!string.IsNullOrEmpty(queryComponent.EndDate)) { setComponents.KeyValues.Add(queryComponent.EndDate); } } else if (queryComponent.GetKeyFamilyComponent().Representation.Representation.MaintainableReference.MaintainableId != null) { // java has different API for MemberValue foreach (ICode code in queryComponent.RetrieveCodes()) { setComponents.KeyValues.Add(code.Id); } if (setComponents.KeyValues.Count == 0) { setComponents.AddValue(SpecialValues.DummyMemberValue); } } else { setComponents.KeyValues.Add(queryComponent.TextValue); } } } if (region.KeyValues.Count > 0) { criteria.IncludedCubeRegion = region; } return(criteria); }