Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve the common <see cref="IMaintainableMutableObject"/> information from Mapping Store ARTEFACT table. This method does not retrieve the Names and Description
        /// </summary>
        /// <param name="sqlQuery">
        /// The SQL Query for artefacts
        /// </param>
        /// <param name="detail">
        /// The structure query detail
        /// </param>
        /// <param name="commandBuilder">
        /// The command Builder.
        /// </param>
        /// <param name="retrieveDetails">
        /// The method to retrieve details of the artefacts
        /// </param>
        /// <param name="extraFields">
        /// The extra Fields.
        /// </param>
        /// <returns>
        /// A dictionary with key the primary key in Mapping Store
        /// </returns>
        protected ISet <T> RetrieveArtefacts(ArtefactSqlQuery sqlQuery, ComplexStructureQueryDetailEnumType detail, Func <ArtefactSqlQuery, DbCommand> commandBuilder = null, Func <T, long, T> retrieveDetails = null, Action <T, IDataReader> extraFields = null)
        {
            commandBuilder  = commandBuilder ?? this.CommandBuilder.Build;
            retrieveDetails = retrieveDetails ?? this.RetrieveDetails;
            var artefactPkPairs = new List <KeyValuePair <T, long> >();

            using (DbCommand command = commandBuilder(sqlQuery))
            {
                _log.InfoFormat(CultureInfo.InvariantCulture, "Executing Query: '{0}' with '{1}'", command.CommandText, sqlQuery.MaintainableRef);
                using (IDataReader dataReader = this._mappingStoreDb.ExecuteReader(command))
                {
                    int sysIdIdx     = dataReader.GetOrdinal("SYSID");
                    int idIdx        = dataReader.GetOrdinal("ID");
                    int version1Idx  = dataReader.GetOrdinal("VERSION");
                    int agencyIdx    = dataReader.GetOrdinal("AGENCY");
                    int validFromIdx = dataReader.GetOrdinal("VALID_FROM");
                    int validToIdx   = dataReader.GetOrdinal("VALID_TO");
                    int isFinalIdx   = dataReader.GetOrdinal("IS_FINAL");
                    int txtIdx       = dataReader.GetOrdinal("TEXT");
                    int langIdx      = dataReader.GetOrdinal("LANGUAGE");
                    int typeIdx      = dataReader.GetOrdinal("TYPE");
                    var artefactMap  = new Dictionary <long, T>();
                    while (dataReader.Read())
                    {
                        long sysId = DataReaderHelper.GetInt64(dataReader, sysIdIdx);
                        T    artefact;
                        if (!artefactMap.TryGetValue(sysId, out artefact))
                        {
                            artefact = this.CreateArtefact();
                            artefact.FinalStructure = DataReaderHelper.GetTristate(dataReader, isFinalIdx);
                            artefact.EndDate        = DataReaderHelper.GetStringDate(dataReader, validToIdx);
                            artefact.StartDate      = DataReaderHelper.GetStringDate(dataReader, validFromIdx);
                            artefact.Version        = DataReaderHelper.GetString(dataReader, version1Idx);
                            artefact.AgencyId       = DataReaderHelper.GetString(dataReader, agencyIdx);
                            artefact.Id             = DataReaderHelper.GetString(dataReader, idIdx);
                            this.HandleArtefactExtraFields(artefact, dataReader);

                            if (extraFields != null)
                            {
                                extraFields(artefact, dataReader);
                            }

                            artefactPkPairs.Add(new KeyValuePair <T, long>(artefact, sysId));
                            artefactMap.Add(sysId, artefact);
                        }

                        if (!artefact.IsDefault())
                        {
                            ReadLocalisedString(artefact, typeIdx, txtIdx, langIdx, dataReader, detail);
                        }
                    }
                }
            }

            if (artefactPkPairs.Count < 1)
            {
                _log.InfoFormat(CultureInfo.InvariantCulture, "No artefacts retrieved for : '{0}'", sqlQuery.MaintainableRef);
                return(new HashSet <T>());
            }

            return(HandleDetailLevel(detail, retrieveDetails, artefactPkPairs));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieve and populate the <see cref="IHierarchyMutableObject.HierarchicalCodeObjects"/> for the HIERARCHY with specified Primary KEY
        /// </summary>
        /// <param name="artefact">
        /// The <see cref="IHierarchyMutableObject"/> instance to populate
        /// </param>
        /// <param name="hid">
        /// The Hierarchy Mapping store Primary KEY
        /// </param>
        private void FillCodeRef(IHierarchyMutableObject artefact, long hid)
        {
            IDictionary <long, ICodeRefMutableObject> allItems = new ListDictionary <long, ICodeRefMutableObject>();
            var childItems = new Dictionary <long, long>();

            using (DbCommand command = this._itemCommandBuilder.Build(new ItemSqlQuery(this._codeRefQueryInfo, hid)))
            {
                using (IDataReader dataReader = this.MappingStoreDb.ExecuteReader(command))
                {
                    int sysIdIdx       = dataReader.GetOrdinal("SYSID");
                    int idIdx          = dataReader.GetOrdinal("CODE_ID");
                    int parentIdx      = dataReader.GetOrdinal("PARENT");
                    int nodeAliasIdIdx = dataReader.GetOrdinal("NodeAliasID");

                    //// TODO check how to set the version (it seems supported in v2.1)
                    int levelRefIdx  = dataReader.GetOrdinal("LEVEL_REF");
                    int clidIdx      = dataReader.GetOrdinal("CLID");
                    int clversionIdx = dataReader.GetOrdinal("CLVERSION");
                    int clagencyIdx  = dataReader.GetOrdinal("CLAGENCY");
                    int validFromIdx = dataReader.GetOrdinal("VALID_FROM");
                    int validToIdx   = dataReader.GetOrdinal("VALID_TO");
                    while (dataReader.Read())
                    {
                        var codeRef = new CodeRefMutableCore
                        {
                            CodelistAliasRef =
                                BuildCodelistRefAlias(
                                    DataReaderHelper.GetString(dataReader, clidIdx),
                                    DataReaderHelper.GetString(dataReader, clagencyIdx),
                                    DataReaderHelper.GetString(dataReader, clversionIdx)),
                            Id     = DataReaderHelper.GetString(dataReader, nodeAliasIdIdx),
                            CodeId = DataReaderHelper.GetString(dataReader, idIdx)
                        };
                        if (artefact.ChildLevel != null)
                        {
                            var levelRef = DataReaderHelper.GetString(dataReader, levelRefIdx);
                            ILevelMutableObject currentLevel = artefact.ChildLevel;
                            var normalizedLevel = GetNormalizedLevel(currentLevel, levelRef);
                            codeRef.LevelReference = normalizedLevel;
                        }
                        if (string.IsNullOrWhiteSpace(codeRef.Id))
                        {
                            codeRef.Id = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", codeRef.CodelistAliasRef.Replace('@', '_'), '_', codeRef.CodeId);
                        }

                        DateTime?validFrom = DataReaderHelper.GetStringDate(dataReader, validFromIdx);
                        if (validFrom != null)
                        {
                            codeRef.ValidFrom = validFrom.Value;
                        }

                        DateTime?validTo = DataReaderHelper.GetStringDate(dataReader, validToIdx);
                        if (validTo != null)
                        {
                            codeRef.ValidTo = validTo.Value;
                        }

                        long sysId        = DataReaderHelper.GetInt64(dataReader, sysIdIdx);
                        long parentItemId = DataReaderHelper.GetInt64(dataReader, parentIdx);
                        if (parentItemId > long.MinValue)
                        {
                            childItems.Add(sysId, parentItemId);
                        }

                        allItems.Add(sysId, codeRef);
                    }
                }
            }

            this._hierarchalCodeAnnotationRetrieverEngine.RetrieveAnnotations(hid, allItems);

            ICollection <KeyValuePair <long, ICodeRefMutableObject> > collection = allItems;

            foreach (KeyValuePair <long, ICodeRefMutableObject> item in collection)
            {
                long sysId = item.Key;
                ICodeRefMutableObject codeRef = item.Value;
                long parentItemId;
                if (childItems.TryGetValue(sysId, out parentItemId))
                {
                    // has parent
                    ICodeRefMutableObject parent = allItems[parentItemId];
                    parent.AddCodeRef(codeRef);
                }
                else
                {
                    // add only root elements
                    artefact.AddHierarchicalCode(codeRef);
                }
            }
        }