public void TestxsiNill_SDOObjectXML() { LearnerPersonal lp = new LearnerPersonal(); // Add a null UPN SifString str = new SifString(null); lp.SetField(LearnerDTD.LEARNERPERSONAL_UPN, str); // Add a null AlertMsg AlertMsg msg = new AlertMsg(AlertMsgType.DISCIPLINE, null); lp.AlertMsgList = new AlertMsgList(msg); msg.SetField(CommonDTD.ALERTMSG, new SifString(null)); // Write the object to a file Console.WriteLine("Writing to file..."); using (Stream fos = File.Open("SifWriterTest.Temp.xml", FileMode.Create, FileAccess.Write)) { SifWriter writer = new SifWriter(fos); lp.SetChanged(true); writer.Write(lp); writer.Flush(); fos.Close(); } // Parse the object from the file Console.WriteLine("Parsing from file..."); SifParser p = SifParser.NewInstance(); using (Stream fis = File.OpenRead("SifWriterTest.Temp.xml")) { lp = (LearnerPersonal)p.Parse(fis, null); } SimpleField upn = lp.GetField(LearnerDTD.LEARNERPERSONAL_UPN); Assert.IsNotNull(upn); SifString rawValue = (SifString)upn.SifValue; Assert.IsNotNull(rawValue); Assert.IsNull(rawValue.Value); Assert.IsNull(upn.Value); AlertMsgList alertMsgs = lp.AlertMsgList; Assert.IsNotNull(alertMsgs); Assert.IsTrue(alertMsgs.Count == 1); msg = (AlertMsg)alertMsgs.GetChildList()[0]; Assert.IsNull(msg.Value); SifSimpleType msgValue = msg.SifValue; Assert.IsNotNull(msgValue); Assert.IsNull(msgValue.RawValue); }
public void OnEvent(Event evnt, IZone zone, IMessageInfo info) { // Demonstrates basic handling of a SIF Event Console.WriteLine("Received a " + evnt.ActionString + " event for LearnerPersonal"); LearnerPersonal sp = (LearnerPersonal)evnt.Data.ReadDataObject(); // Simply write the XML of the event object to System.out Console.WriteLine(sp.ToXml()); Console.WriteLine("End Event"); }
protected override SifDataObject createPersonObject(string id) { LearnerPersonal lp = new LearnerPersonal(); InCare inCare = new InCare(YesNoUnknown.YES, "323"); lp.InCare = inCare; lp.UPN = id; lp.AlertMsgList = new AlertMsgList(new AlertMsg(AlertMsgType.DISCIPLINE, "Student Discipline note")); //lp.AlertMsgList.GetEnumerator return(lp); }
public void OnQueryResults(IDataObjectInputStream data, SIF_Error error, IZone zone, IMessageInfo info) { // Demonstrates basic handling of a SIF_Query response // 1) To read data from a SIF_Response, first check to see if an error was returned if (error != null) { // The provider returned an error message for this SIF_Request Console.WriteLine("An error was received from the provider of LearnerPersonal."); Console.WriteLine(error.SIF_Desc + "\r\n" + error.SIF_ExtendedDesc); return; } // 2) Now, read each object from the DataObjectInputStream until available() returns false while (data.Available) { LearnerPersonal sp = (LearnerPersonal)data.ReadDataObject(); fObjectCount++; Name stuName = sp.PersonalInformation.Name; Console.WriteLine(fObjectCount + ") Refid:{" + sp.RefId + "} Name: " + stuName.GivenName + " " + stuName.FamilyName); } // Demonstration purposes only: print out the total number of objects recieved Console.WriteLine(fObjectCount + " total objects received."); // 3) To determine if you have completed receiving all responses, check the // MorePackets property of the SIFMessageInfo object if ((( SifMessageInfo )info).MorePackets) { Console.WriteLine("Waiting for more packets..."); } else { Console.WriteLine("All requested packets have been received"); } }
void RunLearnerPersonalConsumer() { LearnerPersonalConsumer learnerPersonalConsumer = new LearnerPersonalConsumer(SettingsManager.ConsumerSettings.ApplicationKey); learnerPersonalConsumer.Register(); if (log.IsInfoEnabled) { log.Info("Registered the Consumer."); } try { // Retrieve Bart Simpson using QBE. if (log.IsInfoEnabled) { log.Info("*** Retrieve Bart Simpson using QBE."); } LearnerPersonal exampleLearner = new LearnerPersonal { PersonalInformation = new PersonalInformationType { Name = new NameType { FamilyName = "Simpson", GivenName = "Bart" } } }; IEnumerable <LearnerPersonal> filteredLearners = learnerPersonalConsumer.QueryByExample(exampleLearner); foreach (LearnerPersonal learner in filteredLearners) { if (log.IsInfoEnabled) { log.Info("Filtered learner name is " + learner.PersonalInformation.Name.GivenName + " " + learner.PersonalInformation.Name.FamilyName); } } // Create a new learner. if (log.IsInfoEnabled) { log.Info("*** Create a new learner."); } LearnerPersonal newLearner = ConsumerApp.CreateBruceWayne(); LearnerPersonal retrievedNewLearner = learnerPersonalConsumer.Create(newLearner); if (log.IsInfoEnabled) { log.Info("Created new learner " + newLearner.PersonalInformation.Name.GivenName + " " + newLearner.PersonalInformation.Name.FamilyName); } // Create multiple new learners. if (log.IsInfoEnabled) { log.Info("*** Create multiple new learners."); } List <LearnerPersonal> newLearners = CreateLearners(5); MultipleCreateResponse multipleCreateResponse = learnerPersonalConsumer.Create(newLearners); int count = 0; foreach (CreateStatus status in multipleCreateResponse.StatusRecords) { if (log.IsInfoEnabled) { log.Info("Create status code is " + status.StatusCode); } newLearners[count++].RefId = status.Id; } // Update multiple learners. if (log.IsInfoEnabled) { log.Info("*** Update multiple learners."); } foreach (LearnerPersonal learner in newLearners) { learner.PersonalInformation.Name.GivenName += "o"; } MultipleUpdateResponse multipleUpdateResponse = learnerPersonalConsumer.Update(newLearners); foreach (UpdateStatus status in multipleUpdateResponse.StatusRecords) { if (log.IsInfoEnabled) { log.Info("Update status code is " + status.StatusCode); } } // Delete multiple learners. if (log.IsInfoEnabled) { log.Info("*** Delete multiple learners."); } ICollection <string> refIds = new List <string>(); foreach (CreateStatus status in multipleCreateResponse.StatusRecords) { refIds.Add(status.Id); } MultipleDeleteResponse multipleDeleteResponse = learnerPersonalConsumer.Delete(refIds); foreach (DeleteStatus status in multipleDeleteResponse.StatusRecords) { if (log.IsInfoEnabled) { log.Info("Delete status code is " + status.StatusCode); } } // Retrieve all learners from zone "Gov" and context "Curr". if (log.IsInfoEnabled) { log.Info("*** Retrieve all learners from zone \"Gov\" and context \"Curr\"."); } IEnumerable <LearnerPersonal> learners = learnerPersonalConsumer.Query(zoneId: "Gov", contextId: "Curr"); foreach (LearnerPersonal learner in learners) { if (log.IsInfoEnabled) { log.Info("Learner name is " + learner.PersonalInformation.Name.GivenName + " " + learner.PersonalInformation.Name.FamilyName); } } if (learners.Count() > 1) { // Retrieve a single learner. if (log.IsInfoEnabled) { log.Info("*** Retrieve a single learner."); } string learnerId = learners.ElementAt(1).RefId; LearnerPersonal secondLearner = learnerPersonalConsumer.Query(learnerId); if (log.IsInfoEnabled) { log.Info("Name of second learner is " + secondLearner.PersonalInformation.Name.GivenName + " " + secondLearner.PersonalInformation.Name.FamilyName); } // Update that learner and confirm. if (log.IsInfoEnabled) { log.Info("*** Update that learner and confirm."); } secondLearner.PersonalInformation.Name.GivenName = "Homer"; secondLearner.PersonalInformation.Name.FamilyName = "Simpson"; learnerPersonalConsumer.Update(secondLearner); secondLearner = learnerPersonalConsumer.Query(learnerId); if (log.IsInfoEnabled) { log.Info("Name of second learner has been changed to " + secondLearner.PersonalInformation.Name.GivenName + " " + secondLearner.PersonalInformation.Name.FamilyName); } // Delete that learner and confirm. if (log.IsInfoEnabled) { log.Info("*** Delete that learner and confirm."); } learnerPersonalConsumer.Delete(learnerId); LearnerPersonal deletedLearner = learnerPersonalConsumer.Query(learnerId); bool learnerDeleted = (deletedLearner == null ? true : false); if (learnerDeleted) { if (log.IsInfoEnabled) { log.Info("Learner " + secondLearner.PersonalInformation.Name.GivenName + " " + secondLearner.PersonalInformation.Name.FamilyName + " was successfully deleted."); } } else { if (log.IsInfoEnabled) { log.Info("Learner " + secondLearner.PersonalInformation.Name.GivenName + " " + secondLearner.PersonalInformation.Name.FamilyName + " was NOT deleted."); } } } // Retrieve learners based on Teaching Group using Service Paths. if (log.IsInfoEnabled) { log.Info("*** Retrieve learners based on Teaching Group using Service Paths."); } EqualCondition condition = new EqualCondition() { Left = "TeachingGroups", Right = "597ad3fe-47e7-4b2c-b919-a93c564d19d0" }; IList <EqualCondition> conditions = new List <EqualCondition>(); conditions.Add(condition); IEnumerable <LearnerPersonal> teachingGroupLearners = learnerPersonalConsumer.QueryByServicePath(conditions); foreach (LearnerPersonal learner in teachingGroupLearners) { if (log.IsInfoEnabled) { log.Info("Learner name is " + learner.PersonalInformation.Name.GivenName + " " + learner.PersonalInformation.Name.FamilyName); } } } catch (Exception e) { //if (log.IsInfoEnabled) log.Fatal(e.StackTrace); throw new Exception(this.GetType().FullName, e); } finally { learnerPersonalConsumer.Unregister(); if (log.IsInfoEnabled) { log.Info("Unregistered the Consumer."); } } }
/// <summary> Handles SIF_Responses /// </summary> public virtual void OnQueryResults(IDataObjectInputStream inStream, SIF_Error error, IZone zone, IMessageInfo inf) { SifMessageInfo info = (SifMessageInfo)inf; Console.WriteLine ("\nReceived a query response from agent \"" + info.SourceId + "\" in zone " + zone.ZoneId); IRequestInfo reqInfo = info.SIFRequestInfo; if (reqInfo != null) { Console.WriteLine ("\nResponse was received in {0}. Object State is '{1}'", DateTime.Now.Subtract(reqInfo.RequestTime), reqInfo.UserData); } // // Use the Mappings class to translate the LearnerPersonal objects received // from the zone into a HashMap of field/value pairs, then dump the table // to System.out // // Always check for an error response if (error != null) { Console.WriteLine ("The request for LearnerPersonal failed with an error from the provider:"); Console.WriteLine (" [Category=" + error.SIF_Category + "; Code=" + error.SIF_Code + "]: " + error.SIF_Desc + ". " + error.SIF_ExtendedDesc); return; } // Get the root Mappings object from the configuration file Mappings m = fCfg.Mappings.GetMappings("Default"); // Ask the root Mappings instance to select a Mappings from its // hierarchy. For example, you might have customized the agent.cfg // file with mappings specific to zones, versions of SIF, or // requesting agents. The Mappings.select() method will select // the most appropriate instance from the hierarchy given the // three parameters passed to it. MappingsContext mappings = m.SelectInbound(LearnerDTD.LEARNERPERSONAL, info); Hashtable data = new Hashtable(); StringMapAdaptor sma = new StringMapAdaptor(data, Adk.Dtd.GetFormatter(SifVersion.LATEST)); int count = 0; while (inStream.Available) { Console.WriteLine("Object Number {0}", count++); LearnerPersonal sp = (LearnerPersonal)inStream.ReadDataObject(); // Ask the Mappings object to populate the dictionary with field/value pairs // by using the mapping rules in the configuration file to decompose // the LearnerPersonal object into field values. mappings.Map(sp, sma); // Now dump the field/value pairs to System.out DumpDictionaryToConsole(data); Console.WriteLine(); data.Clear(); } }
/// <summary> Respond to SIF Requests /// </summary> public virtual void OnRequest(IDataObjectOutputStream outStream, Query query, IZone zone, IMessageInfo inf) { SifMessageInfo info = (SifMessageInfo)inf; SifWriter debug = new SifWriter(Console.Out); Console.WriteLine ("Received a request for " + query.ObjectTag + " from agent \"" + info.SourceId + "\" in zone " + zone.ZoneId); // Tell the ADK to automatically filter out any objects that don't meet the requirements // of the query conditions outStream.Filter = query; // Read all learners from the database to populate a HashMap of // field/value pairs. The field names can be whatever we choose as long // as they match the field names used in the <mappings> section of the // agent.cfg configuration file. Each time a record is read, convert it // to a LearnerPersonal object using the Mappings class and stream it to // the supplied output stream. // IDictionary data = new Hashtable(); IDbCommand command = null; Console.WriteLine("The SIF Request was requested in the following SIF Versions"); foreach (SifVersion version in info.SIFRequestVersions) { Console.WriteLine(" - " + version); } Console.WriteLine("This agent will respond in its latest supported version, which is: "); Console.WriteLine(" - " + info.LatestSIFRequestVersion); // Get the root Mappings object from the configuration file Mappings m = fCfg.Mappings.GetMappings("Default").Select (info.SourceId, zone.ZoneId, info.LatestSIFRequestVersion); // Ask the root Mappings instance to select a Mappings from its // hierarchy. For example, you might have customized the agent.cfg // file with mappings specific to zones, versions of SIF, or // requesting agents. The Mappings.selectOutbound() method will select // the most appropriate instance from the hierarchy given the // three parameters passed to it. MappingsContext mappings = m.SelectOutbound(LearnerDTD.LEARNERPERSONAL, info); try { int count = 0; // Query the database for all students command = fConn.CreateCommand(); fConn.Open(); command.CommandText = "SELECT * FROM Learners"; using (IDataReader rs = command.ExecuteReader(CommandBehavior.CloseConnection)) { DataReaderAdaptor dra = new DataReaderAdaptor(rs); while (rs.Read()) { count++; // Finally, create a new LearnerPersonal object and ask the // Mappings to populate it with SIF elements from the HashMap // of field/value pairs. As long as there is an <object>/<field> // definition for each entry in the HashMap, the ADK will take // care of producing the appropriate SIF element/attribute in // the LearnerPersonal object. // LearnerPersonal sp = new LearnerPersonal(); sp.RefId = Adk.MakeGuid(); mappings.Map(sp, dra); // Now write out the LearnerPersonal to the output stream and // we're done publishing this student. // Console.WriteLine("\nThe agent has read these values from the database:"); DumpDictionaryToConsole(data); Console.WriteLine("To produce this LearnerPersonal object:"); debug.Write(sp); debug.Flush(); outStream.Write(sp); data.Clear(); } rs.Close(); } Console.WriteLine ("- Returned " + count + " records from the Student database in response"); } catch (Exception ex) { Console.WriteLine("- Returning a SIF_Error response: " + ex.ToString()); throw new SifException (SifErrorCategoryCode.RequestResponse, SifErrorCodes.REQRSP_GENERIC_ERROR_1, "An error occurred while querying the database for students", ex.ToString(), zone); } finally { if (command != null) { try { fConn.Close(); } catch (Exception ignored) { } } } }