Beispiel #1
0
/// <summary>
/// Clone UserObject
/// </summary>
/// <returns></returns>

        public UserObject Clone()
        {
            UserObject uo = (UserObject)MemberwiseClone();

            return(uo);
        }
Beispiel #2
0
        /// <summary>
        /// Deserialize UserObject XML into an actual UserObject.
        /// </summary>
        /// <param name="userObjectElement"></param>
        /// <returns></returns>
        public static UserObject Deserialize(
            XmlElement userObjectElement)
        {
            UserObject uo = new UserObject();

            if (userObjectElement == null)
            {
                return(uo);
            }
            if (userObjectElement.Name != "UserObject")
            {
                throw new Exception("UserObject.Deserialize - \"UserObject\" element not found");
            }

            foreach (XmlAttribute attribute in userObjectElement.Attributes)
            {
                switch (attribute.Name)
                {
                case "Id": uo.Id = int.Parse(attribute.Value);
                    break;

                case "Name": uo.Name = attribute.Value;
                    break;

                case "Owner": uo.Owner = attribute.Value;
                    break;

                case "Description": uo.Description = attribute.Value;
                    break;

                case "ParentFolder": uo.ParentFolder = attribute.Value;
                    break;

                case "ACL": uo.ACL = attribute.Value;
                    break;

                case "Count": uo.Count = int.Parse(attribute.Value);
                    break;

                case "ParentFolderType": uo.ParentFolderType = (FolderTypeEnum)Enum.Parse(typeof(FolderTypeEnum), attribute.Value);
                    break;

                case "Type": uo.Type = (UserObjectType)Enum.Parse(typeof(UserObjectType), attribute.Value);
                    break;

                case "AccessLevel": uo.AccessLevel = (UserObjectAccess)Enum.Parse(typeof(UserObjectAccess), attribute.Value);
                    break;

                case "CreationDateTime": uo.CreationDateTime = DateTimeUS.Parse(attribute.Value);
                    break;

                case "UpdateDateTime": uo.UpdateDateTime = DateTimeUS.Parse(attribute.Value);
                    break;

                default:
                    throw new Exception("UserObject.Deserialize - UserObject property \"" + attribute.Name + "\" not found");
                }
            }

            // Retrieve Content node
            XmlNode contentElement = userObjectElement.SelectSingleNode("Content");

            if (contentElement == null)
            {
                return(uo);
            }

            // May be multiple CDATA segments. Add these data to the Content Prperty.
            foreach (XmlNode node in contentElement.ChildNodes)
            {
                if (node is XmlCDataSection)
                {
                    uo.Content += node.Value;
                }
            }

            return(uo);
        }
Beispiel #3
0
        /// <summary>
        /// Deserialize
        /// </summary>
        /// <param name="serializedForm"></param>
        /// <returns></returns>

        public static UserObject Deserialize(
            string serializedForm)
        {
            string txt = null;

            UserObject uo = new UserObject();

            if (Lex.IsNullOrEmpty(serializedForm))
            {
                return(uo);
            }

            XmlMemoryStreamTextReader mstr = new XmlMemoryStreamTextReader(serializedForm);

            XmlTextReader tr = mstr.Reader;

            tr.Read();             // get CalcField element
            tr.MoveToContent();

            if (!Lex.Eq(tr.Name, "UserObject"))
            {
                throw new Exception("UserObject.Deserialize - \"UserObject\" element not found");
            }

            XmlUtil.GetIntAttribute(tr, "Id", ref uo.Id);
            if (XmlUtil.GetStringAttribute(tr, "Type", ref txt))
            {
                uo.Type = (UserObjectType)Enum.Parse(typeof(UserObjectType), txt);
            }
            XmlUtil.GetStringAttribute(tr, "Owner", ref uo.Owner);
            XmlUtil.GetStringAttribute(tr, "Name", ref uo.Name);
            XmlUtil.GetStringAttribute(tr, "Description", ref uo.Description);
            XmlUtil.GetStringAttribute(tr, "ParentFolder", ref uo.ParentFolder);
            if (XmlUtil.GetStringAttribute(tr, "ParentFolderType", ref txt))
            {
                uo.ParentFolderType = (FolderTypeEnum)Enum.Parse(typeof(FolderTypeEnum), txt);
            }
            if (XmlUtil.GetStringAttribute(tr, "AccessLevel", ref txt))
            {
                uo.AccessLevel = (UserObjectAccess)Enum.Parse(typeof(UserObjectAccess), txt);
            }
            XmlUtil.GetStringAttribute(tr, "ACL", ref uo.ACL);
            XmlUtil.GetIntAttribute(tr, "Count", ref uo.Count);
            if (XmlUtil.GetStringAttribute(tr, "CreationDateTime", ref txt))
            {
                uo.CreationDateTime = DateTimeUS.Parse(txt);
            }
            if (XmlUtil.GetStringAttribute(tr, "UpdateDateTime", ref txt))
            {
                uo.UpdateDateTime = DateTimeUS.Parse(txt);
            }

            tr.Read();             // get any content
            tr.MoveToContent();

            if (Lex.Eq(tr.Name, "Content"))
            {
                tr.Read();                 // get any CDATA content
                tr.MoveToContent();
                uo.Content = "";
                while (tr.NodeType == XmlNodeType.CDATA)                 // may be multiple CDATA segments
                {
                    uo.Content += tr.Value;
                    tr.Read();                     // more CDATA or content end tag
                    tr.MoveToContent();
                }
            }

            tr.Read();             // UserObject end tag
            tr.MoveToContent();

            mstr.Close();
            return(uo);
        }
