/// <summary> /// Retrieve StudentPersonal records from the database using the specified SQL query and object mappings /// defined in the Agent configuration file. /// </summary> /// <param name="query">SQL query to run.</param> /// <returns>StudentPersonal records.</returns> /// <exception cref="NtAgent.AgentDbException">Error occurred trying to retrieve StudentPersonal records.</exception> /// <exception cref="System.ArgumentException">query parameter is null or empty.</exception> private List <StudentPersonal> GetRecords(String query) { if (String.IsNullOrEmpty(query)) { throw new ArgumentException("query parameter is null or empty."); } List <StudentPersonal> records = new List <StudentPersonal>(); try { DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(databaseDriver); using (IDbConnection conn = dbProviderFactory.CreateConnection()) { conn.ConnectionString = databaseConnectionString; IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = query; conn.Open(); IDataReader reader = cmd.ExecuteReader(); DataReaderAdaptor dataReaderAdaptor = new DataReaderAdaptor(reader); while (reader.Read()) { StudentPersonal record = new StudentPersonal(); mappings.MapOutbound(dataReaderAdaptor, record); records.Add(record); } } } catch (Exception e) { throw new AgentDbException("Unable to retrieve " + typeof(StudentPersonal).Name + " records via database connection " + databaseConnectionString + " due to the following error: " + e.Message, e); } return(records); }
/// <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(StudentDTD.STUDENTPERSONAL, info); try { int count = 0; // Query the database for all students command = fConn.CreateCommand(); fConn.Open(); command.CommandText = "SELECT * FROM Students"; 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. // StudentPersonal sp = new StudentPersonal(); 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) { } } } }
/// <summary> Respond to SIF RequestsGetTopicMap /// </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); // Read all students 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 StudentPersonal object using the Mappings class and stream it to // the supplied output stream. // IDbCommand command = null; // Set a basic filter on the outgoing data stream // What will happen is that any object written to the output stream will // be evaluated based on the query conditions. If the object doesn't meet the // query conditions, it will be excluded outStream.Filter = query; // Get the root Mappings object from the configuration file Edustructures.SifWorks.Tools.Mapping.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. // //IDictionary<string, string> dataMap = new System.Collections.Generic.Dictionary<string, string>(); // IFieldAdaptor adaptor = new StringMapAdaptor(dataMap); //m.MapOutbound(); MappingsContext mc = m.SelectOutbound(StudentDTD.STUDENTPERSONAL, info); try { int count = 0; // Query the database for all students command = fConn.CreateCommand(); fConn.Open(); command.CommandText = "SELECT * FROM Students"; using (IDataReader rs = command.ExecuteReader(CommandBehavior.CloseConnection)) { DataReaderAdaptor dra = new DataReaderAdaptor(rs); while (rs.Read()) { // Finally, create a new StudentPersonal 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 StudentPersonal object. // StudentPersonal sp = new StudentPersonal(); sp.RefId = Adk.MakeGuid(); // TODO: When using custom macros for outboud mapping operations, set the ValueBuilder. // You will need to call SetValueBuilder() giving the MappingsContext a derived version // of DefaultValueBuilder that has the macro methods available in it. mc.SetValueBuilder(new DataUtilMacro(dra)); mc.Map(sp, dra); // Now write out the StudentPersonal to the output stream and // we're done publishing this student. // Console.WriteLine("\nThe agent has read these values from the database:"); DumpFieldsToConsole(rs); Console.WriteLine("To produce this StudentPersonal object:"); debug.Write(sp); debug.Flush(); outStream.Write(sp); } rs.Close(); } Console.WriteLine ("- Returned " + count + " records from the Student database in response"); } catch (Exception ex) { Console.WriteLine("- Returning a SIF_Error response: " + ex); 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) { Log.Warn(ignored.Message, ignored); } } } }
/// <summary> Respond to SIF RequestsGetTopicMap /// </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); // Read all students 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 StudentPersonal object using the Mappings class and stream it to // the supplied output stream. // IDbCommand command = null; // Set a basic filter on the outgoing data stream // What will happen is that any object written to the output stream will // be evaluated based on the query conditions. If the object doesn't meet the // query conditions, it will be excluded outStream.Filter = query; // Get the root Mappings object from the configuration file Edustructures.SifWorks.Tools.Mapping.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. // //IDictionary<string, string> dataMap = new System.Collections.Generic.Dictionary<string, string>(); // IFieldAdaptor adaptor = new StringMapAdaptor(dataMap); //m.MapOutbound(); MappingsContext mc = m.SelectOutbound(StudentDTD.STUDENTPERSONAL, info); try { int count = 0; // Query the database for all students command = fConn.CreateCommand(); fConn.Open(); command.CommandText = "SELECT * FROM Students"; using (IDataReader rs = command.ExecuteReader(CommandBehavior.CloseConnection)) { DataReaderAdaptor dra = new DataReaderAdaptor(rs); while (rs.Read()) { // Finally, create a new StudentPersonal 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 StudentPersonal object. // StudentPersonal sp = new StudentPersonal(); sp.RefId = Adk.MakeGuid(); // TODO: When using custom macros for outboud mapping operations, set the ValueBuilder. // You will need to call SetValueBuilder() giving the MappingsContext a derived version // of DefaultValueBuilder that has the macro methods available in it. mc.SetValueBuilder(new DataUtilMacro(dra)); mc.Map(sp, dra); // Now write out the StudentPersonal to the output stream and // we're done publishing this student. // Console.WriteLine("\nThe agent has read these values from the database:"); DumpFieldsToConsole(rs); Console.WriteLine("To produce this StudentPersonal object:"); debug.Write(sp); debug.Flush(); outStream.Write(sp); } rs.Close(); } Console.WriteLine ("- Returned " + count + " records from the Student database in response"); } catch (Exception ex) { Console.WriteLine("- Returning a SIF_Error response: " + ex); 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) { Log.Warn(ignored.Message, ignored); } } } }