/// <summary> /// IComparer Interface (based on Name/ID value) /// </summary> /// <param name="p1">Value 1</param> /// <param name="p2">Value 2</param> /// <returns>compareTo value</returns> public int Compare(NamePair p1, NamePair p2) { String s1 = p1 == null ? "" : (_sortByName ? p1.GetName() : p1.GetID()); String s2 = p2 == null ? "" : (_sortByName ? p2.GetName() : p2.GetID()); return(s1.CompareTo(s2)); // sort order ?? } // compare
/// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public NamePair GetValidOption(String key) { int index = NamePair.IndexOfKey(_options, key); if (index >= 0) { return((NamePair)_options[index]); } return(null); }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public String GetValue(String key) { NamePair option = GetOption(key); if (option == null) { return(null); } return(option.GetName()); }
/// <summary> ///Get Display for Value (not cached) /// </summary> /// <param name="value"></param> /// <returns></returns> public override String GetDisplay(Object value) { if (value == null || value == DBNull.Value) { return(""); } NamePair pp = Get(value); if (pp == null) { return("<" + value.ToString() + ">"); } return(pp.GetName()); }
/// <summary> /// /// </summary> /// <param name="option"></param> public void AddOption(NamePair option) { if (option != null) { if (NamePair.IndexOfKey(_options, option.GetID()) == -1) { _options.Add(option); } } else { throw new ArgumentException("option should not be null"); } }
/// <summary> /// Get Display value /// </summary> /// <param name="value"></param> /// <returns></returns> public override String GetDisplay(Object value) { if (value == null || value == DBNull.Value) { return(""); } // NamePair display = Get(value); if (display == null) { return("<" + value.ToString() + ">"); } return(display.ToString()); }
/// <summary> /// /// </summary> /// <param name="option"></param> public void PushExceptionOption(NamePair option) { if (_exception_options == null) { _exception_options = new List <NamePair>();// ArrayList(10); } if (option != null) { if (NamePair.IndexOfKey(_exception_options, option.GetID()) == -1) { _exception_options.Add(option); } } else { throw new ArgumentException("option should not be null"); } }
} // getData /// <summary> ///Return data as Array containing Value/KeyNamePair /// </summary> /// <param name="mandatory">if not mandatory, an additional empty value is inserted</param> /// <param name="onlyValidated">only validated</param> /// <param name="onlyActive">only active</param> /// <param name="temporary">force load for temporary display e.g. parent</param> /// <returns>list</returns> public override List <NamePair> GetData(bool mandatory, bool onlyValidated, bool onlyActive, bool temporary) { // create list List <NamePair> list = GetData(onlyValidated, temporary); // Remove inactive choices if (onlyActive && _hasInactive) { // list from the back for (int i = list.Count; i > 0; i--) { Object o = list[i - 1]; if (o != null) { String s = o.ToString(); if (s.StartsWith(INACTIVE_S) && s.EndsWith(INACTIVE_E)) { list.RemoveAt(i - 1); } } } } // Add Optional (empty) selection if (!mandatory) { NamePair p = null; if (_vInfo.keyColumn != null && _vInfo.keyColumn.EndsWith("_ID")) { p = new KeyNamePair(-1, ""); } else { p = new ValueNamePair(" ", ""); } list.Insert(0, p); } return(list); }
/// <summary> /// /// </summary> /// <param name="options"></param> /// <param name="c"></param> /// public static void ConcatNamePairArray(ArrayList options, ArrayList c) public static void ConcatNamePairArray(List <NamePair> options, List <NamePair> c) { if (c == null) { return; } if (options == null) { throw new ArgumentException("options cannot be null"); } if (options != null) { for (int i = 0; i < c.Count; i++) { if (NamePair.IndexOfKey(options, ((NamePair)c[i]).GetID()) == -1) { options.Add(c[i]); } } } }
/// <summary> ///Load from Cache if applicable /// Called from MLookup constructor /// </summary> /// <param name="info"></param> /// <param name="lookupTarget">Target Dictionary</param> /// <returns></returns> public static bool LoadFromCache(VLookUpInfo info, Dictionary <Object, NamePair> lookupTarget, out bool allLoaded, out bool hasInActive) { String key = GetKey(info); Dictionary <object, NamePair> cache = null; allLoaded = false; hasInActive = false; s_loadedLookups.TryGetValue(key, out cache); if (cache == null) { return(false); } // Nothing cached if (cache.Count == 0) { s_loadedLookups.Remove(key); return(false); } // we can use iterator, as the lookup loading is complete (i.e. no additional entries) IEnumerator <Object> iterator = cache.Keys.GetEnumerator(); while (iterator.MoveNext()) { Object cacheKey = iterator.Current; NamePair cacheData = cache[cacheKey]; lookupTarget.Add(cacheKey, cacheData); } if (s_loadedLookupsParam.ContainsKey(key)) { KeyValuePair <bool, bool> o = s_loadedLookupsParam[key]; allLoaded = o.Key; hasInActive = o.Value; } _log.Fine("#" + lookupTarget.Count); return(true); }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public NamePair GetOption(String key) { int index = NamePair.IndexOfKey(_options, key); if (index >= 0) { return((NamePair)_options[index]); } else { if (_exception_options == null) { return(null); } index = NamePair.IndexOfKey(_exception_options, key); if (index >= 0) { return((NamePair)_exception_options[index]); } } return(null); }
} // compareTo /// <summary> /// IComparable Interface (based on toString value) /// </summary> /// <param name="o"> o the Object to be compared.</param> /// <returns>a negative integer, zero, or a positive integer as this object /// is less than, equal to, or greater than the specified object. /// </returns> public int CompareTo(NamePair o) { return(Compare(this, o)); } // compareTo
/// <summary> /// Get Data Direct from Table. /// </summary> /// <param name="key">key</param> /// <param name="saveInCache">save in cache for r/w</param> /// <param name="cacheLocal">cache locally for r/o</param> /// <returns></returns> public override NamePair GetDirect(Object key, bool saveInCache, bool cacheLocal) { // Nothing to query if (key == null || _vInfo.queryDirect == null || _vInfo.queryDirect.Length == 0) { return(null); } if (key.Equals(_directNullKey)) { return(null); } // NamePair directValue = null; if (_lookupDirect != null) // Lookup cache { object o = null; _lookupDirect.TryGetValue(key, out o); if (o != null && o is NamePair) { return((NamePair)o); } } log.Finer(_vInfo.keyColumn + ": " + key + ", SaveInCache=" + saveInCache + ",Local=" + cacheLocal); bool isNumber = _vInfo.keyColumn.EndsWith("_ID"); System.Data.IDataReader dr = null; try { // SELECT Key, Value, Name FROM ... System.Data.SqlClient.SqlParameter[] param = new System.Data.SqlClient.SqlParameter[1]; if (isNumber) { param[0] = new System.Data.SqlClient.SqlParameter("@key", int.Parse(key.ToString())); } else { param[0] = new System.Data.SqlClient.SqlParameter("@key", key.ToString()); } dr = SqlExec.ExecuteQuery.ExecuteReader(_vInfo.queryDirect, param); if (dr.Read()) { String name = dr[2].ToString(); if (isNumber) { int keyValue = Utility.Util.GetValueOfInt(dr[0]); //object keyValue = dr[0]; KeyNamePair p = new KeyNamePair(keyValue, name); if (saveInCache) // save if { _lookup.Add(keyValue, p); } directValue = p; } else { String value = dr.GetString(1); ValueNamePair p = new ValueNamePair(value, name); if (saveInCache) // save if { _lookup.Add(value, p); } directValue = p; } if (dr.Read()) { log.Log(Level.SEVERE, _vInfo.keyColumn + ": Not unique (first returned) for " + key + " SQL=" + _vInfo.queryDirect); } } else { _directNullKey = key; directValue = null; } if (VLogMgt.IsLevelFinest()) { log.Finest(_vInfo.keyColumn + ": " + directValue + " - " + _vInfo); } dr.Close(); dr = null; } catch (Exception e) { log.Log(Level.SEVERE, _vInfo.keyColumn + ": SQL=" + _vInfo.queryDirect + "; Key=" + key, e); if (dr != null) { dr.Close(); dr = null; } directValue = null; } // Cache Local if not added to R/W cache if (cacheLocal && !saveInCache && directValue != null) { if (_lookupDirect == null) { _lookupDirect = new Dictionary <object, object>(); } _lookupDirect.Add(key, directValue); } _hasInactive = true; return(directValue); }
/// <summary> /// Returns the index of the first option found for the key, or -1 if not found. /// </summary> /// <param name="value"></param> /// <returns></returns> public int IndexOfValue(String value) { return(NamePair.IndexOfValue(_options, value)); }
/// <summary> /// Returns the index of the first option found for the key, or -1 if not found. /// </summary> /// <param name="key"></param> /// <returns></returns> public int IndexOfKey(String key) { return(NamePair.IndexOfKey(_options, key)); }