private void AddHomeOnlyBannerTextArea() { var migrationName = MethodBase.GetCurrentMethod().Name; try { var path = HostingEnvironment.MapPath(MigrationMarkersPath + migrationName + ".txt"); if (File.Exists(path)) return; var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService; var communityContentType = contentTypeService.GetContentType("Community"); var propertyTypeAlias = "homeOnlyBanner"; if (communityContentType.PropertyTypeExists(propertyTypeAlias) == false) { var textarea = new DataTypeDefinition("Umbraco.TextboxMultiple"); var textareaPropertyType = new PropertyType(textarea, propertyTypeAlias) { Name = "Banner (only shown on home)" }; communityContentType.AddPropertyType(textareaPropertyType, "Banners"); communityContentType.MovePropertyType("mainNotification", "Banners"); contentTypeService.Save(communityContentType); } string[] lines = { "" }; File.WriteAllLines(path, lines); } catch (Exception ex) { LogHelper.Error<MigrationsHandler>(string.Format("Migration: '{0}' failed", migrationName), ex); } }
public void DataTypeService_Can_Persist_New_DataTypeDefinition() { // Arrange var dataTypeService = ServiceContext.DataTypeService; // Act var dataTypeDefinition = new DataTypeDefinition(-1, "Test.TestEditor") { Name = "Testing Textfield", DatabaseType = DataTypeDatabaseType.Ntext }; dataTypeService.Save(dataTypeDefinition); // Assert Assert.That(dataTypeDefinition, Is.Not.Null); Assert.That(dataTypeDefinition.HasIdentity, Is.True); }
private SyncAttempt<IDataTypeDefinition> DeserializeItem(XElement node, IDataTypeDefinition item) { // pre import var mappedNode = DeserializeGetMappedValues(node); Guid key = node.Attribute("Key").ValueOrDefault(Guid.Empty); var name = node.Attribute("Name").ValueOrDefault(string.Empty); var editorAlias = node.Attribute("Id").ValueOrDefault(string.Empty); var dbType = node.Attribute("DatabaseType").ValueOrDefault(string.Empty); var databaseType = !string.IsNullOrEmpty(dbType) ? dbType.EnumParse<DataTypeDatabaseType>(true) : DataTypeDatabaseType.Ntext; if (item == null && !string.IsNullOrEmpty(name)) { // lookup by alias. LogHelper.Debug<DataTypeSerializer>("Looking up datatype by name: {0}", () => name); item = _dataTypeService.GetDataTypeDefinitionByName(name); } if (item == null) { // create item = new DataTypeDefinition(editorAlias) { Key = key, Name = name, DatabaseType = databaseType }; } if (item.Name != name) { item.Name = name; } if (item.Key != key) { item.Key = key; } if (item.PropertyEditorAlias != editorAlias) { item.PropertyEditorAlias = editorAlias; } if (item.DatabaseType != databaseType) { item.DatabaseType = databaseType; } _dataTypeService.Save(item); DeserializeUpdatePreValues(item, mappedNode); _dataTypeService.Save(item); return SyncAttempt<IDataTypeDefinition>.Succeed(item.Name, item, ChangeType.Import); }
public void DataTypeService_Can_Persist_New_DataTypeDefinition() { // Arrange var dataTypeService = ServiceContext.DataTypeService; var textfieldId = new Guid("ec15c1e5-9d90-422a-aa52-4f7622c63bea"); // Act var dataTypeDefinition = new DataTypeDefinition(-1, textfieldId) { Name = "Testing Textfield", DatabaseType = DataTypeDatabaseType.Ntext }; dataTypeService.Save(dataTypeDefinition); // Assert Assert.That(dataTypeDefinition, Is.Not.Null); Assert.That(dataTypeDefinition.HasIdentity, Is.True); }
public bool Execute( string packageName, XmlNode xmlData ) { Initialize( xmlData ); IEnumerable<IDataTypeDefinition> dataTypeDefinitions = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionByPropertyEditorAlias( _alias ); if ( !dataTypeDefinitions.Any() ) { DataTypeDefinition dataTypeDefinition = new DataTypeDefinition( -1, _alias ) { Name = _name, DatabaseType = _alias != "TeaCommerce.StockManagement" ? DataTypeDatabaseType.Integer : DataTypeDatabaseType.Nvarchar }; ApplicationContext.Current.Services.DataTypeService.Save( dataTypeDefinition ); } return true; }
private IDataTypeDefinition CreateDataTypeDefinition(DataTypeRegistration dataTypeRegistration) { IDataTypeDefinition dataTypeDefinition; if (dataTypeRegistration.DbType == DatabaseType.None) { throw new CodeFirstException("Database type not specified for " + dataTypeRegistration.DataTypeInstanceName); } else if (string.IsNullOrWhiteSpace(dataTypeRegistration.PropertyEditorAlias)) { throw new CodeFirstException("Property Editor Alias not specified for " + dataTypeRegistration.DataTypeInstanceName); } else { dataTypeDefinition = new Umbraco.Core.Models.DataTypeDefinition(-1, dataTypeRegistration.PropertyEditorAlias); dataTypeDefinition.Name = dataTypeRegistration.DataTypeInstanceName; dataTypeDefinition.DatabaseType = dataTypeRegistration.UmbracoDatabaseType; } return(dataTypeDefinition); }
/// <summary> /// Convention to get a DataTypeDefinition from the PropertyTypeAttribute or the type of the property itself /// </summary> /// <param name="attribute"></param> /// <param name="type"></param> /// <returns></returns> public static IDataTypeDefinition GetDataTypeDefinitionByAttributeOrType(PropertyTypeAttribute attribute, Type type) { if (attribute != null) { var instance = Activator.CreateInstance(attribute.Type); var dataType = instance as IDataType; var definition = GetDataTypeByControlId(dataType.Id); //If the DataTypeDefinition doesn't exist we create a new one if (definition == null) { definition = new DataTypeDefinition(-1, dataType.Id) { DatabaseType = attribute.DatabaseType, Name = dataType.DataTypeName }; ApplicationContext.Current.Services.DataTypeService.Save(definition, 0); } return definition; } return GetPredefinedDataTypeDefinitionByType(type); }
/// <summary> /// Creates a new datatypedefinition given its name and the user which creates it. /// </summary> /// <param name="u">The user who creates the datatypedefinition</param> /// <param name="Text">The name of the DataTypeDefinition</param> /// <param name="UniqueId">Overrides the CMSnodes uniqueID</param> /// <returns></returns> public static DataTypeDefinition MakeNew(BusinessLogic.User u, string Text, Guid UniqueId) { var found = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionByName(Text); if (found != null) { throw new DuplicateNameException("A data type with the name " + Text + " already exists"); } var created = new Umbraco.Core.Models.DataTypeDefinition("") { Name = Text, Key = UniqueId }; ApplicationContext.Current.Services.DataTypeService.Save(created); var dtd = new DataTypeDefinition(created); dtd.OnNew(EventArgs.Empty); return(dtd); }
public bool GetOrCreate(out IDataTypeDefinition dtd) { // Get a reference to the data type service IDataTypeService dts = ApplicationContext.Current.Services.DataTypeService; // Attempt to get the DTD dtd = dts.GetDataTypeDefinitionById(Guid); if (dtd != null) { return false; } // Or create a new one dtd = new DataTypeDefinition(EditorAlias) { Key = Guid, Name = Name }; if (PreValues == null) { dts.Save(dtd); } else { dts.SaveDataTypeAndPreValues(dtd, PreValues); } return true; }
/// <summary> /// Imports and saves package xml as <see cref="IDataTypeDefinition"/> /// </summary> /// <param name="element">Xml to import</param> /// <param name="userId"></param> /// <returns>An enumrable list of generated DataTypeDefinitions</returns> public IEnumerable<IDataTypeDefinition> ImportDataTypeDefinitions(XElement element, int userId = 0) { var name = element.Name.LocalName; if (name.Equals("DataTypes") == false && name.Equals("DataType") == false) { throw new ArgumentException("The passed in XElement is not valid! It does not contain a root element called 'DataTypes' for multiple imports or 'DataType' for a single import."); } var dataTypes = new Dictionary<string, IDataTypeDefinition>(); var dataTypeElements = name.Equals("DataTypes") ? (from doc in element.Elements("DataType") select doc).ToList() : new List<XElement> { element.Element("DataType") }; foreach (var dataTypeElement in dataTypeElements) { var dataTypeDefinitionName = dataTypeElement.Attribute("Name").Value; var dataTypeId = new Guid(dataTypeElement.Attribute("Id").Value); var dataTypeDefinitionId = new Guid(dataTypeElement.Attribute("Definition").Value); var databaseTypeAttribute = dataTypeElement.Attribute("DatabaseType"); var definition = _dataTypeService.GetDataTypeDefinitionById(dataTypeDefinitionId); //If the datatypedefinition doesn't already exist we create a new new according to the one in the package xml if (definition == null) { var databaseType = databaseTypeAttribute != null ? databaseTypeAttribute.Value.EnumParse<DataTypeDatabaseType>(true) : DataTypeDatabaseType.Ntext; var dataTypeDefinition = new DataTypeDefinition(-1, dataTypeId) { Key = dataTypeDefinitionId, Name = dataTypeDefinitionName, DatabaseType = databaseType }; dataTypes.Add(dataTypeDefinitionName, dataTypeDefinition); } } var list = dataTypes.Select(x => x.Value).ToList(); if (list.Any()) { _dataTypeService.Save(list, userId); SavePrevaluesFromXml(list, dataTypeElements); } return list; }
private void OverrideYouTrack() { var migrationName = MethodBase.GetCurrentMethod().Name; try { var path = HostingEnvironment.MapPath(MigrationMarkersPath + migrationName + ".txt"); if (File.Exists(path)) return; var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService; var releaseContentType = contentTypeService.GetContentType("Release"); var propertyTypeAlias = "overrideYouTrackDescription"; if (releaseContentType.PropertyTypeExists(propertyTypeAlias) == false) { var checkbox = new DataTypeDefinition("Umbraco.TrueFalse"); var checkboxPropertyType = new PropertyType(checkbox, propertyTypeAlias) { Name = "Override YouTrack Description?" }; releaseContentType.AddPropertyType(checkboxPropertyType, "Content"); contentTypeService.Save(releaseContentType); } propertyTypeAlias = "overrideYouTrackReleaseDate"; if (releaseContentType.PropertyTypeExists(propertyTypeAlias) == false) { var textbox = new DataTypeDefinition("Umbraco.Textbox"); var textboxPropertyType = new PropertyType(textbox, propertyTypeAlias) { Name = "Override YouTrack release date" }; releaseContentType.AddPropertyType(textboxPropertyType, "Content"); contentTypeService.Save(releaseContentType); } string[] lines = { "" }; File.WriteAllLines(path, lines); } catch (Exception ex) { LogHelper.Error<MigrationsHandler>(string.Format("Migration: '{0}' failed", migrationName), ex); } }
/// <summary> /// Creates a new DataTypeDefinition based on the Type in the PropertyTypeAttribute /// </summary> /// <param name="attribute"></param> /// <param name="dataTypeDefinitionName"></param> /// <returns></returns> public static IDataTypeDefinition CreateDataTypeDefinitionFromAttribute(PropertyTypeAttribute attribute, string dataTypeDefinitionName) { var instance = Activator.CreateInstance(attribute.Type); var dataType = instance as IDataType; var definition = new DataTypeDefinition(-1, dataType.Id) { DatabaseType = attribute.DatabaseType, Name = dataTypeDefinitionName }; ApplicationContext.Current.Services.DataTypeService.Save(definition, 0); return definition; }
public DataTypeDisplay GetEmpty() { var dt = new DataTypeDefinition(-1, ""); return Mapper.Map<IDataTypeDefinition, DataTypeDisplay>(dt); }
public void Can_Perform_Delete_On_DataTypeDefinitionRepository() { // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); var repository = new DataTypeDefinitionRepository(unitOfWork); var dataTypeDefinition = new DataTypeDefinition(-1, new Guid("0FE4B127-D48C-4807-8371-67FC2A0E27D7")) { DatabaseType = DataTypeDatabaseType.Integer, Name = "AgeDataType", CreatorId = 0 }; // Act repository.AddOrUpdate(dataTypeDefinition); unitOfWork.Commit(); var existsBefore = repository.Exists(dataTypeDefinition.Id); repository.Delete(dataTypeDefinition); unitOfWork.Commit(); var existsAfter = repository.Exists(dataTypeDefinition.Id); // Assert Assert.That(existsBefore, Is.True); Assert.That(existsAfter, Is.False); }
public void Can_Perform_Update_On_DataTypeDefinitionRepository() { // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); var repository = new DataTypeDefinitionRepository(unitOfWork); var dataTypeDefinition = new DataTypeDefinition(-1, new Guid("0FE4B127-D48C-4807-8371-67FC2A0E27D7")) { DatabaseType = DataTypeDatabaseType.Integer, Name = "AgeDataType", CreatorId = 0 }; repository.AddOrUpdate(dataTypeDefinition); unitOfWork.Commit(); // Act var definition = repository.Get(dataTypeDefinition.Id); definition.Name = "AgeDataType Updated"; repository.AddOrUpdate(definition); unitOfWork.Commit(); var definitionUpdated = repository.Get(dataTypeDefinition.Id); // Assert Assert.That(definitionUpdated, Is.Not.Null); Assert.That(definitionUpdated.Name, Is.EqualTo("AgeDataType Updated")); }
private void UaaSProjectCheckbox() { var migrationName = MethodBase.GetCurrentMethod().Name; try { var path = HostingEnvironment.MapPath(MigrationMarkersPath + migrationName + ".txt"); if (File.Exists(path)) return; var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService; var projectContentType = contentTypeService.GetContentType("Project"); var propertyTypeAlias = "worksOnUaaS"; if (projectContentType.PropertyTypeExists(propertyTypeAlias) == false) { var checkbox = new DataTypeDefinition("Umbraco.TrueFalse"); var checkboxPropertyType = new PropertyType(checkbox, propertyTypeAlias) { Name = "Works on Umbraco as a Service?" }; projectContentType.AddPropertyType(checkboxPropertyType, "Project"); contentTypeService.Save(projectContentType); } string[] lines = { "" }; File.WriteAllLines(path, lines); } catch (Exception ex) { LogHelper.Error<MigrationsHandler>(string.Format("Migration: '{0}' failed", migrationName), ex); } }
protected object ConvertDbToEditor_Fake(string propEditorAlias, string propAlias, object value) { // Create a fake DTD var fakeDtd = new DataTypeDefinition(-1, propEditorAlias); // Create a fake property type var fakePropType = new PropertyType(fakeDtd) { Alias = propAlias }; // Create a fake property var fakeProp = new Property(fakePropType, value); // Lookup the property editor var fakePropEditor = PropertyEditorResolver.Current.GetByAlias(propEditorAlias); // Get the editor to do it's conversion var fakeNewValue = fakePropEditor.ValueEditor.ConvertDbToEditor(fakeProp, fakePropType, ApplicationContext.Current.Services.DataTypeService); // Store the value back return fakeNewValue == null ? null : fakeNewValue.ToString(); }
public void Can_Perform_Delete_On_DataTypeDefinitionRepository() { // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); using (var repository = CreateRepository(unitOfWork)) { var dataTypeDefinition = new DataTypeDefinition(-1, "Test.TestEditor") { DatabaseType = DataTypeDatabaseType.Integer, Name = "AgeDataType", CreatorId = 0 }; // Act repository.AddOrUpdate(dataTypeDefinition); unitOfWork.Commit(); var existsBefore = repository.Exists(dataTypeDefinition.Id); repository.Delete(dataTypeDefinition); unitOfWork.Commit(); var existsAfter = repository.Exists(dataTypeDefinition.Id); // Assert Assert.That(existsBefore, Is.True); Assert.That(existsAfter, Is.False); } }
public void Can_Perform_Update_On_DataTypeDefinitionRepository() { // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); using (var repository = CreateRepository(unitOfWork)) { var dataTypeDefinition = new DataTypeDefinition(-1, "Test.blah") { DatabaseType = DataTypeDatabaseType.Integer, Name = "AgeDataType", CreatorId = 0 }; repository.AddOrUpdate(dataTypeDefinition); unitOfWork.Commit(); // Act var definition = repository.Get(dataTypeDefinition.Id); definition.Name = "AgeDataType Updated"; definition.PropertyEditorAlias = "Test.TestEditor"; //change repository.AddOrUpdate(definition); unitOfWork.Commit(); var definitionUpdated = repository.Get(dataTypeDefinition.Id); // Assert Assert.That(definitionUpdated, Is.Not.Null); Assert.That(definitionUpdated.Name, Is.EqualTo("AgeDataType Updated")); Assert.That(definitionUpdated.PropertyEditorAlias, Is.EqualTo("Test.TestEditor")); } }
private void SynchronizeDataTypes() { 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 = DataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(dataTypeAttr.PropertyEditorAlias).FirstOrDefault(); // 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) { var newDataTypeDefinition = new DataTypeDefinition(dataTypeAttr.ParentId, dataTypeAttr.PropertyEditorAlias) { Name = dataTypeAttr.Name, DatabaseType = dataTypeAttr.DataTypeDatabaseType }; if (!string.IsNullOrEmpty(dataTypeAttr.UniqueId)) { newDataTypeDefinition.Key = new Guid(dataTypeAttr.UniqueId); } var codeFirstSettings = GetEditorSettings(typeDataType); if (codeFirstSettings.Any()) { DataTypeService.SaveDataTypeAndPreValues(newDataTypeDefinition, codeFirstSettings); } else { DataTypeService.Save(newDataTypeDefinition); } } // data type definition already present else { // var settings = GetEditorSettings(typeDataType); var existingSettings = DataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id); if (existingSettings == null || existingSettings.IsDictionaryBased ? !existingSettings.PreValuesAsDictionary.Any() : !existingSettings.PreValuesAsArray.Any()) { var codeFirstSettings = GetEditorSettings(typeDataType); if (codeFirstSettings.Any()) { DataTypeService.SavePreValues(dtd.Id, codeFirstSettings); } } } } }
private IDataTypeDefinition LoadOrCreateStorePickerDataTypeDefinition() { var dataTypeService = ApplicationContext.Current.Services.DataTypeService; var newDataTypesList = new List<IDataTypeDefinition>(); var storePickerDataTypeDef = _umbracoVersion.GetDataTypeDefinition("uWebshop.StorePicker", new Guid("1e8cdc0b-436e-46f5-bfec-57be45745771")); if (storePickerDataTypeDef == null) { storePickerDataTypeDef = new DataTypeDefinition(-1, new Guid("5fa345e3-9352-45d6-adaa-2da6cdc9aca3")); storePickerDataTypeDef.Name = "uWebshop Store Picker"; storePickerDataTypeDef.Key = new Guid("1e8cdc0b-436e-46f5-bfec-57be45745771"); storePickerDataTypeDef.DatabaseType = DataTypeDatabaseType.Integer; newDataTypesList.Add(storePickerDataTypeDef); } if (newDataTypesList.Any()) dataTypeService.Save(newDataTypesList); storePickerDataTypeDef = _umbracoVersion.GetDataTypeDefinition("uWebshop.StorePicker", new Guid("1e8cdc0b-436e-46f5-bfec-57be45745771")); return storePickerDataTypeDef; }
protected object ConvertDbToString_Fake(string propEditorAlias, string propAlias, object value) { // Create a fake DTD var fakeDtd = new DataTypeDefinition(-1, propEditorAlias); // Create a fake property type var fakePropType = new PropertyType(fakeDtd) { Alias = propAlias }; // Create a fake property var fakeProp = new Property(fakePropType, value); // Lookup the property editor var fakePropEditor = PropertyEditorResolver.Current.GetByAlias(propEditorAlias); return fakePropEditor.ValueEditor.ConvertDbToString(fakeProp, fakePropType, ApplicationContext.Current.Services.DataTypeService); }
/// <summary> /// Creates a new dataType /// </summary> /// <param name="type">The type of your model that contains the prevalues for the custom data type.</param> public static void CreateDataType(Type type) { UmbracoDataTypeAttribute dataTypeAttribute = type.GetCustomAttribute<UmbracoDataTypeAttribute>(); if (dataTypeAttribute == null) { return; } else { var dataTypeService = ApplicationContext.Current.Services.DataTypeService; List<IDataTypeDefinition> dataTypeDefinitions = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(dataTypeAttribute.PropertyEditorAlias).ToList(); // Obtains all the property editor alias names. var matchingDataTypeDefinition = dataTypeDefinitions.SingleOrDefault(x => x.Name == dataTypeAttribute.DataTypeName); // Checks to see if the data type name exist. /* If not, create a new datatype based on the property editor. * Do not try to update an existing data type. For the data type itself, Umbraco throws a DuplicateNameException. * Calling fileService.SavePreValues works, but wipes out all the content data even for existing prevalues. Too dangerous to use. */ if (matchingDataTypeDefinition == null) { matchingDataTypeDefinition = new DataTypeDefinition(-1, dataTypeAttribute.PropertyEditorAlias); matchingDataTypeDefinition.Name = dataTypeAttribute.DataTypeName; matchingDataTypeDefinition.DatabaseType = dataTypeAttribute.DatabaseType; dataTypeService.Save(matchingDataTypeDefinition); var preValueProviderType = dataTypeAttribute.PreValues; // error check needs to be added. var preValueProviderInstance = Activator.CreateInstance(preValueProviderType); var preValues = ((IPreValueProvider) preValueProviderInstance).PreValues; dataTypeService.SaveDataTypeAndPreValues(matchingDataTypeDefinition, preValues); } } }