/// <summary> /// 更新策略 /// </summary> public void Update(int strategyId, Strategy newStrategy, string name, string comment) { using (IBlazeDatabase db = DbFactory.GetDatabase()) { try { db.BeginTrans(); SecurityManager sm = AdminServer.TheInstance.SecurityManager; FSEyeObject strategyObject = sm.Get(strategyId); strategyObject.Name = name; strategyObject.Comment = comment; sm.Set(strategyObject, db); newStrategy.SecurityObject = strategyObject; IBlazeTable table = db.GetTable(TableString.StrategyTableName); DataSet strategyData = new DataSet(); table.Get(strategyData); DataTable strategyTable = strategyData.Tables[TableString.StrategyTableName]; AutomationManager am = AdminServer.TheInstance.AutomationManager; byte[] strategyBytes = am.Save(newStrategy.Automation); DataRow[] rows = strategyTable.Select(TableString.StrategyFieldId + "=" + strategyId); if (rows != null && rows.Length > 0) { DataRow row = rows[0]; row[TableString.StrategyFieldEvent] = (int)newStrategy.Event; row[TableString.StrategyFieldEnabled] = (newStrategy.Enabled ? 1 : 0); row[TableString.StrategyFieldAutomation] = strategyBytes; table.Set(strategyTable); } for (int i = 0; i < _strategyList.Count; i++) { Strategy strategy = _strategyList[i] as Strategy; if (strategy.SecurityObject.Id == strategyId) { _strategyList.RemoveAt(i); _strategyList.Add(newStrategy); break; } } db.CommitTrans(); } catch (Exception ex) { if (db != null) db.RollbackTrans(); throw ex; } } }
/// <summary> /// 载入策略配置 /// </summary> public void Load() { this.SecurityObject = AdminServer.TheInstance.SecurityManager.Get(SecurityObjectStrategyManager); _strategyList.Clear(); using (IBlazeDatabase db = DbFactory.GetDatabase()) { IBlazeTable table = db.GetTable(TableString.StrategyTableName); DataSet strategyData = new DataSet(); table.Get(strategyData); DataTable strategyTable = strategyData.Tables[TableString.StrategyTableName]; StrategyManager sm = AdminServer.TheInstance.StrategyManager; AutomationManager am = AdminServer.TheInstance.AutomationManager; foreach (DataRow row in strategyTable.Rows) { int strategyId = (int)row[TableString.StrategyFieldId]; try { Strategy strategy = new Strategy(); strategy.Event = (FSEyeEvent)row[TableString.StrategyFieldEvent]; strategy.Automation = am.Load((byte[])row[TableString.StrategyFieldAutomation]); strategy.Enabled = ((int)row[TableString.StrategyFieldEnabled]) == 0 ? false : true; FSEyeObject secObj = AdminServer.TheInstance.SecurityManager.Get(strategyId); if (secObj != null) { strategy.SecurityObject = secObj; _strategyList.Add(strategy); } } catch (Exception e) { Util.DebugLog(e.StackTrace); continue; } } } }
/// <summary> /// 添加策略 /// </summary> public bool Add(Strategy strategy, string name, string comment) { using (IBlazeDatabase db = DbFactory.GetDatabase()) { try { db.BeginTrans(); SecurityManager sm = AdminServer.TheInstance.SecurityManager; FSEyeObject newObject = sm.Get(SecurityObject.FullPath + SecurityManager.ObjectPathDelimiter + name, db); byte[] strategyBytes = AdminServer.TheInstance.AutomationManager.Save(strategy.Automation); if (newObject != null && strategyBytes != null) { newObject.Comment = comment; sm.Set(newObject); strategy.SecurityObject = newObject; IBlazeTable table = db.GetTable(TableString.StrategyTableName); DataSet strategyData = new DataSet(); table.Get(strategyData); DataTable strategyTable = strategyData.Tables[TableString.StrategyTableName]; DataRow newRow = strategyTable.NewRow(); newRow[TableString.StrategyFieldId] = newObject.Id; newRow[TableString.StrategyFieldEvent] = (int)strategy.Event; newRow[TableString.StrategyFieldAutomation] = strategyBytes; newRow[TableString.StrategyFieldEnabled] = (strategy.Enabled ? 1 : 0); strategyTable.Rows.Add(newRow); table.Set(strategyTable); //设置权限 AdminServer.TheInstance.SecurityManager.CopyAce(newObject.Parent, newObject, true); _strategyList.Add(strategy); db.CommitTrans(); return true; } else { return false; } } catch (Exception ex) { if (db != null) db.RollbackTrans(); throw ex; } } }