Пример #1
0
/// <summary>
/// Normalize compound number for database searching/storage
/// </summary>
/// <param name="cid"></param>
/// <param name="mt"></param>
/// <returns></returns>

        public static string NormalizeForDatabase(
            string unnormalizedCid,
            MetaTable mt)
        {
            RootTable rti = null;
            MetaTable rootMt;
            string    cid, prefix = "", suffix = "", remainder = "", nTxt = "", fmt, nCid;
            bool      dashBeforeNumber = false, canFormat;
            int       number = -1;

//			if (cid.Contains("91341")) cid = cid; // debug
//			if (mt == null) mt = mt; // debug

            cid = NormalizeCase(unnormalizedCid, mt);
            if (String.IsNullOrEmpty(cid))
            {
                return(cid);
            }

            if (cid.Contains(" "))
            {
                cid = cid.Replace(" ", "_");                                // replace any spaces with underscores
            }
            if (cid.Contains("\n"))
            {
                cid = cid.Replace("\n", "");                                 // remove newlines
            }
            if (cid.Contains("\r"))
            {
                cid = cid.Replace("\r", "");                                 // remove returns
            }
            if (mt != null && !RootTable.IsStructureDatabaseRootTable(mt.Root))
            {
                return(cid);
            }

            if (RootTable.IsMultiplePrefixTable(mt))
            {
                return(cid);                // no formatting just return as is
            }
            if (mt != null && UserObject.IsUserDatabaseStructureTableName(mt.Root.Name))
            {                            // user database
                prefix = "userdatabase"; // used to lookup format
                if (Lex.IsInteger(cid))
                {
                    number = Int32.Parse(cid);
                }
                else
                {
                    suffix = cid;
                }
            }

            else             // other database
            {
                if (mt != null && RootTable.GetFromTableName(mt.Root.Name) == null)
                {
                    return(cid);                    // if not a recognized database return cid as is
                }
                AnalyzeCid(cid, mt, out prefix, out dashBeforeNumber, out number, out suffix, out remainder, out rti, out rootMt, out canFormat);
                if (!canFormat)
                {
                    return(cid);
                }

                string mtPrefix = "";
                if (mt != null)                 // get any prefix for metatable
                {
                    mtPrefix = GetPrefixFromRootTable(mt);
                }

                if (mtPrefix.EndsWith("-") && prefix != "" && !prefix.EndsWith("-"))
                {                 // adjust to allow prefix match
                    prefix          += "-";
                    dashBeforeNumber = false;
                }

                if (prefix == "")                 // if no prefix in number then add any metatable prefix
                {
                    if (mtPrefix != "")
                    {
                        prefix = mtPrefix;
                    }
                    else
                    {
                        prefix = "none";
                    }
                }

                else if (mt != null && !Lex.Eq(prefix, mtPrefix))
                {
                    return(null);                    // if prefix invalid for database return null
                }
            }

            rti = RootTable.GetFromPrefix(prefix);
            if (rti == null && prefix == "userdatabase")
            {
                fmt    = "{0,8:00000000}";              // default format for user databases
                prefix = "";
            }

            else if (rti == null || Lex.IsNullOrEmpty(rti.InternalNumberFormat))
            {                // unknown format
                fmt = "{0}"; // default format
                if (rti != null && !rti.PrefixIsStored)
                {
                    prefix = "";                                                     // database doesn't contain prefix
                }
            }

            else             // use supplied format
            {
                fmt = "{0," + rti.InternalNumberFormat + "}";
                if (!rti.PrefixIsStored)
                {
                    prefix = "";                                      // database doesn't contain prefix
                }
            }

            if (number < 0)
            {
                nTxt = "";
            }
            else
            {
                try { nTxt = String.Format(fmt, number); }
                catch (Exception ex) { nTxt = number.ToString(); }
            }

            //if (Lex.StartsWith(cid, "GGO-")) nTxt = "-" + nTxt; // hack for GeneGo database, fix later

            if (prefix == "none")
            {
                prefix = "";
            }

            AddDashIfAppropriate(ref prefix, dashBeforeNumber, rti);

            nCid = (prefix + nTxt + suffix).ToUpper();
            if (nCid.Contains(" "))
            {
                nCid = nCid.Replace(" ", "_");                                 // replace any internal spaces with underscores
            }
            return(nCid);
        }
