Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        /*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;
        }
        /// <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 cube region.
        /// </summary>
        /// <param name="cubeRegionType">
        /// The cube region type.
        /// </param>
        /// <returns>
        /// The <see cref="ICubeRegionMutableObject"/>.
        /// </returns>
        private static ICubeRegionMutableObject BuildCubeRegion(CubeRegionType cubeRegionType)
        {
            ICubeRegionMutableObject cubeRegion = new CubeRegionMutableCore();
            foreach (MemberType memberType in cubeRegionType.Member)
            {
                IKeyValuesMutable keyValues = new KeyValuesMutableImpl();
                keyValues.Id = memberType.ComponentRef;
                cubeRegion.AddKeyValue(keyValues);
                if (memberType.MemberValue == null || memberType.MemberValue.Count == 0)
                {
                    keyValues.AddValue(SpecialValues.DummyMemberValue);
                }
                else
                {
                    foreach (MemberValueType memberValueType in memberType.MemberValue)
                    {
                        keyValues.AddValue(memberValueType.Value);
                    }
                }
            }

            return cubeRegion;
        }
        /// <summary>
        /// Handle Constraint Child elements
        /// </summary>
        /// <param name="parent">
        /// The parent IConstraintMutableObject object
        /// </param>
        /// <param name="localName">
        /// The name of the current xml element
        /// </param>
        /// <returns>
        /// The <see cref="StructureReaderBaseV20.ElementActions"/>.
        /// </returns>
        protected ElementActions HandleChildElements(IContentConstraintMutableObject parent, object localName)
        {
            ElementActions actions = null;
            if (NameTableCache.IsElement(localName, ElementNameTable.CubeRegion))
            {
                var cb = new CubeRegionMutableCore();
                bool isIncluded = Helper.TrySetFromAttribute(this.Attributes, AttributeNameTable.isIncluded, true);
                if (isIncluded)
                {
                    parent.IncludedCubeRegion = cb;
                }
                else
                {
                    parent.ExcludedCubeRegion = cb;
                }

                actions = this.BuildElementActions(cb, this.HandleChildElements, DoNothing);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.ReleaseCalendar))
            {
                parent.ReleaseCalendar = new ReleaseCalendarMutableCore();
                actions = this.BuildElementActions(parent.ReleaseCalendar, DoNothingComplex, this.HandleTextChildElement);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.ReferencePeriod))
            {
                parent.ReferencePeriod = new ReferencePeriodMutableCore();
                parent.ReferencePeriod.StartTime = Helper.TrySetFromAttribute(this.Attributes, AttributeNameTable.startTime, parent.ReferencePeriod.StartTime);
                parent.ReferencePeriod.EndTime = Helper.TrySetFromAttribute(this.Attributes, AttributeNameTable.endTime, parent.ReferencePeriod.EndTime);
                actions = this.BuildElementActions(parent.ReferencePeriod, DoNothingComplex, DoNothing);
            }

            return actions;
        }
Ejemplo n.º 7
0
        /// <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;
        }