Exemplo n.º 1
0
        private void SynchronizeDataTypes()
        {
            var factory = new umbraco.cms.businesslogic.datatype.controls.Factory();

            foreach (Type typeDataType in Util.GetFirstLevelSubTypes(typeof(DataTypeBase)))
            {
                var dataTypeAttr = GetDataTypeAttribute(typeDataType);

                try
                {
                    this.AddToSynchronized(null, dataTypeAttr.Name, typeDataType);
                }
                catch (ArgumentException exc)
                {
                    throw new Exception(
                        string.Format(
                            "DataType with name '{0}' already exists! Please use unique DataType names. DataType causing the problem: '{1}' (assembly: '{2}'). Error message: {3}",
                            dataTypeAttr.Name,
                            typeDataType.FullName,
                            typeDataType.Assembly.FullName,
                            exc.Message));
                }

                var dtd = DataTypeDefinition.GetAll().FirstOrDefault(d => d.Text == dataTypeAttr.Name || (!string.IsNullOrEmpty(dataTypeAttr.UniqueId) && d.UniqueId == new Guid(dataTypeAttr.UniqueId)));

                // If there are no datatypes with name already we can go ahead and create one
                if (dtd == null)
                {
                    if (!string.IsNullOrEmpty(dataTypeAttr.UniqueId))
                    {
                        dtd = DataTypeDefinition.MakeNew(User.GetUser(0), dataTypeAttr.Name,
                                                         new Guid(dataTypeAttr.UniqueId));
                    }
                    else
                    {
                        dtd = DataTypeDefinition.MakeNew(User.GetUser(0), dataTypeAttr.Name);
                    }
                }

                dtd.DataType = factory.DataType(new Guid(dataTypeAttr.RenderControlGuid));
                dtd.Text = dataTypeAttr.Name;

                System.Web.HttpRuntime.Cache.Remove(string.Format("UmbracoDataTypeDefinition{0}", dtd.UniqueId));

                dtd.Save();

                var instance = Activator.CreateInstance(typeDataType, null) as DataTypeBase;
                DataTypePrevalue[] prevalues = instance.Prevalues;
                if (prevalues.Any())
                {
                    var settingsStorage = new DataEditorSettingsStorage();

                    // updating all settings to those defined in datatype class
                    // If you've exported, all settings will be defined here anyway?
                    settingsStorage.ClearSettings(dtd.Id);
                    settingsStorage.InsertSettings(dtd.Id, prevalues.Select(pre => new Setting<string, string> { Key = pre.Alias, Value = pre.Value }).ToList());
                }
            }
        }
Exemplo n.º 2
0
        public static DataTypeDefinition Import(XmlNode xmlData)
        {
            string _name = xmlData.Attributes["Name"].Value;
            string _id   = xmlData.Attributes["Id"].Value;
            string _def  = xmlData.Attributes["Definition"].Value;


            //Make sure that the dtd is not already present
            if (!CMSNode.IsNode(new Guid(_def))
                )
            {
                BusinessLogic.User u = umbraco.BusinessLogic.User.GetCurrent();

                if (u == null)
                {
                    u = BusinessLogic.User.GetUser(0);
                }

                cms.businesslogic.datatype.controls.Factory f = new umbraco.cms.businesslogic.datatype.controls.Factory();


                DataTypeDefinition dtd = MakeNew(u, _name, new Guid(_def));
                var dataType           = f.DataType(new Guid(_id));
                if (dataType == null)
                {
                    throw new NullReferenceException("Could not resolve a data type with id " + _id);
                }

                dtd.DataType = dataType;
                dtd.Save();

                //add prevalues
                foreach (XmlNode xmlPv in xmlData.SelectNodes("PreValues/PreValue"))
                {
                    XmlAttribute val = xmlPv.Attributes["Value"];

                    if (val != null)
                    {
                        PreValue p = new PreValue(0, 0, val.Value);
                        p.DataTypeId = dtd.Id;
                        p.Save();
                    }
                }

                return(dtd);
            }

            return(null);
        }