Beispiel #4
0
        object IInvokeServiceOps.InvokeServiceOperation(int opCode, object[] args)
        {
            MobiusQueryEngineService op = (MobiusQueryEngineService)opCode;

            switch (op)
            {
            case MobiusQueryEngineService.Initialize:
            {
                Qel.QueryEngine.InitializeForSession();
                return(null);
            }

            case MobiusQueryEngineService.CreateInstance:
            {
                Qel.QueryEngine instance = new Mobius.QueryEngineLibrary.QueryEngine();
                return(instance.Id);
            }

            case MobiusQueryEngineService.DisposeInstance:
            {
                int  instanceId = (int)args[0];
                bool disposed   = Qel.QueryEngine.Dispose(instanceId);
                return(disposed);
            }

            case MobiusQueryEngineService.SetParameter:
            {
                string parm = "", value = "";

                if (args.Length == 2)
                {
                    parm  = args[0].ToString();
                    value = args[1].ToString();
                }

                else                                 // old form (remove when all old clients are updated)
                {
                    parm  = "DatabaseSubset";
                    value = args[0].ToString();
                }

                Qel.QueryEngine.SetParameter(parm, value);
                return(null);
            }

            case MobiusQueryEngineService.GetSummarizationDetailQuery:
            {
                int    instanceId     = (int)args[0];
                string metaTableName  = (string)args[1];
                string metaColumnName = (string)args[2];
                int    level          = (int)args[3];
                string resultId       = (string)args[4];

                //Qel.QueryEngine qe = null;
                //lock (Qel.QueryEngine.IdToInstanceDict)
                //{
                //	if (Qel.QueryEngine.IdToInstanceDict.ContainsKey(instanceId))
                //	{
                //		qe = Qel.QueryEngine.IdToInstanceDict[instanceId];
                //	}
                //}
                //if (qe != null)
                //{

                string     queryXml = "";
                Data.Query query    = QueryEngine.GetSummarizationDetailQuery(metaTableName, metaColumnName, level, resultId);
                if (query != null)
                {
                    queryXml = query.Serialize(false);
                }
                return(queryXml);

                //}
                //else
                //{
                //	throw new ArgumentException("Not a valid query engine instance id!");
                //}
            }

            case MobiusQueryEngineService.GetDrilldownDetailQuery:
            {
                int    instanceId     = (int)args[0];
                string metaTableName  = (string)args[1];
                string metaColumnName = (string)args[2];
                int    level          = (int)args[3];
                string resultId       = (string)args[4];

                //Qel.QueryEngine qe = null;
                //lock (Qel.QueryEngine.IdToInstanceDict)
                //{
                //	if (Qel.QueryEngine.IdToInstanceDict.ContainsKey(instanceId))
                //	{
                //		qe = Qel.QueryEngine.IdToInstanceDict[instanceId];
                //	}
                //}
                //if (qe != null)
                //{

                Mobius.Data.MetaTable  metaTable  = Mobius.Data.MetaTableCollection.GetExisting(metaTableName);
                Mobius.Data.MetaColumn metaColumn = metaTable.GetMetaColumnByName(metaColumnName);
                Data.Query             query      = QueryEngine.GetDrilldownDetailQuery(metaTable, metaColumn, level, resultId);
                string queryXml = query.Serialize(false);
                return(queryXml);

                //}
                //else
                //{
                //	throw new ArgumentException("Not a valid query engine instance id!");
                //}
            }

            case MobiusQueryEngineService.GetImage:
            {
                string mtMcName = null;

                int instanceId = (int)args[0];

                if (args[1] is string)                                 // New "mtName.mcName" format for arg (Post Client 5.0)
                {
                    mtMcName = (string)args[1];
                }

                else if (args[1] is MetaColumn)                                 // old Mobius.Services.Types.MetaColumn format
                {
                    MetaColumn mc = (MetaColumn)args[1];
                    mtMcName = mc.MetaTable.Name + "." + mc.Name;
                }

                else
                {
                    return(null);                                 // error
                }
                Mobius.Data.MetaColumn mobiusMC = Mobius.Data.MetaColumn.ParseMetaTableMetaColumnName(mtMcName);

                string graphicsIdString = (string)args[2];
                int    desiredWidth     = (int)args[3];

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);

                //Mobius.Data.MetaColumn mobiusMC = _transHelper.Convert<MetaColumn, Mobius.Data.MetaColumn>(metaColumn);
                System.Drawing.Bitmap bitmap = qe.GetImage(mobiusMC, graphicsIdString, desiredWidth);
                Types.Bitmap          result = new Types.Bitmap(bitmap);
                return(result);
            }

            case MobiusQueryEngineService.ResolveCidListReference:
            {
                string tok = (string)args[0];
                Mobius.Data.UserObject uo     = Qel.QueryEngine.ResolveCidListReference(tok);
                UserObjectNode         result = _transHelper.Convert <Mobius.Data.UserObject, UserObjectNode>(uo);
                return(result);
            }

            case MobiusQueryEngineService.GetRootTable:
            {
                string                queryString = (string)args[0];
                Mobius.Data.Query     mobiusQuery = Data.Query.Deserialize(queryString);
                Mobius.Data.MetaTable mobiusMT    = Qel.QueryEngine.GetRootTable(mobiusQuery);
                string                mtString    = null;
                if (mobiusMT != null)
                {
                    mtString = mobiusMT.Serialize();
                }
                return(mtString);
            }

            case MobiusQueryEngineService.DoPresearchChecksAndTransforms:
            {
                string     serializedQuery = (string)args[0];
                Data.Query q = Data.Query.Deserialize(serializedQuery);
                q = Qel.QueryEngine.DoPreSearchTransformations(q);
                if (q == null)
                {
                    return(null);
                }
                string serializedQuery2 = q.Serialize(true);
                return(serializedQuery2);
            }

            case MobiusQueryEngineService.GetKeys:
            {
                int             instanceId = (int)args[0];
                Qel.QueryEngine qe         = GetQueryEngineInstance(instanceId);
                List <string>   keys       = qe.GetKeys();
                return(keys);
            }

            case MobiusQueryEngineService.ExecuteQuery:
            {
                int             instanceId      = (int)args[0];
                string          serializedQuery = (string)args[1];
                Qel.QueryEngine qe     = GetQueryEngineInstance(instanceId);
                Data.Query      q      = Data.Query.Deserialize(serializedQuery);
                List <string>   result = qe.ExecuteQuery(q, false, false);
                return(result);
            }

            case MobiusQueryEngineService.TransformAndExecuteQuery:
            {
                int    instanceId      = (int)args[0];
                string serializedQuery = (string)args[1];

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);
                Data.Query      q2;
                Data.Query      q       = Data.Query.Deserialize(serializedQuery);
                List <string>   keyList = qe.TransformAndExecuteQuery(q, out q2);
                object[]        sa      = new object[2];
                sa[0] = keyList;
                if (q2 != null)                                 // any transformed query?
                {
                    sa[1] = q2.Serialize(true, true);           // serialize including metatables not previously send to client
                }
                return(sa);
            }

            case MobiusQueryEngineService.BuildSqlStatements:
            {
                int    instanceId      = (int)args[0];
                string serializedQuery = (string)args[1];

                Qel.QueryEngine qe            = GetQueryEngineInstance(instanceId);
                Data.Query      q             = Data.Query.Deserialize(serializedQuery);
                string          sqlStatements = qe.BuildSqlStatements(q);
                return(sqlStatements);
            }

            case MobiusQueryEngineService.SaveSpotfireSql:
            {
                string sqlStmtName     = (string)args[0];
                string serializedQuery = (string)args[1];

                Data.Query q      = Data.Query.Deserialize(serializedQuery);
                int        stmtId = Mobius.QueryEngineLibrary.QueryEngine.SaveSpotfireSql(sqlStmtName, q);
                return(stmtId);
            }

            case MobiusQueryEngineService.SaveSpotfireKeyList:
            {
                string keyColName, listType, keyList;
                if (args.Length >= 3)
                {
                    keyColName = (string)args[0];
                    listType   = (string)args[1];
                    keyList    = (string)args[2];
                }

                else if (args.Length == 2)                                 // old form
                {
                    keyColName = "CORP_ID";
                    listType   = (string)args[0];
                    keyList    = (string)args[1];
                }

                else                                 // old form for cid list
                {
                    keyColName = "CORP_ID";
                    listType   = "CIDLIST";
                    keyList    = (string)args[0];
                }

                string keyListName = Mobius.QueryEngineLibrary.QueryEngine.SaveSpotfireKeyList(keyColName, listType, keyList);
                return(keyListName);
            }

            case MobiusQueryEngineService.ReadSpotfireSql:
            {
                string sqlStmtName = args[0] as string;
                int    version     = (int)args[1];
                string sql         = Mobius.QueryEngineLibrary.QueryEngine.ReadSpotfireSql(sqlStmtName, version);
                return(sql);
            }

            case MobiusQueryEngineService.RemapTablesForRetrieval:                     // DoPreRetrievalTransformation
            {
                int instanceId = (int)args[0];

                Qel.QueryEngine   qe = GetQueryEngineInstance(instanceId);
                Mobius.Data.Query q  = qe.DoPreRetrievalTableExpansions();
                if (q == null)
                {
                    return(null);
                }

                string serializedQuery = q.Serialize(true);                                 // serialize including metatables not previously included
                return(serializedQuery);
            }

            case MobiusQueryEngineService.NextRowsSerialized:
            {
                int  minRows       = 1;
                bool returnQeStats = false;
                List <Mobius.Data.MetaBrokerStats> mbStats = null;

                int instanceId = (int)args[0];
                int ai         = 1;
                if (args.Length >= 4)                                 // newer version with minRows arg
                {
                    minRows = (int)args[ai++];
                }

                int maxRows = (int)args[ai++];
                int maxTime = (int)args[ai++];

                if (ai < args.Length)                                 // newer version with returnQeStats arg
                {
                    returnQeStats = (bool)args[ai++];
                }

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);

                if (returnQeStats)
                {
                    mbStats = new List <Mobius.Data.MetaBrokerStats>();
                }

                byte[] serializedRows = qe.NextRowsSerialized(minRows, maxRows, maxTime, mbStats);

                if (!returnQeStats)
                {
                    return(serializedRows);
                }

                else                                 // return serialized rows and stats in a two-element object array
                {
                    object[] oa = new object[2];
                    oa[0] = serializedRows;
                    oa[1] = Mobius.Data.MetaBrokerStats.SerializeList(mbStats);
                    return(oa);
                }
            }

            case MobiusQueryEngineService.NextRows:
            {
                int minRows = 1;

                int instanceId = (int)args[0];
                int ai         = 1;
                if (args.Length >= 4)                                 // newer version with minRows arg
                {
                    minRows = (int)args[ai++];
                }

                int maxRows = (int)args[ai++];
                int maxTime = (int)args[ai++];

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);

                List <object[]> rows = qe.NextRows(minRows, maxRows, maxTime);

                DataRow        dataRow  = null;
                List <DataRow> dataRows = new List <DataRow>();
                if (rows == null)
                {
                    return(dataRows);
                }

                for (int ri = 0; ri < rows.Count; ri++)                                 // convert each row
                {
                    object[] row = rows[ri];
                    if (row != null)
                    {
                        dataRow = new DataRow();
                        object[] convertedRow = new object[row.Length];
                        for (int i = 0; i < row.Length; i++)
                        {
                            convertedRow[i] = _transHelper.ConvertObject(row[i]);
                        }
                        dataRow.Data = convertedRow;
                    }

                    dataRows.Add(dataRow);
                }
                return(dataRows);
            }

            case MobiusQueryEngineService.NextRow:
            {
                int             instanceId = (int)args[0];
                Qel.QueryEngine qe         = GetQueryEngineInstance(instanceId);

                object[] row     = qe.NextRow();
                DataRow  dataRow = null;
                if (row != null)
                {
                    dataRow = new DataRow();
                    object[] convertedRow = new object[row.Length];
                    for (int i = 0; i < row.Length; i++)
                    {
                        convertedRow[i] = _transHelper.ConvertObject(row[i]);
                    }
                    dataRow.Data = convertedRow;
                }

                return(dataRow);
            }

            case MobiusQueryEngineService.Close:
            {
                int             instanceId = (int)args[0];
                Qel.QueryEngine qe         = GetQueryEngineInstance(instanceId);
                qe.Close();
                return(true);
            }

            case MobiusQueryEngineService.Cancel:
            {
                int             instanceId = (int)args[0];
                Qel.QueryEngine qe         = GetQueryEngineInstance(instanceId);
                qe.Cancel(false);
                return(true);
            }

            case MobiusQueryEngineService.GetQueryEngineState:
            {
                int instanceId = (int)args[0];

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);
                Mobius.Data.QueryEngineState state  = qe.State;
                QueryEngineState             result = _transHelper.Convert <Mobius.Data.QueryEngineState, QueryEngineState>(state);
                return(result);
            }

            case MobiusQueryEngineService.LogExceptionAndSerializedQuery:
            {
                string     exMsg           = (string)args[0];
                string     serializedQuery = (string)args[1];
                Data.Query query           = Data.Query.Deserialize(serializedQuery);
                return(Qel.QueryEngine.LogExceptionAndSerializedQuery(exMsg, query));
            }

            case MobiusQueryEngineService.LogQueryExecutionStatistics:
            {
                Qel.QueryEngine.LogQueryExecutionStatistics();
                return(null);
            }

            case MobiusQueryEngineService.ValidateCalculatedFieldExpression:
            {
                string advExpr = (string)args[0];
                return(Qel.QueryEngine.ValidateCalculatedFieldExpression(advExpr));
            }

            case MobiusQueryEngineService.CreateQueryFromMQL:
            {
                string            mql         = (string)args[0];
                Mobius.Data.Query mobiusQuery = Mobius.Data.MqlUtil.ConvertMqlToQuery(mql);

                Query result = _transHelper.Convert <Mobius.Data.Query, Query>(mobiusQuery);                                //translate the result
                return(result);
            }

            case MobiusQueryEngineService.ExecuteMQLQuery:
            {
                Mobius.Data.Query q2;

                int    instanceId = (int)args[0];
                string mql        = (string)args[1];

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);

                Mobius.Data.Query q      = Mobius.Data.MqlUtil.ConvertMqlToQuery(mql);
                List <string>     result = qe.TransformAndExecuteQuery(q, out q2);
                return(result);
            }

            case MobiusQueryEngineService.GetSelectAllDataQuery:
            {
                string mtName   = (string)args[0];
                string cn       = (string)args[1];
                string queryXml = "";

                Mobius.Data.Query q = QueryEngineLibrary.QueryEngine.GetSelectAllDataQuery(mtName, cn);
                if (q != null)
                {
                    queryXml = q.Serialize(false);
                }
                return(queryXml);
            }

            case MobiusQueryEngineService.GetStandardMobileQueries:
            {
                Data.Query[] mobileQueries = QueryEngineLibrary.QueryEngine.GetStandardMobileQueries();

                List <Query> queries = new List <Query>();

                foreach (Data.Query query in mobileQueries)
                {
                    Query typesQuery = _transHelper.Convert <Mobius.Data.Query, Query>(query);
                    queries.Add(typesQuery);
                }

                return(queries.ToArray());
            }

            case MobiusQueryEngineService.GetMobileQueriesByOwner:
            {
                string       user          = (string)args[0];
                Data.Query[] mobileQueries = QueryEngineLibrary.QueryEngine.GetMobileQueriesByOwner(user);

                List <Query> queries = new List <Query>();

                foreach (Data.Query query in mobileQueries)
                {
                    Query typesQuery = _transHelper.Convert <Mobius.Data.Query, Query>(query);
                    queries.Add(typesQuery);
                }

                return(queries.ToArray());
            }

            case MobiusQueryEngineService.GetAdditionalData:
            {
                int    instanceId = (int)args[0];
                string command    = (string)args[1];

                Qel.QueryEngine qe = GetQueryEngineInstance(instanceId);

                object o = qe.GetAdditionalData(command);
                return(o);
            }

            case MobiusQueryEngineService.ExportDataToSpotfireFiles:
            {
                int             instanceId = (int)args[0];
                Qel.QueryEngine qe         = GetQueryEngineInstance(instanceId);

                ExportParms ep = ExportParms.Deserialize(args[1] as string);

                QueryEngineStats qeStats = qe.ExportDataToSpotfireFiles(ep);

                return(qeStats != null ? qeStats.Serialize() : null);
            }

            case MobiusQueryEngineService.CompleteRowRetrieval:
            {
                int             instanceId = (int)args[0];
                Qel.QueryEngine qe         = GetQueryEngineInstance(instanceId);

                QueryEngineStats qeStats = qe.CompleteRowRetrieval();

                return(qeStats != null ? qeStats.Serialize() : null);
            }

            default:
                throw new InvalidOperationException("Unrecognized operation: " + (int)op);
            }
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }