public bool HandleRename(ISqlParameter parameter, string oldName, string newName) { if (string.IsNullOrWhiteSpace(newName)) { return(false); } if (string.IsNullOrWhiteSpace(oldName)) { return(false); } //they are the same name! if (oldName.Equals(newName)) { return(false); } if (!parameter.ParameterName.Equals(newName)) { throw new ArgumentException("Expected parameter " + parameter + " to have name '" + newName + "' but it's value was " + parameter.ParameterName + ", this means someone was lying about the rename event"); } var owner = parameter.GetOwnerIfAny(); var filter = owner as IFilter; if (filter == null || filter is SpontaneousObject) { return(false); } //There is no WHERE SQL anyway if (string.IsNullOrWhiteSpace(filter.WhereSQL)) { return(false); } string before = filter.WhereSQL; string after = ParameterCreator.RenameParameterInSQL(before, oldName, newName); //no change was actually made if (before.Equals(after)) { return(false); } filter.WhereSQL = after; filter.SaveToDatabase(); if (!RefactoredFilters.Contains(filter)) { RefactoredFilters.Add(filter); } return(true); }
/// <summary> /// Creates a copy of the <paramref name="fromMaster"/> filter and any parameters it might have. This will handle collisions on parameter name with /// <paramref name="existingFiltersAlreadyInScope"/> and will respect any globals this class was constructed with. /// </summary> /// <param name="fromMaster"></param> /// <param name="existingFiltersAlreadyInScope"></param> /// <returns></returns> public IFilter ImportFilter(IFilter fromMaster, IFilter[] existingFiltersAlreadyInScope) { var extractionFilter = fromMaster as ExtractionFilter; if (extractionFilter != null && extractionFilter.ExtractionInformation.ColumnInfo == null) { throw new Exception("Could not import filter " + extractionFilter + " because it could not be traced back to a ColumnInfo"); } //If user is trying to publish a filter into the Catalogue as a new master top level filter, make sure it is properly documented if (_factory is ExtractionFilterFactory) { string reason; if (!IsProperlyDocumented(fromMaster, out reason)) { throw new Exception("Cannot clone filter called '" + fromMaster.Name + "' because:" + reason); } } //Handle problems with existing filters existingFiltersAlreadyInScope = existingFiltersAlreadyInScope ?? new IFilter[0]; if (existingFiltersAlreadyInScope.Contains(fromMaster)) { throw new ArgumentException("Master filter (that you are trying to import) cannot be part of the existing filters collection!"); } //Ensure that the new filter has a unique name within the scope string name = fromMaster.Name; while (existingFiltersAlreadyInScope.Any(f => f.Name.Equals(name))) { name = "Copy of " + name; } //create the filter var newFilter = _factory.CreateNewFilter(name); //Now copy across all the values from the master newFilter.Description = fromMaster.Description; newFilter.IsMandatory = fromMaster.IsMandatory; newFilter.WhereSQL = fromMaster.WhereSQL; //if we are down cloning from a master ExtractionFilter so record that the new filter is if (fromMaster is ExtractionFilter) { newFilter.ClonedFromExtractionFilter_ID = fromMaster.ID;//make the new filters parent the master } //if we are up cloning we are publishing a child into being a new master catalogue filter (ExtractionFilter) if (newFilter is ExtractionFilter) { newFilter.Description += Environment.NewLine + " Published by " + Environment.UserName + " on " + DateTime.Now + " from object " + fromMaster.GetType().Name + " with ID " + fromMaster.ID; fromMaster.ClonedFromExtractionFilter_ID = newFilter.ID;//Make the newly created master our parent (since we are published) } newFilter.SaveToDatabase(); //If there are some filters already in scope then we need to take into account their parameters when it comes to importing, so fetch a union of all the parameters var existingFiltersParametersAlreadyInScope = existingFiltersAlreadyInScope.SelectMany(f => f.GetAllParameters()).ToArray(); //now create parameters while respecting globals var parameterCreator = new ParameterCreator(_factory, _globals, AlternateValuesToUseForNewParameters ?? fromMaster.GetAllParameters()); parameterCreator.CreateAll(newFilter, existingFiltersParametersAlreadyInScope); //Create the parameters while handling the existing parameters in scope return(newFilter); }