source: trunk/src/org/expeditee/stats/StatsLogger.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 3.5 KB
Line 
1/**
2 * StatsLogger.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.stats;
20
21import java.io.BufferedWriter;
22import java.io.File;
23import java.io.FileWriter;
24import java.io.IOException;
25import java.lang.reflect.Method;
26
27import org.expeditee.gui.Frame;
28import org.expeditee.gui.FrameIO;
29import org.expeditee.items.Text;
30import org.expeditee.settings.UserSettings;
31
32/**TODO make this threadsafe*/
33
34/**
35 * This class is used to create all stats log files. Files are created with
36 * filename: Date-Time.stat<br>
37 * Where Date is in the format DDMMMYYYY and Time is in the format HHMM.
38 *
39 * @author mrww1
40 *
41 */
42public class StatsLogger {
43
44 private static String _filename = null;
45
46 private static final String FRAMES_EDITED_FILENAME = "FramesEdited.log";
47
48 /**
49 * Sets the path on disk that the statlog files will be created in.
50 *
51 * @param location
52 * The path on disk to create the log files in.
53 */
54 private static void Init() {
55 if (!org.expeditee.settings.UserSettings.LogStats.get())
56 return;
57
58 File test = new File(org.expeditee.gui.FrameIO.STATISTICS_DIR);
59 if (!test.exists())
60 test.mkdir();
61
62 _filename = Formatter.getDateTime() + ".stat";
63 }
64
65 /**
66 * Writes the current stats to a file.
67 *
68 */
69 public static void WriteStatsFile() {
70 if (!org.expeditee.settings.UserSettings.LogStats.get())
71 return;
72
73 Init();
74
75 String statsFrameset = UserSettings.StatisticsFrameset.get();
76 if (statsFrameset != null) {
77 try {
78 Frame statsFrame = null;
79 if (FrameIO.LoadFrame(statsFrameset + "0") == null) {
80 statsFrame = FrameIO.CreateNewFrameset(statsFrameset);
81 statsFrame.setTitle(Formatter.getDateTime());
82 } else {
83 statsFrame = FrameIO.CreateFrame(statsFrameset, Formatter
84 .getDateTime(), null);
85 }
86
87 //Now check the attribute value pairs
88 for(Text t: statsFrame.getBodyTextItems(false)){
89 Method m = StatsFrame.getMethod(t.getText());
90 if(m != null){
91 String value = m.invoke(null, new Object[]{}).toString();
92 t.setText(t.getText() + ": " + value);
93 }
94 }
95 FrameIO.ForceSaveFrame(statsFrame);
96 } catch (Exception e) {
97 e.printStackTrace();
98 }
99 }
100
101 try {
102 BufferedWriter writer = new BufferedWriter(new FileWriter(
103 org.expeditee.gui.FrameIO.STATISTICS_DIR + _filename, true));
104
105 writer.write(SessionStats.getCurrentStats());
106 writer.newLine();
107 writer.newLine();
108 writer.write(SessionStats.getItemStats());
109 writer.newLine();
110 writer.newLine();
111 writer.write(SessionStats.getEventStats());
112 writer.newLine();
113
114 writer.flush();
115 writer.close();
116
117 writer = new BufferedWriter(new FileWriter(
118 org.expeditee.gui.FrameIO.STATISTICS_DIR
119 + FRAMES_EDITED_FILENAME, true));
120 writer.write(SessionStats.getFramesEdited());
121 writer.newLine();
122
123 writer.flush();
124 writer.close();
125 } catch (IOException ioe) {
126 ioe.printStackTrace();
127 }
128 }
129}
Note: See TracBrowser for help on using the repository browser.