//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ public void AddUser(int boardId, String userName) { ActiveBoard board = this.GetBoard(boardId); /*cream un nou user*/ ActiveUser user = new ActiveUser() { Changes = new List<Change>(), Right = ActiveUser.Rights.Write, Username = userName }; /*cream si o noua entitate*/ UserRight entity = new UserRight() { BoardId = boardId, Right = 1, User = userName }; user.Entity = entity; /*adaugam in bd*/ lock (this._em.UserRights) { this._em.UserRights.AddObject(user.Entity); this._em.SaveChanges(); } /*adaugam si in active board*/ board.UsersTable.Add(user.Username, user); }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ private ActiveBoard _LoadBoard(int id) { ActiveBoard newBoard = new ActiveBoard(); var res = (from d in this._em.Boards where d.Id == id select d).ToList(); if (res.Count() < 1) return null; Board b = res[0]; /* ii salvam entitatea*/ newBoard.BoardEntity = b; /* adaugam layers din bd */ var res1 = (from d in this._em.Layers where d.BoardId == b.Id select d).ToList(); newBoard.LayerStack = new List<ActiveLayer>(res1.Count); foreach (Layer l in res1) { ActiveLayer newLayer = new ActiveLayer(); newLayer.LayerEntity = l; newLayer.Id = l.LayerId; newLayer.Name = l.Name; newBoard.Layers.Add(l.LayerId, newLayer); if (l.Order > newBoard.LayerStack.Count) newBoard.LayerStack.Add(newLayer); else newBoard.LayerStack.Insert(l.Order, newLayer); } /* adaugam widgeturile pentru fiecare layer*/ foreach (KeyValuePair<int, ActiveLayer> activeLayerPair in newBoard.Layers) { var res2 = (from d in this._em.Widgets where d.LayerId == activeLayerPair.Key select d).ToList(); activeLayerPair.Value.WidgetsStack = new List<ActiveWidget>(res2.Count); foreach (Widget w in res2) { ActiveWidget newWidget = new ActiveWidget(); newWidget.WidgetEntity = w; newWidget.WidgetId = w.WidgetId; newWidget.Type = w.Type; newWidget.Data = w.Data; newWidget.Name = w.Name; try { activeLayerPair.Value.Widgets.Add(newWidget.WidgetId, newWidget); if (w.Order > activeLayerPair.Value.WidgetsStack.Count) activeLayerPair.Value.WidgetsStack.Add(newWidget); else activeLayerPair.Value.WidgetsStack.Insert(w.Order, newWidget); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } /* adaugam userii care au drepturi asupra boardului*/ var res3 = (from d in this._em.UserRights where d.BoardId == id select d).ToList(); foreach (UserRight ur in res3) { ActiveUser u = new ActiveUser(); u.Username = ur.User; newBoard.UsersTable.Add(u.Username, u); } /* adaugam noul board incarcat in lista activa*/ this._activeBoards.Add(b.Id, newBoard); return newBoard; }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ public void PushChange(int boardId, Change change, IPrincipal user) { /*punem la ceialti utilizatori modificarea produsa*/ if (!this._activeBoards.ContainsKey(boardId)) { throw new Exception("Board id not found in ActiveBoards"); } ActiveBoard board = null; try { board = this.GetBoard(boardId); } catch (KeyNotFoundException ex) { return; } if (!board.UsersTable.ContainsKey(user.Identity.Name)) { throw new Exception("Username not found in board's userlist"); } switch (change.Operation) { case "add": if (change.Obj == "layer") { this._AddLayer(board, change); } else if (change.Obj == "widget") { this._AddWidget(board, change); } else if (change.Obj == "user") { this._AddUser(board, change); } break; case "remove": /*il stergem din entities si din dictionar*/ if (change.Obj == "layer") { ActiveLayer layer = board.Layers[change.Id]; /*scoatem widgets entities pt acest layer si le stergem din _em*/ foreach (ActiveWidget w in layer.WidgetsStack) { this._em.DeleteObject(w.WidgetEntity); } /*stergem si acest layer din board din _em*/ this._em.DeleteObject(layer.LayerEntity); /*stergem layerul din activelayers pt acest board*/ lock (board.Layers) { board.Layers.Remove(change.Id); } lock (board.LayerStack) { board.LayerStack.Remove(layer); } } else if (change.Obj == "widget") { /*get the layer id*/ var jss = new JavaScriptSerializer(); ChangeDataWidget cd = jss.Deserialize <ChangeDataWidget>(change.ChangeString); /*delete from entities*/ Widget entity = board.Layers[cd.LayerId].GetWidget(cd.Id).WidgetEntity; this._em.Widgets.DeleteObject(entity); /*delete the widget from the layer of that board*/ board.Layers[cd.LayerId].RemoveWidget(cd.Id); } else if (change.Obj == "user") { /*get the layer id*/ var jss = new JavaScriptSerializer(); ChangeDataUser cd = jss.Deserialize <ChangeDataUser>(change.ChangeString); ActiveUser auser = board.UsersTable[cd.Name]; /*delete from entities*/ this._em.DeleteObject(auser.Entity); /*delete the widget from the layer of that board*/ lock (board.Users) { board.Users.Remove(auser); } lock (board.UsersTable) { board.UsersTable.Remove(auser.Username); } } break; case "change": if (change.Obj == "layer") { this._ChangeLayer(board, change); } else if (change.Obj == "widget") { this._ChangeWidget(board, change); } /*pentru user nu avem ce modifica*/ break; } foreach (KeyValuePair <string, ActiveUser> uPair in board.UsersTable) { if (uPair.Value.Username != user.Identity.Name) { uPair.Value.Changes.Add(change); } } }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ private void _AddUser(ActiveBoard b, Change change) { ActiveUser user = new ActiveUser(); /*deserializam obiectul primit*/ var jss = new JavaScriptSerializer(); ChangeDataUser cd = jss.Deserialize<ChangeDataUser>(change.ChangeString); this.AddUser(b.BoardEntity.Id, cd.Name); }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ private ActiveBoard _LoadBoard(int id) { ActiveBoard newBoard = new ActiveBoard(); var res = (from d in this._em.Boards where d.Id == id select d).ToList(); if (res.Count() < 1) { return(null); } Board b = res[0]; /* ii salvam entitatea*/ newBoard.BoardEntity = b; /* adaugam layers din bd */ var res1 = (from d in this._em.Layers where d.BoardId == b.Id select d).ToList(); newBoard.LayerStack = new List <ActiveLayer>(res1.Count); foreach (Layer l in res1) { ActiveLayer newLayer = new ActiveLayer(); newLayer.LayerEntity = l; newLayer.Id = l.LayerId; newLayer.Name = l.Name; newBoard.Layers.Add(l.LayerId, newLayer); if (l.Order > newBoard.LayerStack.Count) { newBoard.LayerStack.Add(newLayer); } else { newBoard.LayerStack.Insert(l.Order, newLayer); } } /* adaugam widgeturile pentru fiecare layer*/ foreach (KeyValuePair <int, ActiveLayer> activeLayerPair in newBoard.Layers) { var res2 = (from d in this._em.Widgets where d.LayerId == activeLayerPair.Key select d).ToList(); activeLayerPair.Value.WidgetsStack = new List <ActiveWidget>(res2.Count); foreach (Widget w in res2) { ActiveWidget newWidget = new ActiveWidget(); newWidget.WidgetEntity = w; newWidget.WidgetId = w.WidgetId; newWidget.Type = w.Type; newWidget.Data = w.Data; newWidget.Name = w.Name; try { activeLayerPair.Value.Widgets.Add(newWidget.WidgetId, newWidget); if (w.Order > activeLayerPair.Value.WidgetsStack.Count) { activeLayerPair.Value.WidgetsStack.Add(newWidget); } else { activeLayerPair.Value.WidgetsStack.Insert(w.Order, newWidget); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } /* adaugam userii care au drepturi asupra boardului*/ var res3 = (from d in this._em.UserRights where d.BoardId == id select d).ToList(); foreach (UserRight ur in res3) { ActiveUser u = new ActiveUser(); u.Username = ur.User; newBoard.UsersTable.Add(u.Username, u); } /* adaugam noul board incarcat in lista activa*/ this._activeBoards.Add(b.Id, newBoard); return(newBoard); }