1 package info.mikethomas.fahweb.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import info.mikethomas.fahweb.model.Team;
26 import info.mikethomas.fahweb.model.User;
27 import info.mikethomas.fahweb.service.TeamService;
28 import info.mikethomas.fahweb.service.UserService;
29 import com.opensymphony.xwork2.ActionSupport;
30 import java.io.BufferedReader;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33
34
35
36
37
38
39
40 public class UpdateAction extends ActionSupport {
41
42
43 public static final String LINE_DELIMITER = "\t";
44 private final TeamService teamService;
45 private final UserService userService;
46
47
48
49
50
51
52
53 public UpdateAction(TeamService teamService, UserService userService) {
54 this.teamService = teamService;
55 this.userService = userService;
56 }
57
58
59 @Override
60 public String execute() throws Exception {
61 updateTeams();
62
63 return SUCCESS;
64 }
65
66
67
68
69
70
71
72 public String updateTeams() throws Exception {
73 InputStream in = this.getClass().getResourceAsStream("/daily_team_summary.txt");
74 InputStreamReader read = new InputStreamReader(in);
75 BufferedReader buf = new BufferedReader(read);
76
77 LOG.info(buf.readLine());
78 LOG.info(buf.readLine());
79
80 while (buf.ready()) {
81 String line = buf.readLine();
82
83 if (line.split(LINE_DELIMITER).length != 4) {
84 line += buf.readLine().replace("\\n", "");
85 }
86
87 teamService.addTeam(createTeamFromLine(line));
88 }
89 return SUCCESS;
90 }
91
92
93
94
95
96
97
98 public String updateUsers() throws Exception {
99 InputStream in = this.getClass().getResourceAsStream("/daily_user_summary.txt");
100 InputStreamReader read = new InputStreamReader(in);
101 BufferedReader buf = new BufferedReader(read);
102
103 LOG.info(buf.readLine());
104 LOG.info(buf.readLine());
105
106 while (buf.ready()) {
107 String line = buf.readLine();
108
109 if (line.split(LINE_DELIMITER).length != 4) {
110 LOG.error(line);
111 continue;
112 }
113
114 userService.addUser(createUserFromLine(line));
115 }
116 return SUCCESS;
117 }
118
119 private Team createTeamFromLine(String line) {
120 String[] array = line.split(LINE_DELIMITER);
121
122 int team = Integer.parseInt(array[0]);
123 String teamname = array[1];
124 long score = Long.parseLong(array[2]);
125 int wu = Integer.parseInt(array[3]);
126
127 return new Team(team, teamname, score, wu);
128 }
129
130 private User createUserFromLine(String line) {
131 String[] array = line.split(LINE_DELIMITER);
132
133 String name = array[0];
134 long newcredit = Long.parseLong(array[1]);
135 long sum = Long.parseLong(array[2]);
136 int team = Integer.parseInt(array[3]);
137
138 return new User(name, newcredit, sum, team);
139 }
140 }