aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/UsersViewVM.cs
blob: 5606d9da8cb9e637d9e2ed9460af8d88fc0b2abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.MachineStudio.Common.Notifications;
using SimpleValidator.Extensions;

namespace Tango.MachineStudio.DB.ViewModels
{
    public class UsersViewVM : DbTableViewModel<User>
    {
        public UsersViewVM(INotificationProvider notification) : base(notification)
        {
            SelectedRoles = new ObservableCollection<MultiComboVM<Role>>();
        }

        private ObservableCollection<MultiComboVM<Role>> _selectedRoles;
        public ObservableCollection<MultiComboVM<Role>> SelectedRoles
        {
            get { return _selectedRoles; }
            set { _selectedRoles = value; RaisePropertyChangedAuto(); }
        }

        protected override void OnEdit()
        {
            SelectedRoles = Adapter.Roles.Select(x => new MultiComboVM<Role>(x, () => RaisePropertyChanged(nameof(SelectedRoles)))).ToObservableCollection();

            foreach (var role in SelectedRoles)
            {
                if (SelectedEntity.UsersRoles.ToList().Exists(x => x.Role == role.Entity))
                {
                    role.IsSelected = true;
                }
            }

            base.OnEdit();
        }

        protected override void OnAdd()
        {
            SelectedRoles = Adapter.Roles.Select(x => new MultiComboVM<Role>(x, () => RaisePropertyChanged(nameof(SelectedRoles)))).ToObservableCollection();

            base.OnAdd();
        }

        protected override void OnBeforeEntitySave(DialogOpenMode mode, User user)
        {
            base.OnBeforeEntitySave(mode, user);

            Adapter.Context.UsersRoles.RemoveRange(user.UsersRoles);

            foreach (var role in SelectedRoles)
            {
                if (role.IsSelected)
                {
                    user.UsersRoles.Add(new UsersRole()
                    {
                        Role = role.Entity,
                        User = user,
                        RoleGuid = role.Entity.Guid,
                        UserGuid = user.Guid
                    });
                }
            }
        }

