1 | public static BibtexDatabase createDatabase(Collection<BibtexEntry> bibentries) { | | 1 | /** |
2 | purgeEmptyEntries(bibentries); | | 2 | * Adds the entries to the database, possibly checking for duplicates first. |
3 | | | 3 | * @param filename If non-null, a message is printed to the status line describing |
4 | BibtexDatabase database = new BibtexDatabase(); | | 4 | * how many entries were imported, and from which file. If null, the message will not |
5 | | | 5 | * be printed. |
6 | for (Iterator<BibtexEntry> i = bibentries.iterator(); i.hasNext();) { | | 6 | * @param intoNew Determines if the entries will be put in a new database or in the current |
7 | BibtexEntry entry = i.next(); | | 7 | * one. |
8 | | | 8 | */ |
9 | try { | | 9 | public int addBibEntries(List<BibtexEntry> bibentries, String filename, |
10 | entry.setId(Util.createNeutralId()); | | 10 | boolean intoNew) { |
11 | database.insertEntry(entry); | | 11 | if (bibentries == null || bibentries.size() == 0) { |
12 | } catch (KeyCollisionException ex) { | | 12 | |
13 | System.err.println("KeyCollisionException [ addBibEntries(...) ]"); | | 13 | // No entries found. We need a message for this. |
14 | } | | 14 | JOptionPane.showMessageDialog(JabRefFrame.this, Globals.lang("No entries found. Please make sure you are " |
15 | } | | 15 | +"using the correct import filter."), Globals.lang("Import failed"), |
16 | | | 16 | JOptionPane.ERROR_MESSAGE); |
17 | return database; | | 17 | return 0; |
18 | } | | 18 | } |
| | | 19 | |
| | | 20 | int addedEntries = 0; |
| | | 21 | |
| | | 22 | // Set owner and timestamp fields: |
| | | 23 | Util.setAutomaticFields(bibentries, Globals.prefs.getBoolean("overwriteOwner"), |
| | | 24 | Globals.prefs.getBoolean("overwriteTimeStamp"), Globals.prefs.getBoolean("markImportedEntries")); |
| | | 25 | |
| | | 26 | if (intoNew || (tabbedPane.getTabCount() == 0)) { |
| | | 27 | // Import into new database. |
| | | 28 | BibtexDatabase database = new BibtexDatabase(); |
| | | 29 | for (BibtexEntry entry : bibentries){ |
| | | 30 | try { |
| | | 31 | entry.setId(Util.createNeutralId()); |
| | | 32 | database.insertEntry(entry); |
| | | 33 | } |
| | | 34 | catch (KeyCollisionException ex) { |
| | | 35 | //ignore |
| | | 36 | System.err.println("KeyCollisionException [ addBibEntries(...) ]"); |
| | | 37 | } |
| | | 38 | } |
| | | 39 | // Metadata are only put in bibtex files, so we will not find it |
| | | 40 | // in imported files. We therefore pass in an empty MetaData: |
| | | 41 | BasePanel bp = new BasePanel(JabRefFrame.this, database, null, new MetaData(), Globals.prefs.get("defaultEncoding")); |
| | | 42 | /* |
| | | 43 | if (prefs.getBoolean("autoComplete")) { |
| | | 44 | db.setCompleters(autoCompleters); |
| | | 45 | } |
| | | 46 | */ |
| | | 47 | addedEntries = database.getEntryCount(); |
| | | 48 | tabbedPane.add(GUIGlobals.untitledTitle, bp); |
| | | 49 | bp.markBaseChanged(); |
| | | 50 | tabbedPane.setSelectedComponent(bp); |
| | | 51 | if (filename != null) |
| | | 52 | output(Globals.lang("Imported database") + " '" + filename + "' " + |
| | | 53 | Globals.lang("with") + " " + |
| | | 54 | database.getEntryCount() + " " + |
| | | 55 | Globals.lang("entries into new database") + "."); |
| | | 56 | } |
| | | 57 | else { |
| | | 58 | // Import into current database. |
| | | 59 | boolean checkForDuplicates = true; |
| | | 60 | BasePanel basePanel = basePanel(); |
| | | 61 | BibtexDatabase database = basePanel.database; |
| | | 62 | int oldCount = database.getEntryCount(); |
| | | 63 | NamedCompound ce = new NamedCompound(Globals.lang("Import entries")); |
| | | 64 | |
| | | 65 | mainLoop: |
| | | 66 | for (BibtexEntry entry : bibentries){ |
| | | 67 | boolean dupli = false; |
| | | 68 | // Check for duplicates among the current entries: |
| | | 69 | if (checkForDuplicates) { |
| | | 70 | loop: for (Iterator<String> i2=database.getKeySet().iterator(); |
| | | 71 | i2.hasNext();) { |
| | | 72 | BibtexEntry existingEntry = database.getEntryById(i2.next()); |
| | | 73 | if (DuplicateCheck.isDuplicate(entry, existingEntry |
| | | 74 | )) { |
| | | 75 | DuplicateResolverDialog drd = new DuplicateResolverDialog |
| | | 76 | (JabRefFrame.this, existingEntry, entry, DuplicateResolverDialog.IMPORT_CHECK); |
| | | 77 | drd.setVisible(true); |
| | | 78 | int res = drd.getSelected(); |
| | | 79 | if (res == DuplicateResolverDialog.KEEP_LOWER) { |
| | | 80 | dupli = true; |
| | | 81 | } |
| | | 82 | else if (res == DuplicateResolverDialog.KEEP_UPPER) { |
| | | 83 | database.removeEntry(existingEntry.getId()); |
| | | 84 | ce.addEdit(new UndoableRemoveEntry |
| | | 85 | (database, existingEntry, basePanel)); |
| | | 86 | } else if (res == DuplicateResolverDialog.BREAK) { |
| | | 87 | break mainLoop; |
| | | 88 | } |
| | | 89 | break loop; |
| | | 90 | } |
| | | 91 | } |
| | | 92 | } |
| | | 93 | |
| | | 94 | if (!dupli) { |
| | | 95 | try { |
| | | 96 | entry.setId(Util.createNeutralId()); |
| | | 97 | database.insertEntry(entry); |
| | | 98 | ce.addEdit(new UndoableInsertEntry |
| | | 99 | (database, entry, basePanel)); |
| | | 100 | addedEntries++; |
| | | 101 | } |
| | | 102 | catch (KeyCollisionException ex) { |
| | | 103 | //ignore |
| | | 104 | System.err.println("KeyCollisionException [ addBibEntries(...) ]"); |
| | | 105 | } |
| | | 106 | } |
| | | 107 | } |
| | | 108 | if (addedEntries > 0) { |
| | | 109 | ce.end(); |
| | | 110 | basePanel.undoManager.addEdit(ce); |
| | | 111 | basePanel.markBaseChanged(); |
| | | 112 | if (filename != null) |
| | | 113 | output(Globals.lang("Imported database") + " '" + filename + "' " + |
| | | 114 | Globals.lang("with") + " " + |
| | | 115 | (database.getEntryCount() - oldCount) + " " + |
| | | 116 | Globals.lang("entries into new database") + "."); |
| | | 117 | } |
| | | 118 | |
| | | 119 | } |
| | | 120 | |
| | | 121 | return addedEntries; |
| | | 122 | } |