EntryDownloader with files

This commit is contained in:
gexce
2017-09-22 16:22:04 +02:00
parent d04c957680
commit cee435f981
5 changed files with 158 additions and 55 deletions
+7 -2
View File
@@ -41,12 +41,17 @@
- Englisch OK - Englisch OK
- Additional Info in einzelnen Zeilen [done] - Additional Info in einzelnen Zeilen [done]
- Comment und Description [done] - Comment und Description [done]
- Status: Liste mit Status != ACCEPTED an Claudia/Heike - Status: Liste mit Status != ACCEPTED an Claudia/Heike [done]
Archiv ist nur andere Kategorie Archiv ist nur andere Kategorie
- Agency: Print Supplier (wenn gesetzt, sonst Bayer AH) --> Martin klärt Prozess - Agency: Print Supplier (wenn gesetzt, sonst Bayer AH) --> Martin/Tilman klären Prozess
- Seite 2 als Preview - Seite 2 als Preview
- Filename Preview nach Feld fileName setzen - Filename Preview nach Feld fileName setzen
----------------------------------------------------- -----------------------------------------------------
Status „Archived“ kann gelöscht werden bzw. muss nicht übernommen werden.
Heike prüft nochmal den Status „angelegt“ und gibt dann Rückmeldung.
Status „gesperrt“ kann gelöscht werden bzw. muss nicht übernommen werden.
Status „Zur Indexierung zugewiesen“ kann gelöscht bzw. muss nicht übernommen werden.
Status „Zum Upload zugewiesen“ bitte behalten.
@@ -1,4 +1,4 @@
# TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF # TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
logging.level.com.bayer=DEBUG logging.level.com.bayer=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level- %msg%n logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level- %msg%n
logging.file=migration.log logging.file=entryDownload.log
@@ -1,5 +1,13 @@
package com.bayer.edam.migration; package com.bayer.edam.migration;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@@ -17,7 +25,7 @@ import com.bayer.edam.migration.mam.Entry.Download;
import com.bayer.edam.migration.mam.Entrylist; import com.bayer.edam.migration.mam.Entrylist;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
//@SpringBootApplication @SpringBootApplication
@PropertySource("file:secret.properties") @PropertySource("file:secret.properties")
public class EntryDownloader implements CommandLineRunner { public class EntryDownloader implements CommandLineRunner {
@@ -37,7 +45,7 @@ public class EntryDownloader implements CommandLineRunner {
@Override @Override
public void run(String... strings) throws Exception { public void run(String... strings) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json"); headers.add("Accept", "application/json");
headers.add("Authorization", "Basic " + mamAuth); headers.add("Authorization", "Basic " + mamAuth);
@@ -51,18 +59,69 @@ public class EntryDownloader implements CommandLineRunner {
lastPage = entrylist.lastPage; lastPage = entrylist.lastPage;
for (int i = 0; i < entrylist.entries.length; i++) { for (int i = 0; i < entrylist.entries.length; i++) {
try {
String entryJson = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, String.class, entrylist.entries[i].mediaAssistantId).getBody(); String entryJson = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, String.class, entrylist.entries[i].mediaAssistantId).getBody();
Entry entry = jsonMapper.readValue(entryJson, Entry.class); Entry entry = jsonMapper.readValue(entryJson, Entry.class);
long aggregatedFilesize = 0; List<Download> downloadSelection = new ArrayList<Download>();
// Select download: Download original only. If original is not available, download largest file
Download selectedDownload = null;
for (Download download : entry.downloadList) { for (Download download : entry.downloadList) {
aggregatedFilesize += download.size; if (download.entryFileVariation.equals("ORIGINAL")) {
selectedDownload = download;
break;
} }
if (selectedDownload == null) {
selectedDownload = download;
}
else if (selectedDownload.size < download.size) {
selectedDownload = download;
}
}
if (selectedDownload != null)
downloadSelection.add(selectedDownload);
// TODO extract, if zip --> postponed
for (Download download : entry.downloadListPublicFolder) { for (Download download : entry.downloadListPublicFolder) {
downloadSelection.add(download);
}
// clean download selection
HashSet<String> hrefs = new HashSet<String>();
List<Download> cleanedDownloads = new ArrayList<Download>();
for (Download download : downloadSelection) {
if (hrefs.add(download.href)) {
cleanedDownloads.add(download);
}
}
long aggregatedFilesize = 0;
for (Download download : cleanedDownloads) {
aggregatedFilesize += download.size; aggregatedFilesize += download.size;
} }
log.info(entry.id + separator + entry.mediaAssistantId + separator + entry.stateId + separator + aggregatedFilesize + separator + entryJson); log.info(entry.id + separator + entry.mediaAssistantId + separator + entry.stateId + separator + aggregatedFilesize + separator + entryJson);
new File("E:\\" + entry.id).mkdir();
for (Download download : cleanedDownloads) {
String filenameToUse;
if (download.fileNameOnDisc != null)
filenameToUse = download.fileNameOnDisc;
else
filenameToUse = download.fileName;
FileUtils.copyURLToFile(new URL(download.href), new File("E:\\" + entry.id + "\\" + filenameToUse), 4000, 240000);
}
}
catch (IOException ioe) {
log.error(ioe.toString());
ioe.printStackTrace();
}
} }
} }
} }
@@ -2,7 +2,9 @@ package com.bayer.edam.migration;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator; import org.apache.commons.io.LineIterator;
@@ -10,6 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.bayer.edam.migration.mam.Entry; import com.bayer.edam.migration.mam.Entry;
import com.bayer.edam.migration.mam.Entry.Download;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
public class EntryProcessor { public class EntryProcessor {
@@ -30,62 +33,98 @@ public class EntryProcessor {
// skip header // skip header
iter.next(); iter.next();
HashSet<String> orgSet = new HashSet<String>();
int count = 0; int count = 0;
long aggregatedFilesize = 0;
while (iter.hasNext()) { while (iter.hasNext()) {
String line = iter.next(); String line = iter.next();
line = line.substring(line.lastIndexOf(fieldSeparator) + fieldSeparator.length()); line = line.substring(line.lastIndexOf(fieldSeparator) + fieldSeparator.length());
Entry entry = jsonMapper.readValue(line, Entry.class); Entry entry = jsonMapper.readValue(line, Entry.class);
if (entry.copyright == null || entry.copyright.organization == null || !entry.copyright.organization.get("id").equals("3740")) { List<Download> downloadSelection = new ArrayList<Download>();
Download selectedDownload = null;
String orgId = null; for (Download download : entry.downloadList) {
if (download.entryFileVariation.equals("ORIGINAL")) {
if (entry.copyright != null && entry.copyright.organization != null) { selectedDownload = download;
orgId = entry.copyright.organization.get("id"); break;
orgSet.add(orgId);
} }
if (selectedDownload == null) {
selectedDownload = download;
}
else if (selectedDownload.size < download.size) {
selectedDownload = download;
}
}
log.warn("Entry " + entry.id + ": License Holder is not set to 'Bayer Animal Health GmbH' " + orgId + ", status: " + entry.stateId); if (selectedDownload != null)
downloadSelection.add(selectedDownload);
for (Download download : entry.downloadListPublicFolder) {
downloadSelection.add(download);
}
HashSet<String> hrefs = new HashSet<String>();
List<Download> cleanedDownloads = new ArrayList<Download>();
for (Download download : downloadSelection) {
if (hrefs.add(download.href)) {
cleanedDownloads.add(download);
}
}
HashSet<String> filenames = new HashSet<String>();
for (Download download : cleanedDownloads) {
String filenameToUse;
if (download.fileNameOnDisc != null)
filenameToUse = download.fileNameOnDisc;
else
filenameToUse = download.fileName;
if (!filenames.add(filenameToUse)) {
log.error(entry.id + ": " + filenameToUse);
System.exit(1);
}
aggregatedFilesize += download.size;
} }
// for (Download download : entry.downloadListPublicFolder) {
//
// }
// boolean originalFound = false; /*
// long largest = 0; HashSet<String> filenames = new HashSet<String>();
// long originalSize = 0; HashSet<String> hrefs = new HashSet<String>();
// for (Download download : entry.downloadList) { for (Download download : downloadSelection) {
// if (largest < download.size) String filenameToUse;
// largest = download.size; if (download.fileNameOnDisc != null)
// filenameToUse = download.fileNameOnDisc;
// if (download.entryFileVariation.equals("ORIGINAL")) { else
// originalFound = true; filenameToUse = download.fileName;
// originalSize = download.size;
// }
// }
//
// if (!originalFound) {
// log.info(entry.id + " - no original - " + entry.stateId);
// count++;
// }
// else if (largest > originalSize) {
// log.info(entry.id + " - original is not largest file - " + entry.stateId);
// count++;
// }
if (filenameToUse == null) {
log.error("ARGH " + entry.id);
System.exit(1);
} }
for (String orgId : orgSet) { if (filenames.add(filenameToUse)) {
log.info(orgId); aggregatedFilesize += download.size;
}
hrefs.add(download.href);
}
if (filenames.size() < hrefs.size()) {
log.error("Problem with " + entry.id + " filenames.size=" + filenames.size() + ", hrefs.size=" + hrefs.size());
for (String s : filenames)
log.error(s);
for (String s : hrefs)
log.error(s);
}
*/
} }
log.info("Count: " + count); log.info("Count: " + count);
log.info("aggregatedFilesize: " + aggregatedFilesize);
} }
@@ -88,7 +88,7 @@ public class MamMigrationSelective implements CommandLineRunner {
Entry entry = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, Entry.class, id).getBody(); Entry entry = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, Entry.class, id).getBody();
postToAdam(entry); // postToAdam(entry);
} }
// // delete all created records // // delete all created records