private void Save_Click(object sender, RoutedEventArgs e) { Save.IsEnabled = false; try { if (Model.Text.Contains("Virtual")) { throw new Exception("Virtual machine is not a valid device model."); } long wsAsset = 0; foreach (var asset in Asset.Text.Split(',')) { if (!string.Equals(asset, "0") && (asset.Length != 8 || !long.TryParse(asset, out wsAsset))) { throw new Exception("Invalid old WS Asset tag, expect 8-digit number or 0."); } } foreach (var asset in AssetTag.Text.Split(',')) { if (!string.IsNullOrWhiteSpace(asset) && (asset.Length != 12 || !long.TryParse(asset, out wsAsset))) { throw new Exception("Invalid new WS Asset tag, expect 12-digit number or null."); } } string msAlias = User.Text.Trim().ToLower(); string wsAlias = string.Empty; wsAlias = msAlias; string sn = SN.Text.Trim(); if (string.IsNullOrWhiteSpace(sn) || string.Equals(sn.ToLower(), "serial number")) { throw new Exception("SN is invalid. Use WS Asset tag if you have no idea."); } var client = new GraphClient(new Uri("http://*****:*****@localhost:7474/db/data")); client.Connect(); Device newDevice = new Device { WSAsset = Asset.Text.Trim(), AssetTag = AssetTag.Text.Trim(), Manufacturer = Manufacturer.Text.Trim(), SN = SN.Text.Trim().ToUpper(), CPU = CPU.Text.Trim(), RAM = RAM.Text.Trim(), Disk = Disk.Text.Trim(), SystemDate = SystemDate.Text.Trim(), OS = OS.Text.Trim(), OSInstallDate = OSInstallDate.Text.Trim(), Model = Model.Text.Trim() }; // Don't create duplicate relationship! var result = client.Cypher .Match("(device:Device)") .Where("device.SN = {sn}") .WithParam("sn", newDevice.SN) .Return(device => device.As <Node <Device> >()); if (result.Results.Count() != 0) { throw new Exception("SN must be unique. Use WS Asset tag if you have no idea."); } if (!string.Equals(newDevice.WSAsset, "0")) { result = client.Cypher .Match("(device:Device)") .Where("device.WSAsset = {asset}") .WithParam("asset", newDevice.WSAsset) .Return(device => device.As <Node <Device> >()); if (result.Results.Count() != 0) { throw new Exception("Old WS asset must be unique."); } } if (!string.IsNullOrWhiteSpace(newDevice.AssetTag)) { result = client.Cypher .Match("(device:Device)") .Where("device.AssetTag = {asset}") .WithParam("asset", newDevice.AssetTag) .Return(device => device.As <Node <Device> >()); if (result.Results.Count() != 0) { throw new Exception("New WS asset must be unique."); } } // Create device if not exist client.Cypher .Merge("(device:Device { SN: {sn} })") .OnCreate() .Set("device = {newDevice}") .WithParams(new { sn = newDevice.SN, newDevice }) .ExecuteWithoutResults(); Owner newOwner = new WPFAssets.Owner { MSAlias = msAlias, WSAlias = wsAlias, Email = msAlias + "@ms;" + wsAlias + "@ws" }; // Create owner if not exist. client.Cypher .Merge("(owner:Owner { MSAlias: {msalias} })") .OnCreate() .Set("owner = {newOwner}") .WithParams(new { msalias = newOwner.MSAlias, newOwner }) .ExecuteWithoutResults(); // Relate owner and device. client.Cypher .Match("(device:Device)", "(owner:Owner)") .Where((Device device) => device.SN == newDevice.SN) .AndWhere((Owner owner) => owner.MSAlias == newOwner.MSAlias) .CreateUnique("device<-[:OWNS]-owner") .ExecuteWithoutResults(); Message.Text = newDevice.SN + " is owned by " + newOwner.MSAlias; SN.Text = newDevice.SN; User.Text = newOwner.MSAlias; } catch (Exception exception) { Message.Text = exception.Message; } Save.IsEnabled = true; }
private void Save_Click(object sender, RoutedEventArgs e) { Save.IsEnabled = false; try { if (Model.Text.Contains("Virtual")) { throw new Exception("Virtual machine is not a valid device model."); } long wsAsset = 0; foreach (var asset in Asset.Text.Split(',')) { if (!string.Equals(asset, "0") && (asset.Length != 8 || !long.TryParse(asset, out wsAsset))) { throw new Exception("Invalid old WS Asset tag, expect 8-digit number or 0."); } } foreach (var asset in AssetTag.Text.Split(',')) { if (!string.IsNullOrWhiteSpace(asset) && (asset.Length != 12 || !long.TryParse(asset, out wsAsset))) { throw new Exception("Invalid new WS Asset tag, expect 12-digit number or null."); } } string msAlias = User.Text.Trim().ToLower(); string wsAlias = string.Empty; wsAlias = msAlias; string sn = SN.Text.Trim(); if (string.IsNullOrWhiteSpace(sn) || string.Equals(sn.ToLower(), "serial number")) { throw new Exception("SN is invalid. Use WS Asset tag if you have no idea."); } var client = new GraphClient(new Uri("http://*****:*****@localhost:7474/db/data")); client.Connect(); Device newDevice = new Device { WSAsset = Asset.Text.Trim(), AssetTag = AssetTag.Text.Trim(), Manufacturer = Manufacturer.Text.Trim(), SN = SN.Text.Trim().ToUpper(), CPU = CPU.Text.Trim(), RAM = RAM.Text.Trim(), Disk = Disk.Text.Trim(), SystemDate = SystemDate.Text.Trim(), OS = OS.Text.Trim(), OSInstallDate = OSInstallDate.Text.Trim(), Model = Model.Text.Trim() }; // Don't create duplicate relationship! var result = client.Cypher .Match("(device:Device)") .Where("device.SN = {sn}") .WithParam("sn", newDevice.SN) .Return(device => device.As<Node<Device>>()); if (result.Results.Count() != 0) { throw new Exception("SN must be unique. Use WS Asset tag if you have no idea."); } if (!string.Equals(newDevice.WSAsset, "0")) { result = client.Cypher .Match("(device:Device)") .Where("device.WSAsset = {asset}") .WithParam("asset", newDevice.WSAsset) .Return(device => device.As<Node<Device>>()); if (result.Results.Count() != 0) { throw new Exception("Old WS asset must be unique."); } } if (!string.IsNullOrWhiteSpace(newDevice.AssetTag)) { result = client.Cypher .Match("(device:Device)") .Where("device.AssetTag = {asset}") .WithParam("asset", newDevice.AssetTag) .Return(device => device.As<Node<Device>>()); if (result.Results.Count() != 0) { throw new Exception("New WS asset must be unique."); } } // Create device if not exist client.Cypher .Merge("(device:Device { SN: {sn} })") .OnCreate() .Set("device = {newDevice}") .WithParams(new { sn = newDevice.SN, newDevice }) .ExecuteWithoutResults(); Owner newOwner = new WPFAssets.Owner { MSAlias = msAlias, WSAlias = wsAlias, Email = msAlias + "@ms;" + wsAlias + "@ws" }; // Create owner if not exist. client.Cypher .Merge("(owner:Owner { MSAlias: {msalias} })") .OnCreate() .Set("owner = {newOwner}") .WithParams(new { msalias = newOwner.MSAlias, newOwner }) .ExecuteWithoutResults(); // Relate owner and device. client.Cypher .Match("(device:Device)", "(owner:Owner)") .Where((Device device) => device.SN == newDevice.SN) .AndWhere((Owner owner) => owner.MSAlias == newOwner.MSAlias) .CreateUnique("device<-[:OWNS]-owner") .ExecuteWithoutResults(); Message.Text = newDevice.SN + " is owned by " + newOwner.MSAlias; SN.Text = newDevice.SN; User.Text = newOwner.MSAlias; } catch (Exception exception) { Message.Text = exception.Message; } Save.IsEnabled = true; }