View Javadoc
1   package info.mikethomas.fahweb.action;
2   
3   /*
4    * #%L
5    * This file is part of FAHWeb.
6    * %%
7    * Copyright (C) 2012 - 2017 Michael Thomas <mikepthomas@outlook.com>
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * <p>UpdateAction class.</p>
36   *
37   * @author Michael Thomas (mikepthomas@outlook.com)
38   * @version $Id: $Id
39   */
40  public class UpdateAction extends ActionSupport {
41  
42      /** Constant <code>LINE_DELIMITER="\t"</code> */
43      public static final String LINE_DELIMITER = "\t";
44      private final TeamService teamService;
45      private final UserService userService;
46  
47      /**
48       * <p>Constructor for UpdateAction.</p>
49       *
50       * @param teamService a {@link info.mikethomas.fahweb.service.TeamService} object.
51       * @param userService a {@link info.mikethomas.fahweb.service.UserService} object.
52       */
53      public UpdateAction(TeamService teamService, UserService userService) {
54          this.teamService = teamService;
55          this.userService = userService;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public String execute() throws Exception {
61          updateTeams();
62          //updateUsers();
63          return SUCCESS;
64      }
65  
66      /**
67       * <p>updateTeams.</p>
68       *
69       * @return a {@link java.lang.String} object.
70       * @throws java.lang.Exception if any.
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()); // Time
78          LOG.info(buf.readLine()); // Headings
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       * <p>updateUsers.</p>
94       *
95       * @return a {@link java.lang.String} object.
96       * @throws java.lang.Exception if any.
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()); // Time
104         LOG.info(buf.readLine()); // Headings
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 }