//Listing 17-8. Opening Screens Conditionally partial void Startup_CanRun(ref bool result) { // Set result to the desired field value if (Application.Current.User.HasPermission(Permissions.CanViewAllIssues)) { this.ShowEngineersManagerGrid(); } else { using (DataWorkspace dw = this.CreateDataWorkspace()) { //This is the code you'd use to find the currently logged on engineer //Engineer currentEng = // dw.ApplicationData.Engineers.Where( // eng => eng.LoginName == // Application.Current.User.Name).FirstOrDefault(); //if (currentEng != null) //{ // this.ShowEngineerDashboard(currentEng.Id); //} this.ShowEngineerDashboard(dw.ApplicationData.Engineers.FirstOrDefault().Id); } } this.ShowInfo(); result = false; }
//Listing 15-5. Button Code for Sending E-mail partial void SendEmail_Execute() { using (var tempWorkspace = new DataWorkspace()) { EmailOperation newEmail = tempWorkspace.ApplicationData.EmailOperations.AddNew(); newEmail.RecipientEmail = User.Email; newEmail.SenderEmail = "*****@*****.**"; newEmail.Subject = SubjectProperty; newEmail.Body = BodyProperty; try { tempWorkspace.ApplicationData.SaveChanges(); //If you want, you can write some code here to create a record in an audit table newEmail.Delete(); tempWorkspace.ApplicationData.SaveChanges(); this.ShowMessageBox("Your email has been sent"); } catch (Exception ex) { this.ShowMessageBox(ex.Message); } } }
//Listing 17-3. .NET code that creates the document partial void DownloadTimesheet_Execute() { // Create a document in a new workspace DataWorkspace workspace = new DataWorkspace(); TimesheetReport rpt = workspace.ApplicationData.TimesheetReports.AddNew(); rpt.EngineerId = this.Engineers.SelectedItem.Id; workspace.ApplicationData.SaveChanges(); // Show the save dialog box Dispatchers.Main.Invoke(() => { System.IO.MemoryStream ms = new System.IO.MemoryStream(rpt.ReportData); Dispatchers.Main.Invoke(() => { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultFileName = "Timesheet.docx"; if (saveDialog.ShowDialog() == true) { using (Stream fileStream = saveDialog.OpenFile()) { ms.WriteTo(fileStream); } } }); }); }
//Listing 15-7. Screen Code to Send E-mail Attachments partial void UserDetail_InitializeDataWorkspace(List <IDataService> saveChangesTo) { var control = this.FindControl("FileUploadButton"); control.ControlAvailable += (object sender, ControlAvailableEventArgs e) => { var fileButton = (Button)e.Control; fileButton.Content = "Send Message With Attachment"; fileButton.Click += (object sender2, RoutedEventArgs e2) => { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog().GetValueOrDefault(false) == true) { byte[] data; using (FileStream stream = dlg.File.OpenRead()) { data = new byte[stream.Length]; stream.Read(data, 0, data.Length); } string filename = dlg.File.Name; //send the email here this.Details.Dispatcher.BeginInvoke(() => { using (var dw = new DataWorkspace()) { EmailOperation newEmail = dw.ApplicationData.EmailOperations.AddNew(); newEmail.RecipientEmail = User.Email; newEmail.SenderEmail = "*****@*****.**"; newEmail.Subject = SubjectProperty; newEmail.Body = BodyProperty; newEmail.Attachment = data; newEmail.AttachmentFileName = filename; try { dw.ApplicationData.SaveChanges(); //If you want, you can write some code here to //create a record in an audit table newEmail.Delete(); dw.ApplicationData.SaveChanges(); } catch (Exception ex) { this.ShowMessageBox(ex.Message); } } }); } ; }; }; }
partial void Application_LoggedIn() { //throw new NotImplementedException(); DataWorkspace dws = CreateDataWorkspace(); var s = dws.ApplicationData.AppUsersSet.Where(x => x.username.ToLower() == Current.User.Name.ToLower()).FirstOrDefault(); gEmployeeCode = s.employee_code; }
partial void TimesheetReports_Inserting(TimesheetReport entity) { string wordDocument; //Retrieve the Engineer DataWorkspace workspace = new DataWorkspace(); var timesheetEngineer = workspace.ApplicationData.Engineers_SingleOrDefault( entity.EngineerId); if (timesheetEngineer != null) { DateTime startOfMonth; startOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); //Retrieve timesheet records for engineer for current month var timesheetRecords = workspace.ApplicationData.Timesheets.Where( tsRec => tsRec.Engineer.Id == entity.EngineerId && tsRec.EntryDate > startOfMonth); wordDocument = HttpContext.Current.Server.MapPath( @"~\bin\HelpDeskCS.Server\Reports\TimesheetTemplate.docx"); Byte[] byteArray = File.ReadAllBytes(wordDocument); using (MemoryStream mem = new MemoryStream()) { mem.Write(byteArray, 0, (int)byteArray.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true)) { MainDocumentPart mainDocPart = wordDoc.MainDocumentPart; //Insert the bookmark values InsertBookmarkValue(ref mainDocPart, "EngineerSurname", timesheetEngineer.Surname); InsertBookmarkValue(ref mainDocPart, "EngineerFirstname", timesheetEngineer.Firstname); IEnumerable <TableProperties> docTableProperties = mainDocPart.Document.Descendants <TableProperties>().Where( prop => (prop.TableCaption != null) && prop.TableCaption.Val.Value == "TimesheetEntries"); //Find a reference to the table Table tableTimesheet = (Table)docTableProperties.First().Parent; IEnumerable <TableRow> rowsTimeseet = tableTimesheet.Descendants <TableRow>(); //Loop through the timesheet records foreach (Timesheet tsRec in timesheetRecords) { TableRow rowCopy = (TableRow)tableTimesheet.Descendants < TableRow>().Skip(1).First().CloneNode(true); IEnumerable <TableCell> rowCells = rowCopy.Descendants <TableCell>(); rowCells.ElementAt(0).Append( GetParagraph(tsRec.EntryDate.ToShortDateString())); rowCells.ElementAt(1).Append( GetParagraph(tsRec.Issue.Subject)); rowCells.ElementAt(2).Append( GetParagraph(tsRec.DurationMins.ToString())); tableTimesheet.InsertAfter(rowCopy.CloneNode(true), tableTimesheet.Descendants <TableRow>().Skip(1).First()); } mainDocPart.Document.Save(); } //Save the Word document to the table entity.ReportData = mem.ToArray(); } } }