-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasterCollectionManager.java
667 lines (580 loc) · 24.8 KB
/
EasterCollectionManager.java
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package attilathehun.songbook.collection;
import attilathehun.songbook.environment.Environment;
import attilathehun.songbook.environment.SettingsManager;
import attilathehun.songbook.util.HTMLGenerator;
import attilathehun.songbook.vcs.CacheManager;
import attilathehun.songbook.window.AlertDialog;
import attilathehun.songbook.window.CodeEditor;
import attilathehun.songbook.window.SongbookApplication;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.util.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
// TODO
public final class EasterCollectionManager extends CollectionManager {
private static final Logger logger = LogManager.getLogger(EasterCollectionManager.class);
private final String collectionName = "easter";
private static final List<CollectionListener> listeners = new ArrayList<>();
private static final EasterCollectionManager INSTANCE = new EasterCollectionManager();
private ArrayList<Song> collection;
private EasterCollectionManager() {
}
private EasterCollectionManager(ArrayList<Song> collection) {
this.collection = new ArrayList<Song>(collection);
}
public static EasterCollectionManager getInstance() {
return INSTANCE;
}
@Override
public void init() {
if (!Environment.IS_IT_EASTER_ALREADY) {
return;
}
try {
final File collectionJSONFile = new File(getCollectionFilePath());
if (!collectionJSONFile.exists()) {
final File songDataFolder = new File(getSongDataFilePath());
if (songDataFolder.exists() && songDataFolder.isDirectory()) {
repairCollectionDialog();
} else {
createCollectionDialog();
return;
}
}
Environment.getInstance().registerCollectionManager(this);
final String json = String.join("", Files.readAllLines(collectionJSONFile.toPath()));
final Type targetClassType = new TypeToken<ArrayList<Song>>(){}.getType();
collection = new Gson().fromJson(json, targetClassType);
for (final Song song : collection) {
song.setManager(this);
}
} catch (final IOException e) {
logger.error(e.getMessage(), e);
new AlertDialog.Builder().setTitle("Easter Collection Initialisation error").setIcon(AlertDialog.Builder.Icon.ERROR)
.setMessage("Can not load the easter song collection, for complete error message view the log file. If the problem persists, try reformatting or deleting the collection file.")
.addOkButton().setParent(SongbookApplication.getMainWindow()).build().open();
return;
}
logger.debug("EasterCollectionManager initialized");
}
@Override
public String getCollectionName() {
return collectionName;
}
@Override
public CollectionManager copy() {
return new EasterCollectionManager(collection);
}
@Override
public void save() {
try {
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final FileWriter writer = new FileWriter(getCollectionFilePath());
gson.toJson(collection, writer);
writer.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
new AlertDialog.Builder().setTitle("Error").setIcon(AlertDialog.Builder.Icon.ERROR)
.setMessage("Can not save the easter song collection, for complete error message view the log file.")
.addOkButton().setParent(SongbookApplication.getMainWindow()).build().open();
}
}
@Override
public Collection<Song> getCollection() {
return collection;
}
@Override
public ArrayList<Song> getSortedCollection() {
final ArrayList<Song> copy = new ArrayList<Song>(collection);
copy.sort((s1, s2) -> Collator.getInstance().compare(s1.name(), s2.name()));
return copy;
}
@Override
public ArrayList<Song> getDisplayCollection() {
return StandardCollectionManager.getInstance().getDisplayCollection();
}
@Override
public ArrayList<Song> getFormalCollection() {
final ArrayList<Song> standardFormalList = StandardCollectionManager.getInstance().getFormalCollection();
final ArrayList<Song> formalList = new ArrayList<Song>();
for (final Song song : standardFormalList) {
boolean esterEggFound = false;
if (!song.isActive()) {
continue;
}
searchForEasterEgg:
for (final Song value : collection) {
if (value.isActive() && value.id() == song.id()) {
formalList.add(value);
esterEggFound = true;
break searchForEasterEgg;
}
}
if (!esterEggFound) {
formalList.add(song);
}
}
return formalList;
}
@Override
public Song addSong(final Song s) {
if (s == null) {
throw new IllegalArgumentException();
}
s.setManager(this);
if (!new HTMLGenerator().generateSongFile(s)) {
return null;
}
int defaultIndex = StandardCollectionManager.getInstance().getCollectionSongIndex(s.id());
if (defaultIndex == -1) {
throw new IllegalArgumentException();
}
collection.add(s);
save();
onSongAdded(s);
CodeEditor.open(s, this);
if (Environment.getInstance().getCollectionManager().equals(this)) {
Environment.navigateWebViewToSong(collection.getLast());
}
return s;
}
@Override
public void removeSong(final Song s) {
collection.remove(getCollectionSongIndex(s));
save();
final File songFile = new File(String.format(getSongDataFilePath() + "/%d.html", s.id()));
if (songFile.delete()) {
new AlertDialog.Builder().setTitle("Success").setIcon(AlertDialog.Builder.Icon.INFO)
.setMessage(String.format("Easter Egg Song '%s' id: %d deleted. Ave Caesar!", s.name(), s.id()))
.addOkButton().build().open();
}
onSongRemoved(s);
}
@Override
public void activateSong(Song s) {
collection.get(getCollectionSongIndex(s)).setActive(true);
save();
onSongUpdated(s);
}
@Override
public void deactivateSong(Song s) {
collection.get(getCollectionSongIndex(s)).setActive(false);
save();
onSongUpdated(s);
}
@Override
public Song updateSongRecord(final Song s) {
int songIdMatches = 0;
for (final Song song : collection) {
if (song.id() == s.id()) {
songIdMatches++;
}
}
if (songIdMatches > 1) {
new AlertDialog.Builder().setTitle("Warning").setIcon(AlertDialog.Builder.Icon.WARNING)
.setMessage("Easter song under this Id already exists. Please resolve this conflict manually by changing the Id of the other song, or by choosing a different Id.")
.addOkButton().build().open();
return null;
}
int index = getCollectionSongIndex(s);
Song song = null;
if (index == -1) {
for (final Song item : collection) {
if (item.id() == s.getFormerId()) {
song = item;
}
}
index = getCollectionSongIndex(song);
final File oldFile= new File(getSongFilePath(s.getFormerId()));
final File newFile = new File(getSongFilePath(s.id()));
if (newFile.exists()) {
new AlertDialog.Builder().setTitle("Warning").setIcon(AlertDialog.Builder.Icon.WARNING)
.setMessage(String.format("A file corresponding to the new Easter song Id already exists. To prevent data loss you should manually rename it to another Id. Alternatively you can delete the file to resolve this issues. File path: %s", newFile.getPath()))
.addOkButton().build().open();
return null;
}
if (!oldFile.renameTo(newFile)) {
new AlertDialog.Builder().setTitle("Error").setIcon(AlertDialog.Builder.Icon.ERROR)
.setMessage("Something went wrong when renaming the song file. More information may be found in application log file.")
.addOkButton().build().open();
return null;
}
collection.remove(index);
collection.add(s);
song = s;
} else {
collection.get(index).setName(s.name());
collection.get(index).setUrl(s.getUrl());
collection.get(index).setActive(s.isActive());
song = collection.get(index);
}
save();
onSongUpdated(song);
return song;
}
@Override
public void updateSongRecordTitleFromHTML(final Song s) {
// It is undesirable to bind title in easter egg collection
}
@Override
public void updateSongHTMLTitleFromRecord(final Song s) {
// five lines above
}
@Override
public void updateSongHTMLFromRecord(final Song s) {
if (!CollectionManager.isValidId(s.id())) {
throw new IllegalArgumentException();
}
try {
final String songHTML = String.join("\n", Files.readAllLines(Path.of(getSongFilePath(s))));
final Document document = Jsoup.parse(songHTML);
document.outputSettings().indentAmount(0).prettyPrint(false);
Element element = document.select("meta[name=url]").first();
element.attr("value", s.getUrl());
element = document.select("meta[name=active]").first();
element.attr("value", String.valueOf(s.isActive()));
final FileOutputStream out = new FileOutputStream(getSongFilePath(s));
out.write(document.select(".song").first().toString().replace("<pre class=\"song-text-formatting\"><span class=\"verse\"", "<pre class=\"song-text-formatting\">\n\n<span class=\"verse\"")
.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
} catch (final Exception e) {
logger.error("Target song: {}", s.id());
logger.error(e.getMessage(), e);
}
}
@Override
public String getSongFilePath(final Song s) {
return getSongFilePath(s.id());
}
@Override
public String getSongFilePath(final int id) {
return Paths.get(String.format("%s/%d.html", getSongDataFilePath(), id)).toString();
}
@Override
public int getCollectionSongIndex(final Song s) {
int i = 0;
for (final Song song : collection) {
if (song.equals(s)) {
return i;
}
i++;
}
return -1;
}
@Override
public int getCollectionSongIndex(final int songId) {
if (songId < 0) {
throw new IllegalArgumentException();
}
int i = 0;
for (final Song song : collection) {
if (song.id() == songId) {
return i;
}
i++;
}
return -1;
}
@Override
public int getSortedCollectionSongIndex(final Song s) {
int i = 0;
for (final Song song : getSortedCollection()) {
if (song.equals(s)) {
return i;
}
i++;
}
return -1;
}
@Override
public int getSortedCollectionSongIndex(final int id) {
if (id < 0) {
throw new IllegalArgumentException();
}
int i = 0;
for (final Song song : getSortedCollection()) {
if (song.id() == id) {
return i;
}
i++;
}
return -1;
}
@Override
public int getDisplayCollectionSongIndex(final Song s) {
int i = 0;
for (final Song song : getDisplayCollection()) {
if (song.equals(s)) {
return i;
}
i++;
}
return -1;
}
@Override
public int getDisplayCollectionSongIndex(final int id) {
if (id < 0) {
throw new IllegalArgumentException();
}
int i = 0;
for (final Song song : getDisplayCollection()) {
if (song.id() == id) {
return i;
}
i++;
}
return -1;
}
@Override
public int getFormalCollectionSongIndex(final Song s) {
int i = 0;
for (final Song song : getFormalCollection()) {
if (song.equals(s)) {
return i;
}
i++;
}
return -1;
}
@Override
public int getFormalCollectionSongIndex(final int id) {
if (id < 0) {
throw new IllegalArgumentException();
}
int i = 0;
for (final Song song : getFormalCollection()) {
if (song.id() == id) {
return i;
}
i++;
}
return -1;
}
@Override
public Song getPlaceholderSong() {
return new Song("Secret Song", CollectionManager.INVALID_SONG_ID);
}
@Override
public void addListener(final CollectionListener listener) {
if (listener == null) {
throw new IllegalArgumentException();
}
listeners.add(listener);
}
@Override
public void removeListener(final CollectionListener listener) {
if (listener == null) {
throw new IllegalArgumentException();
}
listeners.remove(listener);
}
@Override
public String getCollectionFilePath() {
return Paths.get(SettingsManager.getInstance().getValue("DATA_FILE_PATH"), String.format(CollectionManager.COLLECTION_FILE_NAME, collectionName)).toString();
}
@Override
public String getRelativeFilePath() {
return Paths.get(String.format(CollectionManager.RELATIVE_DATA_FILE_NAME, collectionName)).toString();
}
@Override
public String getSongDataFilePath() {
return Paths.get(SettingsManager.getInstance().getValue("SONGS_FILE_PATH"), collectionName).toString();
}
@Override
public CompletableFuture<Song> addSongDialog() {
return addSongDialog(getPlaceholderSong());
}
private CompletableFuture<Song> addSongDialog(final Song s) {
final CompletableFuture<Song> output = new CompletableFuture<>();
final CheckBox songActiveSwitch = new CheckBox("Active");
songActiveSwitch.setSelected(s.isActive());
songActiveSwitch.setTooltip(new Tooltip("When disabled, the song will not be included in the songbook."));
final CompletableFuture<Pair<Integer, ArrayList<Node>>> dialogResult = new AlertDialog.Builder().setTitle("Add an Easter song").setIcon(AlertDialog.Builder.Icon.CONFIRM)
.setParent(SongbookApplication.getMainWindow())
.addTextInput("Name:", s.name(), "Enter song name", "Name of the song. For example 'I Will Always Return'.")
.addTextInput("Id:", String.valueOf(s.id()), "Enter song id", "Identificator of the song. Do not confuse with collection index (n).")
.addTextInput("Author:", s.getAuthor(), "Enter song author", "Author or interpret of the song. For example 'Leonard Cohen'.")
.addTextInput("URL:", s.getUrl(), "Link to a performance of the song.")
.addContentNode(songActiveSwitch)
.addOkButton("Add")
.addCloseButton("Cancel")
.build().awaitData();
dialogResult.thenAccept((result) -> {
if (result.getKey() != AlertDialog.RESULT_OK) {
output.complete(null);
return;
}
Song song = new Song(((TextField) result.getValue().get(1)).getText(), Integer.parseInt(((TextField) result.getValue().get(3)).getText()));
song.setAuthor(((TextField) result.getValue().get(5)).getText());
song.setUrl(((TextField) result.getValue().get(7)).getText());
song.setActive(songActiveSwitch.isSelected());
song = addSong(song);
output.complete(song);
});
return output;
}
public CompletableFuture<Song> addSongFromTemplateDialog(final Song s) {
return addSongDialog(s);
}
@Override
public CompletableFuture<Song> editSongDialog(final Song s) {
final CompletableFuture<Song> output = new CompletableFuture<>();
final CheckBox songActiveSwitch = new CheckBox("Active");
songActiveSwitch.setSelected(s.isActive());
songActiveSwitch.setTooltip(new Tooltip("When disabled, the song will not be included in the songbook."));
final CompletableFuture<Pair<Integer, ArrayList<Node>>> dialogResult = new AlertDialog.Builder().setTitle(String.format("Edit eater song id: %s", s.id())).setIcon(AlertDialog.Builder.Icon.CONFIRM)
.setParent(SongbookApplication.getMainWindow())
.addTextInput("Name:", s.name(), "Enter song name", "Name of the song. For example 'I Will Always Return'.")
.addTextInput("Id:", String.valueOf(s.id()), "Enter song id", "Identificator of the song. Do not confuse with collection index (n).")
.addTextInput("URL:", s.getUrl(), "Link to a performance of the song.")
.addContentNode(songActiveSwitch)
.addOkButton("Edit")
.addCloseButton("Cancel")
.build().awaitData();
dialogResult.thenAccept((result) -> {
if (result.getKey() != AlertDialog.RESULT_OK) {
output.complete(null);
return;
}
Song song = new Song(((TextField) result.getValue().get(1)).getText(), Integer.parseInt(((TextField) result.getValue().get(3)).getText()));
song.setFormerId(s.id());
song.setUrl(((TextField) result.getValue().get(5)).getText());
song.setActive(songActiveSwitch.isSelected());
song.setManager(this);
song = updateSongRecord(song);
output.complete(song);
});
return output;
}
private void onSongAdded(final Song s) {
for (final CollectionListener listener : listeners) {
listener.onSongAdded(s, this);
}
}
private void onSongRemoved(final Song s) {
for (final CollectionListener listener : listeners) {
listener.onSongRemoved(s, this);
}
}
private void onSongUpdated(final Song s) {
for (final CollectionListener listener : listeners) {
listener.onSongUpdated(s, this);
}
}
/* Internal helper methods */
private Song createSongRecordFromLocalFile(final int songId) {
if (!CollectionManager.isValidId(songId)) {
throw new IllegalArgumentException();
}
String songName = null;
try {
String songHTML = String.join("\n", Files.readAllLines(Path.of(getSongFilePath(songId))));
Document document = Jsoup.parse(songHTML);
Element element = document.select(".song-title").first();
songName = element.text();
element = document.select("meta[name=url]").first();
String songURL = (element != null) ? element.attr("value") : "";
element = document.select("meta[name=active]").first();
boolean songActiveStatus = (element != null) ? Boolean.parseBoolean(element.attr("value")) : true;
return new Song(songId, songName, songActiveStatus, songURL);
} catch (final Exception e) {
logger.info("Target song: {} {}", songId, songName);
logger.error(e.getMessage(), e);
}
return null;
}
private void repairCollectionDialog() {
final CompletableFuture<Integer> result = new AlertDialog.Builder().setTitle("Repair easter egg collection").setIcon(AlertDialog.Builder.Icon.CONFIRM)
.setMessage("No collection file was found but it seems there are songs saved in your easter egg data directory. Would you like to generate a collection file to repair the easter egg collection? Alternatively, you can create a new collection.")
.addOkButton("Repair").addCloseButton("Create").setParent(SongbookApplication.getMainWindow()).setCancelable(false).build().awaitResult();
result.thenAccept(dialogResult -> {
if (dialogResult == AlertDialog.RESULT_OK) {
repairMissingCollectionFile();
} else if (dialogResult == AlertDialog.RESULT_CLOSE) {
createNewCollection();
}
});
}
private void createCollectionDialog() {
final CompletableFuture<Integer> result = new AlertDialog.Builder().setTitle("Create an easter egg collection").setIcon(AlertDialog.Builder.Icon.CONFIRM)
.setMessage("Do you want to create an easter egg collection? For more information visit the wiki.")
.addOkButton("Create").addCloseButton("Cancel").build().awaitResult();
result.thenAccept(dialogResult -> {
if (dialogResult == AlertDialog.RESULT_OK) {
createNewCollection();
}
});
}
private void repairMissingCollectionFile() {
logger.info("Repairing easter collection");
collection = new ArrayList<>();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(getSongDataFilePath()))) {
for (final Path path : stream) {
if (!Files.isDirectory(path) && path.toString().trim().endsWith(".html")) {
int songId;
final String pathString = path.toString();
if (pathString.contains(File.separator)) {
songId = Integer.parseInt(pathString.substring(pathString.lastIndexOf(File.separator) + 1, pathString.indexOf(".html")));
} else {
songId = Integer.parseInt(pathString.substring(0, pathString.indexOf(".html")));
}
collection.add(createSongRecordFromLocalFile(songId));
}
}
// cache it before the collection file is created so its last modified date does not get cached
CacheManager.getInstance().cacheSongbookVersionTimestamp();
save();
// no changes were done by creating the collection file, so we do not want to make the VCS think we changed something
new File(getCollectionFilePath()).setLastModified(CacheManager.getInstance().getCachedSongbookVersionTimestamp());
logger.info("Success!");
} catch (final IOException e) {
logger.error(e.getMessage(), e);
}
}
private void createNewCollection() {
try {
final File songDataFolder = new File(getSongDataFilePath());
songDataFolder.mkdirs();
final File collectionJSONFile = new File(getCollectionFilePath());
collectionJSONFile.createNewFile();
collection = new ArrayList<Song>();
final PrintWriter printWriter = new PrintWriter(new FileWriter(collectionJSONFile));
printWriter.write("[]");
printWriter.close();
Environment.getInstance().registerCollectionManager(this);
final CompletableFuture<Integer> result = new AlertDialog.Builder().setTitle("Add Easter Song?").setMessage("Do you want to create your first easter song?").setIcon(AlertDialog.Builder.Icon.CONFIRM)
.addOkButton("Create").addCloseButton("Cancel").build().awaitResult();
result.thenAccept(dialogResult -> {
if (dialogResult == AlertDialog.RESULT_OK) {
addSongDialog();
}
});
} catch (final IOException e) {
logger.error(e.getMessage(), e);
new AlertDialog.Builder().setTitle("Error").setMessage("Could not create a new easter song!").setIcon(AlertDialog.Builder.Icon.ERROR)
.addOkButton().build().open();
}
}
}