From 6273c15f1726ca3031ae3c4f712b65ff78fa7bf2 Mon Sep 17 00:00:00 2001 From: GEZVF Date: Fri, 15 Sep 2017 17:17:19 +0200 Subject: [PATCH] =?UTF-8?q?l=C3=A4uft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AH MAM Migration/questions.txt | 8 + .../com/bayer/edam/migration/Definitions.java | 40 ++++- .../edam/migration/MamMigrationSelective.java | 144 +++++++++++++----- .../com/bayer/edam/migration/ShowRecord.java | 79 ++++++++++ .../edam/migration/adam/WriteRecord.java | 2 +- .../com/bayer/edam/migration/mam/Entry.java | 82 +++++----- .../com/bayer/edam/migration/mam/User.java | 5 + 7 files changed, 272 insertions(+), 88 deletions(-) create mode 100644 AH MAM Migration/questions.txt create mode 100644 AH MAM Migration/src/com/bayer/edam/migration/ShowRecord.java create mode 100644 AH MAM Migration/src/com/bayer/edam/migration/mam/User.java diff --git a/AH MAM Migration/questions.txt b/AH MAM Migration/questions.txt new file mode 100644 index 0000000..6a54486 --- /dev/null +++ b/AH MAM Migration/questions.txt @@ -0,0 +1,8 @@ +- English only? +- CreationDate ist read-only +- Mapping Media Type +- Wer soll als byr_ah_projectLead gesetzt werden? +- "Bayer Animal Health GmbH" immer als License Holder OK? +- "comment" mit in description? +- MAM-Status "ARCHIVED" --> eDAM.Archived? +- Was ist mit Status != ACCEPTED or ARCHIVED? \ No newline at end of file diff --git a/AH MAM Migration/src/com/bayer/edam/migration/Definitions.java b/AH MAM Migration/src/com/bayer/edam/migration/Definitions.java index cf29f3e..51372f9 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/Definitions.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/Definitions.java @@ -13,31 +13,57 @@ public class Definitions { public static final Map classifications; public static final Map fieldIDs; + public static final String defaultLanguage = "c2bd4f9bbb954bcb80c31e924c9c26dc"; //"00000000000000000000000000000000"; + public static final String defaultProjectLead = "21cfce6116484269a351a71300e1fed0"; + public static final String defaultLicenseHolder = "4b82f989fae54a309e17a7470073c36b"; // Bayer Animal Health GmbH + static { classifications = new HashMap(); - classifications.put("Global", new Classification("dff7666b-326b-4e1c-912c-a7b4006c8500", 1)); - classifications.put("Bayer AH", new Classification("8de5ef0e-3c6d-451c-b82b-a7b4006c954c", 1)); - classifications.put("CAP", new Classification("306e050a-dd58-46ad-93a7-a73100a2547f", 1)); - classifications.put("MediaType.Other", new Classification("7c13e620-7eb0-4c12-9dfb-a7b4008f19b1", 1)); - classifications.put("Draft", new Classification("dfee7cb7-fffc-41cb-9ee9-a68a00d4f0c3", 1)); - classifications.put("Unrestricted", new Classification("6b95a635-40e9-4782-ad5f-a73100a2bb94", 1)); + classifications.put("MediaAssistant-Migration", new Classification("2bd24882-b7d8-4d30-8e6f-a7ef00eb33e7", 1)); + + classifications.put("Country.Global", new Classification("dff7666b-326b-4e1c-912c-a7b4006c8500", 1)); // /Definitions/Animal Health/Country + classifications.put("Agencies.Bayer AH", new Classification("8de5ef0e-3c6d-451c-b82b-a7b4006c954c", 1)); // /Definitions/Agencies/Bayer + classifications.put("Usage.Unrestricted", new Classification("6b95a635-40e9-4782-ad5f-a73100a2bb94", 1)); // Alles global/unrestricted? + + // /Bayer/Animal Health/BusinessUnit + classifications.put("BusinessUnit.CAP", new Classification("306e050a-dd58-46ad-93a7-a73100a2547f", 1)); + classifications.put("BusinessUnit.FAP", new Classification("9e1dd62a-dd57-4b57-86d4-a73100a25523", 1)); + classifications.put("BusinessUnit.General", new Classification("2e5bc260-ec2b-40c7-bcaa-a73100a255b9", 1)); + + // /Definitions/Common/Status --> Released oder Archived + classifications.put("Status.Draft", new Classification("dfee7cb7fffc41cb9ee9a68a00d4f0c3", 1)); + classifications.put("Status.Released", new Classification("279fce5751584837bd3aa68a00d4f204", 1)); + classifications.put("Status.Archived", new Classification("4306204af75442fda297a7b4006b4a87", 1)); + + classifications.put("MediaType.Other", new Classification("7c13e620-7eb0-4c12-9dfb-a7b4008f19b1", 1)); // Mapping von Martin fieldIDs = new HashMap(); fieldIDs.put("byr_Title", "31f337fa-add6-4e25-a672-a68a00d4ff3b"); + fieldIDs.put("byr_ah_description", "f9e40f87-493e-4f17-b129-a6dd00d063b2"); // Additional Information fieldIDs.put("byr_ah_projectLead", "5400d446-9d95-4158-ac06-a6dd00d06a12"); fieldIDs.put("byr_ah_licenseHolderList", "de3f83cb50e747228bfea731010bc27c"); + fieldIDs.put("byr_creationDate", "403fea1c-c154-4e3b-a941-a68a00d4fcc3"); + fieldIDs.put("byr_ah_keywords_tl", "6d582cfaa5b94101ab1fa6fd007e2fcd"); } public static Field createSingleValueField(String name, String value) { String id = fieldIDs.get(name); + + if (id == null) + throw new NullPointerException("Could not find ID for field " + name); + Field newField = new Field(id); - newField.localizedValues.add(new LocalizedValue("Happy Doggy")); + newField.localizedValues.add(new LocalizedValue(value)); return newField; } public static Field createMultiValueField(String name, List values) { String id = fieldIDs.get(name); + + if (id == null) + throw new NullPointerException("Could not find ID for field " + name); + Field newField = new Field(id); newField.localizedValues.add(new LocalizedValue(values)); return newField; diff --git a/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java b/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java index b1d6356..2083d31 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java @@ -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 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 interceptors = new ArrayList(); - interceptors.add(new LoggingRequestInterceptor()); - restTemplate.setInterceptors(interceptors); + interceptors.add(new LoggingRequestInterceptor()); + restTemplate.setInterceptors(interceptors); adamHeaders = createAdamHeaders(); adamHeadersEntity = new HttpEntity("", adamHeaders); mamHeaders = createMamHeaders(); - + HttpEntity entity = new HttpEntity("", 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 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 httpEntity = new HttpEntity(newRecord, adamHeaders); - try { - URI location = restTemplate.postForLocation(adamHost + "/records", httpEntity, WriteRecord.class); - log.info("Location: " + location); - - ResponseEntity 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 keywords = Arrays.asList(entry.keyword.get("en").split("\\s*,\\s*")); + record.addField("byr_ah_keywords_tl", keywords); + + HttpEntity httpEntity = new HttpEntity(record, adamHeaders); + try { + URI location = restTemplate.postForLocation(adamHost + "/records", httpEntity, WriteRecord.class); + log.info("Location: " + location); + + ResponseEntity 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 entity = new HttpEntity("", 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 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); } diff --git a/AH MAM Migration/src/com/bayer/edam/migration/ShowRecord.java b/AH MAM Migration/src/com/bayer/edam/migration/ShowRecord.java new file mode 100644 index 0000000..4e7ecac --- /dev/null +++ b/AH MAM Migration/src/com/bayer/edam/migration/ShowRecord.java @@ -0,0 +1,79 @@ +package com.bayer.edam.migration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.PropertySource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import com.bayer.edam.migration.adam.Token; + +//@SpringBootApplication +@PropertySource("file:secret.properties") +public class ShowRecord implements CommandLineRunner { + + private static final Logger log = LoggerFactory.getLogger(MamMigrationSelective.class); + + static String adamHost = "https://edam-q.bayer.com:2015"; + + @Value("${adam.auth}") + private String adamAuth; + + RestTemplate restTemplate; + + @Override + public void run(String... strings) throws Exception { + restTemplate = new RestTemplate(); + + HttpHeaders headers = new HttpHeaders(); + headers.add("Registration", "ADAMBAYER"); + headers.add("API-VERSION", "1"); + headers.add("Accept", "application/hal+json"); + headers.add("Content-Type", "application/json"); + headers.add("Authorization", "Basic " + adamAuth); + HttpEntity headersEntity = new HttpEntity("", headers); + Token token = restTemplate.exchange(adamHost + "/auth", HttpMethod.GET, headersEntity, 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"); + + headersEntity = new HttpEntity("", headers); + + + + ResponseEntity r1 = restTemplate.exchange( + "https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720", HttpMethod.GET, + headersEntity, String.class); + log.info(r1.getBody()); + r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/fields", + HttpMethod.GET, headersEntity, String.class); + log.info(r1.getBody()); + r1 = restTemplate.exchange( + "https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/classifications", + HttpMethod.GET, headersEntity, String.class); + log.info(r1.getBody()); + + + // logout + ResponseEntity response = restTemplate.exchange(adamHost + "/auth", HttpMethod.DELETE, headersEntity, + String.class); + log.info("Logged out from adam API: " + response.toString()); + + } + + public static void main(String args[]) { + SpringApplication.run(ShowRecord.class, args); + } + +} diff --git a/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java b/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java index d5ff836..5c2b00d 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java @@ -22,7 +22,7 @@ public class WriteRecord { } public static class LocalizedValue { - public String languageId = "00000000000000000000000000000000"; + public String languageId = Definitions.defaultLanguage; public String value; public List values = new ArrayList(); diff --git a/AH MAM Migration/src/com/bayer/edam/migration/mam/Entry.java b/AH MAM Migration/src/com/bayer/edam/migration/mam/Entry.java index dbd0580..1529982 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/mam/Entry.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/mam/Entry.java @@ -1,11 +1,22 @@ package com.bayer.edam.migration.mam; +import java.lang.reflect.Field; import java.util.Date; import java.util.List; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonCreator; + +/** + * Representation of /rest/migration/entry + */ public class Entry { + private static final Logger log = LoggerFactory.getLogger(Entry.class); + public static class Type { public Long id; public Map name; @@ -31,7 +42,7 @@ public class Entry { public Boolean usageVideo; public Boolean regionalLicense; public Boolean personalLicense; - + public Copyright() { } } @@ -47,44 +58,10 @@ public class Entry { public Map description; public String fileName; public String fileNameOnDisc; - + public Download() { } } - - public static class FileFolder { - public Long id; - public String href; - public String mimeType; - public Integer contentSuperTypeId; - public Long size; - public String entryFileVariation; - public String watermarkVariation; - public Map description; - public String fileName; - public String fileNameOnDisc; - - public FileFolder() { - } - } - - -// public class Download { -// public Long id; -// public String href; -// public String mimeType; -// public Integer contentSuperTypeId; -// public Long size; -// public String entryFileVariation; -// public String watermarkVariation; -// public Map description; -// public String fileName; -// public String fileNameOnDisc; -//// public String duration; -// -// public Download() { -// } -// } public Long id; public String href; @@ -107,14 +84,14 @@ public class Entry { public String master; public Map project; public Type imageType; -// public String gpc; + // public String gpc; public String mediaAssistantId; public Map metaData; public List categories; public Integer stateId; public Integer contentSuperTypeId; public List downloadList; - public List downloadListFileFolder; + public List downloadListPublicFolder; public Entry() { } @@ -125,4 +102,33 @@ public class Entry { + user.get("id") + ", copyright.organization.id=" + copyright.organization.get("id") + '}'; } + public String toFullString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + // determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + // print field names paired with their values + for (Field field : fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + // requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + log.error(ex.toString()); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } + } diff --git a/AH MAM Migration/src/com/bayer/edam/migration/mam/User.java b/AH MAM Migration/src/com/bayer/edam/migration/mam/User.java new file mode 100644 index 0000000..85c5b85 --- /dev/null +++ b/AH MAM Migration/src/com/bayer/edam/migration/mam/User.java @@ -0,0 +1,5 @@ +package com.bayer.edam.migration.mam; + +public class User { + +}