/// <summary> /// Creates a child entity /// </summary> /// <param name="parent">Parent entity</param> /// <param name="server">Aptify server connection</param> /// <param name="store">Entity storage</param> private AptifyEntity(AptifyEntity parent, AptifyServer server, EntityStore store) { if (server == null) { throw new ArgumentNullException("server"); } if (store == null) { throw new ArgumentNullException("store"); } this.parent = parent; this.server = server; this.store = store; // Get the entity that is mapped to this model instance this.table = server.Tables.GetTableMetadata(store.EntityObject); // Get the aptify generic entity and load its contents this.genericEntity = GetGenericEntity(server, parent, store, this.table.Entity); UpdateEntityContent( ); // Load any child items if (store.Persister.HasCascades) { this.children = LoadChildEntities( ).ToArray( ); } }
/// <summary> /// Delete the aptify entity /// </summary> public void Delete( ) { try { AptifyGenericEntityBase aptifyEntity = this.server.GetEntity(this.table.Entity, this.store); AptifyNHibernateTransaction transaction = this.store.Transaction; if (transaction == null) { if (!aptifyEntity.Delete( )) { throw new DataException(aptifyEntity.LastError); } } else { if (!aptifyEntity.Delete(transaction.TransactionName)) { throw new DataException(aptifyEntity.LastError); } } Log.InfoFormat("Deleted {0}[{1}]", this.table.Name, aptifyEntity.RecordID); } catch (Exception e) { Log.ErrorFormat("Could not delete entity {0}: {1}", this.table.Name, e); throw; } }
/// <summary> /// Save the given entity to the database /// </summary> /// <param name="genericEntity">Generic entity to save</param> /// <param name="transaction">Transaction to save within</param> /// <returns>The new ID of the entity</returns> public int SaveEntity(AptifyGenericEntityBase genericEntity, IAptifyTransaction transaction) { string message = String.Empty; string transactionName = (transaction == null) ? null : transaction.TransactionName; // Validate the entity for errors if (!genericEntity.Validate(ref message)) { throw new DataException(message); } // Try saving the entity if (!genericEntity.Save(false, ref message, transactionName)) { // It's possible the error message is not returned if (String.IsNullOrEmpty(message)) { message = genericEntity.LastError; } throw new DataException(message); } return(( int )genericEntity.RecordID); }
/// <summary> /// Updates the column /// </summary> /// <param name="genericEntityBase">Entity to update</param> /// <param name="column">Column description</param> /// <param name="state">Column state</param> /// <param name="doCascade">Should this column be persisted if it's a foreign key column</param> private void SetColumnValue(AptifyGenericEntityBase genericEntityBase, AptifyColumnMetadata column, object state, bool doCascade) { if (genericEntityBase == null) { throw new ArgumentNullException("genericEntityBase"); } if (column == null) { throw new ArgumentNullException("column"); } if (column.IsForeignKeyColumn && state != null || state is INHibernateProxy) { if (doCascade) { var cascadeStore = new EntityStore(this.store.Session, state); AptifyEntity cascadeEntity; if (column.IsEmbedded) { cascadeEntity = new AptifyEntity(this.GenericEntity.Fields[column.Name], this.server, cascadeStore); } else { cascadeEntity = new AptifyEntity(this.server, cascadeStore); } state = cascadeEntity.SaveOrUpdate( ); } else { state = this.store.Session.GetIdentifier(state); } } genericEntityBase.SetValue(column.Name, state); }
private AptifyEntity(AptifyDataFieldBase field, AptifyServer server, EntityStore store) { if (!field.EmbeddedObjectExists) { throw new InvalidOperationException(field.Name + " is not an embedded object"); } this.server = server; this.store = store; // Get the entity that is mapped to this model instance this.table = server.Tables.GetTableMetadata(store.EntityObject); this.genericEntity = field.EmbeddedObject; UpdateEntityContent( ); // Load any child items if (store.Persister.HasCascades) { this.children = LoadChildEntities( ).ToArray( ); } //this.store.MarkAsPersisted( 0 ); }