private WebServiceAPIResult uploadCachedOrderInternal(CacheOrder cachedOrder, out string errorMessage) { errorMessage = null; if (IsConnected() == false) { errorMessage = "Cannot upload order while service is disconnected"; return(WebServiceAPIResult.ConnectivityFail); } // Add any new notes foreach (NoteInfo note in cachedOrder.NewNotes) { if (this.wsAPI.AddNote(cachedOrder.Order.ID, note) == false) { errorMessage = string.Format("Failed to add a new note to existing order ID == {0}", cachedOrder.Order.ID); return(WebServiceAPIResult.Fail); } } // Add any new photos foreach (PhotoInfo photo in cachedOrder.NewPhotos) { if (uploadPhoto(cachedOrder.Order.ID, photo, out errorMessage) == false) { return(WebServiceAPIResult.Fail); } } return(WebServiceAPIResult.Success); }
public PhotoInfo[] GetPhotosForOrder(int orderID) { CacheOrder order = GetOrder(orderID); if (order == null) { return(null); } return(order.Order.Photos); }
private CacheOrder getCachedOrder(string existingOrderFile) { try { string xml = File.ReadAllText(existingOrderFile); return(CacheOrder.ParseXml(xml)); } catch { return(null); } }
// brute force upload - no checking for what needs to be sent public WebServiceAPIResult UploadCachedOrder(CacheOrder cachedOrder, out string errorMessage) { assertInitialized(); try { return(uploadCachedOrderInternal(cachedOrder, out errorMessage)); } catch (Exception ex) { errorMessage = buildExceptionErrorMessage(ex); return(WebServiceAPIResult.Fail); } }
public void DeleteOrder(int orderID) { CacheOrder cachedOrder = GetOrder(orderID); if (cachedOrder != null) { // Delete photo files (downloaded and new ones) deleteDirectory(GetOrderSpecificPhotosFolderName(orderID)); // Delete diagram file(s) deleteDirectory(GetDownloadedDiagramsFolderName(orderID)); // Delete the order file string path = GetOrderFilePath(orderID); bestEffortDeleteFile(path); } }
public bool SaveOrder(CacheOrder orderToSave) { try { // get the cache file name that we will be working with string orderFile = GetOrderFilePath(orderToSave.Order.ID); // re-order the indexes on the new notes so they go from [0 .. N-1] int index = 0; foreach (NoteInfo newNote in orderToSave.NewNotes) { newNote.ID = index; index++; } // re-order the indexes on the new photos so they go from [0 .. N-1] index = 0; foreach (PhotoInfo newPhoto in orderToSave.NewPhotos) { newPhoto.ID = index; index++; } string orderXml = orderToSave.ToXml(); if (File.Exists(orderFile) == true) { try { File.Delete(orderFile); } catch { } } File.WriteAllText(orderFile, orderXml); return(true); } catch { return(false); } }
public CacheOrder[] GetAllOrders() { List <CacheOrder> cachedOrders = new List <CacheOrder>(); foreach (string file in getOrderFiles()) { // wrap in try/catch in case file is corrupted try { string xml = File.ReadAllText(file); CacheOrder cacheOrder = CacheOrder.ParseXml(xml); if (cacheOrder == null) { // assume corrupted file and delete try { File.Delete(file); } catch { } } else { cachedOrders.Add(cacheOrder); } } catch { try { File.Delete(file); } catch { } } } return(cachedOrders.ToArray()); }
public CacheOrder GetOrder(int orderID) { try { List <string> orderFiles = getOrderFiles(); foreach (string file in orderFiles) { if (file.Contains(orderID.ToString()) == true) { if (orderID.ToString() == Path.GetFileNameWithoutExtension(file).Replace(orderFilePrefix, "")) { string xml = File.ReadAllText(file); return(CacheOrder.ParseXml(xml)); } } } return(null); } catch { return(null); } }
private WebServiceAPIResult synchronizeCachedOrderInternal(CacheOrder cachedOrder, out string errorMessage) { errorMessage = null; if (cachedOrder.Status != CacheStatus.SynchPending) { // this is an API usage error throw new InvalidOperationException("Cannot synchronize a cached order that has not been set to the synch pending state"); } if (IsConnected() == false) { errorMessage = "Cannot synchronize order while service is disconnected"; return(WebServiceAPIResult.ConnectivityFail); } // establish the baseline client order and the server order against which to compare it OrderInfo clientOrder = cachedOrder.Order; OrderInfo serverOrder = wsAPI.GetOrderByID(clientOrder.ID); if (serverOrder == null) { errorMessage = string.Format("Could not download OrderID={0} PO_Number={1} for syncronization", clientOrder.ID, clientOrder.PONumber); return(WebServiceAPIResult.Fail); } // calculate the differences between the local client version of the order and the server version. // This is necessary because the server data can be updated out of band to this client end-point // get a list of notes that are NEW in the client but not yet on the server. We do it this way in case previous sync attempts failed // part-way through, so instead of just uploading the NEW notes we make sure the server does not already have the note. List <NoteInfo> missingServerNotes = getMissingNotes(serverOrder.Notes, cachedOrder.NewNotes); // get a list of photos that are NEW in the client but not yet on the server. We do it this way in case previous sync attempts failed // part-way through, so instead of just uploading the NEW photos we make sure the server does not already have the photo. List <PhotoInfo> missingServerPhotos = getMissingPhotos(serverOrder.Photos, cachedOrder.NewPhotos); // Note: the client does not create diagrams so there will never be a missing server diagram // upload the notes that are missing on the server onNotesUploadStarted(missingServerNotes.Count); int numNotesUploaded = 0;; foreach (NoteInfo note in missingServerNotes) { if (wsAPI.AddNote(clientOrder.ID, note) == false) { errorMessage = string.Format("Failed to upload note to server for order ID={0} PO_Number={1)", clientOrder.ID, clientOrder.PONumber); return(WebServiceAPIResult.Fail); } numNotesUploaded++; onNoteUploaded(numNotesUploaded, missingServerNotes.Count); } onNotesUploadEnded(missingServerNotes.Count); // upload the photos that are missing on the server onPhotosUploadStarted(missingServerPhotos.Count); int numPhotosUploaded = 0;; foreach (PhotoInfo photo in missingServerPhotos) { if (uploadPhoto(clientOrder.ID, photo, out errorMessage) == false) { return(WebServiceAPIResult.Fail); } numPhotosUploaded++; onPhotoUploaded(numNotesUploaded, missingServerNotes.Count); } onPhotosUploadEnded(missingServerNotes.Count); // download the item into the cache from the server to account for any server items that may have // been missing from the client (or were different) var result = downLoadOrderToCacheInternal(clientOrder.ID, out errorMessage); if (result != WebServiceAPIResult.Success) { return(result); } CacheOrder updatedClientOrder = this.Cache.GetOrder(clientOrder.ID); if (updatedClientOrder == null) { errorMessage = string.Format("Failed to download order ID={0} PO_Number={1} for synchronization", clientOrder.ID, clientOrder.PONumber); return(WebServiceAPIResult.Fail); } // confirm that the download from the server contains everything it should. foreach (NoteInfo note in missingServerNotes) { if (containsNote(note, updatedClientOrder.Order.Notes) == false) { errorMessage = string.Format("Failed to confirm note synch for order ID={0} PO_Number={1} for synchronization", clientOrder.ID, clientOrder.PONumber); return(WebServiceAPIResult.Fail); } } foreach (PhotoInfo photo in missingServerPhotos) { if (containsPhoto(photo, updatedClientOrder.Order.Photos) == false) { errorMessage = string.Format("Failed to confirm photo synch for order ID={0} PO_Number={1} for synchronization", clientOrder.ID, clientOrder.PONumber); return(WebServiceAPIResult.Fail); } } return(WebServiceAPIResult.Success); }
private WebServiceAPIResult downLoadOrderToCacheInternal(int id, out string errorMessage) { errorMessage = null; if (IsConnected() == false) { errorMessage = "Cannot download order while service is disconnected"; return(WebServiceAPIResult.ConnectivityFail); } // get the order details, notes, and photo references OrderInfo order = wsAPI.GetOrderByID(id); if (order == null) { errorMessage = string.Format("Could not download order ID={0}", id); return(WebServiceAPIResult.Fail); } // conditionally download the photos if needed and save the images in the cache List <PhotoInfo> photosToDownload = new List <PhotoInfo>(); foreach (PhotoInfo photo in order.Photos) { // Check here to see if the photo is in the 'NewPhotos' section of the cached order. If it is then we can just // copy the photo from new to previous and avoid the download. Note that we still needed to get the server's // file name bool foundMatch = false; PhotoInfo[] photosInCache = Cache.GetNewPhotosForOrder(order.ID); PhotoInfo matchingCachePhoto = null; if (photosInCache != null) { foreach (PhotoInfo cachePhoto in photosInCache) { if (cachePhoto.Equals(photo)) { foundMatch = true; matchingCachePhoto = cachePhoto; break; } } } if (foundMatch == true) { // modify the cache order by copying the file to its new location and using the server photo info as the updated info string targetDirectory = Cache.GetDownloadedPhotosFolderName(order.ID); string targetFileName = photo.FilePath; string targetFullPath = Path.Combine(targetDirectory, targetFileName); // copy the file to the existing photo area with the proper info from the server try { if (Directory.Exists(targetDirectory) == false) { Directory.CreateDirectory(targetDirectory); } File.Copy(matchingCachePhoto.FilePath, targetFullPath); } catch { photosToDownload.Add(photo); continue; } } else { // check cache first. we want to see if we have the servers photo in our cache but file name/path // cannot be be used. this is because we use a temp path on the client and it is ultimately resolved on the server. // so we check the combination of 'entered by' and when it was taken to see if it is a match foundMatch = false; photosInCache = Cache.GetPhotosForOrder(order.ID); if (photosInCache != null) { foreach (PhotoInfo cachePhoto in photosInCache) { if (cachePhoto.Equals(photo)) { foundMatch = true; break; } } } if (foundMatch == false) { photosToDownload.Add(photo); } } } int numPhotosDownloaded = 0; if (photosToDownload.Count > 0) { onPhotosDownloadStarted(photosToDownload.Count); } foreach (PhotoInfo photo in photosToDownload) { byte[] photoBytes = wsAPI.DownloadPhoto(photo.ID); if (photoBytes != null) { this.Cache.SaveDownloadedPhoto(order.ID, photo, photoBytes); } numPhotosDownloaded++; onPhotoDownloaded(numPhotosDownloaded, photosToDownload.Count); } if (photosToDownload.Count > 0) { onPhotosDownloadEnded(numPhotosDownloaded); } // Download the diagram if needed and save the image in the cache if (order.HasDiagram == true) { byte[] diagramBytes = wsAPI.DownloadDiagram(order.DiagramNumber); if (diagramBytes != null) { this.Cache.SaveDownloadedDiagram(order.ID, order.DiagramNumber, diagramBytes); } } // now that we have the files copied from the new area to the download area we need to empty out the new photos folder Cache.ClearNewPhotoDirectory(order.ID); // save the new order CacheOrder cacheOrderToSave = new CacheOrder(order, null, null, CacheStatus.Synched);; if (this.cache.SaveOrder(cacheOrderToSave) == true) { return(WebServiceAPIResult.Success); } else { errorMessage = "Failed to save order to local cache"; return(WebServiceAPIResult.Fail); } }