Commit 87c923c4 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added DescriptorList#extra map of Strings

parent 19cb5f5d
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -16,22 +16,28 @@
package org.genesys.catalog.model.traits;
import java.util.List;
import java.util.Map;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.MapKeyEnumerated;
import javax.persistence.OrderColumn;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
......@@ -40,6 +46,7 @@ import org.genesys.common.model.SelfCleaning;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
......@@ -52,10 +59,17 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Cacheable
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class DescriptorList extends UuidModel implements Publishable, SelfCleaning, AclAwareModel {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 4873500390485739013L;
/**
* Extras in Descriptor lists
*/
public static enum Extra {
JSON_MAPPING
}
/**
* Trait title in English.
*/
......@@ -101,6 +115,16 @@ public class DescriptorList extends UuidModel implements Publishable, SelfCleani
@Column(name = "crop", length = 100, nullable = true)
private String crop;
/** Extra information associated with the descriptor list */
@JsonView({ JsonViews.Update.class, JsonViews.Public.class })
@ElementCollection(fetch = FetchType.EAGER)
@JoinTable(name = "DescriptorListExtra", joinColumns = { @JoinColumn(name = "descriptorListId") })
@MapKeyColumn(name = "extra", length = 25)
@MapKeyEnumerated(EnumType.STRING)
@Column(name = "v")
@Lob
private Map<Extra, String> extras;
/**
* Pre-persist, pre-update
*/
......@@ -109,7 +133,7 @@ public class DescriptorList extends UuidModel implements Publishable, SelfCleani
private void preupdate() {
trimStringsToNull();
}
/**
* Gets the publisher.
*
......@@ -271,4 +295,22 @@ public class DescriptorList extends UuidModel implements Publishable, SelfCleani
public void setCrop(final String crop) {
this.crop = crop;
}
/**
* Gets the extra information for this descriptor list
*
* @return the extras
*/
public Map<Extra, String> getExtras() {
return extras;
}
/**
* Sets the extra information for the descriptor list
*
* @param extras the extras
*/
public void setExtras(Map<Extra, String> extras) {
this.extras = extras;
}
}
......@@ -19,6 +19,7 @@ import static org.genesys.catalog.model.traits.QDescriptorList.descriptorList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
......@@ -202,6 +203,8 @@ public class DescriptorListServiceImpl implements DescriptorListService {
descriptorList.getDescriptors().size();
if (descriptorList.getOwner() != null)
descriptorList.getOwner().getId();
if (descriptorList.getExtras() != null)
descriptorList.getExtras().keySet();
return descriptorList;
}
......@@ -240,6 +243,14 @@ public class DescriptorListServiceImpl implements DescriptorListService {
if (source.getDescriptors() != null) {
target.setDescriptors(source.getDescriptors());
}
if (source.getExtras() != null) {
if (target.getExtras() == null) {
target.setExtras(new HashMap<>());
}
// removes existing extras and puts in new ones
target.getExtras().clear();
target.getExtras().putAll(source.getExtras());
}
}
@Override
......
......@@ -15,13 +15,7 @@
*/
package org.genesys.catalog.service;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
......@@ -32,12 +26,10 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
......@@ -51,8 +43,9 @@ import org.genesys.common.model.Partner;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import au.com.bytecode.opencsv.CSVReader;
......@@ -439,4 +432,27 @@ public class DescriptorListServiceTest extends CatalogServiceTest {
result = descriptorListService.updateDescriptorList(result);
assertThat(result.getOwner(), not(partner2));
}
@Test
public void updateDescriptorListExtras() throws Exception {
DescriptorList descriptorList = setupDescriptorList(DESCRIPTION_LIST_TITLE1, VERSION_1_0, DESCRIPTION_1);
DescriptorList result = descriptorListService.createDescriptorList(descriptorList);
assertThat(result.getExtras(), is(nullValue()));
// set extra
result.setExtras(new HashMap<>());
result.getExtras().put(DescriptorList.Extra.JSON_MAPPING, "This is JSON extra");
result = descriptorListService.updateDescriptorList(result);
assertThat(result.getExtras(), not(nullValue()));
assertThat(result.getExtras().keySet(), hasSize(1));
result.getExtras().clear();
result = descriptorListService.updateDescriptorList(result);
assertThat(result.getExtras(), not(nullValue()));
assertThat(result.getExtras().keySet(), hasSize(0));
}
}
......@@ -3495,3 +3495,40 @@ databaseChangeLog:
name: term_count
type: INT
tableName: controlled_vocabulary
- changeSet:
id: 1517853993215-1
author: mobreza (generated)
comment: Added DescriptorList#extra map of strings
changes:
- createTable:
columns:
- column:
constraints:
nullable: false
name: descriptor_list_id
type: BIGINT
- column:
name: v
type: LONGTEXT
- column:
constraints:
nullable: false
name: extra
type: VARCHAR(50)
tableName: descriptor_list_extra
- addPrimaryKey:
columnNames: descriptor_list_id, extra
constraintName: PRIMARY
tableName: descriptor_list_extra
- addForeignKeyConstraint:
baseColumnNames: descriptor_list_id
baseTableName: descriptor_list_extra
constraintName: FK_h68mcdqhvsnqok3dx33a52kcd
deferrable: false
initiallyDeferred: false
onDelete: NO ACTION
onUpdate: NO ACTION
referencedColumnNames: id
referencedTableName: descriptor_list
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment