/// <summary> /// Clone and import one or more <see cref="CohortIdentificationConfiguration"/> into the target <paramref name="into"/> /// </summary> /// <param name="cics"></param> /// <param name="into">The container into which you want to add the <paramref name="cics"/></param> public void Import(CohortIdentificationConfiguration[] cics, CohortAggregateContainer into) { var cicInto = into.GetCohortIdentificationConfiguration(); if (cicInto == null) { throw new ArgumentException($"Cannot import into orphan container '{into}'", nameof(into)); } //clone them var cicClones = new CohortIdentificationConfiguration[cics.Length]; try { for (int i = 0; i < cics.Length; i++) { cicClones[i] = cics[i].CreateClone(new ThrowImmediatelyCheckNotifier()); } } catch (Exception ex) { throw new Exception("Error during pre import cloning stage, no import will be attempted", ex); } using (_repository.BeginNewTransactedConnection()) { //Grab the root container of each of the input cics foreach (CohortIdentificationConfiguration cic in cicClones) { var container = cic.RootCohortAggregateContainer; //clear them to avoid dual parentage cic.RootCohortAggregateContainer_ID = null; cic.SaveToDatabase(); //add them into the target SET operation container you are importing into into.AddChild(container); // Make the new name of all the AggregateConfigurations match the owner of import into container foreach (var child in container.GetAllAggregateConfigurationsRecursively()) { EnsureNamingConvention(cicInto, child); } // Delete the old now empty clones cic.DeleteInDatabase(); } //finish transaction _repository.EndTransactedConnection(true); } }
/// <summary> /// Splits the root container of a <see cref="CohortIdentificationConfiguration"/> into multiple new cic. /// </summary> /// <param name="rootContainer"></param> /// <returns>All new configurations unmerged out of the <paramref name="rootContainer"/></returns> public CohortIdentificationConfiguration[] UnMerge(CohortAggregateContainer rootContainer) { if (!rootContainer.IsRootContainer()) { throw new ArgumentException("Container must be a root container to be unmerged", nameof(rootContainer)); } if (rootContainer.GetAggregateConfigurations().Any()) { throw new ArgumentException("Container must contain only sub-containers (i.e. no aggregates)", nameof(rootContainer)); } if (rootContainer.GetSubContainers().Length <= 1) { throw new ArgumentException("Container must contain 2+ sub-containers to be unmerged", nameof(rootContainer)); } var cic = rootContainer.GetCohortIdentificationConfiguration(); var toReturn = new List <CohortIdentificationConfiguration>(); try { // clone the input cic cic = cic.CreateClone(new ThrowImmediatelyCheckNotifier()); // grab the new clone root container rootContainer = cic.RootCohortAggregateContainer; } catch (Exception ex) { throw new Exception("Error during pre merge cloning stage, no UnMerge will be attempted", ex); } using (_repository.BeginNewTransactedConnection()) { // For each of these foreach (var subContainer in rootContainer.GetSubContainers().OrderBy(c => c.Order)) { // create a new config var newCic = new CohortIdentificationConfiguration(_repository, $"Un Merged { subContainer.Name } ({subContainer.ID }) "); //take the container we are splitting out subContainer.MakeIntoAnOrphan(); //make it the root container of the new cic newCic.RootCohortAggregateContainer_ID = subContainer.ID; newCic.SaveToDatabase(); // Make the new name of all the AggregateConfigurations match the new cic foreach (var child in subContainer.GetAllAggregateConfigurationsRecursively()) { EnsureNamingConvention(newCic, child); } toReturn.Add(newCic); } //Now delete the original clone that we unmerged the containers out of cic.DeleteInDatabase(); //finish transaction _repository.EndTransactedConnection(true); } return(toReturn.ToArray()); }