Commit 47a282b5 authored by Matija Obreza's avatar Matija Obreza
Browse files

Accession#storage (MCPD)

parent 4220f74d
...@@ -28,11 +28,14 @@ import javax.persistence.FetchType; ...@@ -28,11 +28,14 @@ import javax.persistence.FetchType;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table; import javax.persistence.Table;
import org.genesys2.server.model.VersionedAuditedModel; import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.model.impl.Country; import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute; import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.util.MCPDUtil;
@Entity @Entity
@Table(name = "accession") @Table(name = "accession")
...@@ -106,9 +109,21 @@ public class Accession extends VersionedAuditedModel { ...@@ -106,9 +109,21 @@ public class Accession extends VersionedAuditedModel {
@OrderBy("storage") @OrderBy("storage")
private List<Integer> stoRage = new ArrayList<Integer>(); private List<Integer> stoRage = new ArrayList<Integer>();
@Column(name = "storage", length = 50, nullable = true)
private String storage;
public Accession() { public Accession() {
} }
/**
* Update MCPD {@link #storage}
*/
@PrePersist
@PreUpdate
private void prePersist() {
this.storage = MCPDUtil.toMcpdArray(this.stoRage);
}
public String getUuid() { public String getUuid() {
return uuid; return uuid;
} }
...@@ -262,6 +277,22 @@ public class Accession extends VersionedAuditedModel { ...@@ -262,6 +277,22 @@ public class Accession extends VersionedAuditedModel {
this.stoRage = stoRage; this.stoRage = stoRage;
} }
/**
* Returns {@link #getStoRage()} as MCPD string
*
* @return MCPD array string
*/
public String getStorage() {
return storage;
}
/**
* Use {@link #setStoRage(List)} instead.
*/
protected void setStorage(String storage) {
this.storage = storage;
}
@Override @Override
public String toString() { public String toString() {
return MessageFormat.format("Accession id={0,number,#} A={3} inst={1} genus={2}", id, instituteCode, taxGenus, accessionName); return MessageFormat.format("Accession id={0,number,#} A={3} inst={1} genus={2}", id, instituteCode, taxGenus, accessionName);
......
...@@ -230,7 +230,7 @@ public class DownloadServiceImpl implements DownloadService { ...@@ -230,7 +230,7 @@ public class DownloadServiceImpl implements DownloadService {
} }
createCell(row, 33, accession.getDuplSite()); createCell(row, 33, accession.getDuplSite());
createCell(row, 35, toMcpdArray(accession.getStoRage())); createCell(row, 35, accession.getStorage());
createCell(row, 36, accession.getMlsStatus()); createCell(row, 36, accession.getMlsStatus());
if (names != null && names.size() > 0) { if (names != null && names.size() > 0) {
...@@ -321,18 +321,4 @@ public class DownloadServiceImpl implements DownloadService { ...@@ -321,18 +321,4 @@ public class DownloadServiceImpl implements DownloadService {
return name; return name;
return otherNames + ";" + name; return otherNames + ";" + name;
} }
public static String toMcpdArray(List<Integer> integers) {
if (integers == null || integers.size() == 0)
return null;
String s = "";
for (Integer i : integers) {
if (i == null)
continue;
if (s.length() > 0)
s += ";";
s += i;
}
return s;
}
} }
/**
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.util;
import java.util.List;
import org.apache.commons.lang.StringUtils;
public class MCPDUtil {
public static String toMcpdArray(List<Integer> integers) {
if (integers == null || integers.size() == 0)
return null;
String s = "";
for (Integer i : integers) {
if (i == null)
continue;
if (s.length() > 0)
s += ";";
s += i;
}
return StringUtils.defaultIfBlank(s, null);
}
}
/**
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.model.impl;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.test.BatchRESTServiceTest;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.util.MCPDUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BatchRESTServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
public class AccessionStorageTest {
@Autowired
private GenesysService genesysService;
@Test
public void nullStorage() {
Accession a = new Accession();
a.setInstituteCode("INS000");
a.setAccessionName("A-1");
genesysService.saveAccession(a);
}
@Test
public void blankStorage() {
Accession a = new Accession();
a.setInstituteCode("INS000");
a.setAccessionName("A-" + System.currentTimeMillis());
List<Integer> stoRage = new ArrayList<Integer>();
a.setStoRage(stoRage);
genesysService.saveAccession(a);
Long accessionId = a.getId();
Accession b = genesysService.getAccession(accessionId);
assertTrue(b.getStorage() == null);
assertTrue(b.getStoRage() != null);
assertThat("Accession#stoRage size is not 0", b.getStoRage().size(), is(0));
}
@Test
public void oneStorage() {
Accession a = new Accession();
a.setInstituteCode("INS000");
a.setAccessionName("A-" + System.currentTimeMillis());
List<Integer> stoRage = new ArrayList<Integer>();
stoRage.add(1);
a.setStoRage(stoRage);
genesysService.saveAccession(a);
Long accessionId = a.getId();
Accession b = genesysService.getAccession(accessionId);
assertTrue(b.getStorage() != null);
assertThat("Storage value should be '1'", a.getStorage(), is("1"));
assertTrue(b.getStoRage() != null);
}
@Test
public void twoStorage() {
Accession a = new Accession();
a.setInstituteCode("INS000");
a.setAccessionName("A-" + System.currentTimeMillis());
List<Integer> stoRage = new ArrayList<Integer>();
stoRage.add(1);
stoRage.add(2);
a.setStoRage(stoRage);
genesysService.saveAccession(a);
Long accessionId = a.getId();
Accession b = genesysService.getAccession(accessionId);
assertTrue(b.getStorage() != null);
assertThat("Storage value should be '1'", a.getStorage(), is("1;2"));
assertTrue(b.getStoRage() != null);
}
@Test
public void clearStorage() {
Accession a = new Accession();
a.setInstituteCode("INS000");
a.setAccessionName("A-" + System.currentTimeMillis());
List<Integer> stoRage = new ArrayList<Integer>();
stoRage.add(1);
stoRage.add(2);
a.setStoRage(stoRage);
genesysService.saveAccession(a);
Long accessionId = a.getId();
assertNotNull("Accession#id must not be null", a.getId());
Accession b = genesysService.getAccession(accessionId);
assertTrue(b.getStorage() != null);
assertThat("Storage value should be '1'", a.getStorage(), is("1;2"));
assertTrue(b.getStoRage() != null);
b.getStoRage().clear();
genesysService.saveAccession(b);
Accession c = genesysService.getAccession(accessionId);
assertNotNull("Accession#stoRage must not be null", c.getStoRage());
assertThat("Accession#stoRage size is not 0", c.getStoRage().size(), is(0));
assertThat("Accession#storage is not null", c.getStorage(), is(nullValue()));
}
@Test
public void updateStorage() {
Accession a = new Accession();
a.setInstituteCode("INS000");
a.setAccessionName("A-" + System.currentTimeMillis());
List<Integer> stoRage = new ArrayList<Integer>();
stoRage.add(1);
stoRage.add(2);
stoRage.add(3);
a.setStoRage(stoRage);
System.out.println("Saving 1");
genesysService.saveAccession(a);
System.out.println("Saving 2");
genesysService.saveAccession(a);
Long accessionId = a.getId();
assertNotNull("Accession#id must not be null", a.getId());
Accession b = genesysService.getAccession(accessionId);
assertNotNull(b.getStorage());
assertNotNull(b.getStoRage());
assertThat("StoRage length is not 3", a.getStoRage().size(), is(3));
assertThat("Storage value should be '1;2;3'", a.getStorage(), is("1;2;3"));
b.getStoRage().remove(0);
genesysService.saveAccession(b);
Accession c = genesysService.getAccession(accessionId);
assertNotNull("Accession#stoRage must not be null", c.getStoRage());
assertThat("Accession#stoRage size is not 2", c.getStoRage().size(), is(2));
assertThat("Accession#storage is not '2;3'", c.getStorage(), is("2;3"));
assertThat("Accession#stoRage does not generate MCPD style string'", c.getStorage(), is(MCPDUtil.toMcpdArray(c.getStoRage())));
System.out.println("Saving 3");
genesysService.saveAccession(c);
}
}
...@@ -16,9 +16,8 @@ ...@@ -16,9 +16,8 @@
package org.genesys2.server.test; package org.genesys2.server.test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -37,6 +36,7 @@ import org.genesys2.server.persistence.domain.CountryRepository; ...@@ -37,6 +36,7 @@ import org.genesys2.server.persistence.domain.CountryRepository;
import org.genesys2.server.service.AclService; import org.genesys2.server.service.AclService;
import org.genesys2.server.service.BatchRESTService; import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.ContentService; import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService; import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService; import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.HtmlSanitizer; import org.genesys2.server.service.HtmlSanitizer;
...@@ -47,6 +47,7 @@ import org.genesys2.server.service.UserService; ...@@ -47,6 +47,7 @@ import org.genesys2.server.service.UserService;
import org.genesys2.server.service.impl.AclServiceImpl; import org.genesys2.server.service.impl.AclServiceImpl;
import org.genesys2.server.service.impl.BatchRESTServiceImpl; import org.genesys2.server.service.impl.BatchRESTServiceImpl;
import org.genesys2.server.service.impl.ContentServiceImpl; import org.genesys2.server.service.impl.ContentServiceImpl;
import org.genesys2.server.service.impl.CropServiceImpl;
import org.genesys2.server.service.impl.GenesysServiceImpl; import org.genesys2.server.service.impl.GenesysServiceImpl;
import org.genesys2.server.service.impl.GeoServiceImpl; import org.genesys2.server.service.impl.GeoServiceImpl;
import org.genesys2.server.service.impl.InstituteServiceImpl; import org.genesys2.server.service.impl.InstituteServiceImpl;
...@@ -57,6 +58,7 @@ import org.genesys2.server.service.impl.RESTApiException; ...@@ -57,6 +58,7 @@ import org.genesys2.server.service.impl.RESTApiException;
import org.genesys2.server.service.impl.TaxonomyServiceImpl; import org.genesys2.server.service.impl.TaxonomyServiceImpl;
import org.genesys2.server.service.impl.UserServiceImpl; import org.genesys2.server.service.impl.UserServiceImpl;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson; import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -68,6 +70,7 @@ import org.springframework.context.annotation.Bean; ...@@ -68,6 +70,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.velocity.VelocityEngineFactoryBean; import org.springframework.ui.velocity.VelocityEngineFactoryBean;
...@@ -76,11 +79,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -76,11 +79,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = BatchRESTServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class) @ContextConfiguration(classes = BatchRESTServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
public class BatchRESTServiceTest { public class BatchRESTServiceTest {
private final ObjectMapper mapper = new ObjectMapper(); private final ObjectMapper mapper = new ObjectMapper();
@Import(JpaDataConfig.class) @Import({ JpaDataConfig.class, HazelcastConfig.class })
@ComponentScan(basePackages = { "org.genesys2.server.persistence.domain" }) @ComponentScan(basePackages = { "org.genesys2.server.persistence.domain" })
public static class Config { public static class Config {
...@@ -109,6 +113,11 @@ public class BatchRESTServiceTest { ...@@ -109,6 +113,11 @@ public class BatchRESTServiceTest {
return new BatchRESTServiceImpl(); return new BatchRESTServiceImpl();
} }
@Bean
public CropService cropService() {
return new CropServiceImpl();
}
@Bean @Bean
public GenesysService genesysService() { public GenesysService genesysService() {
return new GenesysServiceImpl(); return new GenesysServiceImpl();
...@@ -311,4 +320,72 @@ public class BatchRESTServiceTest { ...@@ -311,4 +320,72 @@ public class BatchRESTServiceTest {
assertTrue(accession.getCountryOfOrigin() == null); assertTrue(accession.getCountryOfOrigin() == null);
} }
@Test
public void testUpdateStorage() throws NonUniqueAccessionException {
final String instCode = "INS002";
final FaoInstitute institute = instituteService.getInstitute(instCode);
assertTrue("institute is null", institute != null);
System.err.println(institute);
final Map<AccessionHeaderJson, ObjectNode> batch = new HashMap<AccessionHeaderJson, ObjectNode>();
final AccessionHeaderJson dataJson = new AccessionHeaderJson();
dataJson.acceNumb = "AC 100";
dataJson.instCode = instCode;
final ObjectNode json = mapper.createObjectNode();
json.put("genus", "Hordeum");
json.put("orgCty", "CTY");
json.put("storage", "10;30");
batch.put(dataJson, json);
System.err.println(json);
try {
batchRESTService.upsertAccessionData(institute, batch);
} catch (final RESTApiException e) {
fail(e.getMessage());
}
Accession accession = genesysService.getAccession(instCode, "AC 100");
assertTrue(accession.getId() != null);
assertTrue(accession.getInstituteCode().equals(instCode));
assertTrue(accession.getInstitute().getId().equals(institute.getId()));
assertTrue(accession.getOrigin() != null);
assertTrue("CTY".equals(accession.getOrigin()));
assertTrue(accession.getCountryOfOrigin() != null);
assertTrue("Country".equals(accession.getCountryOfOrigin().getName()));
assertNotNull("StoRage must be", accession.getStoRage());
assertNotNull("Storage must be", accession.getStorage());
assertThat("Storage must be 10;30", accession.getStorage(), is("10;30"));
// Modify STORAGE
json.put("storage", "40");
System.err.println(json);
try {
batchRESTService.upsertAccessionData(institute, batch);
} catch (final RESTApiException e) {
fail(e.getMessage());
}
// reload
accession = genesysService.getAccession(instCode, "AC 100");
assertNotNull("storage should not be null", accession.getStorage());
assertThat("stoRage should 40", accession.getStorage(), is("40"));
assertThat("stoRage should be 0-size", accession.getStoRage().size(), is(1));
// Clear STORAGE
json.putNull("storage");
System.err.println(json);
try {
batchRESTService.upsertAccessionData(institute, batch);
} catch (final RESTApiException e) {
fail(e.getMessage());
}
// reload
accession = genesysService.getAccession(instCode, "AC 100");
assertNull("storage should be null", accession.getStorage());
assertNotNull("stoRage should be null", accession.getStoRage());
assertThat("stoRage should be 0-size", accession.getStoRage().size(), is(0));
}
} }
...@@ -30,6 +30,7 @@ import org.springframework.context.annotation.PropertySource; ...@@ -30,6 +30,7 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator; import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
...@@ -44,7 +45,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; ...@@ -44,7 +45,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@PropertySource("classpath:/spring/spring.properties") @PropertySource("classpath:/spring/spring.properties")
@EnableJpaRepositories(basePackages = { "org.genesys2.server.persistence.acl", "org.genesys2.server.persistence.domain" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager") @EnableJpaRepositories(basePackages = { "org.genesys2.server.persistence.acl", "org.genesys2.server.persistence.domain" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@EnableTransactionManagement @EnableTransactionManagement
@TransactionConfiguration(transactionManager = "transactionManager") @TransactionConfiguration(defaultRollback = false, transactionManager = "transactionManager")
public class JpaDataConfig { public class JpaDataConfig {
@Autowired @Autowired
private Environment env; private Environment env;
...@@ -61,6 +62,11 @@ public class JpaDataConfig { ...@@ -61,6 +62,11 @@ public class JpaDataConfig {
return dataSource; return dataSource;
} }
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "entityManagerFactory") @Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception { public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
...@@ -93,7 +99,7 @@ public class JpaDataConfig { ...@@ -93,7 +99,7 @@ public class JpaDataConfig {
public PersistenceExceptionTranslator hibernateExceptionTranslator() { public PersistenceExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator(); return new HibernateExceptionTranslator();
} }
private Properties jpaProperties() throws Exception { private Properties jpaProperties() throws Exception {
Properties jpaProp = new Properties(); Properties jpaProp = new Properties();
jpaProp.load(getClass().getResourceAsStream("/spring/hibernate.properties")); jpaProp.load(getClass().getResourceAsStream("/spring/hibernate.properties"));
......
Markdown is supported
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