/// <summary>
 /// Get default field value
 /// </summary>
 /// <returns>if no default value, return null</returns>
 public IFieldValue GetDefaultValue()
 {
     HierarchyNodeValueCollection selectedHierarchyNodes = new HierarchyNodeValueCollection();
     GetSelectedHierarchyNodes(selectedHierarchyNodes, this.Node);
     return selectedHierarchyNodes.FieldValue();
 }
        public void AddExtensionObjectTest()
        {
            using (SampleObjectDataContext ctx = CreateSampleObjectDataContext())
            {
                SampleObject sampleObject = new SampleObject { Name = "RapidWebDev", ExtensionDataTypeId = this.ObjectMetadataTypeId };
                sampleObject["Name"] = "Eunge";
                sampleObject["Birthday"] = new DateTime(1982, 2, 7);
                sampleObject["Level"] = 10;
                sampleObject["Salary"] = 5000m;
                sampleObject["Position"] = new HierarchyNodeValueCollection { "Dev" };
                sampleObject["HuKou"] = new EnumerationValueCollection { "SiChuan" };

                ctx.SampleObjects.InsertOnSubmit(sampleObject);
                ctx.SubmitChanges();

                sampleObject = ctx.SampleObjects.FirstOrDefault(so => so.ObjectId == sampleObject.ObjectId);
                Assert.AreEqual("Eunge", sampleObject["Name"]);
                Assert.AreEqual(new DateTime(1982, 2, 7), sampleObject["Birthday"]);
                Assert.AreEqual(10, sampleObject["Level"]);
                Assert.AreEqual(5000m, sampleObject["Salary"]);
                Assert.AreEqual("Dev", (sampleObject["Position"] as HierarchyNodeValueCollection)[0]);
                Assert.AreEqual("SiChuan", (sampleObject["HuKou"] as EnumerationValueCollection)[0]);
            }
        }
        private static void GetSelectedHierarchyNodes(HierarchyNodeValueCollection selectedHierarchyNodes, HierarchyNode[] hierarchyNodes)
        {
            if (hierarchyNodes == null) return;

            foreach (HierarchyNode hierarchyNode in hierarchyNodes)
            {
                if (hierarchyNode.Selected)
                    selectedHierarchyNodes.Add(hierarchyNode.Value);

                GetSelectedHierarchyNodes(selectedHierarchyNodes, hierarchyNode.Node);
            }
        }
        public void AllFieldTypesSerializationTest()
        {
            MockExtensionObject4Serializer entityToSerialize = new MockExtensionObject4Serializer();
            entityToSerialize["Name"] = "Eunge";
            entityToSerialize["Birthday"] = new DateTime(1982, 2, 7);
            entityToSerialize["Level"] = 10;
            entityToSerialize["Salary"] = 13000m;
            entityToSerialize["Position"] = new HierarchyNodeValueCollection { "Dev" };
            entityToSerialize["HuKou"] = new EnumerationValueCollection { "SiChuan" };
            entityToSerialize["UnknownProperty"] = "Lucy";
            entityToSerialize.ExtensionDataTypeId = this.UserTypeId;

            IMetadataApi metadataApi = SpringContext.Current.GetObject<IMetadataApi>();
            ExtensionObjectSerializer serializer = new ExtensionObjectSerializer(metadataApi);
            serializer.Serialize(entityToSerialize);

            using(StringReader stringReader = new StringReader(entityToSerialize.ExtensionData))
            using (XmlReader xmlreader = XmlReader.Create(stringReader))
            {
                XDocument xmldoc = XDocument.Load(xmlreader);
                IEnumerable<XElement> fieldNameValuePairs = xmldoc.Descendants(XName.Get("FieldNameValuePair", ServiceNamespaces.ExtensionModel));
                Assert.AreEqual(7, fieldNameValuePairs.Count());
            }

            MockExtensionObject4Serializer entityToDeserialize = new MockExtensionObject4Serializer()
            {
                ExtensionData = entityToSerialize.ExtensionData,
                ExtensionDataTypeId = entityToSerialize.ExtensionDataTypeId
            };

            IDictionary<string, object> properties = serializer.Deserialize(entityToDeserialize);
            Assert.AreEqual("Eunge", properties["Name"]);
            Assert.AreEqual(new DateTime(1982, 2, 7), properties["Birthday"]);
            Assert.AreEqual(10, properties["Level"]);
            Assert.AreEqual(13000m, properties["Salary"]);
            Assert.AreEqual("Dev", (properties["Position"] as HierarchyNodeValueCollection)[0]);
            Assert.AreEqual("SiChuan", (properties["HuKou"] as EnumerationValueCollection)[0]);
            Assert.AreEqual("Lucy", properties["UnknownProperty"]);
        }