Exemplo n.º 3
0
        public static DataTypeDefinition Import(XmlNode xmlData)
        {
            string _name = xmlData.Attributes["Name"].Value;
            string _id   = xmlData.Attributes["Id"].Value;
            string _def  = xmlData.Attributes["Definition"].Value;


            //Make sure that the dtd is not already present
            if (!CMSNode.IsNode(new Guid(_def))
                )
            {
                BasePages.UmbracoEnsuredPage uep = new umbraco.BasePages.UmbracoEnsuredPage();
                BusinessLogic.User           u   = uep.getUser();

                if (u == null)
                {
                    u = BusinessLogic.User.GetUser(0);
                }

                cms.businesslogic.datatype.controls.Factory f = new umbraco.cms.businesslogic.datatype.controls.Factory();


                DataTypeDefinition dtd = MakeNew(u, _name, new Guid(_def));
                dtd.DataType = f.DataType(new Guid(_id));
                dtd.Save();

                //add prevalues
                foreach (XmlNode xmlPv in xmlData.SelectNodes("PreValues/PreValue"))
                {
                    XmlAttribute val = xmlPv.Attributes["Value"];

                    if (val != null && !string.IsNullOrEmpty(val.Value))
                    {
                        PreValue p = new PreValue(0, 0, val.Value);
                        p.DataTypeId = dtd.Id;
                        p.Save();
                    }
                }

                return(dtd);
            }

            return(null);
        }
		public void Get_All_Instances()
		{
			var factory = new umbraco.cms.businesslogic.datatype.controls.Factory();
			Assert.AreEqual(2, factory.GetAll().Count());
		}
Exemplo n.º 5
0
        public void Get_All_Instances()
        {
            var factory = new umbraco.cms.businesslogic.datatype.controls.Factory();

            Assert.AreEqual(2, factory.GetAll().Count());
        }
Exemplo n.º 6
0
        public static DataTypeDefinition Import(XmlNode xmlData)
        {
            string _name = xmlData.Attributes["Name"].Value;
            string _id = xmlData.Attributes["Id"].Value;
            string _def = xmlData.Attributes["Definition"].Value;


            //Make sure that the dtd is not already present
            if (!CMSNode.IsNode(new Guid(_def))
            )
            {

                BusinessLogic.User u = umbraco.BusinessLogic.User.GetCurrent();

                if (u == null)
                    u = BusinessLogic.User.GetUser(0);

                cms.businesslogic.datatype.controls.Factory f = new umbraco.cms.businesslogic.datatype.controls.Factory();


                DataTypeDefinition dtd = MakeNew(u, _name, new Guid(_def));
				var dataType = f.DataType(new Guid(_id));
				if (dataType == null)
					throw new NullReferenceException("Could not resolve a data type with id " + _id);

	            dtd.DataType = dataType;
                dtd.Save();

                //add prevalues
                foreach (XmlNode xmlPv in xmlData.SelectNodes("PreValues/PreValue"))
                {


                    XmlAttribute val = xmlPv.Attributes["Value"];

                    if (val != null)
                    {
                        PreValue p = new PreValue(0, 0, val.Value);
                        p.DataTypeId = dtd.Id;
                        p.Save();
                    }
                }

                return dtd;
            }

            return null;
        }
        protected override bool RefreshFromXml(XDocument xmlDoc)
        {
            try
            {
                dynamic dtXml = new umbraco.MacroEngines.DynamicXml(xmlDoc.Root);
                var node = xmlDoc.Element("DataType");
                var name = node.Attribute("Name").Value;
                var alias = name.ToAlias();
                var dataTypeId = new Guid(node.Attribute("Id").Value);

                var dataType = DataTypeDefinition.GetAll().Where(x=>x.Text.ToAlias() == alias).FirstOrDefault();

                if (dataType == null)
                {
                    dataType = DataTypeDefinition.MakeNew(User.GetUser(0), name);
                }
                var dtFactory = new umbraco.cms.businesslogic.datatype.controls.Factory();
                var dt = dtFactory.DataType(dataTypeId);
                dataType.DataType = dt;

                List<string> newValues = node.Element("PreValues").Elements("PreValue").Select(x => x.Attribute("Value").Value).ToList();
                List<PreValue> toKeep = new List<PreValue>();

                var preValues = PreValues.GetPreValues(dataType.Id).Values.OfType<PreValue>();
                var sortOrder = 0;
                foreach(var nv in newValues)
                {
                    sortOrder ++;
                    var pv = preValues.Where(x=>x.Value == nv).FirstOrDefault();
                    if(pv == null){
                        pv = PreValue.MakeNew(dataType.Id, nv);
                    }
                    pv.SortOrder = sortOrder;
                    toKeep.Add(pv);
                }

                var toDelete = preValues.Where(x=>!toKeep.Contains(x));

                foreach(var pv in toDelete)
                {
                    pv.Delete();
                }

                foreach(var pv in toKeep)
                {
                    pv.Save();
                }

                dataType.Save();

                return true;
            }
            catch(Exception ex)
            {

            }
            return false;
        }