Commit 680fd109 authored by Matija Obreza's avatar Matija Obreza

Improved support for Accession 'other' names

parent de92e570
......@@ -101,11 +101,6 @@
<version>${commons.beanutils.version}</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
......@@ -352,6 +347,11 @@
<artifactId>spring-aspects</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
......
/**
* Copyright 2013 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.genesys;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import org.genesys2.server.model.BusinessModel;
/**
* Accession "alias"
*/
@Entity
@Table(name = "accessionalias")
public class AccessionAlias extends BusinessModel {
public static enum AliasType {
GENERIC(0), DONORNAME(1), BREEDERNAME(2), DATABASEID(3), LOCALNAME(4);
private int id;
private AliasType(int id) {
this.id = id;
}
public static AliasType getType(Integer id) {
if (id == null) {
return null;
}
for (AliasType aliasType : AliasType.values()) {
if (id.equals(aliasType.getId())) {
return aliasType;
}
}
throw new IllegalArgumentException("No matching type for id " + id);
}
public int getId() {
return id;
}
}
@Version
private long version = 0;
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "accessionId", nullable = false, updatable = false)
private Accession accession;
@Column(name = "name", length = 150)
private String name;
@Column(length = 7)
private String instCode;
@Column
private int aliasType = AliasType.GENERIC.id;
@Column(length = 2)
private String lang;
public AccessionAlias() {
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
public Accession getAccession() {
return accession;
}
public void setAccession(Accession accession) {
this.accession = accession;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInstCode() {
return instCode;
}
public void setInstCode(String instCode) {
this.instCode = instCode;
}
public AliasType getAliasType() {
return AliasType.getType(this.aliasType);
}
public void setAliasType(AliasType aliasType) {
this.aliasType = aliasType.getId();
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
}
/**
* Copyright 2013 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.persistence.domain;
import java.util.List;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AccessionAliasRepository extends JpaRepository<AccessionAlias, Long> {
List<AccessionAlias> findByAccession(Accession accession);
}
......@@ -17,6 +17,7 @@
package org.genesys2.server.service;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import org.genesys2.server.model.impl.AccessionIdentifier3;
......@@ -28,6 +29,8 @@ public interface BatchRESTService {
boolean upsertAccessionData(FaoInstitute institute, Map<DataJson, ObjectNode> batch);
void upsertAccessionNames(FaoInstitute institute, List<AcceNames> batch);
public static class DataJson implements AccessionIdentifier3 {
public String instCode;
public String acceNumb;
......@@ -54,4 +57,18 @@ public interface BatchRESTService {
}
}
public static class AcceNames extends BatchRESTService.DataJson {
public List<AcceAlias> aliases;
}
public static class AcceAlias {
public String name;
public String instCode;
public int type = 0;
@Override
public String toString() {
return "" + name + "@" + instCode;
}
}
}
......@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Set;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AllAccnames;
import org.genesys2.server.model.genesys.AllAcqBreeding;
......@@ -60,6 +61,8 @@ public interface GenesysService {
AllAccnames listAccessionNames(Accession accession);
List<AccessionAlias> listAccessionAliases(Accession accession);
AllAcqExchange listAccessionExchange(Accession accession);
AllAcqCollect listAccessionCollect(Accession accession);
......@@ -138,4 +141,8 @@ public interface GenesysService {
void writeAccessions(FaoInstitute faoInstitute, OutputStream outputStream) throws IOException;
void saveAliases(List<AccessionAlias> aliases);
void removeAliases(List<AccessionAlias> aliases);
}
......@@ -21,13 +21,17 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AllAcqCollect;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.AccessionAlias.AliasType;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.BatchRESTService;
......@@ -248,4 +252,65 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return toSave.size() > 0 || toSaveColl.size() > 0 || toSaveGeo.size() > 0;
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public void upsertAccessionNames(FaoInstitute institute, List<AcceNames> batch) {
LOG.info("Batch processing " + batch.size() + " entries for " + institute);
List<AccessionAlias> toSave = new ArrayList<AccessionAlias>();
List<AccessionAlias> toRemove = new ArrayList<AccessionAlias>();
for (AcceNames dataJson : batch) {
Accession accession = genesysService.getAccession(institute.getCode(), dataJson.genus, dataJson.acceNumb);
if (accession == null) {
LOG.warn("No such accession " + dataJson);
continue;
}
// LOG.info("Updating " + dataJson + " with=" + dataJson.aliases);
List<AccessionAlias> existingAliases = genesysService.listAccessionAliases(accession);
// Find aliases to remove
for (final AccessionAlias aa : existingAliases) {
if (null == CollectionUtils.find(dataJson.aliases, new Predicate<AcceAlias>() {
@Override
public boolean evaluate(AcceAlias alias) {
return StringUtils.equals(alias.name, aa.getName());
}
})) {
toRemove.add(aa);
}
}
// Add or update
for (final AcceAlias aa : dataJson.aliases) {
AccessionAlias accessionAlias = CollectionUtils.find(existingAliases, new Predicate<AccessionAlias>() {
@Override
public boolean evaluate(AccessionAlias alias) {
return StringUtils.equals(alias.getName(), aa.name);
}
});
if (accessionAlias == null) {
accessionAlias = new AccessionAlias();
accessionAlias.setAccession(accession);
}
accessionAlias.setName(aa.name);
accessionAlias.setInstCode(aa.instCode);
accessionAlias.setAliasType(AliasType.getType(aa.type));
toSave.add(accessionAlias);
}
}
if (toSave.size() > 0) {
LOG.info("Saving aliases for instCode=" + institute.getCode() + " count=" + toSave.size());
genesysService.saveAliases(toSave);
}
if (toRemove.size() > 0) {
LOG.info("Removing aliases for instCode=" + institute.getCode() + " count=" + toRemove.size());
genesysService.removeAliases(toRemove);
}
}
}
......@@ -35,6 +35,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionTrait;
import org.genesys2.server.model.genesys.AllAccnames;
......@@ -54,6 +55,7 @@ import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.server.persistence.domain.AccessionAliasRepository;
import org.genesys2.server.persistence.domain.AccessionBreedingRepository;
import org.genesys2.server.persistence.domain.AccessionCollectRepository;
import org.genesys2.server.persistence.domain.AccessionExchangeRepository;
......@@ -146,6 +148,10 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Autowired
private HtmlSanitizer htmlSanitizer;
@Autowired
private AccessionAliasRepository accessionAliasRepository;
@Override
public long countByInstitute(FaoInstitute institute) {
return accessionRepository.countByInstitute(institute);
......@@ -256,6 +262,11 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public AllAccnames listAccessionNames(Accession accession) {
return accessionNamesRepository.findByAccession(accession);
}
@Override
public List<AccessionAlias> listAccessionAliases(Accession accession) {
return accessionAliasRepository.findByAccession(accession);
}
@Override
public List<Metadata> listMetadata(Accession accession) {
......@@ -800,4 +811,16 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public void refreshMetadataMethods() {
genesysLowlevelRepository.refreshMetadataMethods();
}
@Override
@Transactional
public void saveAliases(List<AccessionAlias> aliases) {
accessionAliasRepository.save(aliases);
}
@Override
@Transactional
public void removeAliases(List<AccessionAlias> aliases) {
accessionAliasRepository.delete(aliases);
}
}
......@@ -22,7 +22,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -54,7 +54,6 @@ public class InstituteServiceImpl implements InstituteService {
public static final Log LOG = LogFactory.getLog(InstituteServiceImpl.class);
@SuppressWarnings("unchecked")
private static final List<FaoInstitute> EMPTY_LIST = ListUtils.unmodifiableList(new ArrayList<FaoInstitute>());
@Autowired
......
......@@ -68,6 +68,7 @@ public class AccessionController extends BaseController {
model.addAttribute("accession", accession);
model.addAttribute("faoInstitute", instituteService.getInstitute(accession.getInstituteCode()));
model.addAttribute("accessionNames", genesysService.listAccessionNames(accession));
model.addAttribute("accessionAliases", genesysService.listAccessionAliases(accession));
model.addAttribute("accessionExchange", genesysService.listAccessionExchange(accession));
model.addAttribute("accessionCollect", genesysService.listAccessionCollect(accession));
model.addAttribute("accessionBreeding", genesysService.listAccessionBreeding(accession));
......
......@@ -18,6 +18,7 @@ package org.genesys2.server.servlet.controller.rest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.genesys2.server.model.genesys.Accession;
......@@ -185,6 +186,27 @@ public class AccessionController extends RestController {
return JSON_OK;
}
/**
* Update accessions in the system
*
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
String upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<BatchRESTService.AcceNames> batch) {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
}
batchRESTService.upsertAccessionNames(institute, batch);
return JSON_OK;
}
private BatchRESTService.DataJson readAid3(JsonNode json) {
BatchRESTService.DataJson dataJson = new BatchRESTService.DataJson();
......
......@@ -201,6 +201,11 @@ accession.sampleStatus.420=Mutant/genetic stock
accession.sampleStatus.500=Advanced/improved cultivar
accession.sampleStatus.999=Other
accession.availability=Availability
accession.aliasType.GENERIC=
accession.aliasType.DONORNAME=(Donor accession name)
accession.aliasType.BREEDERNAME=(Used by breeders)
accession.aliasType.DATABASEID=(Database ID)
accession.aliasType.LOCALNAME=(Local name)
accession.availability.=Unknown
accession.availability.true=Available
......
......@@ -109,10 +109,24 @@
<td><spring:message code="accession.availability.${accession.availability}" /></td>
</tr>
<tr>
<td><spring:message code="accession.otherNames" /></td>
<td><c:out value="${accessionNames.accNames}" /><br /> <c:out value="${accessionNames.otherIds}" /></td>
<td>
<c:forEach items="${accessionAliases}" var="accessionAlias">
<div>
<c:out value="${accessionAlias.name}" />
<c:if test="${accessionAlias.instCode != ''}">
<a href="<c:url value="/wiews/${accessionAlias.instCode.toLowerCase()}" />">
${accessionAlias.instCode}
</a>
</c:if>
<c:if test="${accessionAlias.lang != ''}">
<c:out value="${accessionAlias.lang}" />
</c:if>
<spring:message code="accession.aliasType.${accessionAlias.aliasType}" />
</div>
</c:forEach>
</td>
</tr>
<tr>
......
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