Beispiel #1
0
        /// <summary>
        /// Finds an existing item by IFC identifier
        /// </summary>
        /// <param name="baseurl"></param>
        /// <param name="sessionid"></param>
        /// <param name="identifier"></param>
        /// <returns></returns>
        private IfdConcept SearchConcept(string identifier, IfdConceptTypeEnum type)
        {
            try
            {
                string         url     = this.m_uri + "api/4.0/IfdConcept/search/filter/language/" + LanguageID + "/type/" + type.ToString() + "/" + identifier;
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method        = "GET";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = 0;
                request.Accept        = "application/json";
                request.Headers.Add("cookie", "peregrineapisessionid=" + this.m_session);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream          stream   = response.GetResponseStream();

                DataContractJsonSerializer ser    = new DataContractJsonSerializer(typeof(ResponseSearch));
                ResponseSearch             search = (ResponseSearch)ser.ReadObject(stream);
                if (search != null && search.IfdConcept != null && search.IfdConcept.Length > 0)
                {
                    return(search.IfdConcept[search.IfdConcept.Length - 1]); // use last one
                }
            }
            catch (Exception xx)
            {
                System.Diagnostics.Debug.WriteLine(xx.Message);
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Writes a data type and all public instance properties.
        /// Such type may correspond to an IFC entity or a ConceptRoot of a model view definition (which may also correspond to a property set)
        /// The type is nested according to namespace tokens.
        /// The type is linked to superclass according to base type.
        /// The type is expanded into subclasses if the last property is an enumeration for a classification (e.g. PredefinedType).
        /// Any referenced types are also retrieved and written if they don't yet exist.
        /// Localized names and descriptions are extracted from .NET resources.
        /// </summary>
        /// <param name="type"></param>
        public void WriteType(Type type)
        {
            if (type == null)
            {
                return;
            }

            if (m_mapTypes.ContainsKey(type))
            {
                return; // already written
            }
            string name = type.Name;
            DisplayNameAttribute attrName = (DisplayNameAttribute)type.GetCustomAttribute <DisplayNameAttribute>();

            if (attrName != null)
            {
                name = attrName.DisplayName;
            }

            string desc = null;
            DescriptionAttribute attrDesc = (DescriptionAttribute)type.GetCustomAttribute <DescriptionAttribute>();

            if (attrDesc != null)
            {
                desc = attrDesc.Description;
            }

            IfdConceptTypeEnum      conctype = IfdConceptTypeEnum.SUBJECT;
            IfdRelationshipTypeEnum relbase  = IfdRelationshipTypeEnum.SPECIALIZES;

            if (type.Name.StartsWith("Pset") || type.Name.StartsWith("Qto"))
            {
                // hack
                conctype = IfdConceptTypeEnum.BAG;
                relbase  = IfdRelationshipTypeEnum.ASSIGNS_COLLECTIONS;
            }
            else if (type.IsValueType || type.IsEnum)
            {
                conctype = IfdConceptTypeEnum.MEASURE;
            }

            // retrieve existing -- enable once final uploaded correctly!
#if false
            IfdConcept conc = SearchConcept(type.Name, conctype);
            if (conc != null)
            {
                this.m_mapTypes.Add(type, conc.guid);
                return;
            }
#endif

            DisplayAttribute[] localize = (DisplayAttribute[])type.GetCustomAttributes(typeof(DisplayAttribute), false);
            IfdBase            ifdThis  = CreateConcept(type.Name, name, desc, conctype, localize);
            if (ifdThis == null)
            {
                return;
            }

            this.m_mapTypes.Add(type, ifdThis.guid);

            // get namespace
            string[] namespaces       = type.Namespace.Split('.');
            string   guidSchemaParent = null;
            string   guidSchemaChild  = null;
            for (int iNS = 0; iNS < namespaces.Length; iNS++)
            {
                string ns = namespaces[iNS];
                if (!this.m_mapNamespaces.TryGetValue(ns, out guidSchemaChild))
                {
                    StringBuilder sbQual = new StringBuilder();
                    for (int x = 0; x <= iNS; x++)
                    {
                        if (x > 0)
                        {
                            sbQual.Append(".");
                        }
                        sbQual.Append(namespaces[x]);
                    }
                    string qualname = sbQual.ToString();

                    // call server to find namespace
                    IfdConcept ifdNamespace = SearchConcept(qualname, IfdConceptTypeEnum.BAG);
                    if (ifdNamespace != null)
                    {
                        guidSchemaChild = ifdNamespace.guid;
                    }
                    else
                    {
                        IfdBase ifdNS = CreateConcept(qualname, ns, String.Empty, IfdConceptTypeEnum.BAG, null);
                        guidSchemaChild = ifdNS.guid;
                    }

                    this.m_mapNamespaces.Add(ns, guidSchemaChild);

                    if (guidSchemaParent != null)
                    {
                        CreateRelationship(guidSchemaParent, guidSchemaChild, IfdRelationshipTypeEnum.COLLECTS);
                    }
                }

                if (iNS == namespaces.Length - 1)
                {
                    CreateRelationship(guidSchemaChild, ifdThis.guid, IfdRelationshipTypeEnum.COLLECTS);
                }

                guidSchemaParent = guidSchemaChild;
            }

            // get base type
            if (type.IsClass && type.BaseType != typeof(object))
            {
                WriteType(type.BaseType);

                string guidbase = null;
                if (m_mapTypes.TryGetValue(type.BaseType, out guidbase))
                {
                    CreateRelationship(guidbase, ifdThis.guid, relbase);
                }
            }

            //PropertyInfo[] props = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            //foreach (PropertyInfo prop in props)
            FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            foreach (FieldInfo prop in fields)
            {
                // write the property itself

                // resolve property type
                Type typeProp = prop.FieldType;
                if (typeProp.IsGenericType && typeProp.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    typeProp = typeProp.GetGenericArguments()[0];
                }

                if (typeProp.IsGenericType && typeProp.GetGenericTypeDefinition() == typeof(IList <>))
                {
                    typeProp = typeProp.GetGenericArguments()[0];
                }

                if (typeProp.IsGenericType && typeProp.GetGenericTypeDefinition() == typeof(ISet <>))
                {
                    typeProp = typeProp.GetGenericArguments()[0];
                }

                if (typeProp.IsArray)
                {
                    typeProp = typeProp.GetElementType();
                }

                // special case for specialization
                if (prop.Name.Equals("_PredefinedType"))
                {
                    FieldInfo[] enumvals = typeProp.GetFields(BindingFlags.Public | BindingFlags.Static);
                    foreach (FieldInfo f in enumvals)
                    {
                        if (f.Name != "USERDEFINED" && f.Name != "NOTDEFINED")
                        {
                            string fname = f.Name;
                            DisplayNameAttribute attrFName = (DisplayNameAttribute)f.GetCustomAttribute <DisplayNameAttribute>();
                            if (attrName != null)
                            {
                                fname = attrName.DisplayName;
                            }

                            string fdesc = null;
                            DescriptionAttribute attrFDesc = (DescriptionAttribute)f.GetCustomAttribute <DescriptionAttribute>();
                            if (attrDesc != null)
                            {
                                fdesc = attrDesc.Description;
                            }

                            DisplayAttribute[] localizePredef = (DisplayAttribute[])f.GetCustomAttributes(typeof(DisplayAttribute), false);
                            IfdBase            ifdPredef      = CreateConcept(type.Name + "." + f.Name, fname, fdesc, IfdConceptTypeEnum.SUBJECT, localizePredef);
                            if (ifdPredef != null)
                            {
                                CreateRelationship(ifdThis.guid, ifdPredef.guid, IfdRelationshipTypeEnum.SPECIALIZES);
                            }
                        }
                    }
                }
                else if (conctype == IfdConceptTypeEnum.BAG)// psetprop.FieldType.IsValueType) //!!!
                {
                    name     = type.Name;
                    attrName = (DisplayNameAttribute)prop.GetCustomAttribute <DisplayNameAttribute>();
                    if (attrName != null)
                    {
                        name = attrName.DisplayName;
                    }

                    desc     = null;
                    attrDesc = (DescriptionAttribute)prop.GetCustomAttribute <DescriptionAttribute>();
                    if (attrDesc != null)
                    {
                        desc = attrDesc.Description;
                    }

                    string  propidentifier = type.Name + "." + prop.Name.Substring(1);
                    IfdBase ifdProp        = CreateConcept(propidentifier, name, desc, IfdConceptTypeEnum.PROPERTY, null);
                    if (ifdProp == null)
                    {
                        return;
                    }

                    // include the property
                    CreateRelationship(ifdThis.guid, ifdProp.guid, IfdRelationshipTypeEnum.COLLECTS);

                    WriteType(typeProp);

                    if (typeProp.IsValueType)
                    {
                        string guidDataType = null;
                        if (m_mapTypes.TryGetValue(typeProp, out guidDataType))
                        {
                            CreateRelationship(ifdProp.guid, guidDataType, IfdRelationshipTypeEnum.ASSIGNS_MEASURES);//...verify
                        }
                    }
                }
            }
        }
Beispiel #3
0
        private IfdBase CreateConcept(string identifier, string name, string desc, IfdConceptTypeEnum conctype, DisplayAttribute[] localize)
        {
            System.Diagnostics.Debug.WriteLine(DateTime.Now + " " + conctype.ToString() + " " + identifier + " START");

            List <IfdName> listNames = new List <IfdName>();
            List <IfdBase> listDescs = new List <IfdBase>();

            // create name for IFC
            IfdName ifdNameIFC = CreateName(LanguageID, identifier);

            if (ifdNameIFC == null)
            {
                return(null);
            }

            listNames.Add(ifdNameIFC);

            // localization
            bool hasEnglishName = false;
            bool hasEnglishDesc = false;

            if (localize != null)
            {
                foreach (DisplayAttribute docLoc in localize)
                {
                    string locale = docLoc.ShortName;

                    // look up language id
                    string langid = GetLanguageId(locale);
                    if (langid != null)
                    {
                        if (!String.IsNullOrEmpty(docLoc.Name))
                        {
                            string  locname = HttpUtility.UrlEncode(docLoc.Name);
                            IfdName ifdName = CreateName(langid, locname);
                            if (ifdName == null)
                            {
                                return(null);
                            }

                            listNames.Add(ifdName);

                            if (langid == LanguageEN)
                            {
                                hasEnglishName = true;
                            }
                        }

                        if (!String.IsNullOrEmpty(docLoc.Description))
                        {
                            string  locdesc = HttpUtility.UrlEncode(docLoc.Description);
                            IfdBase ifdDesc = CreateDescription(langid, locdesc);
                            if (ifdDesc == null)
                            {
                                return(null);
                            }

                            listDescs.Add(ifdDesc);

                            if (langid == LanguageEN)
                            {
                                hasEnglishDesc = true;
                            }
                        }
                    }
                }
            }

            if (!hasEnglishName && !String.IsNullOrEmpty(name))
            {
                // add default english name
                IfdName ifdNameEN = CreateName(LanguageEN, name);
                if (ifdNameEN == null)
                {
                    return(null);
                }

                listNames.Add(ifdNameEN);
            }

            if (!hasEnglishDesc && !String.IsNullOrEmpty(desc))
            {
                if (desc.Length > 8192)
                {
                    desc = "!BSDD DATA TRANSFER ERROR -- description too large to encode within URL";
                }

                string  encodedesc = HttpUtility.UrlEncode(desc);
                IfdBase ifdDescEN  = CreateDescription(LanguageEN, encodedesc);
                if (ifdDescEN == null)
                {
                    return(null);
                }

                listDescs.Add(ifdDescEN);
            }

            // create concept
            StringBuilder sb = new StringBuilder();

            sb.Append(this.m_uri);
            sb.Append("api/4.0/IfdConcept?fullNameGuids=");
            for (int i = 0; i < listNames.Count; i++)
            {
                if (i != 0)
                {
                    sb.Append(",");
                }
                sb.Append(listNames[i].guid);
            }
            sb.Append("&conceptType=");
            sb.Append(conctype.ToString());

            if (listDescs.Count > 0)
            {
                sb.Append("&definitionGuids=");
                for (int i = 0; i < listDescs.Count; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(listDescs[i].guid);
                }
            }

            string url = sb.ToString();

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = 0;
            request.Accept        = "application/json";
            request.Headers.Add("cookie", "peregrineapisessionid=" + this.m_session);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream          stream   = response.GetResponseStream();

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(IfdBase));
            IfdBase ifdRoot = (IfdBase)ser.ReadObject(stream);

            System.Diagnostics.Debug.WriteLine(DateTime.Now + " " + conctype.ToString() + " " + identifier + " FINISH");

            return(ifdRoot);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="baseurl">URL of server</param>
        /// <param name="sessionid">identifies server session</param>
        /// <param name="docObject">object to be published</param>
        /// <param name="identifier">identifier to use for IFC name</param>
        /// <param name="name">identifier to use for English name</param>
        /// <param name="type">identifier to use for IFC property type encoding, which is stored within description of IFC language</param>
        /// <param name="conctype">type of concept</param>
        /// <returns>guid of new concept</returns>
        public static IfdBase CreateConcept(string baseurl, string sessionid, DocObject docObject, string identifier, string name, string type, IfdConceptTypeEnum conctype)
        {
            System.Diagnostics.Debug.WriteLine(DateTime.Now + " " + conctype.ToString() + " " + identifier + " START");

            List <IfdName> listNames = new List <IfdName>();
            List <IfdBase> listDescs = new List <IfdBase>();

            // create name for IFC
            IfdName ifdNameIFC = CreateName(baseurl, sessionid, LanguageID, identifier);

            if (ifdNameIFC == null)
            {
                return(null);
            }

            listNames.Add(ifdNameIFC);

            // create type identifier if indicated
            if (!String.IsNullOrEmpty(type))
            {
                string  desc      = HttpUtility.UrlEncode(type);
                IfdBase ifdDescID = CreateDescription(baseurl, sessionid, LanguageID, desc);
                if (ifdDescID == null)
                {
                    return(null);
                }

                listDescs.Add(ifdDescID);
            }

            // localization
            bool hasEnglishName = false;
            bool hasEnglishDesc = false;

            foreach (DocLocalization docLoc in docObject.Localization)
            {
                // look up language id
                string langid = GetLanguageId(docLoc.Locale);
                if (langid != null)
                {
                    if (!String.IsNullOrEmpty(docLoc.Name))
                    {
                        string  locname = HttpUtility.UrlEncode(docLoc.Name);
                        IfdName ifdName = CreateName(baseurl, sessionid, langid, locname);
                        if (ifdName == null)
                        {
                            return(null);
                        }

                        listNames.Add(ifdName);

                        if (langid == LanguageEN)
                        {
                            hasEnglishName = true;
                        }
                    }

                    if (!String.IsNullOrEmpty(docLoc.Documentation))
                    {
                        string  locdesc = HttpUtility.UrlEncode(docLoc.Documentation);
                        IfdBase ifdDesc = CreateDescription(baseurl, sessionid, langid, locdesc);
                        if (ifdDesc == null)
                        {
                            return(null);
                        }

                        listDescs.Add(ifdDesc);

                        if (langid == LanguageEN)
                        {
                            hasEnglishDesc = true;
                        }
                    }
                }
            }

            if (!hasEnglishName && !String.IsNullOrEmpty(name))
            {
                // add default english name
                IfdName ifdNameEN = CreateName(baseurl, sessionid, LanguageEN, name);
                if (ifdNameEN == null)
                {
                    return(null);
                }

                listNames.Add(ifdNameEN);
            }

            if (!hasEnglishDesc && !String.IsNullOrEmpty(docObject.Documentation))
            {
                string  desc      = HttpUtility.UrlEncode(docObject.Documentation);
                IfdBase ifdDescEN = CreateDescription(baseurl, sessionid, LanguageEN, desc);
                if (ifdDescEN == null)
                {
                    return(null);
                }

                listDescs.Add(ifdDescEN);
            }

            // create concept
            StringBuilder sb = new StringBuilder();

            sb.Append(baseurl);
            sb.Append("api/4.0/IfdConcept?fullNameGuids=");
            for (int i = 0; i < listNames.Count; i++)
            {
                if (i != 0)
                {
                    sb.Append(",");
                }
                sb.Append(listNames[i].guid);
            }
            sb.Append("&conceptType=");
            sb.Append(conctype.ToString());

            if (listDescs.Count > 0)
            {
                sb.Append("&definitionGuids=");
                for (int i = 0; i < listDescs.Count; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(listDescs[i].guid);
                }
            }

            string url = sb.ToString();

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = 0;
            request.Accept        = "application/json";
            request.Headers.Add("cookie", "peregrineapisessionid=" + sessionid);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream          stream   = response.GetResponseStream();

            DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
            DataContractJsonSerializer         ser      = new DataContractJsonSerializer(typeof(IfdBase));
            IfdBase ifdRoot = (IfdBase)ser.ReadObject(stream);

            // record the ID on the doc object
            docObject.RegisterDictionary(baseurl, ifdRoot.guid);

            System.Diagnostics.Debug.WriteLine(DateTime.Now + " " + conctype.ToString() + " " + identifier + " FINISH");

            return(ifdRoot);
        }
Beispiel #5
0
        private void backgroundWorkerConcepts_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TreeNode tn = (TreeNode)e.Result;

            object[] list = (object[])tn.Nodes[0].Tag;
            tn.Nodes.Clear();

            if (list == null)
            {
                return;
            }

            foreach (object conc in list)
            {
                TreeNode tnConc = new TreeNode();
                tnConc.Tag = conc;

                if (conc is Type)
                {
                    Type t = (Type)conc;
                    tnConc.Tag        = t;
                    tnConc.Text       = t.Name;
                    tnConc.ImageIndex = 5;

                    foreach (PropertyInfo prop in t.GetProperties())
                    {
                        TreeNode tnProp = new TreeNode();
                        tnProp.Tag        = prop;
                        tnProp.Text       = tnProp.Name;
                        tnProp.ImageIndex = 9;
                    }
                }
                else if (conc is string)
                {
                    tnConc.Text       = conc.ToString();
                    tnConc.ImageIndex = 24;

                    TreeNode tnSub = new TreeNode();
                    tnSub.Text = "Loading...";
                    tnConc.Nodes.Add(tnSub);
                }

#if false
                IfdConceptTypeEnum ifdConceptType = IfdConceptTypeEnum.UNDEFINED;
                Enum.TryParse <IfdConceptTypeEnum>(conc.conceptType, out ifdConceptType);

                switch (ifdConceptType)
                {
                case IfdConceptTypeEnum.BAG:
                    tnConc.ImageIndex = 24;
                    break;

                case IfdConceptTypeEnum.NEST:
                    tnConc.ImageIndex = 10;
                    break;

                case IfdConceptTypeEnum.PROPERTY:
                    tnConc.ImageIndex = 9;
                    break;

                case IfdConceptTypeEnum.SUBJECT:
                    tnConc.ImageIndex = 5;     // DocEntity
                    break;

                case IfdConceptTypeEnum.MEASURE:
                    tnConc.ImageIndex = 4;
                    break;

                case IfdConceptTypeEnum.ACTOR:
                    tnConc.ImageIndex = 8;
                    break;

                case IfdConceptTypeEnum.DOCUMENT:
                    tnConc.ImageIndex = 20;
                    break;

                case IfdConceptTypeEnum.CLASSIFICATION:
                    tnConc.ImageIndex = 1;
                    break;

                default:
                    break;
                }
#endif
                tnConc.SelectedImageIndex = tnConc.ImageIndex;

                tn.Nodes.Add(tnConc);
            }

            //tn.Text += " (" + list.Count + ")";
        }
Beispiel #6
0
        private void backgroundWorkerConcepts_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TreeNode tn = (TreeNode)e.Result;

            IList <IfdConceptInRelationship> list = (IList <IfdConceptInRelationship>)tn.Nodes[0].Tag;

            tn.Nodes.Clear();

            foreach (IfdConceptInRelationship conc in list)
            {
                TreeNode tnConc = new TreeNode();
                tnConc.Tag  = conc;
                tnConc.Text = conc.ToString();

                IfdConceptTypeEnum ifdConceptType = IfdConceptTypeEnum.UNDEFINED;
                Enum.TryParse <IfdConceptTypeEnum>(conc.conceptType, out ifdConceptType);

                switch (ifdConceptType)
                {
                case IfdConceptTypeEnum.BAG:
                    tnConc.ImageIndex = 24;
                    break;

                case IfdConceptTypeEnum.NEST:
                    tnConc.ImageIndex = 10;
                    break;

                case IfdConceptTypeEnum.PROPERTY:
                    tnConc.ImageIndex = 9;
                    break;

                case IfdConceptTypeEnum.SUBJECT:
                    tnConc.ImageIndex = 5;     // DocEntity
                    break;

                case IfdConceptTypeEnum.MEASURE:
                    tnConc.ImageIndex = 4;
                    break;

                case IfdConceptTypeEnum.ACTOR:
                    tnConc.ImageIndex = 8;
                    break;

                case IfdConceptTypeEnum.DOCUMENT:
                    tnConc.ImageIndex = 20;
                    break;

                case IfdConceptTypeEnum.CLASSIFICATION:
                    tnConc.ImageIndex = 1;
                    break;

                default:
                    break;
                }
                tnConc.SelectedImageIndex = tnConc.ImageIndex;

                tn.Nodes.Add(tnConc);

                TreeNode tnSub = new TreeNode();
                tnSub.Text = "Loading...";
                tnConc.Nodes.Add(tnSub);
            }

            tn.Text += " (" + list.Count + ")";
        }