示例#1
0
        /// <summary>  Handles SIF_Responses
        /// </summary>
        public virtual void OnQueryResults(IDataObjectInputStream in_Renamed,
                                           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 StudentPersonal 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 StudentPersonal 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
            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.
            //

            SifFormatter    textFormatter = Adk.Dtd.GetFormatter(info.SifVersion);
            MappingsContext context       =
                m.SelectInbound(StudentDTD.STUDENTPERSONAL, info);

            while (in_Renamed.Available)
            {
                StudentPersonal sp = (StudentPersonal)in_Renamed.ReadDataObject();

                //  Ask the Mappings object to populate a HashMap with field/value pairs
                //  by using the mapping rules in the configuration file to decompose
                //  the StudentPersonal object into field values.
                //
                Hashtable        data = new Hashtable();
                StringMapAdaptor sma  = new StringMapAdaptor(data, textFormatter);
                context.Map(sp, sma);

                //  Now dump the field/value pairs to System.out
                DumpDictionaryToConsole(data);
            }
        }
示例#2
0
        /// <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);
                    }
                }
            }
        }