läuft
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.bayer.edam.migration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -31,9 +33,11 @@ import com.bayer.edam.migration.mam.Entry;
|
||||
public class MamMigrationSelective implements CommandLineRunner {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MamMigrationSelective.class);
|
||||
|
||||
|
||||
static String adamHost = "https://edam-q.bayer.com:2015";
|
||||
static int[] selection = { 71048, /* 70778, 70746 ,70142,70361,63924,71263,63201,56942,57641,70745,70440,70340,70288,71725 */ };
|
||||
static int[] selection = { 71948 }; // { 71048, 70778, 70746
|
||||
// ,70142,70361,63924,71263,63201,56942,57641,70745,70440,70340,70288,71725 */
|
||||
// };
|
||||
|
||||
@Value("${mam.auth}")
|
||||
private String mamAuth;
|
||||
@@ -41,67 +45,106 @@ public class MamMigrationSelective implements CommandLineRunner {
|
||||
@Value("${adam.auth}")
|
||||
private String adamAuth;
|
||||
|
||||
|
||||
RestTemplate restTemplate;
|
||||
HttpHeaders adamHeaders, mamHeaders;
|
||||
HttpEntity<String> adamHeadersEntity;
|
||||
|
||||
private static SimpleDateFormat isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
|
||||
private static SimpleDateFormat stringDate = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
@Override
|
||||
public void run(String... strings) throws Exception {
|
||||
|
||||
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
|
||||
interceptors.add(new LoggingRequestInterceptor());
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
interceptors.add(new LoggingRequestInterceptor());
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
|
||||
adamHeaders = createAdamHeaders();
|
||||
adamHeadersEntity = new HttpEntity<String>("", adamHeaders);
|
||||
mamHeaders = createMamHeaders();
|
||||
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<String>("", mamHeaders);
|
||||
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();
|
||||
|
||||
// log.info(entry.toFullString());
|
||||
|
||||
postToAdam(entry);
|
||||
}
|
||||
|
||||
invalidateAdamToken();
|
||||
}
|
||||
|
||||
void invalidateAdamToken() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(adamHost + "/auth", HttpMethod.DELETE, adamHeadersEntity, String.class);
|
||||
log.info("Logged out from adam API: " + response.toString());
|
||||
}
|
||||
|
||||
void postToAdam(Entry entry) {
|
||||
log.info("Migrating entry " + entry.id);
|
||||
|
||||
WriteRecord newRecord = new WriteRecord();
|
||||
newRecord.addClassification("Global");
|
||||
newRecord.addClassification("Bayer AH");
|
||||
newRecord.addClassification("CAP");
|
||||
newRecord.addClassification("MediaType.Other");
|
||||
newRecord.addClassification("Draft");
|
||||
newRecord.addClassification("Unrestricted");
|
||||
|
||||
newRecord.addField("byr_Title", "Happy Doggy!");
|
||||
newRecord.addField("byr_ah_projectLead", Collections.singletonList("21cfce6116484269a351a71300e1fed0"));
|
||||
newRecord.addField("byr_ah_licenseHolderList", Collections.singletonList("4b82f989fae54a309e17a7470073c36b"));
|
||||
|
||||
HttpEntity<WriteRecord> httpEntity = new HttpEntity<WriteRecord>(newRecord, adamHeaders);
|
||||
try {
|
||||
URI location = restTemplate.postForLocation(adamHost + "/records", httpEntity, WriteRecord.class);
|
||||
log.info("Location: " + location);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(adamHost + location, HttpMethod.DELETE, adamHeadersEntity, String.class);
|
||||
log.info("Deleted record: " + response.toString());
|
||||
}
|
||||
catch (HttpClientErrorException hcee) {
|
||||
log.error(hcee.getStatusCode().toString());
|
||||
log.error(hcee.getResponseBodyAsString());
|
||||
}
|
||||
WriteRecord record = new WriteRecord();
|
||||
|
||||
// globally applicable classifications
|
||||
record.addClassification("MediaAssistant-Migration");
|
||||
record.addClassification("Country.Global");
|
||||
record.addClassification("Agencies.Bayer AH");
|
||||
record.addClassification("Usage.Unrestricted");
|
||||
|
||||
// status
|
||||
if (entry.stateId == 6) { // ACCEPTED
|
||||
record.addClassification("Status.Released");
|
||||
} else if (entry.stateId == 10) { // ARCHIVED
|
||||
record.addClassification("Status.Archived");
|
||||
} else {
|
||||
log.error("Skipping entry " + entry.id + ": No target status for stateId == " + entry.stateId);
|
||||
return;
|
||||
}
|
||||
|
||||
// business unit
|
||||
record.addClassification("BusinessUnit.CAP");
|
||||
|
||||
// media type
|
||||
record.addClassification("MediaType.Other");
|
||||
|
||||
// Fields
|
||||
record.addField("byr_Title", entry.name.get("en"));
|
||||
record.addField("byr_ah_projectLead", Collections.singletonList(Definitions.defaultProjectLead));
|
||||
record.addField("byr_ah_licenseHolderList", Collections.singletonList(Definitions.defaultLicenseHolder));
|
||||
|
||||
// Description / Additional Information
|
||||
String description = entry.description.get("en");
|
||||
if (description == null)
|
||||
description = "";
|
||||
else
|
||||
description += "\n\n";
|
||||
|
||||
if (entry.comment.get("en") != null)
|
||||
description += "Comment: " + entry.comment.get("en") + "\n\n";
|
||||
|
||||
String migrationDescription = "Migrated from Media Assistant ID " + entry.id + ", Date of Issue: "
|
||||
+ stringDate.format(entry.creationDate);
|
||||
description += "[" + migrationDescription + "]";
|
||||
record.addField("byr_ah_description", description);
|
||||
|
||||
// Media Type
|
||||
// TODO
|
||||
|
||||
// Keywords
|
||||
List<String> keywords = Arrays.asList(entry.keyword.get("en").split("\\s*,\\s*"));
|
||||
record.addField("byr_ah_keywords_tl", keywords);
|
||||
|
||||
HttpEntity<WriteRecord> httpEntity = new HttpEntity<WriteRecord>(record, adamHeaders);
|
||||
try {
|
||||
URI location = restTemplate.postForLocation(adamHost + "/records", httpEntity, WriteRecord.class);
|
||||
log.info("Location: " + location);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(adamHost + location, HttpMethod.DELETE,
|
||||
adamHeadersEntity, String.class);
|
||||
log.info("Deleted record: " + response.toString());
|
||||
} catch (HttpClientErrorException hcee) {
|
||||
log.error(hcee.getStatusCode().toString());
|
||||
log.error(hcee.getResponseBodyAsString());
|
||||
}
|
||||
}
|
||||
|
||||
private HttpHeaders createMamHeaders() {
|
||||
@@ -110,7 +153,7 @@ public class MamMigrationSelective implements CommandLineRunner {
|
||||
headers.add("Authorization", "Basic " + mamAuth);
|
||||
headers.add("Content-Type", "application/json");
|
||||
headers.add("From", "migration@bayer.com");
|
||||
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
@@ -122,19 +165,36 @@ public class MamMigrationSelective implements CommandLineRunner {
|
||||
headers.add("Content-Type", "application/json");
|
||||
headers.add("Authorization", "Basic " + adamAuth);
|
||||
HttpEntity<String> entity = new HttpEntity<String>("", headers);
|
||||
Token token = restTemplate.exchange("https://edam-q.bayer.com:2015/auth", HttpMethod.GET, entity, Token.class)
|
||||
.getBody();
|
||||
Token token = restTemplate.exchange(adamHost + "/auth", HttpMethod.GET, entity, Token.class).getBody();
|
||||
|
||||
// replace auth header
|
||||
headers.remove("Authorization");
|
||||
headers.add("Authorization", "Token " + token.getToken());
|
||||
|
||||
|
||||
// replace accept-charset header (in order to declutter log)
|
||||
headers.add("Accept-Charset", "us-ascii, utf-16, utf-8");
|
||||
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
private void invalidateAdamToken() {
|
||||
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");
|
||||
*
|
||||
* 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[]) {
|
||||
SpringApplication.run(MamMigrationSelective.class, args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user