Пример #2
0
/// <summary>
/// Convert a compound number from internal to external format
/// </summary>
/// <param name="cid"></param>
/// <param name="mt"></param>
/// <returns></returns>

        public static string Format(
            string normalizedCid,
            MetaTable mt)
        {
            string    prefix = "", suffix = "", remainder = "", nTxt, fmt, fCid;
            bool      dashBeforeNumber, canFormat;
            RootTable rti = null, rtiBase;
            MetaTable rootMt;
            int       number = -1;

            //if (mt == null) mt = mt; // debug

            string cid = normalizedCid;

            if (String.IsNullOrEmpty(cid))
            {
                return(cid);
            }

            if (mt != null && !RootTable.IsStructureDatabaseRootTable(mt.Root))             // non structure root table
            {
                if (mt.Root.KeyMetaColumn.IsNumeric && Lex.IsInteger(cid))
                {                 // if numeric then return as simple integer with leading zeros removed
                    number = int.Parse(cid);
                    fCid   = number.ToString();
                    return(fCid);
                }

                else
                {
                    return(cid);                 // just return as is
                }
            }

            cid = Lex.RemoveAllQuotes(cid.Trim()).Trim();             // clean up
            if (cid.Length == 0)
            {
                return("");                           // something really there
            }
            if (mt != null && UserObject.IsUserDatabaseStructureTableName(mt.Root.Name))
            {                           // user database
                if (Lex.IsInteger(cid)) // if number format with no leading zeros
                {
                    return(Int32.Parse(cid).ToString());
                }
                else
                {
                    return(cid);                 // otherwise return as is
                }
            }

            if (RootTable.IsMultiplePrefixTable(mt))
            {
                return(cid);                // no formatting just return as is
            }
            AnalyzeCid(cid, mt, out prefix, out dashBeforeNumber, out number, out suffix, out remainder, out rti, out rootMt, out canFormat);
            if (!canFormat)
            {
                return(cid);
            }

            if (prefix == "")
            {
                prefix = "none";
            }

            rti = RootTable.GetFromPrefix(prefix);

            if (rti == null || Lex.IsNullOrEmpty(rti.ExternalNumberFormat))
            {
                fmt = "{0}";                 // unknown prefix
            }
            else
            {
                fmt = "{0," + rti.ExternalNumberFormat + "}";
            }

            if (prefix == "none")
            {
                prefix = "";
            }

            if (number < 0)
            {
                nTxt = "";
            }
            else
            {
                try { nTxt = String.Format(fmt, number); }
                catch (Exception ex) { nTxt = number.ToString(); }
            }

            AddDashIfAppropriate(ref prefix, dashBeforeNumber, rti);

            fCid = prefix + nTxt + suffix;

            if (fCid == "")
            {
                return(cid);
            }
            return(fCid);
        }
