This commit is contained in:
GEZVF
2017-09-15 17:17:19 +02:00
parent c5bf394af9
commit 6273c15f17
7 changed files with 272 additions and 88 deletions
+8
View File
@@ -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?
@@ -13,31 +13,57 @@ public class Definitions {
public static final Map<String, Classification> classifications;
public static final Map<String, String> 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<String, Classification>();
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<String, String>();
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<String> 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;
@@ -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;
@@ -33,7 +35,9 @@ 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,11 +45,13 @@ 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 {
@@ -60,45 +66,82 @@ public class MamMigrationSelective implements CommandLineRunner {
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) {
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;
}
void postToAdam(Entry entry) {
log.info("Migrating entry " + entry.id);
// business unit
record.addClassification("BusinessUnit.CAP");
WriteRecord newRecord = new WriteRecord();
newRecord.addClassification("Global");
newRecord.addClassification("Bayer AH");
newRecord.addClassification("CAP");
newRecord.addClassification("MediaType.Other");
newRecord.addClassification("Draft");
newRecord.addClassification("Unrestricted");
// media type
record.addClassification("MediaType.Other");
newRecord.addField("byr_Title", "Happy Doggy!");
newRecord.addField("byr_ah_projectLead", Collections.singletonList("21cfce6116484269a351a71300e1fed0"));
newRecord.addField("byr_ah_licenseHolderList", Collections.singletonList("4b82f989fae54a309e17a7470073c36b"));
// 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));
HttpEntity<WriteRecord> httpEntity = new HttpEntity<WriteRecord>(newRecord, adamHeaders);
// 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);
ResponseEntity<String> response = restTemplate.exchange(adamHost + location, HttpMethod.DELETE,
adamHeadersEntity, String.class);
log.info("Deleted record: " + response.toString());
}
catch (HttpClientErrorException hcee) {
} catch (HttpClientErrorException hcee) {
log.error(hcee.getStatusCode().toString());
log.error(hcee.getResponseBodyAsString());
}
@@ -122,8 +165,7 @@ 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");
@@ -135,6 +177,24 @@ public class MamMigrationSelective implements CommandLineRunner {
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);
}
@@ -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<String> headersEntity = new HttpEntity<String>("", 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<String>("", headers);
ResponseEntity<String> 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<String> 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);
}
}
@@ -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<String> values = new ArrayList<String>();
@@ -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<String, String> name;
@@ -52,40 +63,6 @@ public class Entry {
}
}
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<String, String> 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<String, String> description;
// public String fileName;
// public String fileNameOnDisc;
//// public String duration;
//
// public Download() {
// }
// }
public Long id;
public String href;
public Long fileSize;
@@ -114,7 +91,7 @@ public class Entry {
public Integer stateId;
public Integer contentSuperTypeId;
public List<Download> downloadList;
public List<FileFolder> downloadListFileFolder;
public List<Download> 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();
}
}
@@ -0,0 +1,5 @@
package com.bayer.edam.migration.mam;
public class User {
}