From fae7943dc6e65ea3c5f798c0d3244caae2c28ada Mon Sep 17 00:00:00 2001 From: gexce Date: Wed, 25 Oct 2017 11:17:39 +0200 Subject: [PATCH] creating campaigns including campaign years --- .../edam/migration/MamMigrationSelective.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) 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 23e53c4..7847386 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java @@ -73,7 +73,7 @@ public class MamMigrationSelective implements CommandLineRunner { HttpHeaders adamHeaders, mamHeaders; HttpEntity adamHeadersEntity; - private SimpleDateFormat isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); +// private SimpleDateFormat isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); private SimpleDateFormat stringDate = new SimpleDateFormat("yyyy-MM-dd"); private ObjectMapper jsonMapper = new ObjectMapper(); @@ -103,17 +103,15 @@ public class MamMigrationSelective implements CommandLineRunner { mamHeaders = createMamHeaders(); log.info("Read campaigns"); - HttpEntity httpEntity = new HttpEntity("parameters", adamHeaders); - campaignYears = restTemplate.exchange(adamHost + "/classification/" + Definitions.campaignRoot + "/children", HttpMethod.GET, httpEntity, ClassificationList.class).getBody().items; + campaignYears = restTemplate.exchange(adamHost + "/classification/" + Definitions.campaignRoot + "/children", HttpMethod.GET, adamHeadersEntity, ClassificationList.class).getBody().items; for (Classification yearClassification : campaignYears) { - List campaignClassifications = restTemplate.exchange(adamHost + "/classification/" + yearClassification.id + "/children", HttpMethod.GET, httpEntity, ClassificationList.class).getBody().items; + List campaignClassifications = restTemplate.exchange(adamHost + "/classification/" + yearClassification.id + "/children", HttpMethod.GET, adamHeadersEntity, ClassificationList.class).getBody().items; for (Classification campaignClassification : campaignClassifications) { log.debug(yearClassification.name + ": " + campaignClassification.name + ", " + campaignClassification.labels[0].value + ", " + campaignClassification.id); Campaign campaign = new Campaign(campaignClassification.name, campaignClassification.labels[0].value, yearClassification.name); campaign.guid = campaignClassification.id; campaigns.add(campaign); } - } HttpEntity entity = new HttpEntity("", mamHeaders); @@ -220,18 +218,30 @@ public class MamMigrationSelective implements CommandLineRunner { UploadClassification newYear = new UploadClassification(campaignYear, campaignYear); + HttpEntity httpEntity = new HttpEntity(newYear, adamHeaders); + try { + EntityId classificationId = restTemplate.exchange(adamHost + "/classifications", HttpMethod.POST, httpEntity, EntityId.class).getBody(); + newYear.guid = classificationId.id; + + log.info("Created campaign year: " + campaignYear); + + // retrieve the newly created classification back + yearClassification = restTemplate.exchange(adamHost + "/classification/" + newYear.guid, HttpMethod.GET, adamHeadersEntity, Classification.class).getBody(); + campaignYears.add(yearClassification); + } catch (HttpStatusCodeException hsce) { + log.error("Could not create campaign year '" + campaignYear + "' for entry " + entry.id + ": " + hsce.getStatusCode().toString()); + log.error(hsce.getResponseBodyAsString()); + } - // TODO HIER GEHT'S WEITER - - // (s. Z. 243 ff) } - // check whether campain already exists + // check whether campaign already exists String campaignGuid = null; for (Campaign c : campaigns) { if (c.name.equals(campaignName)) { - campaignGuid = c.guid; log.debug("Found already existing campaign '" + campaignName + "' with guid " + campaignGuid); + campaignGuid = c.guid; + record.addClassification(new WriteClassification(campaignGuid, 1)); } } @@ -334,15 +344,6 @@ public class MamMigrationSelective implements CommandLineRunner { FileUtils.writeStringToFile(problemLog, entry.id + fieldSeparator + entryJson + lineSeparator, Charset.forName("utf-8"), true); } } - - /** - * Checks whether the given campaign already exists and creates it, if not. - * - * @return the guid of the campaign - */ - private String getCampaign(int year, String label, String name) { - return null; - } private HttpHeaders createMamHeaders() { HttpHeaders headers = new HttpHeaders();