/// <summary> /// Method for creating a new version of the data associated to the Content. /// /// </summary> /// <returns>The new version Id</returns> protected Guid createNewVersion() { ClearLoadedProperties(); Guid newVersion = Guid.NewGuid(); bool tempHasVersion = hasVersion(); foreach (propertytype.PropertyType pt in this.ContentType.PropertyTypes) { object oldValue = ""; if (tempHasVersion) { try { oldValue = this.getProperty(pt.Alias).Value; } catch { } } property.Property p = this.addProperty(pt, newVersion); if (oldValue != null && oldValue.ToString() != "") { p.Value = oldValue; } } SqlHelper.ExecuteNonQuery("Insert into cmsContentVersion (ContentId,versionId) values (" + this.Id + ",'" + newVersion + "')"); this.Version = newVersion; return(newVersion); }
/// <summary> /// Return a value of a PreValue by Property /// </summary> /// <param name="parameter">UmbracoType</param> /// <param name="value">value as a Property</param> /// <returns>Returns the value of the Property if exists, if not returns an empty string</returns> public static string PropertyValue(UmbracoType parameter, Property value) { string result = string.Empty; if (value.Value.ToString() != "") { int id = Convert.ToInt32(GetParameterValue(parameter)); IEnumerable <PreValue> data = DataTypeValue(id); PreValue preValue = data.SingleOrDefault(dt => dt.Id == Convert.ToInt32(value.Value)); result = (preValue != null) ? preValue.Value : string.Empty; } return(result); }
/// <summary> /// Get the Content of a UComponenet Grid /// </summary> /// <param name="property">Property</param> /// <returns>List of dymamic objects</returns> public static IEnumerable <dynamic> GetDataTypeGrid(Property property) { List <dynamic> result = new List <dynamic>(); if (property != null) { foreach (XElement element in XDocument.Parse(property.Value.ToString()).Descendants("item")) { dynamic data = new ExpandoObject(); ((IDictionary <string, object>)data).Add("id", element.Attribute("id").Value); ((IDictionary <string, object>)data).Add("sortorder", element.Attribute("sortOrder").Value); foreach (XElement item in element.Descendants()) { ((IDictionary <string, object>)data).Add(item.Name.LocalName, item.Value); } result.Add(data); } } return(result); }
/// <summary> /// Method for creating a new version of the data associated to the Content. /// /// </summary> /// <returns>The new version Id</returns> protected Guid createNewVersion(DateTime versionDate = default(DateTime)) { if (versionDate == default(DateTime)) { versionDate = DateTime.Now; } ClearLoadedProperties(); Guid newVersion = Guid.NewGuid(); bool tempHasVersion = hasVersion(); // we need to ensure that a version in the db exist before we add related data SqlHelper.ExecuteNonQuery("Insert into cmsContentVersion (ContentId,versionId,versionDate) values (" + this.Id + ",'" + newVersion + "', @updateDate)", SqlHelper.CreateParameter("@updateDate", versionDate)); List <PropertyType> pts = ContentType.PropertyTypes; foreach (propertytype.PropertyType pt in pts) { object oldValue = ""; if (tempHasVersion) { try { oldValue = this.getProperty(pt.Alias).Value; } catch { } } property.Property p = this.addProperty(pt, newVersion); if (oldValue != null && oldValue.ToString() != "") { p.Value = oldValue; } } this.Version = newVersion; return(newVersion); }
/// <summary> /// Loads all properties from database into objects. If this method is re-called, it will re-query the database. /// </summary> /// <remarks> /// This optimizes sql calls. This will first check if all of the properties have been loaded. If not, /// then it will query for all property types for the current version from the db. It will then iterate over each /// cmdPropertyData row and store the id and propertyTypeId in a list for lookup later. Once the records have been /// read, we iterate over the cached property types for this ContentType and create a new property based on /// our stored list of proeprtyTypeIds. We then have a cached list of Property objects which will get returned /// on all subsequent calls and is also used to return a property with calls to getProperty. /// </remarks> private void InitializeProperties() { _loadedProperties = new Properties(); if (ContentBase != null) { //NOTE: we will not load any properties where HasIdentity = false - this is because if properties are // added to the property collection that aren't persisted we'll get ysods _loadedProperties.AddRange(ContentBase.Properties.Where(x => x.HasIdentity).Select(x => new Property(x))); return; } if (this.ContentType == null) { return; } //Create anonymous typed list with 2 props, Id and PropertyTypeId of type Int. //This will still be an empty list since the props list is empty. var propData = _loadedProperties.Select(x => new { Id = 0, PropertyTypeId = 0 }).ToList(); string sql = @"select id, propertyTypeId from cmsPropertyData where versionId=@versionId"; using (var sqlHelper = Application.SqlHelper) using (IRecordsReader dr = sqlHelper.ExecuteReader(sql, sqlHelper.CreateParameter("@versionId", Version))) { while (dr.Read()) { //add the item to our list propData.Add(new { Id = dr.Get <int>("id"), PropertyTypeId = dr.Get <int>("propertyTypeId") }); } } foreach (PropertyType pt in this.ContentType.PropertyTypes) { if (pt == null) { continue; } //get the propertyId var property = propData.LastOrDefault(x => x.PropertyTypeId == pt.Id); if (property == null) { continue; //var prop = Property.MakeNew(pt, this, Version); //property = new {Id = prop.Id, PropertyTypeId = pt.Id}; } var propertyId = property.Id; Property p = null; try { p = new Property(propertyId, pt); } catch { continue; //this remains from old code... not sure why we would do this? } _loadedProperties.Add(p); } }
public static void SetDefaultValuesOnNew(Document newDocument, umbraco.cms.businesslogic.NewEventArgs e) { log.Info("SetDefaultValuesOnNew Startet"); log.InfoFormat("Document id: {1} - Document Type Alias: {0}", newDocument.ContentType.Alias, newDocument.Id); // We wrap it in a try since this part should not break for the user in umbraco try { string path = newDocument.Path; if (string.IsNullOrEmpty(path)) { StopWithError("Path on new document not found!"); return; } string[] strings = StringUtil.Split(path, ",", true); string strHomeDocId = strings[1]; // Now we have the doc home id, lets see if the default value document based on document type alias exists string cacheKeyDefaultValueDocumentTypeAlias = string.Format("SetDefaultValuesOnNew#homeDoc:{0}#DefaultValueDocumentTypeAlias:{1}", strHomeDocId, newDocument.ContentType.Alias); int defaultValueDocId = -1; if (CacheUtil.Exist(cacheKeyDefaultValueDocumentTypeAlias)) { defaultValueDocId = CacheUtil.Get <int>(cacheKeyDefaultValueDocumentTypeAlias); } else { // Does not exist in the cache, lets try to look it up int defaultValuesFolderDocId = GetDefaultValuesFolderId(strHomeDocId); if (defaultValuesFolderDocId == -1) { StopWithError("Could not load default values folder id!"); return; } // For speed we use the published node factory Node node = UmbracoUtil.GetNode(defaultValuesFolderDocId); if (node == null) { StopWithError(string.Format("Could not load published node for default values folder id: {0}!", defaultValuesFolderDocId)); return; } Node defaultValueNode = node.GetDescendantViaDocumentTypePath(newDocument.ContentType.Alias); if (defaultValueNode != null) { defaultValueDocId = defaultValueNode.Id; CacheUtil.Insert(cacheKeyDefaultValueDocumentTypeAlias, defaultValueDocId, 10, false); } else { log.DebugFormat("No default node for document type alias: {0}", newDocument.ContentType.Alias); } } if (defaultValueDocId > -1) { Document defaultValueDocument = new Document(true, defaultValueDocId); log.DebugFormat("ContentType {0} match. Let's start copying", newDocument.ContentType.Alias); umbraco.cms.businesslogic.property.Properties newDocumentProperties = newDocument.GenericProperties; umbraco.cms.businesslogic.property.Properties defaultValueProperties = defaultValueDocument.GenericProperties; for (int i = 0; i < newDocumentProperties.Count; i++) { umbraco.cms.businesslogic.property.Property newDocumentProperty = newDocumentProperties[i]; umbraco.cms.businesslogic.property.Property defaultDocumentProperty = defaultValueProperties[i]; if (newDocumentProperty.Value != defaultDocumentProperty.Value) { log.DebugFormat("Set value on property alias {0}", newDocumentProperty.PropertyType.Alias); newDocumentProperty.Value = defaultDocumentProperty.Value; } } } log.Info("SetDefaultValuesOnNew Ended"); } catch (Exception ex) { log.Error("Could not complete SetDefaultValuesOnNew - stopped with exception", ex); } }
/// <summary> /// Loads all properties from database into objects. If this method is re-called, it will re-query the database. /// </summary> /// <remarks> /// This optimizes sql calls. This will first check if all of the properties have been loaded. If not, /// then it will query for all property types for the current version from the db. It will then iterate over each /// cmdPropertyData row and store the id and propertyTypeId in a list for lookup later. Once the records have been /// read, we iterate over the cached property types for this ContentType and create a new property based on /// our stored list of proeprtyTypeIds. We then have a cached list of Property objects which will get returned /// on all subsequent calls and is also used to return a property with calls to getProperty. /// </remarks> private void InitializeProperties() { m_LoadedProperties = new Properties(); if (ContentBase != null) { m_LoadedProperties.AddRange(ContentBase.Properties.Select(x => new Property(x))); return; } if (this.ContentType == null) return; //Create anonymous typed list with 2 props, Id and PropertyTypeId of type Int. //This will still be an empty list since the props list is empty. var propData = m_LoadedProperties.Select(x => new { Id = 0, PropertyTypeId = 0 }).ToList(); string sql = @"select id, propertyTypeId from cmsPropertyData where versionId=@versionId"; using (IRecordsReader dr = SqlHelper.ExecuteReader(sql, SqlHelper.CreateParameter("@versionId", Version))) { while (dr.Read()) { //add the item to our list propData.Add(new { Id = dr.Get<int>("id"), PropertyTypeId = dr.Get<int>("propertyTypeId") }); } } foreach (PropertyType pt in this.ContentType.PropertyTypes) { if (pt == null) continue; //get the propertyId var property = propData.LastOrDefault(x => x.PropertyTypeId == pt.Id); if (property == null) { continue; //var prop = Property.MakeNew(pt, this, Version); //property = new {Id = prop.Id, PropertyTypeId = pt.Id}; } var propertyId = property.Id; Property p = null; try { p = new Property(propertyId, pt); } catch { continue; //this remains from old code... not sure why we would do this? } m_LoadedProperties.Add(p); } }
public static void ReadProperties(Type contentType, Content content, object output) { foreach (PropertyInfo propInfo in contentType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { DocumentTypePropertyAttribute propAttr = Util.GetAttribute <DocumentTypePropertyAttribute>(propInfo); if (propAttr == null) { continue; // skip this property - not part of a Document Type } string propertyName; string propertyAlias; DocumentTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias); umbraco.cms.businesslogic.property.Property property = content.getProperty(propertyAlias); object value = null; try { if (property == null) { value = null; } else if (propInfo.PropertyType.Equals(typeof(System.Boolean))) { if (String.IsNullOrEmpty(Convert.ToString(property.Value)) || Convert.ToString(property.Value) == "0") { value = false; } else { value = true; } } else if (PropertyConvertors.ContainsKey(propInfo.PropertyType)) { value = ContentUtil.GetInnerXml(content.Id.ToString(), propertyAlias); } else if (String.IsNullOrEmpty(Convert.ToString(property.Value))) { // if property type is string or if it's some custom type, try to get the inner xml of this property within a node. if (propInfo.PropertyType == typeof(string) || PropertyConvertors.ContainsKey(propInfo.PropertyType)) { value = ContentUtil.GetInnerXml(content.Id.ToString(), propertyAlias); if (value == null && propInfo.PropertyType == typeof(string)) { value = string.Empty; } } else { value = null; } } else if (propInfo.PropertyType.IsGenericType && propInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { value = Convert.ChangeType(property.Value, Nullable.GetUnderlyingType(propInfo.PropertyType)); // TODO: If data type is DateTime and is nullable and is less than 1.1.1000 than set it to NULL } else { value = Convert.ChangeType(property.Value, propInfo.PropertyType); } if (PropertyConvertors.ContainsKey(propInfo.PropertyType)) { value = PropertyConvertors[propInfo.PropertyType].ConvertValueWhenRead(value); } propInfo.SetValue(output, value, null); } catch (Exception exc) { throw new Exception(string.Format("Cannot set the value of a document type property {0}.{1} (document type: {2}) to value: '{3}' (value type: {4}). Error: {5}", contentType.Name, propInfo.Name, propInfo.PropertyType.FullName, value, value != null ? value.GetType().FullName : "", exc.Message)); } } }
/// <summary> /// Return a value of a PreValue by Property /// </summary> /// <param name="parameter">UmbracoType</param> /// <param name="value">value as a Property</param> /// <returns>Returns the value of the Property if exists, if not returns an empty string</returns> public static string PropertyValue(UmbracoType parameter, Property value) { string result = string.Empty; if (value.Value.ToString() != "") { int id = Convert.ToInt32(GetParameterValue(parameter)); IEnumerable<PreValue> data = DataTypeValue(id); PreValue preValue = data.SingleOrDefault(dt => dt.Id == Convert.ToInt32(value.Value)); result = (preValue != null) ? preValue.Value : string.Empty; } return result; }
/// <summary> /// Get the Content of a UComponenet Grid /// </summary> /// <param name="property">Property</param> /// <returns>List of dymamic objects</returns> public static IEnumerable<dynamic> GetDataTypeGrid(Property property) { List<dynamic> result = new List<dynamic>(); if (property != null) { foreach (XElement element in XDocument.Parse(property.Value.ToString()).Descendants("item")) { dynamic data = new ExpandoObject(); ((IDictionary<string, object>)data).Add("id", element.Attribute("id").Value); ((IDictionary<string, object>)data).Add("sortorder", element.Attribute("sortOrder").Value); foreach (XElement item in element.Descendants()) { ((IDictionary<string, object>)data).Add(item.Name.LocalName, item.Value); } result.Add(data); } } return result; }