This commit is contained in:
gexce
2017-09-19 17:08:25 +02:00
parent 70bd8f3443
commit 9d8699004a
6 changed files with 153 additions and 103 deletions
@@ -1,6 +1,8 @@
package com.bayer.edam.migration;
import java.io.File;
import java.net.URI;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
@@ -9,6 +11,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -29,15 +32,23 @@ import org.springframework.web.client.RestTemplate;
import com.bayer.edam.migration.adam.Token;
import com.bayer.edam.migration.adam.WriteRecord;
import com.bayer.edam.migration.mam.Entry;
import com.fasterxml.jackson.databind.ObjectMapper;
@SpringBootApplication
@PropertySource("file:secret.properties")
public class MamMigrationSelective implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(MamMigrationSelective.class);
static final String adamHost = "https://edam-q.bayer.com:2015";
static final String mamHost = "https://service.media-assistant.animalhealth.bayer.com";
static final int[] selection = { 71948, /*71048, 70778, 70746, 70142,*/ 70361, /*63924, 71263, 63201, 56942, 57641, 70745, 70440, 70340,
70288, 71725*/ };
static String adamHost = "https://edam-q.bayer.com:2015";
static int[] selection = { 71948, 71048,70778/*,70746,70142,70361,63924,71263,63201,56942,57641,70745,70440,70340,70288,71725*/};
static final File entryLog = new File("C:\\Temp\\Migration AH\\entry.log");
static final File problemLog = new File("C:\\Temp\\Migration AH\\problem.log");
static final String fieldSeparator = "|~|";
static final String lineSeparator = "\r\n";
@Value("${mam.auth}")
private String mamAuth;
@@ -51,12 +62,18 @@ public class MamMigrationSelective implements CommandLineRunner {
private static SimpleDateFormat isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
private static SimpleDateFormat stringDate = new SimpleDateFormat("yyyy-MM-dd");
private static ObjectMapper jsonMapper = new ObjectMapper();
private List<String> locations = new ArrayList<String>();
@Override
public void run(String... strings) throws Exception {
log.info("Write log headers");
FileUtils.writeStringToFile(entryLog, "Location" + fieldSeparator + "Entry" + lineSeparator, Charset.forName("utf-8"));
FileUtils.writeStringToFile(problemLog, "entryId" + fieldSeparator + "Entry" + lineSeparator, Charset.forName("utf-8"));
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
@@ -70,28 +87,24 @@ public class MamMigrationSelective implements CommandLineRunner {
for (int id : selection) {
log.info("GET entry " + id);
Entry entry = restTemplate
.exchange("https://service.media-assistant.animalhealth.bayer.com/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();
// log.info(entry.toFullString());
postToAdam(entry);
}
// delete all created records
// // delete all created records
// for (String location : locations) {
// ResponseEntity<String> response = restTemplate.exchange(adamHost + location, HttpMethod.DELETE,
// adamHeadersEntity, String.class);
// ResponseEntity<String> response = restTemplate.exchange(adamHost + location, HttpMethod.DELETE, adamHeadersEntity,
// String.class);
// log.info("Deleted " + location + ": " + response.toString());
// }
invalidateAdamToken();
}
void postToAdam(Entry entry) {
void postToAdam(Entry entry) throws Exception {
WriteRecord record = new WriteRecord();
String entryJson = jsonMapper.writeValueAsString(entry);
// globally applicable classifications
record.addClassification("MediaAssistant-Migration");
@@ -106,6 +119,7 @@ public class MamMigrationSelective implements CommandLineRunner {
record.addClassification("Status.Archived");
} else {
log.error("Skipping entry " + entry.id + ": No target status for stateId == " + entry.stateId);
FileUtils.writeStringToFile(problemLog, entry.id + fieldSeparator + entryJson + lineSeparator, Charset.forName("utf-8"), true);
return;
}
@@ -113,13 +127,15 @@ public class MamMigrationSelective implements CommandLineRunner {
record.addClassification("BusinessUnit.CAP");
// media type
// TODO
record.addClassification("MediaType.Other");
// Fields
record.addField("byr_Title", entry.name.get("en"));
// TODO
record.addField("byr_ah_projectLead", Collections.singletonList(Definitions.defaultProjectLead));
record.addField("byr_ah_licenseHolderList", Collections.singletonList(Definitions.defaultLicenseHolder));
record.addField("byr_ah_licenseAdditionalPermittedUse", Collections.singletonList(Definitions.defaultAdditionalPermittedUse));
record.addField("byr_ah_brandCommunicationRepresentative", Collections.singletonList(Definitions.defaultProjectLead));
// Description / Additional Information
String description = entry.description.get("en");
@@ -136,25 +152,39 @@ public class MamMigrationSelective implements CommandLineRunner {
description += "[" + migrationDescription + "]";
record.addField("byr_ah_description", description);
// Media Type
// TODO
// Keywords
if (entry.keyword.get("en") != null && entry.keyword.get("en").length() > 0) {
Set<String> keywords = new HashSet<String>(Arrays.asList(entry.keyword.get("en").split("\\s*,\\s*")));
record.addField("byr_ah_keywords_tl", new ArrayList<String>(keywords));
}
// License Information
record.addField("byr_ah_licenseHolderList", Collections.singletonList(Definitions.defaultLicenseHolder));
record.addField("byr_ah_licenseAdditionalPermittedUse", Collections.singletonList(Definitions.defaultAdditionalPermittedUse));
String licenseDescription = "";
if (entry.copyright.use != null)
licenseDescription += entry.copyright.use + "\n";
licenseDescription += "Released for internet: " + entry.release.get("forInternet") + "\nReleased for intranet: " + entry.release.get("forIntranet");
record.addField("byr_ah_licenseComments", licenseDescription);
if (entry.gpc != null)
record.addField("byr_ah_approvalNumber",entry.gpc.gpcNumber); // TODO
HttpEntity<WriteRecord> httpEntity = new HttpEntity<WriteRecord>(record, adamHeaders);
try {
URI location = restTemplate.postForLocation(adamHost + "/records", httpEntity, WriteRecord.class);
log.info("Created " + location);
FileUtils.writeStringToFile(entryLog, location.toString() + fieldSeparator + entryJson + lineSeparator,
Charset.forName("utf-8"), true);
locations.add(location.toString());
} catch (HttpStatusCodeException hsce) {
log.error(hsce.getStatusCode().toString());
log.error("Skipping entry " + entry.id + ": Error while creating record " + hsce.getStatusCode().toString());
log.error(hsce.getResponseBodyAsString());
FileUtils.writeStringToFile(problemLog, entry.id + fieldSeparator + entryJson + lineSeparator, Charset.forName("utf-8"), true);
}
}
@@ -175,7 +205,8 @@ public class MamMigrationSelective implements CommandLineRunner {
headers.add("Accept", "application/hal+json");
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Basic " + adamAuth);
headers.add("set-immediateSearchIndexUpdate", "true"); // see https://edam.bayer.com:2015/docs/resources/record#example-records-direct-push
headers.add("set-immediateSearchIndexUpdate", "true"); // see
// https://edam.bayer.com:2015/docs/resources/record#example-records-direct-push
HttpEntity<String> entity = new HttpEntity<String>("", headers);
Token token = restTemplate.exchange(adamHost + "/auth", HttpMethod.GET, entity, Token.class).getBody();
@@ -190,21 +221,16 @@ public class MamMigrationSelective implements CommandLineRunner {
}
private void invalidateAdamToken() {
ResponseEntity<String> response = restTemplate.exchange(adamHost + "/auth", HttpMethod.DELETE,
adamHeadersEntity, String.class);
ResponseEntity<String> response = restTemplate.exchange(adamHost + "/auth", HttpMethod.DELETE, adamHeadersEntity, String.class);
log.info("Logged out from adam API: " + response.toString());
}
/*
* // Quoted "Z" to indicate UTC, no timezone offset private static
* SimpleDateFormat outputDateFormat = new
* SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); private static SimpleDateFormat
* inputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
* // Quoted "Z" to indicate UTC, no timezone offset private static SimpleDateFormat outputDateFormat = new
* SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); private static SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
*
* public static String toIso8601(String isoDate) { try { return
* outputDateFormat.format(inputDateFormat.parse(isoDate)); } catch
* (ParseException e) { log.error(e.toString()); e.printStackTrace(); } return
* null; }
* public static String toIso8601(String isoDate) { try { return outputDateFormat.format(inputDateFormat.parse(isoDate)); } catch
* (ParseException e) { log.error(e.toString()); e.printStackTrace(); } return null; }
*/
public static void main(String args[]) {