Skip to content

Commit

Permalink
Fixes #530 illegal chars
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre committed Mar 16, 2021
1 parent 6e4c2b8 commit 12bd499
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
22 changes: 18 additions & 4 deletions src/MoBi.Presentation/DTO/ObjectBaseDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
using System.ComponentModel;
using System.Linq;
using MoBi.Assets;
using OSPSuite.Utility.Extensions;
using OSPSuite.Utility.Reflection;
using OSPSuite.Utility.Validation;
using MoBi.Core.Domain.Model;
using MoBi.Core.Helper;
using OSPSuite.Assets;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Presentation.Core;
using OSPSuite.Presentation.DTO;
using OSPSuite.Utility.Extensions;
using OSPSuite.Utility.Reflection;
using OSPSuite.Utility.Validation;

namespace MoBi.Presentation.DTO
{
Expand Down Expand Up @@ -80,6 +81,14 @@ public virtual void HandlePropertyChanged(object sender, PropertyChangedEventArg

private static class AllRules
{
private static bool nameDoesNotContainerIllegalCharacters(string name)
{
if (string.IsNullOrEmpty(name))
return true;

return !Constants.ILLEGAL_CHARACTERS.Any(name.Contains);
}

private static IBusinessRule notEmptyNameRule { get; } = CreateRule.For<IObjectBaseDTO>()
.Property(x => x.Name)
.WithRule((dto, name) => dto.IsNameDefined(name))
Expand All @@ -90,9 +99,15 @@ private static class AllRules
.WithRule((dto, name) => dto.IsNameUnique(name))
.WithError(AppConstants.Validation.NameAlreadyUsed);

private static IBusinessRule nameDoesNotContainIllegalCharacters { get; } = CreateRule.For<IObjectBaseDTO>()
.Property(item => item.Name)
.WithRule((dto, name) => nameDoesNotContainerIllegalCharacters(name))
.WithError(Error.NameCannotContainIllegalCharacters(Constants.ILLEGAL_CHARACTERS));

public static IEnumerable<IBusinessRule> All()
{
yield return notEmptyNameRule;
yield return nameDoesNotContainIllegalCharacters;
yield return uniqueNameRule;
}
}
Expand Down Expand Up @@ -136,7 +151,6 @@ public class FavoritesNodeViewItem : ObjectBaseDTO

public class UserDefinedNodeViewItem : ObjectBaseDTO
{

}

public class BuildingBlockInfoViewItem : ObjectBaseDTO
Expand Down
25 changes: 14 additions & 11 deletions src/MoBi.Presentation/Tasks/Interaction/ObjectBaseTask.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System.Collections.Generic;
using System.Linq;
using MoBi.Assets;
using OSPSuite.Core.Commands.Core;
using OSPSuite.Utility.Extensions;
using MoBi.Core.Commands;
using MoBi.Core.Domain;
using MoBi.Core.Domain.Model;
using MoBi.Core.Services;
using MoBi.Presentation.Presenter;
using OSPSuite.Core.Commands.Core;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Services;
using OSPSuite.Presentation.Presenters;
using OSPSuite.Utility.Extensions;

namespace MoBi.Presentation.Tasks.Interaction
{
public interface IObjectBaseTask
{
IMoBiCommand Rename(IObjectBase objectBase, IEnumerable<string> allreadyNames, IBuildingBlock buildingBlock);
IMoBiCommand Rename(IObjectBase objectBase, IEnumerable<string> alreadyUsedNames, IBuildingBlock buildingBlock);

/// <summary>
/// Search for usages of <paramref name="oldName" /> in the project and asks the user if they should be renamed as well
Expand All @@ -40,23 +39,27 @@ internal class ObjectBaseTask : IObjectBaseTask
private readonly IMoBiContext _context;
private readonly ICheckNameVisitor _checkNamesVisitor;
private readonly IMoBiApplicationController _applicationController;
private readonly IDialogCreator _dialogCreator;

public ObjectBaseTask(IObjectTypeResolver objectTypeResolver, IMoBiContext context, ICheckNameVisitor checkNamesVisitor, IMoBiApplicationController applicationController, IDialogCreator dialogCreator)
public ObjectBaseTask(IObjectTypeResolver objectTypeResolver, IMoBiContext context, ICheckNameVisitor checkNamesVisitor, IMoBiApplicationController applicationController)
{
_objectTypeResolver = objectTypeResolver;
_context = context;
_checkNamesVisitor = checkNamesVisitor;
_applicationController = applicationController;
_dialogCreator = dialogCreator;
}

public IMoBiCommand Rename(IObjectBase objectBase, IEnumerable<string> allreadyNames, IBuildingBlock buildingBlock)
public IMoBiCommand Rename(IObjectBase objectBase, IEnumerable<string> alreadyUsedNames, IBuildingBlock buildingBlock)
{
var unallowedNames = new List<string>(allreadyNames);
var unallowedNames = new List<string>(alreadyUsedNames);
unallowedNames.AddRange(AppConstants.UnallowedNames);
var objectName = _objectTypeResolver.TypeFor(objectBase);
string newName = _dialogCreator.AskForInput(AppConstants.Dialog.AskForNewName(objectBase.Name), AppConstants.Captions.NewName, objectBase.Name, unallowedNames);

string newName;
using (var renameObjectPresenter = _applicationController.Start<IRenameObjectPresenter>())
{
newName = renameObjectPresenter.NewNameFrom(objectBase, unallowedNames);
}


if (string.IsNullOrEmpty(newName))
return new MoBiEmptyCommand();
Expand Down

0 comments on commit 12bd499

Please sign in to comment.