private static bool UpdateMyAttribute(IServerSession session) { var attrBroker = session.DataAccess.Brokers.NodeAttributeDefinition; var dacFactory = session.DataAccess.ModelFactory; IChangeSet changes = session.DataAccess.CreateChangeSet(); INodeAttributeDefinition attr = dacFactory.CreateNodeAttributeDefinition(); attr.DefaultValue = "hello"; attrBroker.UpdateNodeAttributeDefinitions(changes, attr, dacFactory.CreateSelectorByFilter(dacFactory.CreateFilterExpression("Name LIKE \"" + ATTRIBUTE_NAME + "\""))); Console.Write("Updating my NodeAttributeDefinition... "); using (var result = changes.Save()) { if (result.IsSuccess) { Console.WriteLine("success"); return(true); } else { Console.WriteLine("failed: " + result.ToString()); return(false); } } }
private static bool CreateMyAttribute(IServerSession session) { var attrBroker = session.DataAccess.Brokers.NodeAttributeDefinition; IChangeSet changes = session.DataAccess.CreateChangeSet(); INodeAttributeDefinition attr = session.DataAccess.ModelFactory.CreateNodeAttributeDefinition(); attr.Id = new Oid(); attr.Name = ATTRIBUTE_NAME; attr.DefaultValue = "hi"; attrBroker.CreateNodeAttributeDefinition(changes, attr); Console.Write("Creating my NodeAttributeDefinition... "); using (var result = changes.Save()) { if (result.IsSuccess) { // extract the actual id of the created attr def Oid newId = result.IdMap[attr.Id]; Console.WriteLine("success, my attribute def has id {0}", newId); return(true); } else { Console.WriteLine("failed: " + result.ToString()); return(false); } } }
private static bool ActivateTrigger(IServerSession session) { Console.Write("Adding row to trigger payload table..."); IChangeSet changes = session.DataAccess.CreateChangeSet(); IDataRow row = session.ModelFactory.CreateDataRow(); row.ActivationDate = DateTime.Now; row.ExpirationDate = DateTime.Now.AddMinutes(1); // short expirations are good on triggers, because they usually represent timely events and in the case of a player being offline you don't want it to get a wave of stale triggers row.SequenceNumber = 0; row.Values["foo"] = "bar"; // the column names depend upon the actual columns in the trigger session.DataAccess.Brokers.DataTable.CreateDataRow(changes, g_triggerDataTableId, row); using (var result = changes.Save()) { if (result.IsSuccess) { Console.WriteLine("success"); return(true); } else { Console.WriteLine("failed: " + result.ToString()); return(false); } } }
private static bool SetNodeAttributeValue(Oid nodeId, string value, IServerSession session) { IModelFactory dacFactory = session.DataAccess.ModelFactory; INodeBroker nodeBroker = session.DataAccess.Brokers.Node; ISelector nodeIdSel = dacFactory.CreateSelectorById(nodeId); Console.WriteLine("Updating attribute values for Node {0}... ", nodeId); NodeAttributeValues attrValues; using (var result = nodeBroker.ReadSingle(nodeIdSel, null)) { if (result.IsSuccess) { attrValues = result.Value.Attributes; } else { Console.WriteLine("failed: " + result.ToString()); return(false); } } IChangeSet changes = session.DataAccess.CreateChangeSet(); INode node = session.DataAccess.ModelFactory.CreateNode(); node.Attributes = attrValues; node.Attributes[ATTRIBUTE_NAME] = value; nodeBroker.UpdateNodes(changes, node, nodeIdSel); using (var result = changes.Save()) { if (result.IsSuccess) { Console.WriteLine("success"); return(true); } else { Console.WriteLine("failed: " + result.ToString()); return(false); } } }
private static bool DeactivateTriggers(IServerSession session) { Console.Write("Deleting existing rows from trigger payload table..."); IChangeSet changes = session.DataAccess.CreateChangeSet(); session.DataAccess.Brokers.DataTable.DeleteDataRows(changes, g_triggerDataTableId, session.ModelFactory.CreateAllSelector()); using (var result = changes.Save()) { if (result.IsSuccess) { Console.WriteLine("success"); return(true); } else { Console.WriteLine("failed: " + result.ToString()); return(false); } } }
private void m_saveRowsButton_Click(object sender, RoutedEventArgs e) { IChangeSet changes = m_session.DataAccess.CreateChangeSet(); // delete all rows first m_session.DataAccess.Brokers.DataTable.DeleteDataRows(changes, m_table.Id, m_session.ModelFactory.CreateAllSelector()); // delete all files first m_session.DataAccess.Brokers.DataTable.DeleteFileInDataTables(changes, m_table.Id, m_session.ModelFactory.CreateAllSelector()); List <IDataTableField> mediaFields = new List <IDataTableField>(); foreach (IDataTableField field in m_table.DataTableDesigns.Items.FirstOrDefault().DataTableFields.Items) { if (field.Type == DataTableFieldType.Media) { mediaFields.Add(field); } } List <string> mediaFileNames = new List <string>(); foreach (IDataRow row in m_rows) { foreach (IDataTableField mediaField in mediaFields) { if (row.Values.ContainsKey(mediaField.Name) && !mediaFileNames.Contains(row.Values[mediaField.Name])) { mediaFileNames.Add(row.Values[mediaField.Name]); } } m_session.DataAccess.Brokers.DataTable.CreateDataRow(changes, m_table.Id, row); } // delete orphaned files foreach (IFileInDataTable fidt in new List <IFileInDataTable>(m_files)) { if (!mediaFileNames.Contains(fidt.Name)) { m_files.Remove(fidt); } } foreach (IFileInDataTable fidt in m_files) { m_session.DataAccess.Brokers.DataTable.CreateFileInDataTable(changes, m_table.Id, fidt); } using (var result = changes.Save()) { if (result.IsSuccess) { m_errors.Text = ""; } else { m_errors.Text = "Failed to update data table: " + result.ToString(); } } }
private static int importItem(IServerSession session, SetConfig workingConf) { // hook API logger Logger.SetLogSink(HandleLogEntry); try { /*string ncHostname = "10.50.149.13"; * int ncPort = 80;9(*/ Oid targetTable = new Oid(workingConf.oidForWrite); //using (session /*IServerSession session = CSAPI.Create().CreateServerSession(ncHostname, ncPort)*/) //{ //if (false/*!Authenticate(session)*/) //{ // session.Dispose(); // return -1; //} // else // { /* Get access to read the table */ try { var result = session.DataAccess.Brokers.DataTable.ReadSingle( session.ModelFactory.CreateSelectorById(targetTable), new IRelationshipMetaData[] { MetaData.DataTableToDataTableDesign, MetaData.DataTableDesignToDataTableField, MetaData.DataTableToFileInDataTable, MetaData.DataTableToDataRow }); IDataTable datatable = result.Value; if (result.Value == null) { return(2); } } catch (InvalidOperationException) { Console.WriteLine("Connection/authentication error. Reauthenticating before next iteration"); return(-1); } /* The change set is a list of all changes to the database that will be sent after all edits are done */ IChangeSet changes = session.DataAccess.CreateChangeSet(); /* Delete all rows from the table */ session.DataAccess.Brokers.DataTable.DeleteDataRows(changes, targetTable, session.ModelFactory.CreateAllSelector()); /* TODO: Allow support for appending to tables instead of a fixed number of rows */ int i = 1; if (workingConf.allOneRecord == false) { List <List <string> > col_lists = new List <List <string> >(); List <IChangeSet> trigger_changes = new List <IChangeSet>(); /* TODO: Fork the process so that general datatable updates don't have to happen unless there is a change in the state of the triggered table */ foreach (colConf entry in workingConf.cols) { List <string> col_vals = new List <string>(); col_vals.Add(entry.name_of_col); XmlDocument xd = new XmlDocument(); int retries = 3; do //Solves transient network issues that occur when repeatedly pulling updated XML documents { try { xd.Load(entry.source); break; } catch (WebException) { Console.WriteLine("Failed to get XML file from server, retrying . . ."); if (retries <= 0) { Console.WriteLine("Retrying failed. Skipping document until next iteration."); } else { Thread.Sleep(300); } } }while (retries-- > 0); if (retries <= 0) { break; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(xd.NameTable); /* Namespace entries (does not have to be exclusive to the entry that uses it, so long as there are no conflicts!) */ /* nsmgr.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0"); * nsmgr.AddNamespace("m", "http://www.w3.org/2005/Atom"); * nsmgr.AddNamespace("cap", "urn:oasis:names:tc:emergency:cap:1.1"); * nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); */ /* (Experimental) Add user specified namespaces [TODO: Should add error handling so that program can continue if it fails] */ foreach (nsConf ns_info in workingConf.ns_list) { nsmgr.AddNamespace(ns_info.ns, ns_info.ns_source); } XmlNodeList found_nodes = xd.SelectNodes(entry.description, nsmgr); if (entry.firesTrigger) { bool triggered = false; /* Create new trigger_change set if column is triggerable*/ IChangeSet trigger_change = session.DataAccess.CreateChangeSet(); var result2 = session.DataAccess.Brokers.Trigger.ReadSingle(session.ModelFactory.CreateSelectorById(new Oid(entry.triggerID)), null); Oid m_triggerDataTableId = result2.Value.DataTable.Id; foreach (XmlNode nodeInList in found_nodes) { if (!triggered) { triggered = true; // Prevents multiple triggers when there is more than one node in the triggerable column /* Adding a row to the trigger payload table activates the trigger */ IDataRow trigger_row = session.ModelFactory.CreateDataRow(); trigger_row.ActivationDate = DateTime.Now; trigger_row.ExpirationDate = DateTime.Now.AddDays(5); //I really need to figure out how to write this in more clearly. Perhaps being able to clear triggers? trigger_row.SequenceNumber = 0; session.DataAccess.Brokers.DataTable.CreateDataRow(trigger_change, m_triggerDataTableId, trigger_row); trigger_changes.Add(trigger_change); } if (!String.IsNullOrEmpty(entry.attrib)) { col_vals.Add(nodeInList.Attributes[entry.attrib].Value); } else { string valueToAdd = String.IsNullOrEmpty(nodeInList.Value) ? nodeInList.InnerText : nodeInList.Value; col_vals.Add(valueToAdd); } } if (!triggered) // Clears the trigger if no data is added to the table upon update { session.DataAccess.Brokers.DataTable.DeleteDataRows(changes, m_triggerDataTableId, session.ModelFactory.CreateAllSelector()); trigger_changes.Add(trigger_change); } } else { foreach (XmlNode nodeInList in found_nodes) { if (!String.IsNullOrEmpty(entry.attrib)) { col_vals.Add(nodeInList.Attributes[entry.attrib].Value); } else { string valueToAdd = String.IsNullOrEmpty(nodeInList.Value) ? nodeInList.InnerText : nodeInList.Value; col_vals.Add(valueToAdd); } } } col_lists.Add(col_vals); } for (int j = 1; j <= workingConf.numRows; j++) { /* Add new rows to the table */ IDataRow row = session.ModelFactory.CreateDataRow(); row.SequenceNumber = i; row.ActivationDate = new DateTime(1601, 1, 1); row.ExpirationDate = new DateTime(3000, 1, 1); foreach (List <string> col in col_lists) { try { row.Values[col[0]] = col[j]; } catch (ArgumentOutOfRangeException) { break; } catch (IndexOutOfRangeException) { break; } } session.DataAccess.Brokers.DataTable.CreateDataRow(changes, targetTable, row); i++; } changes.Save(); foreach (IChangeSet trig_cha in trigger_changes) { trig_cha.Save(); } } else { /* TODO: Add a generic trigger to sources that are all on one record */ /* Access the Xml data source */ String URLString = workingConf.sourceUrl; XmlDocument xd = new XmlDocument(); xd.Load(URLString); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xd.NameTable); /* (Experimental) Add user specified namespaces [Should add error handling so that program can continue if it fails] */ foreach (nsConf ns_info in workingConf.ns_list) { nsmgr.AddNamespace(ns_info.ns, ns_info.ns_source); } //nsmgr.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0"); XmlNodeList nodeOfRec = xd.SelectNodes(workingConf.itemOfRec, nsmgr); foreach (XmlNode node in nodeOfRec) { //Add new rows to the table IDataRow row = session.ModelFactory.CreateDataRow(); row.SequenceNumber = i; row.ActivationDate = new DateTime(1601, 1, 1); row.ExpirationDate = new DateTime(3000, 1, 1); if (node.HasChildNodes) /* Then the items we are looking for are not attributes of the node of record (but could be attributes of a child node) */ { foreach (XmlNode cnode in node.ChildNodes) { foreach (colConf testcols in workingConf.cols) { if (testcols.description == cnode.Name) { if (!String.IsNullOrEmpty(testcols.attrib)) { row.Values[testcols.name_of_col] = cnode.Attributes[testcols.attrib].Value; } else { row.Values[testcols.name_of_col] = cnode.Value; } } } } } else /* We are looking at the node of record's attributes only */ { XmlAttributeCollection xac = node.Attributes; foreach (XmlAttribute xa in xac) { foreach (colConf testcols in workingConf.cols) { if (testcols.attrib == xa.Name) { row.Values[testcols.name_of_col] = xa.Value; break; } } } session.DataAccess.Brokers.DataTable.CreateDataRow(changes, targetTable, row); i++; } } changes.Save(); } return(0); } finally { // unhook logger Logger.SetLogSink(null); } }