/// <summary> /// Inserts the specified item. /// </summary> /// <param name="state">The state.</param> /// <param name="schemaMaps">The schema maps.</param> /// <param name="structureSetId">The structure set identifier.</param> /// <returns> /// The primary key value /// </returns> public IEnumerable <long> Insert(DbTransactionState state, IEnumerable <TSchemaMap> schemaMaps, long structureSetId) { var procedure = new TProc(); var cache = new StructureCache(); var primaryKeys = new List <long>(); foreach (var schemaMap in schemaMaps) { long primaryKey; using (var command = procedure.CreateCommandWithDefaults(state)) { procedure.CreateParentIdParameter(command).Value = structureSetId; var sourceCodelistStatus = this._validateStatusEngine.GetReferenceStatus(state, schemaMap.SourceRef, cache); var targetCodelistStatus = this._validateStatusEngine.GetReferenceStatus(state, schemaMap.TargetRef, cache); procedure.CreateSourceIdParameter(command).Value = sourceCodelistStatus.FinalStatus.PrimaryKey; procedure.CreateTargetIdParameter(command).Value = targetCodelistStatus.FinalStatus.PrimaryKey; primaryKey = this._nameableImportEngine.RunCommand(schemaMap, command, procedure, state); } primaryKeys.Add(primaryKey); this.WriteItemMaps(state, schemaMap, primaryKey); } return(primaryKeys); }
/// <summary> /// Insert the specified <paramref name="maintainables"/> to the mapping store. /// </summary> /// <param name="maintainables"> /// The maintainable. /// </param> /// <returns> /// The <see cref="IEnumerable{ArtefactImportStatus}"/>. /// </returns> public new IEnumerable <ArtefactImportStatus> Insert(IEnumerable <ICategorisationObject> maintainables) { var cache = new StructureCache(); foreach (var artefact in maintainables) { using (DbTransactionState state = DbTransactionState.Create(this.Database)) { ArtefactImportStatus artefactImportStatus; try { artefactImportStatus = this.InsertInternal(state, artefact, cache); state.Commit(); } catch (MappingStoreException e) { _log.Error(artefact.Urn.ToString(), e); state.RollBack(); artefactImportStatus = new ArtefactImportStatus(-1, artefact.AsReference.GetErrorMessage(e)); } catch (DbException e) { _log.Error(artefact.Urn.ToString(), e); state.RollBack(); artefactImportStatus = new ArtefactImportStatus(-1, artefact.AsReference.GetErrorMessage(e)); } yield return(artefactImportStatus); } } }
/// <summary> /// Insert the specified <paramref name="items"/> to the mapping store with <paramref name="state"/> /// </summary> /// <param name="state"> /// The MAPPING STORE connection and transaction state /// </param> /// <param name="items"> /// The items. /// </param> /// <param name="parentArtefact"> /// The primary key of the parent artefact. /// </param> /// <returns> /// The <see cref="IEnumerable{Long}"/>. /// </returns> public ItemStatusCollection Insert(DbTransactionState state, IEnumerable <IComponent> items, long parentArtefact) { var components = items as IComponent[] ?? items.ToArray(); if (components.Length == 0) { return(new ItemStatusCollection()); } var cache = new StructureCache(); var returnValues = new ItemStatusCollection(); var dsd = components[0].MaintainableParent as IDataStructureObject; if (dsd == null) { throw new ArgumentException(Resources.ExceptionCannotDetermineParent, "items"); } foreach (var component in components) { returnValues.Add(new ItemStatus(component.Id, Insert(state, component, cache, parentArtefact))); } return(returnValues); }
/// <summary> /// Writes the item maps. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="schemaMap"> /// The schema map. /// </param> /// <param name="primaryKey"> /// The primary key. /// </param> protected override void WriteItemMaps(DbTransactionState state, IStructureMapObject schemaMap, long primaryKey) { var componentMapProcedure = new InsertComponentMapProcedure(); var sourceMap = StructureCache.GetComponentMapIds(state, schemaMap.SourceRef); var targetMap = StructureCache.GetComponentMapIds(state, schemaMap.TargetRef); using (var command = componentMapProcedure.CreateCommandWithDefaults(state)) { componentMapProcedure.CreateParentIdParameter(command).Value = primaryKey; foreach (var componentMapObject in schemaMap.Components) { componentMapProcedure.CreateSourceIdParameter(command).Value = sourceMap[componentMapObject.MapConceptRef]; componentMapProcedure.CreateTargetIdParameter(command).Value = targetMap[componentMapObject.MapTargetConceptRef]; command.ExecuteNonQuery(); } } }
/// <summary> /// Insert the specified <paramref name="maintainable"/> to the mapping store with <paramref name="state"/> /// </summary> /// <param name="state"> /// The MAPPING STORE connection and transaction state /// </param> /// <param name="maintainable"> /// The maintainable. /// </param> /// <returns> /// The <see cref="ArtefactImportStatus"/>. /// </returns> public override ArtefactImportStatus Insert(DbTransactionState state, IHierarchicalCodelistObject maintainable) { var codelistCache = new StructureCache(); foreach (var codelistRef in maintainable.CodelistRef) { ICrossReference codelistReference = codelistRef.CodelistReference; ItemSchemeFinalStatus itemSchemeFinalStatus = codelistCache.GetStructure(state, codelistReference); var codelistMaintainableReference = codelistReference.MaintainableReference; if (itemSchemeFinalStatus.FinalStatus.PrimaryKey <= 0) { string message = string.Format( "HierarchicalCodeList {0} uses the CodeList:\r\n ID: {1}\r\n VERSION:{2}\r\n AGENCY: {3}\r\n which doesn't exist in the Mapping Store", maintainable.Id, codelistMaintainableReference.MaintainableId, codelistMaintainableReference.Version, codelistMaintainableReference.AgencyId); return(new ArtefactImportStatus(-1, new ImportMessage(ImportMessageStatus.Error, codelistReference, message))); } if (!itemSchemeFinalStatus.FinalStatus.IsFinal) { string message = string.Format( "HierarchicalCodeList {0} uses the CodeList:\r\n ID: {1}\r\n VERSION:{2}\r\n AGENCY: {3}\r\n which is not Final", maintainable.Id, codelistMaintainableReference.MaintainableId, codelistMaintainableReference.Version, codelistMaintainableReference.AgencyId); return(new ArtefactImportStatus(-1, new ImportMessage(ImportMessageStatus.Error, codelistReference, message))); } } _log.DebugFormat(CultureInfo.InvariantCulture, "Importing artefact {0}", maintainable.Urn); var artefactStoredProcedure = _storedProcedures.InsertHcl; var artefactStatus = this.InsertArtefactInternal(state, maintainable, artefactStoredProcedure); foreach (KeyValuePair <long, IHierarchy> hierarchy in this.InsertHierarchies(state, maintainable.Hierarchies, artefactStatus.PrimaryKeyValue)) { var levelIds = this.InsertLevels(state, hierarchy.Value.Level, hierarchy.Key); this.InsertCodeReferences(state, hierarchy.Value.HierarchicalCodeObjects, hierarchy.Key, codelistCache, levelIds); } return(artefactStatus); }
/// <summary> /// Inserts the content constraint attachment. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="maintainable"> /// The maintainable. /// </param> /// <param name="artefactStatus"> /// The artefact status. /// </param> private void InsertContentConstraintAttachment(DbTransactionState state, IContentConstraintObject maintainable, ArtefactImportStatus artefactStatus) { if (maintainable.ConstraintAttachment == null) { return; } var procedure = new InsertContentConstraintAttachmentProcedure(); var structureCache = new StructureCache(); using (var command = procedure.CreateCommand(state)) { procedure.CreateContentConstraintIdParameter(command, artefactStatus.PrimaryKeyValue); foreach (var crossReference in maintainable.ConstraintAttachment.StructureReference) { var itemSchemeFinalStatus = structureCache.GetStructure(state, crossReference); this._validateStatusEngine.ValidateFinalStatus(itemSchemeFinalStatus.FinalStatus, crossReference); procedure.CreateArtefactIdParameter(command, itemSchemeFinalStatus.FinalStatus.PrimaryKey); command.ExecuteNonQuery(); } } }
/// <summary> /// Writes the item maps. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="schemaMap"> /// The schema map. /// </param> /// <param name="primaryKey"> /// The primary key. /// </param> protected override void WriteItemMaps(DbTransactionState state, TSchemaMap schemaMap, long primaryKey) { var cache = new StructureCache(); var mapProcedure = new TMapItemProc(); var sourceItemSchemeRef = schemaMap.SourceRef; var targetItemSchemeRef = schemaMap.TargetRef; using (var command = mapProcedure.CreateCommandWithDefaults(state)) { mapProcedure.CreateParentIdParameter(command).Value = primaryKey; foreach (var itemMap in schemaMap.Items) { var sourceRef = new StructureReferenceImpl(sourceItemSchemeRef.AgencyId, sourceItemSchemeRef.MaintainableId, sourceItemSchemeRef.Version, this._childType, itemMap.SourceId); var sourceRefStatus = this.GetReferenceStatus(state, sourceRef, cache); mapProcedure.CreateSourceIdParameter(command).Value = sourceRefStatus.ItemIdMap[itemMap.SourceId].SysID; var targetRef = new StructureReferenceImpl(targetItemSchemeRef.AgencyId, targetItemSchemeRef.MaintainableId, targetItemSchemeRef.Version, this._childType, itemMap.TargetId); var targetRefStatus = this.GetReferenceStatus(state, targetRef, cache); mapProcedure.CreateTargetIdParameter(command).Value = targetRefStatus.ItemIdMap[itemMap.TargetId].SysID; command.ExecuteNonQuery(); } } }
/// <summary> /// Gets the CodeList status. /// </summary> /// <param name="state">The state.</param> /// <param name="codeListReference">The codeList reference.</param> /// <param name="itemScheme">The item scheme.</param> /// <returns> /// The <see cref="ItemSchemeFinalStatus"/>. /// </returns> protected ItemSchemeFinalStatus GetReferenceStatus(DbTransactionState state, IStructureReference codeListReference, StructureCache itemScheme) { return(this._validateStatusEngine.GetReferenceStatus(state, codeListReference, itemScheme)); }
/// <summary> /// Insert the specified <paramref name="maintainable"/> to mapping store <c>CATEGORISATION</c> table /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="maintainable"> /// The maintainable. /// </param> /// <param name="cache"> /// The cached Dataflow and Category Scheme /// </param> /// <returns> /// The <see cref="ArtefactImportStatus"/>. /// </returns> private ArtefactImportStatus InsertInternal(DbTransactionState state, ICategorisationObject maintainable, StructureCache cache) { var dataflowStatus = cache.GetStructure(state, maintainable.StructureReference); ArtefactImportStatus returnValue; if (dataflowStatus.FinalStatus.PrimaryKey > 0) { var categoryScheme = cache.GetStructure(state, maintainable.CategoryReference); if (categoryScheme.FinalStatus.PrimaryKey > 0 && categoryScheme.FinalStatus.IsFinal) { var categoryPrimaryKey = this.GetCategoryPrimaryKey(state, maintainable, categoryScheme); if (!ExistsCategorisation(state, dataflowStatus.FinalStatus.PrimaryKey, categoryPrimaryKey.SysID)) { var artefactStoredProcedure = _storedProcedures.InsertCategorisation; returnValue = this.InsertArtefactInternal( state, maintainable, artefactStoredProcedure, command => { artefactStoredProcedure.CreateArtIdParameter(command).Value = dataflowStatus.FinalStatus.PrimaryKey; artefactStoredProcedure.CreateCatIdParameter(command).Value = categoryPrimaryKey.SysID; }); string message = string.Format( CultureInfo.InvariantCulture, "Successfully categorized {0} with Category {1} of {2}\n", maintainable.StructureReference.GetAsHumanReadableString(), maintainable.CategoryReference.ChildReference.Id, maintainable.CategoryReference.GetAsHumanReadableString()); returnValue = new ArtefactImportStatus(returnValue.PrimaryKeyValue, new ImportMessage(ImportMessageStatus.Success, maintainable.AsReference, message)); } else { returnValue = BuildWarningMessage(maintainable, "Warning: Ignoring duplicate categorisation of {0} with {1}\n"); } } else if (!categoryScheme.FinalStatus.IsFinal) { returnValue = BuildWarningMessage(maintainable, "Failure: {0} is not Final so it cannot be referenced from {1}\n"); } else { returnValue = BuildWarningMessage(maintainable, "Failure: {0} does not exist so it cannot be referenced from {1}\n"); } } else { string message = string.Format(CultureInfo.InvariantCulture, "Failure: Cannot categorize {0}, because it does not exist\n", maintainable.StructureReference.GetAsHumanReadableString()); returnValue = new ArtefactImportStatus(-1, new ImportMessage(ImportMessageStatus.Warning, maintainable.AsReference, message)); } return(returnValue); }
/// <summary> /// Insert the specified <paramref name="maintainable"/> to the mapping store with <paramref name="state"/> /// </summary> /// <param name="state"> /// The MAPPING STORE connection and transaction state /// </param> /// <param name="maintainable"> /// The maintainable. /// </param> /// <returns> /// The <see cref="ArtefactImportStatus"/>. /// </returns> public override ArtefactImportStatus Insert(DbTransactionState state, ICategorisationObject maintainable) { var cache = new StructureCache(); return(this.InsertInternal(state, maintainable, cache)); }
/// <summary> /// The insert. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="component"> /// The component. /// </param> /// <param name="itemScheme"> /// The item scheme. /// </param> /// <param name="parentArtefact"> /// The parent artefact. /// </param> /// <returns> /// The <see cref="long"/>. /// </returns> /// <exception cref="MappingStoreException"> /// THere was a problem with the <paramref name="component"/> references. /// </exception> private static long Insert( DbTransactionState state, IComponent component, StructureCache itemScheme, long parentArtefact) { var conceptStatus = itemScheme.GetStructure(state, component.ConceptRef); ItemStatus conceptID = ValidateConceptScheme(conceptStatus, component.ConceptRef); var codelistStatus = GetCodelistStatus(state, component, itemScheme); var formats = new List <KeyValuePair <long, ITextFormat> >(); long compID; var attribute = component as IAttributeObject; var dimension = component as IDimension; var storedProcedure = _storedProcedures.InsertComponent; using (DbCommand command = storedProcedure.CreateCommand(state)) { DbParameter idParameter = storedProcedure.CreateIdParameter(command); idParameter.Value = component.Id; SetDsd(parentArtefact, storedProcedure, command); SetConcept(storedProcedure, command, conceptID); SetComponentType(component, storedProcedure, command); SetCodelist(storedProcedure, command, codelistStatus, component); DbParameter isFreqDimParameter = storedProcedure.CreateIsFreqDimParameter(command); DbParameter isMeasureDimParameter = storedProcedure.CreateIsMeasureDimParameter(command); DbParameter attAssLevelParameter = storedProcedure.CreateAttAssLevelParameter(command); DbParameter attStatusParameter = storedProcedure.CreateAttStatusParameter(command); DbParameter attIsTimeFormatParameter = storedProcedure.CreateAttIsTimeFormatParameter(command); DbParameter xsMeasureCodeParameter = storedProcedure.CreateXsMeasureCodeParameter(command); DbParameter outputParameter = storedProcedure.CreateOutputParameter(command); switch (component.StructureType.EnumType) { case SdmxStructureEnumType.Dimension: case SdmxStructureEnumType.MeasureDimension: { SetDimensionParameters(dimension, isFreqDimParameter, isMeasureDimParameter); } break; case SdmxStructureEnumType.TimeDimension: break; case SdmxStructureEnumType.DataAttribute: { SetAttributeParameters(attribute, attAssLevelParameter, attStatusParameter, attIsTimeFormatParameter); } break; case SdmxStructureEnumType.PrimaryMeasure: break; case SdmxStructureEnumType.CrossSectionalMeasure: { SetCrossSectionalMeasureParameters(component, xsMeasureCodeParameter); } break; } SetCrossSectionalLevels(component, storedProcedure, command); command.ExecuteNonQuery(); compID = (long)outputParameter.Value; if (component.Representation != null && component.Representation.TextFormat != null) { formats.Add(new KeyValuePair <long, ITextFormat>(compID, component.Representation.TextFormat)); } } InsertTextFormats(state, formats); _annotationInsertEngine.Insert(state, compID, _insertComponentAnnotation, component.Annotations); return(compID); }
/// <summary> /// Gets the CodeList status. /// </summary> /// <param name="state">The state.</param> /// <param name="component">The component.</param> /// <param name="itemScheme">The item scheme.</param> /// <returns> /// The <see cref="ItemSchemeFinalStatus" />. /// </returns> private static ItemSchemeFinalStatus GetCodelistStatus(DbTransactionState state, IComponent component, StructureCache itemScheme) { ItemSchemeFinalStatus codelistStatus = null; if (component.HasCodedRepresentation()) { var dimension = component as IDimension; var crossDsd = component.MaintainableParent as ICrossSectionalDataStructureObject; IStructureReference reference; if (dimension != null && dimension.MeasureDimension && crossDsd != null) { reference = crossDsd.GetCodelistForMeasureDimension(dimension.Id); } else { reference = component.Representation.Representation; } codelistStatus = itemScheme.GetStructure(state, reference); ValidateCodelist(codelistStatus.FinalStatus, reference); } return(codelistStatus); }
/// <summary> /// Gets the CodeList status. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="reference"> /// The reference. /// </param> /// <param name="itemScheme"> /// The item scheme. /// </param> /// <returns> /// The <see cref="ItemSchemeFinalStatus"/>. /// </returns> public ItemSchemeFinalStatus GetReferenceStatus(DbTransactionState state, IStructureReference reference, StructureCache itemScheme) { ItemSchemeFinalStatus codelistStatus = itemScheme.GetStructure(state, reference); this.ValidateFinalStatus(codelistStatus.FinalStatus, reference); return(codelistStatus); }
/// <summary> /// Insert a <see cref="IHierarchicalCode"/> to mapping store table <c>HCL_CODE</c>. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="coderef"> /// The Hierarchical code reference. /// </param> /// <param name="hierarchyID"> /// The hierarchy ID. /// </param> /// <param name="parentID"> /// The parent id. /// </param> /// <param name="codelistReferences"> /// The codelist references. /// </param> /// <param name="levelIds"> /// The level Ids. /// </param> /// <returns> /// The primary key of the ne /// </returns> private long InsertCodeReference(DbTransactionState state, IHierarchicalCode coderef, long hierarchyID, long parentID, StructureCache codelistReferences, IDictionary <string, long> levelIds) { var artefactStoredProcedure = _storedProcedures.InsertHclCode; long artefactImportStatus; using (DbCommand command = artefactStoredProcedure.CreateCommandWithDefaults(state)) { DbParameter parentHcodeIDIdParameter = artefactStoredProcedure.CreateParentHcodeIdParameter(command); parentHcodeIDIdParameter.Value = parentID > 0 ? (object)parentID : null; DbParameter lcdIddParameter = artefactStoredProcedure.CreateLcdIdParameter(command); ItemSchemeFinalStatus itemSchemeFinalStatus = codelistReferences.GetStructure(state, coderef.CodeReference); ItemStatus codeStatus; if (itemSchemeFinalStatus.ItemIdMap.TryGetValue(coderef.CodeReference.ChildReference.Id, out codeStatus)) { lcdIddParameter.Value = codeStatus.SysID; } DbParameter hierarchyIdParameter = artefactStoredProcedure.CreateHIdParameter(command); hierarchyIdParameter.Value = hierarchyID; DbParameter levelIdParameter = artefactStoredProcedure.CreateLevelIdParameter(command); ILevelObject levelObject = coderef.GetLevel(false); long levelPrimaryKey; if (levelObject != null && levelIds.TryGetValue(levelObject.Id, out levelPrimaryKey)) { levelIdParameter.Value = levelPrimaryKey; } if (coderef.Uri != null) { artefactStoredProcedure.CreateUriParameter(command).Value = coderef.Uri.ToString(); } artefactImportStatus = this.RunIdentifiableArterfactCommand(coderef, command, artefactStoredProcedure); } return(artefactImportStatus); }
/// <summary> /// Insert code references. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="hierarchicalCodeObjects"> /// The hierarchical code objects. /// </param> /// <param name="hierarchyID"> /// The hierarchy id. /// </param> /// <param name="codelistCache"> /// The codeList cache. /// </param> /// <param name="levelIds"> /// The level ids. /// </param> private void InsertCodeReferences(DbTransactionState state, IEnumerable <IHierarchicalCode> hierarchicalCodeObjects, long hierarchyID, StructureCache codelistCache, IDictionary <string, long> levelIds) { var queue = new Queue <KeyValuePair <long, IHierarchicalCode> >(hierarchicalCodeObjects.Select(code => new KeyValuePair <long, IHierarchicalCode>(0, code))); while (queue.Count > 0) { var keyValue = queue.Dequeue(); long primaryKey = this.InsertCodeReference(state, keyValue.Value, hierarchyID, keyValue.Key, codelistCache, levelIds); foreach (var hierarchicalCode in keyValue.Value.CodeRefs) { queue.Enqueue(new KeyValuePair <long, IHierarchicalCode>(primaryKey, hierarchicalCode)); } } }
/// <summary> /// Gets the CodeList status. /// </summary> /// <param name="state"> /// The state. /// </param> /// <param name="reference"> /// The reference. /// </param> /// <param name="itemScheme"> /// The item scheme. /// </param> /// <returns> /// The <see cref="ItemSchemeFinalStatus"/>. /// </returns> public ItemSchemeFinalStatus GetReferenceStatus(DbTransactionState state, IStructureReference reference, StructureCache itemScheme) { ItemSchemeFinalStatus codelistStatus = itemScheme.GetStructure(state, reference); this.ValidateFinalStatus(codelistStatus.FinalStatus, reference); return codelistStatus; }