static void Main(string[] args) { try { //-- Creating Event with default values (check if these vary...) Event newEvent = new Event(); //set the basics newEvent.EventID = "1839-a784-199d-fe33"; newEvent.EventMessageDateTime = System.DateTime.UtcNow; newEvent.EventTypeDescriptor.CodeValue = EventTypeCodeList.Tasking; newEvent.EventValidityDateTimeRange.StartDate = System.DateTime.UtcNow; newEvent.EventValidityDateTimeRange.EndDate = System.DateTime.UtcNow.AddHours(1.0); //set the location EventLocation location = new EventLocation(); location.LocationCylinder.CodeValue = LocationCreationCodeList.MACHINE; location.LocationCylinder.LocationPoint.Point.id = "ID1"; location.LocationCylinder.LocationPoint.Point.Lat = 30.1; location.LocationCylinder.LocationPoint.Point.Lon = 30.1; location.LocationCylinder.LocationCylinderHalfHeightValue = (decimal)1.4; location.LocationCylinder.LocationCylinderRadiusValue = (decimal)1.0; newEvent.EventLocation = location; //set a comment /* EventComment comment = new EventComment(); * comment.CommentText = ""; * comment.DateTime = System.DateTime.UtcNow; * comment.OrganizationIdentification = "ARDENTMC General"; * comment.PersonHumanResourceIdentification = "Dr. Brian Wilkins"; * newEvent.EventComment = new List<EventComment>(); * newEvent.EventComment.Add(comment);*/ //-- Creating MutualAidDetail MutualAidDetail md = new MutualAidDetail(); //-- Aid Request areq1 AidRequested areq1 = new AidRequested(); GenericResource grA = new GenericResource(); grA.Quantity = 1; grA.ResourceKind.ResourceTypeCodeValue = EventTypeCodeList.ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_EMS_AMBULANCE; areq1.Resource = new RequestedResources(grA); GenericResource grB = new GenericResource(); grB.Quantity = 1; grB.ResourceKind.ResourceTypeCodeValue = EventTypeCodeList.ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_FIRE; areq1.Resource.ResourceList.Add(grB); areq1.location = new LocationExtension(new Address(new LocationStreet())); areq1.location.Address.LocationStreet.StreetName = "2332"; areq1.location.Address.LocationStreet.StreetNumberText = "Riverside"; areq1.location.Address.LocationStreet.StreetCategoryText = "Pkwy"; areq1.location.Address.LocationState = USStateCodeList.CA; areq1.location.Address.LocationCityName = "Sacramento"; areq1.location.Address.LocationPostalCode = "95605"; //-- Aid Request areq2 AidRequested areq2 = new AidRequested(); SpecificResource srA = new SpecificResource(); srA.ResourceIdentifier = "3-508-1028"; areq2.Resource = new RequestedResources(srA); //-- Aid Request areq3 AidRequested areq3 = new AidRequested(); MissionNeed mn1 = new MissionNeed(); mn1.Quantity = 1; mn1.ValueText = "ATOM.GRDTRK.EQT.GRDVEH.CVLVEH.EM.PUBLICWORKS.TRUCK.WATER"; // will need to change here... areq3.Resource = new RequestedResources(mn1); //-- Aid Request ares1 AidResponding ares1 = new AidResponding(); ares1.Approved = true; Equipment eq1 = new Equipment(); eq1.ID = "3-508-1023"; eq1.EstimatedArrival = DateTime.UtcNow.AddHours(1); eq1.ResourceKind.ResourceTypeCodeValue = EventTypeCodeList.ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_FIRE_TRUCK; eq1.EstimatedAvailability.StartDate = DateTime.UtcNow; eq1.EstimatedAvailability.EndDate = DateTime.UtcNow.AddMinutes(30); ares1.Resources = new RespondingResource(eq1); ares1.ContactInformation.Entity = "ArdentMC Fire Co."; ares1.ContactInformation.Responder = "John Smith"; //-- Aid Request ares2 AidResponding ares2 = new AidResponding(); ares2.Approved = true; Person p1 = new Person(); p1.ID = "ID2"; p1.EstimatedArrival = DateTime.UtcNow.AddMinutes(20); p1.EstimatedAvailability.StartDate = DateTime.UtcNow; p1.EstimatedAvailability.EndDate = DateTime.UtcNow.AddMinutes(50); ares2.Resources = new RespondingResource(p1); Person p2 = new Person(); p2.ID = "ID3"; p2.EstimatedArrival = DateTime.UtcNow.AddMinutes(20); p2.EstimatedAvailability.StartDate = DateTime.UtcNow; p2.EstimatedAvailability.EndDate = DateTime.UtcNow.AddMinutes(50); p2.Rank = new Rank(); p2.Rank.ValueText = "EMT-P"; // may need to change since this since this isn't on any list I know of ares2.Resources.ResourceList.Add(p2); ares2.ContactInformation.Entity = "ArdentMC Medical Facilities"; // Adding Detail to Event string xmlSample = ""; // areq1 md.Message = areq1; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidReq1.xml", xmlSample); // areq2 md.Message = areq2; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidReq2.xml", xmlSample); // areq3 md.Message = areq3; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidReq3.xml", xmlSample); // ares1 md.Message = ares1; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidRes1.xml", xmlSample); // ares2 md.Message = ares2; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidRes2.xml", xmlSample); /* * //-- Serializing Event Object * * string xml = newEvent.ToString(); * File.WriteAllText(@"C:\event.xml", xml); * * * //-- Deserializing from newly created xml * * string json = NIEMEmlcUtil.xmlToJson(xml); * Event testEv = JsonConvert.DeserializeObject<Event>(json, new NIEMSHARP.NIEMEMLCLib.deSerialEventConverter(xml); * string newXML = testEv.ToString(); * File.WriteAllText(@"C:\newEvent.xml", newXML); * * * // */ //--------- Test with provided string /* * * string xmlString; * xmlString = ""; // where string goes * * Event stringEv = JsonConvert.DeserializeObject<Event>(json, new NIEMSHARP.NIEMEMLCLib.deSerialEventConverter(xmlString); * string stringXML = stringEv.ToString(); * File.WriteAllText(@"C:\stringEvent.xml", stringXML); * * // */ } catch (Exception e) { string s = e.Message + "\n"; } }
/// <summary> /// Deserializes Event object with it's proper event detail. /// Requires the xmlString which is used for the deserialization /// </summary> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { try { JObject obj = JObject.Load(reader); Object root = null; if (xmlString != null) { //-- Deserializing Event without detail XmlDocument xD = new XmlDocument(); xD.LoadXml(xmlString); string eventString = ""; foreach (XmlNode child in xD.ChildNodes) { if (child.Name == "emlc:Event") { eventString = child.OuterXml; break; } } xD.LoadXml(eventString); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Event)); StringReader xmlReader = new StringReader(eventString); Event myEvent = (Event)xmlSerializer.Deserialize(xmlReader); //-- Deserializing EventDetails if it exists JToken incTok = obj.SelectToken("emlc:Event.emlc:IncidentDetail"); JToken resTok = obj.SelectToken("emlc:Event.emlc:ResourceDetail"); JToken maTok = obj.SelectToken("emlc:Event.maid:MutualAidDetail"); JToken infTok = obj.SelectToken("emlc:Event.emlc:InfrastructureDetail"); if (incTok != null) // If Details is an IncidentDetail { string elementName = "emlc:IncidentDetail"; Type detailType = typeof(IncidentDetail); string detailXML = ""; // Getting XML for just this detail foreach (XmlNode child in xD.FirstChild.ChildNodes) { if (child.Name == elementName) { detailXML = child.OuterXml; break; } } // Deserializing XmlSerializer detailSerializer = new XmlSerializer(detailType); StringReader detailReader = new StringReader(detailXML); IncidentDetail myDetail = (IncidentDetail)detailSerializer.Deserialize(detailReader); myEvent.Details = myDetail; } else if (resTok != null) // If Details is a ResourceDetail { Type detailType = typeof(ResourceDetail); JToken detailToken = resTok; string elementName = "emlc:ResourceDetail"; string detailXML = ""; // Getting XML for just this detail foreach (XmlNode child in xD.FirstChild.ChildNodes) { if (child.Name == elementName) { detailXML = child.OuterXml; break; } } // Deserializing XmlSerializer detailSerializer = new XmlSerializer(detailType); StringReader detailReader = new StringReader(detailXML); ResourceDetail myDetail = (ResourceDetail)detailSerializer.Deserialize(detailReader); myEvent.Details = myDetail; } else if (infTok != null) // If Details is an InfrastructureDetail { Type detailType = typeof(InfrastructureDetail); JToken detailToken = infTok; string elementName = "emlc:InfrastructureDetail"; string detailXML = ""; // Getting XML for just this detail foreach (XmlNode child in xD.FirstChild.ChildNodes) { if (child.Name == elementName) { detailXML = child.OuterXml; break; } } // Deserializing XmlSerializer detailSerializer = new XmlSerializer(detailType); StringReader detailReader = new StringReader(detailXML); InfrastructureDetail myDetail = (InfrastructureDetail)detailSerializer.Deserialize(detailReader); myEvent.Details = myDetail; } else if (maTok != null) // If Details is a MutualAidDetail { JToken detailToken = maTok; string elementName = "maid:MutualAidDetail"; string detailXML = ""; // Getting XML for just this detail foreach (XmlNode child in xD.FirstChild.ChildNodes) { if (child.Name == elementName) { detailXML = child.OuterXml; break; } } // Deserializing Mutual Aid Detail (requires MA Converter) string json = detailToken.ToString(); NIEMUtil.setDefaultDeseralizeSetting(); MutualAidDetail myDetail = JsonConvert.DeserializeObject <MutualAidDetail>(json, new JsonConverter[] { new deserialMAConvert(detailXML) }); myEvent.Details = myDetail; } else { throw new JsonSerializationException("XML string must be specified"); } return(myEvent); } } catch (Exception e) { string r = e.ToString(); } return(null); }
public Event(MutualAidDetail det) : this() { this.Details = det; }