Пример #3
0
/// <summary>
/// Convert a compound number from external to internal format
/// normalizing and adding a prefix as needed.
/// A false prefix is added for compound collections that don't normally include
/// a prefix or contain a overlapping prefix to keep compoundIds associated
/// with the proper compound collection.
/// This routine handles several cases:
/// 1 - If mt defined & root is not a compound table then just return key as is
/// 2 - If mt defined & root is a UCDB then normalize to fixed length if numeric otherwise return as is
/// 3 - If mt defined & compound table root & no prefix then get any default prefix
/// 4 - If string cid value then add any prefix & return as is
/// 5 - if numeric cid (stored as integer or string) then add any prefix & format numeric part
/// </summary>
/// <param name="cid"></param>
/// <param name="mt"></param>
/// <returns></returns>

        public static string Normalize(
            string unnormalizedCid,
            MetaTable mt)
        {
            RootTable rti = null;
            MetaTable rootMt;
            string    prefix = "", suffix = "", remainder = "", nTxt = "", fmt, nCid;
            bool      dashBeforeNumber, canFormat;
            int       number = -1;

            if (mt == null)
            {
                mt = mt;                         // debug
            }
            string cid = NormalizeCase(unnormalizedCid, mt);

            if (String.IsNullOrEmpty(cid))
            {
                return(cid);
            }

            if (cid.IndexOf(",") >= 0)             // if comma in list then just use chars up to comma
            {
                cid = cid.Substring(0, cid.IndexOf(","));
            }

            AnalyzeCid(cid, mt, out prefix, out dashBeforeNumber, out number, out suffix, out remainder, out rti, out rootMt, out canFormat);
            if (!canFormat)
            {
                return(cid);
            }

            //if (!CanFormat(cid, prefix

            //if (!String.IsNullOrEmpty(remainder)) // multiple tokens, just return as is
            //  return cid; // (for a cid like "2." the wrong value is returned, i.e. 2.

//			if (Lex.Eq(prefix, "MDDR") && number == 91341) prefix = prefix; // debug

            if (mt != null)                                           // know the metatable this should belong to
            {                                                         // note: for multi-root table search the root table may not correspond to the compoundId prefix & value
                if (!RootTable.IsStructureDatabaseRootTable(mt.Root)) // non structure root table?
                {
                    if (mt.Root.KeyMetaColumn.IsNumeric && Lex.IsInteger(cid))
                    {                     // if numeric then default internal format is length 10 with leading zeros
                        number = int.Parse(cid);
                        nCid   = string.Format("{0:0000000000}", number);
                        return(nCid);
                    }

                    else
                    {
                        return(cid);                     // just return as is
                    }
                }

                else if (UserObject.IsUserDatabaseStructureTableName(mt.Root.Name))
                {
                    return(NormalizeForDatabase(cid, mt)); // same as for database if user database
                }
                else                                       // Normal case, non-userDatabase structure root table
                {
                    rti = RootTable.GetFromTableName(mt.Root.Name);
                    if (prefix == "")
                    {
                        prefix = GetPrefixFromRootTable(mt);
                    }

                    if (rti != null && rti.IsStringType && String.IsNullOrEmpty(rti.InternalNumberFormat))
                    {                     // general unformatted string, just add any required prefix & return
                        if (!String.IsNullOrEmpty(rti.Prefix) && !Lex.StartsWith(cid, rti.Prefix))
                        {
                            nCid = rti.Prefix + cid;                             // add prefix if needed
                        }
                        else
                        {
                            nCid = cid;                          // just return as is
                        }
                        return(nCid);
                    }
                }
            }

            else             // metatable not defined, try to determine from prefix
            {
                if (!String.IsNullOrEmpty(prefix))
                {
                    rti = RootTable.GetFromPrefix(prefix);
                }

                else if (Lex.IsInteger(cid))                 // if integer cid then use default integer cid database
                {
                    rti = RootTable.GetFromPrefix("");
                }
            }

            if (rti == null || Lex.IsNullOrEmpty(rti.InternalNumberFormat))
            {
                fmt = "{0}";                 // unknown prefix
            }
            else
            {
                fmt = "{0," + rti.InternalNumberFormat + "}";
            }

            if (prefix == "")
            {
                suffix = "";                 // if no prefix, disallow suffix to avoid invalid numbers for numeric keys (possibly enhance later)
            }
            if (number < 0)
            {
                nTxt = "";                       // no number
            }
            else
            {
                try { nTxt = String.Format(fmt, number); }
                catch (Exception ex) { nTxt = number.ToString(); }
            }

            AddDashIfAppropriate(ref prefix, dashBeforeNumber, rti);

            nCid = prefix + nTxt + suffix;
            if (nCid == "")
            {
                nCid = cid.Trim();                         // if nothing then return original input
            }
            return(nCid);
        }