/// <summary> /// Método que genera la cadena de un Insert a partir de una entidad /// </summary> /// <param name="entity">entidad de la cual se queire obtener un insert</param> /// <returns>Statement</returns> public static Statement GetInsert(IEntity entity) { if (entity == null) { throw new ZeroException(ZeroCodes.ERR_00_01); } StringBuilder statement = new StringBuilder(); StringBuilder statementParams = new StringBuilder(); IDictionary <string, object> values = new Dictionary <string, object>(); //obtenemos el nombre de la tabla Table table = entity.GetType().GetCustomAttribute <Table>(); string nameTable = table == null?entity.GetType().Name : table.Name; //se deben obtener todas las propiedades, incluidas las de la super clase PropertyInfo[] properties = entity.GetType().GetProperties(); //generamos la base de la sentencia de insert statement.Append("Insert into "); statement.Append(nameTable); statement.Append(" ("); bool isFirst = true; string columnaId = null; bool isIdSequence = false; foreach (PropertyInfo property in properties) { Column columna = property.GetCustomAttribute <Column>(); IdColumn idColumn = property.GetCustomAttribute <IdColumn>(); if (columna != null && columna.Exclude) { continue; } if (idColumn != null) { columnaId = property.Name; } bool sequence = idColumn != null && idColumn.Identity; if (sequence) { isIdSequence = true; } string nameColumn = columna == null ? property.Name : columna.Name; //si no es secuencia hacemos el proceso if (!sequence) { if (isFirst) { isFirst = false; } else { statement.Append(", "); statementParams.Append(","); } statementParams.Append("@"); statementParams.Append(nameColumn); //si no hay anotacion de columna ponemos el nombre de la propiedad de la entidad statement.Append(nameColumn); object value = GetValue(property.GetValue(entity, null)); values.Add("@" + nameColumn, value); } } statement.Append(" ) Values ("); statement.Append(statementParams); statement.Append(")"); Statement statementComplete = new Statement(statement.ToString(), values) { ColumnId = columnaId, IdentityColumn = isIdSequence }; return(statementComplete); }
public static Statement GetUpdate(IEntity entity) { if (entity == null) { throw new ZeroException(ZeroCodes.ERR_00_01); } StringBuilder statement = new StringBuilder(); StringBuilder statementWhere = new StringBuilder(); IDictionary <string, object> values = new Dictionary <string, object>(); //obtenemos el nombre de la tabla Table table = entity.GetType().GetCustomAttribute <Table>(); string nameTable = table == null?entity.GetType().Name : table.Name; //se deben obtener todas las propiedades, incluidas las de la super clase PropertyInfo[] properties = entity.GetType().GetProperties(); statement.Append("Update "); statement.Append(nameTable); statement.Append(" SET "); bool isFirst = true; object valueId = null; foreach (PropertyInfo property in properties) { Column columna = property.GetCustomAttribute <Column>(); IdColumn idColumn = property.GetCustomAttribute <IdColumn>(); if (columna != null && columna.Exclude) { continue; } string nameColumn = columna == null ? property.Name : columna.Name; //obtenemos el valor del id y la columna para el where del update if (idColumn != null) { statementWhere.Append(" Where "); statementWhere.Append(nameColumn); statementWhere.Append(" = @"); statementWhere.Append(nameColumn); valueId = GetValue(property.GetValue(entity, null)); } else //si no es id { if (isFirst) { isFirst = false; } else { statement.Append(", "); } statement.Append(nameColumn); statement.Append(" = @"); statement.Append(nameColumn); } values.Add("@" + nameColumn, GetValue(property.GetValue(entity, null))); } if (valueId == null) { throw new ZeroException(ZeroCodes.ERR_00_02); } statement.Append(statementWhere); Statement statementComplete = new Statement(statement.ToString(), values); return(statementComplete); }
/// <summary> /// Initializes /// </summary> public void Initialize() { Builder.AddFromFile("MainWin.glade"); TaskPropertyDialogMaker.Initialize(); MainWindow = Builder.GetObject("MainWindow") as Window; MainWindow.Destroyed += app_quit; TaskStore = Builder.GetObject("TaskStore") as TreeStore; TaskList = (TreeView)Builder.GetObject("TaskView"); TaskSelection = (TreeSelection)Builder.GetObject("TaskSelection"); //TaskStore = new TreeStore (GType.Int, GType.String, GType.String); TaskStore = (TreeStore)Builder.GetObject("TaskStore"); CatStore = (ListStore)Builder.GetObject("CatStore"); buildCats(); TaskList.Model = TaskStore; IdColumn = (TreeViewColumn)Builder.GetObject("TaskIdColumn"); NameColumn = (TreeViewColumn)Builder.GetObject("TaskNameColumn"); StateColumn = (TreeViewColumn)Builder.GetObject("TaskStatusColumn"); var idCellRendererText = new CellRendererText(); var nameCellRendererText = new CellRendererText(); var stateCellRendererText = new CellRendererText(); IdColumn.PackStart(idCellRendererText, true); NameColumn.PackStart(nameCellRendererText, true); StateColumn.PackStart(stateCellRendererText, true); IdColumn.AddAttribute(idCellRendererText, "text", (int)ColAssign.Id); NameColumn.AddAttribute(nameCellRendererText, "text", (int)ColAssign.Name); StateColumn.AddAttribute(stateCellRendererText, "text", (int)ColAssign.State); nameCellRendererText.Editable = true; nameCellRendererText.Edited += nameChanged; var catNameCellRender = (CellRendererText)Builder.GetObject("CatStatusNameRender"); catNameCellRender.Editable = true; catNameCellRender.Edited += delegate(object o, EditedArgs args) { TreeIter iter; CatStore.GetIterFromString(out iter, args.Path); CatStore.SetValue(iter, 1, args.NewText); var catId = (int)CatStore.GetValue(iter, 0); Tasks.GetById <Category> (catId).Name = args.NewText; }; StatusBar = Builder.GetObject("Status bar") as Statusbar; NewTaskAction = Builder.GetObject("actNewTask") as Gtk.Action; NewChildTask = Builder.GetObject("actNewChild") as Gtk.Action; RemoveTask = Builder.GetObject("actRemove") as Gtk.Action; StartTask = Builder.GetObject("actStart") as Gtk.Action; StopTask = Builder.GetObject("actStop") as Gtk.Action; FinishTask = Builder.GetObject("actFinish") as Gtk.Action; EditTask = Builder.GetObject("actEditTask") as Gtk.Action; ((Gtk.Action)Builder.GetObject("actSave")).Activated += save; ((Gtk.Action)Builder.GetObject("actSaveAs")).Activated += saveAs; ((Gtk.Action)Builder.GetObject("actLoad")).Activated += load; ((Gtk.Action)Builder.GetObject("actExit")).Activated += app_quit; ((Gtk.Action)Builder.GetObject("cat.AddCat")).Activated += delegate { var newCat = Tasks.AddCategory(); newCat.Name = "Cat"; CatStore.AppendValues(newCat.Id, newCat.Name, true, true); }; var toggleCatRender = ((CellRendererToggle)Builder.GetObject("CatStatusCellRender")); toggleCatRender.Toggled += delegate(object o, ToggledArgs args) { TreeIter iter; if (!CatStore.GetIterFromString(out iter, args.Path)) { return; } var state = (bool)CatStore.GetValue(iter, 3); var indet = (bool)CatStore.GetValue(iter, 2); if (indet) { indet = false; state = false; } else { if (state) { indet = true; } else { state = true; } } CatStore.SetValue(iter, 3, state); CatStore.SetValue(iter, 2, indet); CurrentFilter.Refilter(); }; ((Gtk.Action)Builder.GetObject("actContractAll")).Activated += delegate { TaskList.CollapseAll(); }; ((Gtk.Action)Builder.GetObject("actExpandAll")).Activated += expandTasks; ((Gtk.Action)Builder.GetObject("actFilterAll")).Activated += delegate { var togActive = (ToggleToolButton)Builder.GetObject("togFilterActive"); var togComplt = (ToggleToolButton)Builder.GetObject("togFilterComplete"); togActive.Active = false; togComplt.Active = false; FilterOptions.ShowCompleted = true; FilterOptions.ShowInactive = true; CurrentFilter.Refilter(); }; ((Gtk.Action)Builder.GetObject("actFilterActive")).Activated += delegate { FilterOptions.ShowInactive = !FilterOptions.ShowInactive; CurrentFilter.Refilter(); }; ((Gtk.Action)Builder.GetObject("actFilterUnfinished")).Activated += delegate { FilterOptions.ShowCompleted = !FilterOptions.ShowCompleted; CurrentFilter.Refilter(); }; NewTaskAction.Activated += newTask; NewChildTask.Activated += newChild; RemoveTask.Activated += removeTask; EditTask.Activated += delegate { TaskPropertyDialogMaker.Task = GetSelectedTask(); TaskPropertyDialogMaker.BuildWindow(); TaskPropertyDialogMaker.AfterResponse = delegate { // iter.HasValue is asserted var iter = GetSelectedIter().Value; reloadIter(iter); }; TaskPropertyDialogMaker.Dialog.Run(); }; StartTask.Activated += delegate { setTaskStatus(GetSelectedIter().Value, TaskStatus.Active); }; StopTask.Activated += delegate { setTaskStatus(GetSelectedIter().Value, TaskStatus.Inactive); }; FinishTask.Activated += delegate { setTaskStatus(GetSelectedIter().Value, TaskStatus.Completed); }; FilterOptions = new TaskFilter(Tasks); FilterOptions.CatRules = getCurrentCatFilter; CurrentFilter = new TreeModelFilter(TaskStore, null); CurrentFilter.VisibleFunc = FilterOptions.ApplyFilter; TaskList.Model = CurrentFilter; TaskSelection.Changed += updateSensibility; updateSensibility(this, null); }