private async Task btnValidateProject() { WcfExt116 cAX = null; try { SetProgress(.0); //lvProjects.ItemsSource = m_ocProjSearch; //this.pbDownload.Visibility = Windows.UI.Xaml.Visibility.Collapsed; //this.tbDownloadStatus.Text = String.Empty; //this.m_bIgnoreReset = true; this.txtProjectNo.Text = this.txtProjectNo.Text.Trim().ToUpper(); if (this.txtProjectNo.Text.Length == 0) { await DisplayAlert("Project no required.", "You need to provide a project number before you can validate.", "OK"); this.txtProjectNo.Focus(); return; } //Check project is not already downloaded. bool bAlreadyDownloaded = await this.IsProjectAlreadyOnDevice(this.txtProjectNo.Text); SetProgress(.1); if (bAlreadyDownloaded == true) { return; } bool bConnectedOK = await Main.p_cSettings.IsAXSystemAvailable(true); if (bConnectedOK == true) { SetProgress(.2); await Main.CheckForUpdates(); SetProgress(.4); cAX = new WcfExt116(); ProjectValidationResult cResult = await cAX.ValidateProjectNo(this.txtProjectNo.Text); if (cResult != null) { if (cResult.bSuccessfull == true) { SetProgress(.6); if (cResult.bProjectFound == true) { //Mark project as valid. //this.m_bProjectValid = true; //Fetch status name from base enum table. string sStatus = Main.p_cDataAccess.GetEnumValueName("ProjTable", "Status", Convert.ToInt32(cResult.ValidationResult.Status)); SearchResult srResult = new SearchResult(); SetProgress(.8); srResult.ProjectName = cResult.ValidationResult.ProjectName; srResult.ProjectNo = cResult.ValidationResult.ProjectNo; srResult.Status = sStatus; try { m_ocProjSearch.Clear(); } catch (Exception ex) { lvProjects.ItemsSource = null; } //m_ocProjSearch.Clear(); m_ocProjSearch.Add(srResult); lvProjects.ItemsSource = m_ocProjSearch; SetProgress(.9); //Display project details. //this.DisplayProjectDetails(cResult.ValidationResult.ProjectName, sStatus); //Display download section. //this.UpdateValidationStatus(ValidationStatus.Valid); //this.gdDownload.Visibility = Windows.UI.Xaml.Visibility.Visible; } else { SearchResult srResult = new SearchResult(); SetProgress(.8); srResult.ProjectName = "N/A"; srResult.ProjectNo = "N/A"; srResult.Status = "N/A"; try { m_ocProjSearch.Clear(); } catch (Exception ex) { lvProjects.ItemsSource = null; } m_ocProjSearch.Add(srResult); lvProjects.ItemsSource = m_ocProjSearch; SetProgress(.9); //Mark project as invalid. //this.m_bProjectValid = false; //this.UpdateValidationStatus(ValidationStatus.Invalid); //this.gdDownload.Visibility = Windows.UI.Xaml.Visibility.Collapsed; } } else { //Mark project as invalid. //this.m_bProjectValid = false; //this.UpdateValidationStatus(ValidationStatus.Error); //this.gdDownload.Visibility = Windows.UI.Xaml.Visibility.Collapsed; } } } SetProgress(1.0); } catch (Exception ex) { throw new Exception(ex.Message); //cMain.ReportError(ex, cMain.GetCallerMethodName(), string.Empty); } }
private async void DownloadProject() { WcfExt116 cAX = null; //this.tbDownloadStatus.Visibility = Windows.UI.Xaml.Visibility.Visible; string rResponse = "Abort"; DataAccess.SaveSubProjectDataResult srSaved; bool bDownloadCompletedOK = false; try { LoggingSubProject(Color.White, "Checking if project already downloaded.", .0); //await this.UpdateDownloadStatus("Checking if project already downloaded."); //Check project is not already downloaded. bool bAlreadyDownloaded = await IsProjectAlreadyOnDevice(this.txtProjectNo.Text); if (bAlreadyDownloaded == true) { return; } LoggingSubProject(Color.White, "Checking connection.", .3); //await this.UpdateDownloadStatus("Checking connection."); bool bConnected = await Main.p_cSettings.IsAXSystemAvailable(true); if (bConnected == false) { //LoggingSubProject(Color.Red, "Failed Checking connection.", .3); } else { LoggingSubProject(Color.White, "Checking for configuration updates.", .6); //await this.UpdateDownloadStatus("Checking for configuration updates."); await Main.CheckForUpdates(); LoggingSubProject(Color.White, "Completed Checking.", 1.0); cAX = new WcfExt116(); SubProjectData spData; object sfProject = null; LoggingSubProject(Color.White, "Fetching list of sub projects.", .0); //await this.UpdateDownloadStatus("Fetching list of sub projects."); List <string> lSubProjects = await cAX.ReturnListOfSubProjectsToDownload(txtProjectNo.Text); if (lSubProjects != null) { //If no sub projects, let user know. if (lSubProjects.Count == 0) { await DisplayAlert("No SubProjects", "This project does not have any sub projects to download.", "OK"); return; } //this.pbDownload.Visibility = Windows.UI.Xaml.Visibility.Visible; //this.pbDownload.IsIndeterminate = true; int iSubProjectCount = 0; bool bSubProjectOK = false; bool bErrorOccured = false; string sErrorMessage = String.Empty; double percent = .0; foreach (string sSubProjectNo in lSubProjects) { Random rnd = new Random(); SetBarColor(Color.FromRgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))); //Increment by one. iSubProjectCount++; bSubProjectOK = false; //Update screen. LoggingSubProject(Color.White, "Downloading Sub Project (" + iSubProjectCount.ToString() + " of " + lSubProjects.Count() + ") - " + sSubProjectNo, percent); //await this.UpdateDownloadStatus("Downloading Sub Project (" + iSubProjectCount.ToString() + " of " + lSubProjects.Count() + ") - " + sSubProjectNo); percent = Convert.ToDouble(iSubProjectCount) / Convert.ToDouble(lSubProjects.Count); do { bErrorOccured = false; bSubProjectOK = false; try { //Fetch sub project data. LoggingStatus(Color.White, "Fetch sub project data", Percent.START); spData = await cAX.DownloadSubProjectData(sSubProjectNo); if (spData.ProjId == null) { throw new Exception("No data returned for sub project."); } else { //Save sub project data LoggingStatus(Color.White, "Save sub project data", Percent.S1); srSaved = Main.p_cDataAccess.SaveSubProjectData(txtProjectNo.Text, txtProjectName.Text, spData); if (srSaved.bSavedOK == false) { throw new Exception("Cannot save sub project to database."); } else { LoggingStatus(Color.White, "Fetch list of files for sub project", Percent.S2); //Fetch list of files for sub project. List <SubProjectFile> sfFiles = await cAX.ReturnListOfSubProjectFiles(spData.ProjId); if (sfFiles == null) { throw new Exception("Cannot retrieve list of files."); } else { //If files found. LoggingStatus(Color.Yellow, "No files for sub project", Percent.S3); if (sfFiles.Count > 0) { LoggingStatus(Color.White, "Return local sub project image folder.", Percent.S3); //Create- return local sub project image folder. sfProject = await DependencyService.Get <ISettings>().ReturnSubProjectImagesFolder(spData.ProjId); if (sfProject == null) { throw new Exception("Unable to retrieve local image folder."); } else { //Loop through list of files. int fileCount = 0; LoggingStatus(Color.White, "Loop through list of files", Percent.S4); foreach (SubProjectFile sfFile in sfFiles) { LoggingStatus( Color.White, "Download file data for (" + sfFile.FileName + ")", Percent.S4 + (.6 / sfFiles.Count) / 3 + (.6 / sfFiles.Count) * fileCount); //Download file data SubProjectFileDownloadResult sfDownload = await cAX.ReturnFileData(sfFile.FileName); if (sfDownload == null) { throw new Exception("Unable to download file data for (" + sfFile.FileName + ")"); } else { LoggingStatus( Color.White, "Save file to device (" + sfFile.FileName + ")", Percent.S4 + (.6 / sfFiles.Count) / 3 * 2 + (.6 / sfFiles.Count) * fileCount); //Save file to device. bool bFileSaved = await DependencyService.Get <ISettings>().SaveFileLocally(sSubProjectNo, sfDownload.byFileData, sfFile.FileName); if (bFileSaved == false) { throw new Exception("Unable to save file to device (" + sfFile.FileName + ")"); } LoggingStatus( Color.White, "Save file details to database (" + sfFile.FileName + ")", Percent.S4 + .6 / sfFiles.Count + (.6 / sfFiles.Count) * fileCount); //Save file record into files table. bool bSavesOK = Main.p_cDataAccess.SaveSubProjectFile( spData.ProjId, sfFile.FileName, sfFile.Comments, sfFile.ModifiedDate, false); if (bSavesOK == false) { throw new Exception("Unable to save file details to database (" + sfFile.FileName + ")"); } } fileCount++; } } } } } } LoggingStatus(Color.White, "Downloading this file was done", Percent.END); //If we get here then all OK. bSubProjectOK = true; } catch (Exception ex) { bErrorOccured = true; sErrorMessage = ex.Message; //cMain.ReportError(ex, cMain.GetCallerMethodName(), "Downloading sub project (" + sSubProjectNo + ")"); } //If error occurred if (bErrorOccured == true) { //Ask user what they want to do. rResponse = await PromptOnError(sSubProjectNo, sErrorMessage); if (rResponse == "Abort") { LoggingStatus(Color.Red, "Removing previously downloaded data.", .0); //await UpdateDownloadStatus("Removing previously downloaded data."); await Main.p_cDataAccess.DeleteProjectFromDevice(this.txtProjectNo.Text); return; } else if (rResponse == "Ignore") { bSubProjectOK = true; } else if (rResponse == "Retry") { //Wait } } }while (bSubProjectOK == false); } } bDownloadCompletedOK = true; LoggingSubProject(Color.White, "OK Downloading.", 1.0); //this.UpdateDownloadStatus(true); } } catch (Exception ex) { //cMain.ReportError(ex, cMain.GetCallerMethodName(), string.Empty); } finally { //this.pbDownload.IsIndeterminate = false; //this.pbDownload.Visibility = Windows.UI.Xaml.Visibility.Collapsed; //Only clear if download not successful. if (bDownloadCompletedOK == false) { LoggingStatus(Color.Red, "Failed Downloading.", .0); //this.tbDownloadStatus.Text = String.Empty; } SetBarColor(Color.Black); } }
/// <summary> /// Process project search. /// </summary> private async Task ProcessProjectSearch() { WcfExt116 cAX = null; try { try { //await this.EnableSearchScreenControls(false); //UserDialogs.Instance.ShowLoading("Searching Projects...", MaskType.Black); SetProgress(.0); //lvProjects.ItemsSource = m_ocProjSearch; bool bConnected = await Main.p_cSettings.IsAXSystemAvailable(true); SetProgress(.1); if (bConnected == true) { cAX = new WcfExt116(); //v1.0.19 - Add wild cards to search text string sSearchText = Settings.AddWildCardsToSearchString(txtProjectNo.Text.Trim().ToUpper()); SetProgress(.2); ObservableCollection <SearchResult> ocResult = await cAX.SearchForProject(sSearchText); SearchResult srResult; SetProgress(.3); List <cBaseEnumsTable> cEnums = Main.p_cDataAccess.GetEnumsForField("Status"); cBaseEnumsTable cEnum = null; //Clear out existing results. try { m_ocProjSearch.Clear(); } catch (Exception ex) { lvProjects.ItemsSource = null; } int count = ocResult.Count; int index = 0; //lvProjects.ItemsSource = ""; lvProjects.ItemsSource = m_ocProjSearch; foreach (SearchResult sResult in ocResult) { double percent = .3 + .7 / count * index; SetProgress(percent); //Find matching enum. cEnum = cEnums.Find(mc => mc.EnumValue.Equals(Convert.ToInt32(sResult.Status))); srResult = new SearchResult(); srResult.ProjectName = sResult.ProjectName; srResult.ProjectNo = sResult.ProjectNo; if (cEnum != null) { srResult.Status = cEnum.EnumName; } else { srResult.Status = "N\\A"; } index++; m_ocProjSearch.Add(srResult); } //lvProjects.ItemsSource = m_ocProjSearch; } } catch (Exception ex) { throw new Exception(ex.Message); } if (cAX != null) { await DependencyService.Get <IWcfExt116>().CloseAXConnection(); SetProgress(.9); } //await this.EnableSearchScreenControls(true); } catch (Exception ex) { throw new Exception(ex.Message); //cMain.ReportError(ex, cMain.GetCallerMethodName(), string.Empty); } finally { SetProgress(1.0); //UserDialogs.Instance.HideLoading(); } }