/// <summary> /// Extract from a collection of sync commands to create a list of strings. This function is generally for /// unit testing. /// </summary> /// <param name="commands">A set of sync commands</param> /// <returns>String collection. Each string represents a sync command.</returns> public static StringCollection ExtractSifcStrings(IList <SyncMLCommand> commands) { if (commands == null) { return(null); } StringCollection collection = new StringCollection(); foreach (SyncMLCommand command in commands) { SyncMLAdd addCommand = command as SyncMLAdd; if (addCommand != null) // handle Add commands { //at this stage, I would assume <Format>b64</Format><Type>text/x-s4j-sifc</Type>. In the future, use this as condition. //Decode the b64 SIF-C XML collection.Add(Utility.ConvertFromBase64(addCommand.ItemCollection[0].Data.Content)); continue; } SyncMLReplace replaceCommand = command as SyncMLReplace; if (replaceCommand != null) // handle Replace commands { continue; } SyncMLDelete deleteCommand = command as SyncMLDelete; if (deleteCommand != null) // handle Delete commands { continue; } } return(collection); }
public void TestDelete2() { XElement nav = XElement.Load(CaseFile("Delete2.xml")); SyncMLDelete f = SyncMLDelete.Create(nav); Assert.IsTrue(CompareXml(nav, f.Xml), f.Xml.ToString()); }
private void AddDeleteCommandsToCollection(IList <SyncMLCommand> commands) { IEnumerable <XElement> deleteNodes = XmlHelpers.SafeElementsQuery(sifcChangeLogXml, "Changes", "Delete", "D"); foreach (XElement node in deleteNodes) { SyncMLDelete deleteCommand = SyncMLDelete.Create(); SyncMLItem item = SyncMLItem.Create(); item.Source.LocURI.Content = node.Attribute("ID").Value; deleteCommand.ItemCollection.Add(item); commands.Add(deleteCommand); } }
/// <summary> /// /// </summary> /// <param name="commands"></param> /// <returns>A list of Id pair separated by link break</returns> public string ApplySyncCommands(IList <SyncMLCommand> commands) { if (commands == null) { throw new ArgumentNullException("commands"); } StringBuilder builder = new StringBuilder(); foreach (SyncMLCommand command in commands) { SyncMLAdd addCommand = command as SyncMLAdd; if (addCommand != null) { string idPair = ApplyAddOrReplaceCommand(addCommand); if (!String.IsNullOrEmpty(idPair)) { builder.AppendLine(idPair); } continue; } SyncMLReplace replaceCommand = command as SyncMLReplace; if (replaceCommand != null) { string idPair = ApplyAddOrReplaceCommand(replaceCommand); if (!String.IsNullOrEmpty(idPair)) { builder.AppendLine(idPair); } continue; } SyncMLDelete deleteCommand = command as SyncMLDelete; if (deleteCommand != null) { string localID = deleteCommand.ItemCollection[0].Target.LocURI.Content; bool deletionOK = outlookAgent.DeleteItem(localID); if (!deletionOK) { System.Diagnostics.Trace.TraceInformation(String.Format( "The deletion can not be done. Contact with this id {0} can not be found.", localID)); } continue; } } return(builder.ToString()); }
private void PrepareCommands(IList <SyncMLCommand> commands) { IEnumerable <T> newContacts = outlookItems.GetNewItems(AnchorTime); if (forSlowSync) { foreach (T contact in newContacts) { SyncMLReplace updateCommand = SyncMLReplace.Create(); AddContentToCommand(updateCommand, contact); commands.Add(updateCommand); } } else { foreach (T contact in newContacts) { SyncMLAdd addCommand = SyncMLAdd.Create(); AddContentToCommand(addCommand, contact); commands.Add(addCommand); } } IEnumerable <T> updatedContacts = outlookItems.GetUpdatedItems(AnchorTime); foreach (T contact in updatedContacts) { SyncMLReplace updateCommand = SyncMLReplace.Create(); AddContentToCommand(updateCommand, contact); commands.Add(updateCommand); } string[] ids = deletionLog.ReadAll(); if (ids != null) { foreach (string id in ids) { SyncMLDelete deleteCommand = SyncMLDelete.Create(); SyncMLItem item = SyncMLItem.Create(); item.Source.LocURI.Content = id; deleteCommand.ItemCollection.Add(item); commands.Add(deleteCommand); } } }
private static XElement GenerateSifCChangeLogXml(IList <SyncMLCommand> commands, string lastAnchor) { XElement r = new XElement("SIFCChangeLog", new XElement("LastAnchor", lastAnchor), new XElement("Source", "SyncML Server")); XElement changesElement = new XElement("Changes"); r.Add(changesElement); foreach (SyncMLCommand command in commands) { SyncMLAdd addCommand = command as SyncMLAdd; if (addCommand != null) { WriteAddOrReplaceCommandToXml(changesElement, addCommand); continue; } SyncMLReplace replaceCommand = command as SyncMLReplace; if (replaceCommand != null) { WriteAddOrReplaceCommandToXml(changesElement, replaceCommand); continue; } SyncMLDelete deleteCommand = command as SyncMLDelete; if (deleteCommand != null) // handle Delete commands { string localID = deleteCommand.ItemCollection[0].Target.LocURI.Content; changesElement.Add(new XElement("Delete", new XElement("D", new XAttribute("ID", localID)))); continue; } } //foreach return(r); } // using xmlWriter
/// <summary> /// Generate status commands for received Sync command, and put the commands into the pool. /// </summary> /// <param name="syncCommand">Sync command from the server.</param> private void GenerateStatusCommandsForSyncCommand(SyncMLSync syncCommand) { SyncMLStatus syncStatus = SyncMLStatus.Create(); syncStatus.Cmd.Content = "Sync"; syncStatus.CmdRef.Content = syncCommand.CmdID.Content; syncStatus.Data.Content = "200"; syncStatus.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; syncStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(syncCommand.Target.LocURI.Content)); syncStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(syncCommand.Source.LocURI.Content)); Facade.ResponseCommandPool.Add(syncStatus); Collection <SyncMLCommand> commands = syncCommand.Commands; if (commands != null) { foreach (SyncMLCommand command in commands) { SyncMLAdd addCommand = command as SyncMLAdd; if (addCommand != null) { SyncMLStatus addStatus = SyncMLStatus.Create(); addStatus.Cmd.Content = "Add"; addStatus.CmdRef.Content = command.CmdID.Content; addStatus.Data.Content = "200"; addStatus.MsgRef.Content = syncStatus.MsgRef.Content; addStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(addCommand.ItemCollection[0].Source.LocURI.Content)); Facade.ResponseCommandPool.Add(addStatus); continue; } SyncMLReplace replaceCommand = command as SyncMLReplace; if (replaceCommand != null) { SyncMLStatus replaceStatus = SyncMLStatus.Create(); replaceStatus.Cmd.Content = "Replace"; replaceStatus.CmdRef.Content = command.CmdID.Content; replaceStatus.Data.Content = "200"; replaceStatus.MsgRef.Content = syncStatus.MsgRef.Content; replaceStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(replaceCommand.ItemCollection[0].Target.LocURI.Content)); Facade.ResponseCommandPool.Add(replaceStatus); continue; } SyncMLDelete deleteCommand = command as SyncMLDelete; if (deleteCommand != null) { SyncMLStatus deleteStatus = SyncMLStatus.Create(); deleteStatus.Cmd.Content = "Delete"; deleteStatus.CmdRef.Content = command.CmdID.Content; deleteStatus.Data.Content = "200"; deleteStatus.MsgRef.Content = syncStatus.MsgRef.Content; deleteStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(deleteCommand.ItemCollection[0].Target.LocURI.Content)); Facade.ResponseCommandPool.Add(deleteStatus); continue; } } } }