        protected override void OnValidating()
        {
            base.OnValidating();

            if (EditEntity.Email != null)
            {
                if (Adapter.Users.ToList().Exists(x => x.Guid != EditEntity.Guid && x.Email.ToLower() == EditEntity.Email.ToLower()))
                {
                    ValidationErrors.Add("Email already exist");
                }
            }

            if (!EditEntity.Email.IsEmail())
            {
                ValidationErrors.Add("Email address is invalid");
            }

            if (!EditEntity.Password.IsMinLength(4))
            {
                ValidationErrors.Add("Password must have at least 4 characters");
            }
        }
    }
}
class="p">.Length; lastDelimiterEnd = ds.Offset + ds.Length; lines.Add(ls); ls = new DocumentLine(document); ds = NewLineFinder.NextNewLine(document, lastDelimiterEnd); } ls.ResetLine(); ls.TotalLength = document.TextLength - lastDelimiterEnd; lines.Add(ls); documentLineTree.RebuildTree(lines); foreach (ILineTracker lineTracker in lineTrackers) lineTracker.RebuildDocument(); } #endregion #region Remove public void Remove(int offset, int length) { Debug.Assert(length >= 0); if (length == 0) return; DocumentLine startLine = documentLineTree.GetByOffset(offset); int startLineOffset = startLine.Offset; Debug.Assert(offset < startLineOffset + startLine.TotalLength); if (offset > startLineOffset + startLine.Length) { Debug.Assert(startLine.DelimiterLength == 2); // we are deleting starting in the middle of a delimiter // remove last delimiter part SetLineLength(startLine, startLine.TotalLength - 1); // remove remaining text Remove(offset, length - 1); return; } if (offset + length < startLineOffset + startLine.TotalLength) { // just removing a part of this line //startLine.RemovedLinePart(ref deferredEventList, offset - startLineOffset, length); SetLineLength(startLine, startLine.TotalLength - length); return; } // merge startLine with another line because startLine's delimiter was deleted // possibly remove lines in between if multiple delimiters were deleted int charactersRemovedInStartLine = startLineOffset + startLine.TotalLength - offset; Debug.Assert(charactersRemovedInStartLine > 0); //startLine.RemovedLinePart(ref deferredEventList, offset - startLineOffset, charactersRemovedInStartLine); DocumentLine endLine = documentLineTree.GetByOffset(offset + length); if (endLine == startLine) { // special case: we are removing a part of the last line up to the // end of the document SetLineLength(startLine, startLine.TotalLength - length); return; } int endLineOffset = endLine.Offset; int charactersLeftInEndLine = endLineOffset + endLine.TotalLength - (offset + length); //endLine.RemovedLinePart(ref deferredEventList, 0, endLine.TotalLength - charactersLeftInEndLine); //startLine.MergedWith(endLine, offset - startLineOffset); // remove all lines between startLine (excl.) and endLine (incl.) DocumentLine tmp = startLine.NextLine; DocumentLine lineToRemove; do { lineToRemove = tmp; tmp = tmp.NextLine; RemoveLine(lineToRemove); } while (lineToRemove != endLine); SetLineLength(startLine, startLine.TotalLength - charactersRemovedInStartLine + charactersLeftInEndLine); } void RemoveLine(DocumentLine lineToRemove) { foreach (ILineTracker lt in lineTrackers) lt.BeforeRemoveLine(lineToRemove); documentLineTree.RemoveLine(lineToRemove); // foreach (ILineTracker lt in lineTracker) // lt.AfterRemoveLine(lineToRemove); // deletedLines.Add(lineToRemove); // deletedOrChangedLines.Add(lineToRemove); } #endregion #region Insert public void Insert(int offset, string text) { DocumentLine line = documentLineTree.GetByOffset(offset); int lineOffset = line.Offset; Debug.Assert(offset <= lineOffset + line.TotalLength); if (offset > lineOffset + line.Length) { Debug.Assert(line.DelimiterLength == 2); // we are inserting in the middle of a delimiter // shorten line SetLineLength(line, line.TotalLength - 1); // add new line line = InsertLineAfter(line, 1); line = SetLineLength(line, 1); } SimpleSegment ds = NewLineFinder.NextNewLine(text, 0); if (ds == SimpleSegment.Invalid) { // no newline is being inserted, all text is inserted in a single line //line.InsertedLinePart(offset - line.Offset, text.Length); SetLineLength(line, line.TotalLength + text.Length); return; } //DocumentLine firstLine = line; //firstLine.InsertedLinePart(offset - firstLine.Offset, ds.Offset); int lastDelimiterEnd = 0; while (ds != SimpleSegment.Invalid) { // split line segment at line delimiter int lineBreakOffset = offset + ds.Offset + ds.Length; lineOffset = line.Offset; int lengthAfterInsertionPos = lineOffset + line.TotalLength - (offset + lastDelimiterEnd); line = SetLineLength(line, lineBreakOffset - lineOffset); DocumentLine newLine = InsertLineAfter(line, lengthAfterInsertionPos); newLine = SetLineLength(newLine, lengthAfterInsertionPos); line = newLine; lastDelimiterEnd = ds.Offset + ds.Length; ds = NewLineFinder.NextNewLine(text, lastDelimiterEnd); } //firstLine.SplitTo(line); // insert rest after last delimiter if (lastDelimiterEnd != text.Length) { //line.InsertedLinePart(0, text.Length - lastDelimiterEnd); SetLineLength(line, line.TotalLength + text.Length - lastDelimiterEnd); } } DocumentLine InsertLineAfter(DocumentLine line, int length) { DocumentLine newLine = documentLineTree.InsertLineAfter(line, length); foreach (ILineTracker lt in lineTrackers) lt.LineInserted(line, newLine); return newLine; } #endregion #region SetLineLength /// <summary> /// Sets the total line length and checks the delimiter. /// This method can cause line to be deleted when it contains a single '\n' character /// and the previous line ends with '\r'. /// </summary> /// <returns>Usually returns <paramref name="line"/>, but if line was deleted due to /// the "\r\n" merge, returns the previous line.</returns> DocumentLine SetLineLength(DocumentLine line, int newTotalLength) { // changedLines.Add(line); // deletedOrChangedLines.Add(line); int delta = newTotalLength - line.TotalLength; if (delta != 0) { foreach (ILineTracker lt in lineTrackers) lt.SetLineLength(line, newTotalLength); line.TotalLength = newTotalLength; DocumentLineTree.UpdateAfterChildrenChange(line); } // determine new DelimiterLength if (newTotalLength == 0) { line.DelimiterLength = 0; } else { int lineOffset = line.Offset; char lastChar = document.GetCharAt(lineOffset + newTotalLength - 1); if (lastChar == '\r') { line.DelimiterLength = 1; } else if (lastChar == '\n') { if (newTotalLength >= 2 && document.GetCharAt(lineOffset + newTotalLength - 2) == '\r') { line.DelimiterLength = 2; } else if (newTotalLength == 1 && lineOffset > 0 && document.GetCharAt(lineOffset - 1) == '\r') { // we need to join this line with the previous line DocumentLine previousLine = line.PreviousLine; RemoveLine(line); return SetLineLength(previousLine, previousLine.TotalLength + 1); } else { line.DelimiterLength = 1; } } else { line.DelimiterLength = 0; } } return line; } #endregion } }