using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.OleDb;
using DatabaseExport.Properties;
namespace DatabaseExport
{
///
/// this is a helper class wrapping access to the iso13499 access db
/// it also now makes use of datapro database tables which mimic the access db
///
public class ISO13499FileDb
{
public class ExpiredISOFieldException : System.Exception
{
public ExpiredISOFieldException(string remark) : base(remark) { }
}
#region dictionaries
///
/// list of directions, populated when first loaded
///
private Dictionary _directionsDictionary = new Dictionary();
///
/// list of filter classes, populated when first loaded
///
private Dictionary _filterClassesDictionary = new Dictionary();
///
/// list of figures, populated when first loaded
///
private List _figures = new List();
///
/// list of all known fine 1 locations, populated when first loaded
///
private Dictionary _fineLoc1Dictionary = new Dictionary();
///
/// list of all known fine 2 locations, populated when first loaded
///
private Dictionary _fineLoc2Dictionary = new Dictionary();
///
/// list of all known fine 3 locations, populated when first loaded
///
private Dictionary _fineLoc3Dictionary = new Dictionary();
///
/// list of all known physical dimensions, populated when first loaded
///
private Dictionary _physicalDimensionsDictionary = new Dictionary();
///
/// list of all known positions, populated when first loaded
///
private Dictionary _positionsDictionary = new Dictionary();
///
/// list of all known possible channels, populated when first loaded
///
private List _possibleChannels = new List();
///
/// list of possible test objects, populated when first loaded
///
private Dictionary _testObjectsDictionary = new Dictionary();
///
/// list of all possible transducer locations, populated when first loaded
///
private Dictionary _transducerMainLoc = new Dictionary();
#endregion
///
/// connection string for the iso13499 database
/// you must also call refreshall to load data
///
public string ConnectionStr { get; set; } = "";
///
/// constructs an iso13499file db, you must still set the connection str
///
public ISO13499FileDb()
{
}
public static bool Is646bitProcess { get; } = (IntPtr.Size == 8);
private static ISO13499FileDb _isoDb;
public static ISO13499FileDb IsoDb
{
get
{
var log = new System.Diagnostics.EventLog { Source = "DataPROInstaller" };
if (null == _isoDb)
{
log.WriteEntry("_isoDb is null");
_isoDb = new ISO13499FileDb();
log.WriteEntry("Got a new ISO13499FileDb");
_isoDb.RefreshAllData();
log.WriteEntry("Back from RefreshAllData");
}
//log.WriteEntry("Returning from IsoDb get");
return _isoDb;
}
}
///
/// scans all possible channels for unique types, returns the list of unique types
///
private List _uniquePossibleChannelTypes = null;
public string[] GetUniquePossibleChannelTypes()
{
RefreshIfNeeded();
if (null == _uniquePossibleChannelTypes)
{
_uniquePossibleChannelTypes = new List();
var uniquetypes = (from pc in _possibleChannels where !pc.Expired orderby pc.Text_L1 select pc.Type).Distinct().ToArray();
if (null != uniquetypes && uniquetypes.Length > 0)
{
_uniquePossibleChannelTypes.AddRange(uniquetypes);
_uniquePossibleChannelTypes.Sort();
}
}
return _uniquePossibleChannelTypes.ToArray();
}
public string[] GetUniquePossibleChannelTypes(string typeToRemove)
{
return GetUniquePossibleChannelTypes().Where(uniquePossibleChannelType => !uniquePossibleChannelType.StartsWith(typeToRemove)).ToArray();
}
private void RefreshIfNeeded()
{
lock (RefreshLock)
{
if (!_bLoaded) { RefreshAllData(); }
}
}
///
/// gets a list of all possible channels given a type
///
///
///
public MMEPossibleChannels[] GetPossibleChannelsForType(string type)
{
RefreshIfNeeded();
//Don't include 2535, the Thoracic Compression Criterion channel which contains an expired main location (TCCR), but isn't itself marked as expired (FB 9891).
return (from pc in _possibleChannels.AsParallel() where pc.Type == type && pc.Id != 2535 && (pc.Direction != "R" || pc.Text_L1.ToLower().Contains("seat") || pc.Text_L1.ToLower().Contains("load")) && !pc.Expired && pc.Default_Filter_Class != "V" orderby pc.Text_L1 select pc).ToArray();
}
///
/// gets the possible channels just from DataPRO exclusive (so excluding ISO13499 origined channels)
///
///
public MMEPossibleChannels[] GetSQLPossibleChannels()
{
RefreshIfNeeded();
var list = (from pc in _possibleChannels.AsParallel() where pc.MMEChannelType == (int)MMEPossibleChannels.MMEChannelTypes.SQL select pc).ToArray();
if (null != list && list.Any())
{
return list.Select(li => new MMEPossibleChannels(li)).ToArray();
}
return new MMEPossibleChannels[0];
}
///
/// gets the "type" field from all possible channels where the channel test object matchines the input string in to
///
///
///
public string[] GetTestObjectTypeForTestObject(string to)
{
RefreshIfNeeded();
return (from pc in _possibleChannels.AsParallel() where pc.Test_Object == to orderby pc.Type select pc.Type).Distinct().ToArray();
}
///
/// dictionary of possible channels keyed by id, there's a separate dictionary for iso13499 origined channels
/// and DataPRO origined channels
///
private Dictionary _mmePossibleChannelsDict = null;
private Dictionary _mmePossibleChannelsDictOurs = null;
public MMEPossibleChannels GetPossibleChannel(long id, int channelType)
{
RefreshIfNeeded();
if (1 == channelType)
{
if (null == _mmePossibleChannelsDictOurs)
{
_mmePossibleChannelsDictOurs = new Dictionary();
foreach (var channel in _possibleChannels)
{
if (channel.MMEChannelType != channelType) { continue; }
if (!_mmePossibleChannelsDictOurs.ContainsKey(channel.Id)) { _mmePossibleChannelsDictOurs.Add(channel.Id, channel); }
}
}
return _mmePossibleChannelsDictOurs.ContainsKey(id) ? _mmePossibleChannelsDictOurs[id] : null;
}
if (null == _mmePossibleChannelsDict)
{
_mmePossibleChannelsDict = new Dictionary();
foreach (var channel in _possibleChannels)
{
if (channel.MMEChannelType != channelType) { continue; }
if (!_mmePossibleChannelsDict.ContainsKey(channel.Id)) { _mmePossibleChannelsDict.Add(channel.Id, channel); }
}
}
return _mmePossibleChannelsDict.ContainsKey(id) ? _mmePossibleChannelsDict[id] : null;
}
///
/// gets all possible test objects from test object tables
///
///
///
public MMETestObjects[] GetTestObjects(bool bIncludeExpired)
{
RefreshIfNeeded();
return bIncludeExpired ? _testObjectsDictionary.Values.ToArray() : (from to in _testObjectsDictionary.Values.ToArray().AsParallel() where !to.Expired select to).ToArray();
}
///
/// returns any test objects which match test object test object iso code.
/// returns null if not found
///
///
///
public MMETestObjects GetTestObjectByIso(string iso)
{
RefreshIfNeeded();
return _testObjectsDictionary.ContainsKey(iso) ? _testObjectsDictionary[iso] : null;
}
///
/// returns a position if any that has a matching iso code position field
/// returns null if not found
///
/// position code to look for
///
public MMEPositions GetPositionByISO(string key)
{
RefreshIfNeeded();
return _positionsDictionary.ContainsKey(key) ? _positionsDictionary[key] : null;
}
///
/// returns the first position with a guid which matches the requested guid
/// returns null if not found
///
/// guid to look for
///
public MMEPositions GetPosition(string GUID)
{
RefreshIfNeeded();
var positions = (from p in _positionsDictionary.Values.ToArray().AsParallel() where p.S_GUID == GUID select p);
if (null != positions && positions.Any()) { return positions.First(); }
return null;
}
///
/// gets all possible filter classes
///
///
public MMEFilterClasses[] GetFilterClasses()
{
RefreshIfNeeded();
return _filterClassesDictionary.Values.ToArray();
}
///
/// returns filter class matching the iso code filter class field
///
///
///
public MMEFilterClasses GetFilterClassByIso(string key)
{
RefreshIfNeeded();
return _filterClassesDictionary.ContainsKey(key) ? _filterClassesDictionary[key] : null;
}
///
/// gets all possible directions
///
///
public MMEDirections[] GetDirections()
{
RefreshIfNeeded();
return _directionsDictionary.Values.ToArray();
}
///
/// returns a direction given the isocode direction field
///
///
///
public MMEDirections GetDirectionByIso(string key)
{
RefreshIfNeeded();
return _directionsDictionary.ContainsKey(key) ? _directionsDictionary[key] : null;
}
///
/// returns all possible fine location 1
///
///
public MMEFineLocations1[] GetFineLocations1()
{
RefreshIfNeeded();
return _fineLoc1Dictionary.Values.ToArray();
}
///
/// returns a fine location 1, given a matching iso fine location field
///
///
///
public MMEFineLocations1 GetFineLocation1ByIso(string key)
{
RefreshIfNeeded();
return _fineLoc1Dictionary.ContainsKey(key) ? _fineLoc1Dictionary[key] : null;
}
///
/// returns all possible fine location 2
///
///
public MMEFineLocations2[] GetFineLocations2()
{
RefreshIfNeeded();
return _fineLoc2Dictionary.Values.ToArray();
}
///
/// returns a fine location 2 based on fine location 2 iso code field
///
///
///
public MMEFineLocations2 GetFineLocation2ByIso(string key)
{
RefreshIfNeeded();
return _fineLoc2Dictionary.ContainsKey(key) ? _fineLoc2Dictionary[key] : null;
}
///
/// gets all possible fine locations 3
///
///
public MMEFineLocations3[] GetFineLocations3()
{
RefreshIfNeeded();
return _fineLoc3Dictionary.Values.ToArray();
}
///
/// gets fine location 3 based on fine location 3 isocode field
///
///
///
public MMEFineLocations3 GetFineLocation3ByIso(string key)
{
RefreshIfNeeded();
if (_fineLoc3Dictionary.ContainsKey(key)) { return _fineLoc3Dictionary[key]; }
else { return null; }
}
///
/// gets all possible physical dimensions
///
///
public MMEPhysicalDimensions[] GetPhysicalDimensions()
{
RefreshIfNeeded();
return _physicalDimensionsDictionary.Values.ToArray();
}
///
/// gets physical dimension by isocode physical dimension field
///
///
///
public MMEPhysicalDimensions GetPhysicalDimensionByIso(string key)
{
RefreshIfNeeded();
return _physicalDimensionsDictionary.ContainsKey(key) ? _physicalDimensionsDictionary[key] : null;
}
public MMEPositions[] GetPositions()
{
RefreshIfNeeded();
return _positionsDictionary.Values.ToArray();
}
///
/// gets all possible main locations
///
///
public MMETransducerMainLocation[] GetMainLocations()
{
RefreshIfNeeded();
return _transducerMainLoc.Values.ToArray();
}
Dictionary _expiredMainLocations = new Dictionary();
///
/// gets a main location given an isocode main location field
///
///
///
public MMETransducerMainLocation GetMainLocationByIso(string key)
{
RefreshIfNeeded();
if (_transducerMainLoc.ContainsKey(key)) { return _transducerMainLoc[key]; }
if (_expiredMainLocations.ContainsKey(key)) { throw new Exception(/*ExpiredISOFieldException*/_expiredMainLocations[key].Remarks); }
return null;
}
private bool _bLoaded = false;
private static readonly object RefreshLock = new object();
///
/// loads all data from iso13499 and datapro databases
///
public void RefreshAllData()
{
var log = new System.Diagnostics.EventLog { Source = "DataPROInstaller" };
log.WriteEntry("In RefreshAllData");
lock (RefreshLock)
{
try
{
_mmePossibleChannelsDict = new Dictionary();
_mmePossibleChannelsDictOurs = new Dictionary();
_uniquePossibleChannelTypes = null;
_directionsDictionary.Clear();
_figures.Clear();
_filterClassesDictionary.Clear();
_fineLoc1Dictionary.Clear();
_fineLoc2Dictionary.Clear();
_fineLoc3Dictionary.Clear();
_physicalDimensionsDictionary.Clear();
_positionsDictionary.Clear();
_possibleChannels.Clear();
_testObjectsDictionary.Clear();
_transducerMainLoc.Clear();
_expiredMainLocations.Clear();
log.WriteEntry("Entering try");
try
{
foreach (var dir in MMEDirections.GetDirections()) { if (!_directionsDictionary.ContainsKey(dir.Direction)) { _directionsDictionary.Add(dir.Direction, dir); } }
_figures.AddRange(MMEFigures.GetFigures());
foreach (var fc in MMEFilterClasses.GetFilterClasses()) { if (!_filterClassesDictionary.ContainsKey(fc.Filter_Class)) { _filterClassesDictionary.Add(fc.Filter_Class, fc); } }
foreach (var loc in MMEFineLocations1.GetFineLocations1()) { if (!_fineLoc1Dictionary.ContainsKey(loc.Fine_Loc_1)) { _fineLoc1Dictionary.Add(loc.Fine_Loc_1, loc); } }
foreach (var loc in MMEFineLocations2.GetFineLocations2()) { if (!_fineLoc2Dictionary.ContainsKey(loc.FINE_LOC_2)) { _fineLoc2Dictionary.Add(loc.FINE_LOC_2, loc); } }
foreach (var loc in MMEFineLocations3.GetFineLocations3()) { if (!_fineLoc3Dictionary.ContainsKey(loc.FINE_LOC_3)) { _fineLoc3Dictionary.Add(loc.FINE_LOC_3, loc); } }
foreach (var pd in MMEPhysicalDimensions.GetPhysicalDimensions()) { if (!_physicalDimensionsDictionary.ContainsKey(pd.Physical_Dimension)) { _physicalDimensionsDictionary.Add(pd.Physical_Dimension, pd); } }
foreach (var pos in MMEPositions.GetPositions()) { if (!_positionsDictionary.ContainsKey(pos.Position)) { _positionsDictionary.Add(pos.Position, pos); } }
_possibleChannels.AddRange(MMEPossibleChannels.GetPossibleChannels());
log.WriteEntry("Getting TestObjects");
foreach (var to in MMETestObjects.GetTestObjects())
{
log.WriteEntry("Checking " + to.ToString());
if (!_testObjectsDictionary.ContainsKey(to.Test_Object))
{
log.WriteEntry("Adding " + to.ToString());
_testObjectsDictionary.Add(to.Test_Object, to);
}
}
log.WriteEntry("Done Getting TestObjects");
foreach (var mainloc in MMETransducerMainLocation.GetTransducerMainLocations())
{
if (mainloc.Expired)
{
if (!_expiredMainLocations.ContainsKey(mainloc.Trans_Main_Loc)) { _expiredMainLocations.Add(mainloc.Trans_Main_Loc, mainloc); }
}
else
{
if (!_transducerMainLoc.ContainsKey(mainloc.Trans_Main_Loc)) { _transducerMainLoc.Add(mainloc.Trans_Main_Loc, mainloc); }
}
}
foreach (var pc in _possibleChannels)
{
if (pc.MMEChannelType == (int)MMEPossibleChannels.MMEChannelTypes.SQL) { _mmePossibleChannelsDictOurs[pc.Id] = pc; }
else { _mmePossibleChannelsDict[pc.Id] = pc; }
}
}
catch (Exception ex)
{
log.WriteEntry("Exception before setting _bLoaded to True: " + ex.Message);
//DTS.Utilities.Logging.APILogger.Log("failure to load ISO tables", ex);
}
_bLoaded = true;
}
catch (Exception ex)
{
log.WriteEntry("Exception before setting _bLoaded to False: " + ex.Message);
_bLoaded = false;
//DTS.Utilities.Logging.APILogger.Log(ex);
}
}
}
}
}