///// <summary> ///// Gets the id of entity. ///// </summary> ///// <value>The entity id or null for new instance.</value> //[TikProperty(".id", typeof(string), true, TikPropertyEditMode.ReadOnly)] //public string Id //{ // get { return properties.GetAsStringOrNull(".id"); } //} /// <summary> /// See <see cref="ITikEntity.LoadFromEntityRow"/> for details. /// Calls <see cref="OnCustomLoadFromEntityRow"/>. /// </summary> /// <remarks>Sets object state to unmodified by <see cref="MarkClear"/> call!</remarks> public void LoadFromEntityRow(ITikEntityRow entityRow) { //attributes.CreateAttribute(".id", entityRow.GetValue(".Id")); TikEntityMetadata entityMetadata = TikEntityMetadata.Get(GetType()); foreach (KeyValuePair <string, TikPropertyAttribute> propPair in entityMetadata.Properties) { if (propPair.Value.PropertyType == typeof(string)) { Properties.CreatePropertyWithValue(propPair.Key, entityRow.GetStringValueOrNull(propPair.Key, propPair.Value.Mandatory)); } else if ((propPair.Value.PropertyType == typeof(long)) || propPair.Value.PropertyType == typeof(long?)) { Properties.CreatePropertyWithValue(propPair.Key, entityRow.GetInt64ValueOrNull(propPair.Key, propPair.Value.Mandatory)); //long.Parse(entityRow.GetValue(propPair.Key), System.Globalization.CultureInfo.CurrentCulture) } else if ((propPair.Value.PropertyType == typeof(bool)) || (propPair.Value.PropertyType == typeof(bool?))) { Properties.CreatePropertyWithValue(propPair.Key, entityRow.GetBoolValueOrNull(propPair.Key, propPair.Value.Mandatory)); //string.Equals(entityRow.GetValue(propPair.Key), "true", StringComparison.OrdinalIgnoreCase) } else { throw new NotImplementedException(string.Format(CultureInfo.CurrentCulture, "Not supported property '{0}' type '{1}' in {2}.", propPair.Key, propPair.Value.PropertyType, this)); } //catch (FormatException ex) //{ // throw new FormatException(string.Format("Value '{0}' of property '{1}' can not be parsed to type '{2}' in {3}", // entityRow.GetStringValue(propPair.Key), propPair.Key, propPair.Value.PropertyType, this), ex); //} } OnCustomLoadFromEntityRow(entityRow); MarkClear(); }
/// <summary> /// Saves this instance (is protected because not all list are savable = <see cref="IEditableTikList"/> - derived class should expose public Save() method.) /// - saves all entities tha are in <see cref="TikEntityBase.IsModified"/>, /// <see cref="TikEntityBase.IsMarkedDeleted"/> and <see cref="TikEntityBase.IsMarkedNew"/> states. /// Uses session from constructor. /// </summary> protected void SaveInternal() { if (typeof(TEntity).GetInterface(typeof(ITikEntityWithId).Name) == null) throw new NotSupportedException("TEntity must be of ITikEntityWithId type when usign SaveInternal method."); TikEntityMetadata metadata = TikEntityMetadata.Get(typeof(TEntity)); logger.DebugFormat("Going to save {0}/{1}/{2} new/update/delete items.", NewCount, UpdatedCount, DeletedCount); SaveAllNew(metadata); //must be before position change!!! AfterSaveAllNew(metadata, session); SaveAllUpdated(metadata); AfterSaveAllUpdated(metadata, session); SaveAllDeleted(metadata); // must be after position changes AfterSaveAllDeleted(metadata, session); logger.Debug("Successfully saved."); }
protected Dictionary <string, string> GetAllModifiedProperties() { TikEntityMetadata metadata = TikEntityMetadata.Get(GetType()); Dictionary <string, string> result = new Dictionary <string, string>(); foreach (KeyValuePair <string, TikPropertyAttribute> propPair in metadata.Properties) { bool found; bool modified; bool hasValue; properties.GetPropertyState(propPair.Key, out found, out modified, out hasValue); if ((propPair.Value.EditMode == TikPropertyEditMode.Editable) && found && modified) { string valueAsText; if (!hasValue) { valueAsText = ""; } else { if (propPair.Value.PropertyType == typeof(string)) { valueAsText = properties.GetAsString(propPair.Key); } else if ((propPair.Value.PropertyType == typeof(long)) || propPair.Value.PropertyType == typeof(long?)) { valueAsText = properties.GetAsInt64(propPair.Key).ToString(CultureInfo.InvariantCulture); } else if ((propPair.Value.PropertyType == typeof(bool)) || (propPair.Value.PropertyType == typeof(bool?))) { valueAsText = properties.GetAsBoolean(propPair.Key) ? "yes" : "no"; } else { throw new NotImplementedException(string.Format(CultureInfo.CurrentCulture, "Not supported property '{0}' type '{1}' in {2}.", propPair.Key, propPair.Value.PropertyType, this)); } } result.Add(propPair.Key, valueAsText); } } return(result); }
private List<TEntity> LoadItemsInternal(TikConnectorQueryFilterDictionary filter, TikSession session) { List<TEntity> result = new List<TEntity>(); TikEntityMetadata entityMetadata = TikEntityMetadata.Get(typeof(TEntity)); IEnumerable<ITikEntityRow> response; if (filter != null) response = session.Connector.ExecuteReader(entityMetadata.EntityPath, entityMetadata.ReaderBehavior, entityMetadata.PropertyNames, filter); else response = session.Connector.ExecuteReader(entityMetadata.EntityPath, entityMetadata.ReaderBehavior, entityMetadata.PropertyNames); VerifyResponseRows(response); foreach (ITikEntityRow entityRow in response) { TEntity item = new TEntity(); item.LoadFromEntityRow(entityRow); result.Add(item); } logger.DebugFormat("{0} items loaded.", result.Count); return result; }