-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReporterTask.java
More file actions
85 lines (70 loc) · 2.2 KB
/
ReporterTask.java
File metadata and controls
85 lines (70 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.javaee7.chapter11;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.ejb.EJB;
import org.javaee7.entity.Jobs;
import org.javaee7.entity.Product;
import org.javaee7.jpa.session.JobsSession;
import org.javaee7.jpa.session.ProductSession;
/**
*
* @author Juneau
*/
public class ReporterTask implements Runnable {
String reportName;
@EJB
private JobsSession jobsFacade;
@EJB
private ProductSession productFacade;
public ReporterTask(String reportName) {
this.reportName = reportName;
}
public void run() {
// Run the named report
if ("JobsReport".equals(reportName)) {
runJobReport();
} else if ("ProductReport".equals(reportName)) {
runProductReport();
}
}
/**
* Prints a list of jobs to the system log.
*/
public void runJobReport() {
List<Jobs> jobs = jobsFacade.getJobList();
System.out.println("Job Listing Report");
System.out.println("=====================");
for (Jobs job : jobs) {
System.out.println(job.getTitle() + " " + job.getDivision());
}
}
/**
* Prints a list of products
*/
void runProductReport() {
System.out.println("Querying the database");
Path reportFile = Paths.get("ProductReport.txt");
try (BufferedWriter writer = Files.newBufferedWriter(
reportFile, Charset.defaultCharset())) {
Files.deleteIfExists(reportFile);
reportFile = Files.createFile(reportFile);
writer.append("Product Listing Report");
writer.newLine();
writer.append("===================");
writer.newLine();
List<Product> products = productFacade.obtainProduct();
for (Product product : products) {
writer.append(product.getName());
writer.newLine();
}
writer.flush();
} catch (IOException exception) {
System.out.println("Error writing to file");
}
}
}