//-----------------------------------------------------------------------
//
// Copyright (c) 2009 Deltares. All rights reserved.
//
// B. Faassen
// barry.faassen@deltares.nl
// 1-8-2012
// n.a.
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Serialization;
using Deltares.Standard;
using Deltares.Standard.Attributes;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.IO.Xml;
using ValidationResult = Deltares.Standard.Specifications.ValidationResult;
using Validator = Deltares.Standard.Specifications.Validator;
using XmlSerializer = Deltares.Standard.IO.Xml.XmlSerializer;
namespace Deltares.Dam.Data.Sensors
{
[Serializable]
public class Group
{
///
/// Holds a reference to the set of selected sensors
///
private readonly HashSet sensors;
private List pickSensors;
private int id;
///
/// Holds a reference to a set of relative loctions along the profile
///
private readonly IDictionary relativeLocationAlongProfileDictionary;
public Group()
{
sensors = new HashSet();
relativeLocationAlongProfileDictionary = new Dictionary();
ID = -1;
DataEventPublisher.AfterChange(this);
}
///
/// Gets or sets the ID.
///
///
/// The ID value should be unique.
///
[PropertyOrder(1, 1)]
public int ID
{
get { return id; }
set
{
id = value;
DataEventPublisher.AfterChange(this, g => g.ID);
}
}
///
/// Gets the selected sensors.
///
[XmlIgnore]
[Browsable(false)]
public IEnumerable Selection
{
get { return sensors; }
}
///
/// Gets or sets the selection as list for UI (table) purposes.
///
///
/// The selection as list.
///
[XmlIgnore]
[PropertyOrder(1, 2)]
public string SelectionAsString
{
get
{
string res = "";
foreach (var sensor in sensors)
{
res = res + sensor.ID + "; ";
}
return res;
}
set
{
ClearSelection();
var locSensors = ParseStringToSensorIDs(value);
if (pickSensors != null)
{
foreach (var sensorID in locSensors)
{
var sensor = PickSensors.Find(x => x.ID == sensorID);
if (sensor != null)
{
Add(sensor);
}
}
}
}
}
private List ParseStringToSensorIDs(string value)
{
value = value.Trim();
var ids = new List();
var idsarr = value.Split(new Char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in idsarr)
{
try
{
var val = Int32.Parse(s);
ids.Add(val);
}
catch (Exception)
{
// surpress errors, just do not use value
}
}
return ids;
}
///
/// Persistable sensor array. Only for internal use
///
[Browsable(false)]
public Sensor[] SensorArray
{
get { return sensors.ToArray(); }
set {
ClearSelection();
foreach (var sensor in value)
{
Add(sensor);
}
}
}
///
/// Gets the sensor count.
///
[Browsable(false)]
public int SensorCount
{
get { return sensors.Count; }
}
[XmlIgnore]
[Browsable(false)]
public IEnumerable> SensorRelativeLocations
{
get { return relativeLocationAlongProfileDictionary; }
}
[XmlIgnore]
[ReadOnly(true)]
public List PickSensors
{
get { return pickSensors; }
set { pickSensors = value; }
}
///
/// Determines whether this instance is valid.
///
///
public bool IsValid()
{
IEnumerable validationResults = Validator.Validate(this);
return !validationResults.Any();
}
///
/// Determines whether this instance is transient (associated with an invalid ID).
///
///
/// true if this instance is transient; otherwise, false.
///
public bool IsTransient()
{
return ID <= 0;
}
///
/// Sets the relative location.
///
/// The sensor.
/// The value.
public void SetRelativeLocation(Sensor sensor, double value)
{
relativeLocationAlongProfileDictionary[sensor] = value;
}
///
/// Adds the specified sensor to the selected sensor list for this group.
///
/// The sensor to add.
public void Add(Sensor sensor)
{
sensors.Add(sensor);
if (!relativeLocationAlongProfileDictionary.ContainsKey(sensor))
relativeLocationAlongProfileDictionary.Add(sensor, 0);
}
///
/// Removes the specified sensor from the selected sensor list from this group.
///
/// The sensor to remove.
public void Remove(Sensor sensor)
{
sensors.Remove(sensor);
if (relativeLocationAlongProfileDictionary.ContainsKey(sensor))
relativeLocationAlongProfileDictionary.Remove(sensor);
}
///
/// Clears the sensor selection
///
public void ClearSelection()
{
sensors.Clear();
relativeLocationAlongProfileDictionary.Clear();
}
public string Serialize()
{
return new XmlSerializer().SerializeToString(this);
}
public static Group Deserialize(string xml)
{
return (Group)new XmlDeserializer().XmlDeserializeFromString(xml, typeof(Group));
}
public override string ToString()
{
return ID.ToString();
}
}
}