Menu

[r1]: / src / EventLog.java  Maximize  Restore  History

Download this file

323 lines (284 with data), 10.4 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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import difflib.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
import javax.swing.*;
public class EventLog {
private Project project;
private long nextEventID;
private long nextAttachID;
private List<Event> events;
private File logFile;
private File logAttachDir;
private List<EventLogListener> listeners;
public EventLog(File logFile, File logAttachDir) {
this(null, logFile, logAttachDir);
}
public EventLog(Project project, File logFile, File logAttachDir) {
this.project = project;
this.nextEventID = 1;
this.nextAttachID = 1;
this.events = new ArrayList<Event>();
this.logFile = logFile;
this.logAttachDir = logAttachDir;
this.listeners = new ArrayList<EventLogListener>();
if (logFile.exists()) {
loadFromDisk();
}
if (!logAttachDir.exists()) {
if (!logAttachDir.mkdirs()) {
JOptionPane.showMessageDialog(null,
"ERROR: Could not create log attachment subdirectory!",
project.getName(), JOptionPane.ERROR_MESSAGE);
this.logAttachDir = null;
}
} else if (!logAttachDir.isDirectory()) {
JOptionPane.showMessageDialog(null,
"ERROR: Could not create log attachment subdirectory; " +
"another file already exists with the same name!",
project.getName(), JOptionPane.ERROR_MESSAGE);
this.logAttachDir = null;
}
}
public Project getProject() {
return project;
}
public List<Event> getAllEvents() {
return events;
}
public String getLogAttachDir() {
return logAttachDir.getAbsolutePath();
}
public Event createEvent() {
return createEvent(new Date());
}
public Event createEvent(Date timestamp) {
Event evt = new Event(nextEventID, timestamp, this);
nextEventID++;
return evt;
}
public Event createEvent(String tag) {
return createEvent(new Date(), tag);
}
public Event createEvent(Date timestamp, String tag) {
Event evt = new Event(nextEventID, timestamp, this, tag);
nextEventID++;
return evt;
}
public Attachment createAttachment(String text) {
Attachment att = new Attachment(nextAttachID, text, this);
nextAttachID++;
return att;
}
public Attachment createAttachment(File path) {
Attachment att = new Attachment(nextAttachID, path, this);
nextAttachID++;
return att;
}
public void addEvent(Event evt) {
int i;
boolean added = false;
for (i=0; !added && i<events.size(); i++) {
if (evt.compareTo(events.get(i)) < 0) {
events.add(i, evt);
added = true;
notifyReloadListeners();
}
}
if (!added) {
events.add(evt);
notifyAddListeners(evt);
}
saveToDisk();
}
public void removeEvent(Event evt) {
events.remove(evt);
notifyRemoveListeners(evt);
}
public void mergeEvents(List<Event> eventsToMerge) {
// special handling to build the new aggregate event
// and remove the old ones
int i;
if (eventsToMerge.size() < 2) {
return;
}
Collections.sort(eventsToMerge);
// set up merged "modify" event hash
Map<String,Event> originalModify = new HashMap<String,Event>();
Map<String,Event> currentModify = new HashMap<String,Event>();
// aggregate's event's attributes
Date newTime = eventsToMerge.get(eventsToMerge.size()-1).getTimestamp();
StringBuffer newDetails = new StringBuffer();
String details, path = null, temp;
Event evt; Attachment att;
SortedSet<String> allTags = new TreeSet<String>();
for (i=0; i<eventsToMerge.size(); i++) {
evt = eventsToMerge.get(i);
//System.out.println("merging: " + evt.getID());
if (evt.hasTag("modify") && !evt.hasTag("aggregate") &&
evt.hasProperty("path") && evt.hasAttachment("snapshot")) {
temp = evt.getProperty("path");
if (!originalModify.containsKey(temp)) {
originalModify.put(temp, evt);
}
currentModify.put(temp, evt);
} else {
details = evt.getDetails();
if (details == null) {
att = evt.getAttachment("diff");
if (att != null) {
details = att.getTextData();
} else {
att = evt.getAttachment("snapshot");
if (att != null) {
details = att.getTextData();
}
}
}
if (details != null) {
if (i > 0) {
newDetails.append("\n\n");
}
newDetails.append(details);
}
}
allTags.addAll(evt.getTags());
if (i==0) {
if (evt.hasProperty("path")) {
path = evt.getProperty("path");
}
} else if (path != null && evt.hasProperty("path") &&
!evt.getProperty("path").equals(path)) {
path = null;
}
}
// create merged modification diffs
for (Map.Entry<String,Event> entry : originalModify.entrySet()) {
String p = entry.getKey();
Event orig = entry.getValue();
String original = orig.getAttachment("snapshot").getTextData();
if (orig.hasAttachment("original")) {
// diff from original version if available
original = orig.getAttachment("original").getTextData();
}
String current = currentModify.get(p).getAttachment("snapshot").getTextData();
java.util.List<String> origList = Arrays.asList(original.split("\n"));
java.util.List<String> currList = Arrays.asList(current.split("\n"));
Patch patch = DiffUtils.diff(origList, currList);
java.util.List<String> udiff = DiffUtils.generateUnifiedDiff(p, p, origList, patch, 3);
if (newDetails.length() > 0) {
newDetails.append("\n\n");
}
if (!orig.hasAttachment("original") && orig.hasAttachment("diff")) {
// if we don't have the original, add the first diff
newDetails.append(orig.getAttachment("diff").getTextData());
newDetails.append("\n\n");
}
boolean first = true;
for (String s : udiff) {
if (!first) {
newDetails.append("\n");
} else {
first = false;
}
newDetails.append(s);
}
}
// create aggregate event
Event newEvent = createEvent(newTime, "aggregate");
for (String t : allTags) {
newEvent.addTag(t);
}
if (path != null) {
newEvent.setProperty("path", path);
}
newEvent.setAttachment("details",
createAttachment(newDetails.toString()));
for (i=0; i<eventsToMerge.size(); i++) {
newEvent.addSubEvent(eventsToMerge.get(i));
removeEvent(eventsToMerge.get(i));
}
addEvent(newEvent);
}
public void splitEvent(Event eventToSplit) {
if (eventToSplit.hasTag("aggregate")) {
removeEvent(eventToSplit);
for (Event evt : eventToSplit.getSubEvents()) {
addEvent(evt);
}
}
}
public String getRelativePath(String fullPath) {
// TODO: search project roots and find the parent of this pathname,
// then strip it
return fullPath;
}
public String getAbsolutePath(String fileName) {
// TODO: search project roots and build the full path
return fileName;
}
public Attachment getLastSnapshot(String path) {
// TODO: search the events to find snapshot for this path, then return
// the latest one
return null;
}
public void loadFromDisk() {
Event evt;
int num, i;
try {
ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(
new GZIPInputStream(
new FileInputStream(logFile))));
project = (Project)in.readObject();
nextEventID = in.readLong();
nextAttachID = in.readLong();
num = in.readInt();
for (i=0; i<num; i++) {
evt = (Event)in.readObject();
evt.setEventLog(this);
events.add(evt);
}
in.close();
} catch (IOException e) { }
catch (ClassNotFoundException e) { }
}
public void saveToDisk() {
try {
ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream(logFile))));
out.writeObject(project);
out.writeLong(nextEventID);
out.writeLong(nextAttachID);
out.writeInt(events.size());
for (Event e : events) {
out.writeObject(e);
}
out.close();
} catch (IOException e) { }
}
public void addEventLogListener(EventLogListener ell) {
listeners.add(ell);
}
public void notifyAddListeners(Event evt) {
for (EventLogListener l : listeners) {
l.eventAdded(evt);
}
}
public void notifyRemoveListeners(Event evt) {
for (EventLogListener l : listeners) {
l.eventRemoved(evt);
}
}
public void notifyReloadListeners() {
for (EventLogListener l : listeners) {
l.reloadEvents();
}
}
public void close() {
saveToDisk();
}
}
MongoDB Logo MongoDB