Commit 6fd9dedc authored by Matija Obreza's avatar Matija Obreza
Browse files

Refactored AccessionTrait method and metadata to only use referenced IDs

parent e8965443
...@@ -14,40 +14,31 @@ ...@@ -14,40 +14,31 @@
* limitations under the License. * limitations under the License.
**/ **/
package org.genesys2.server.model.genesys; package org.genesys2.server.model.genesys;
import java.io.Serializable; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
@Entity @Entity
@Table(name = "accessiontrait") @Table(name = "accessiontrait")
public class AccessionTrait implements Serializable { public class AccessionTrait extends BusinessModel {
private static final long serialVersionUID = -240056837800843686L; private static final long serialVersionUID = -240056837800843686L;
private Long id; @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "accessionId")
private Accession accession; private Accession accession;
private Metadata metadata;
private Method method;
@Id @Column(name = "metadataId")
@GeneratedValue private long metadataId;
public Long getId() {
return id; @Column(name = "methodId")
} private long methodId;
public void setId(Long id) {
this.id = id;
}
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "accessionId")
public Accession getAccession() { public Accession getAccession() {
return accession; return accession;
} }
...@@ -56,24 +47,19 @@ public class AccessionTrait implements Serializable { ...@@ -56,24 +47,19 @@ public class AccessionTrait implements Serializable {
this.accession = accession; this.accession = accession;
} }
@ManyToOne(cascade = {}, optional = false) public long getMetadataId() {
@JoinColumn(name = "metadataId") return metadataId;
public Metadata getMetadata() {
return metadata;
} }
public void setMetadata(Metadata metadata) { public void setMetadataId(long metadataId) {
this.metadata = metadata; this.metadataId = metadataId;
} }
@ManyToOne(cascade = {}, optional = false) public long getMethodId() {
@JoinColumn(name = "methodId") return methodId;
public Method getMethod() {
return method;
} }
public void setMethod(Method method) { public void setMethodId(long methodId) {
this.method = method; this.methodId = methodId;
} }
} }
...@@ -20,8 +20,6 @@ import java.util.List; ...@@ -20,8 +20,6 @@ import java.util.List;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionTrait; import org.genesys2.server.model.genesys.AccessionTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
...@@ -31,22 +29,13 @@ public interface AccessionTraitRepository extends JpaRepository<AccessionTrait, ...@@ -31,22 +29,13 @@ public interface AccessionTraitRepository extends JpaRepository<AccessionTrait,
List<AccessionTrait> findByAccession(Accession accession); List<AccessionTrait> findByAccession(Accession accession);
List<AccessionTrait> findByMetadata(Metadata metadata); @Query("select distinct at.metadataId from AccessionTrait at where at.accession = ?1")
List<Long> listMetadataIds(Accession accession);
List<AccessionTrait> findByMetadataAndAccession(Metadata metadata, Accession accession); @Query("select distinct at.methodId from AccessionTrait at where at.accession = ?1")
List<Long> listMethodIds(Accession accession);
List<AccessionTrait> findByMethod(Method method); @Query("select distinct at.accession from AccessionTrait at where at.metadataId = ?1")
Page<Accession> listMetadataAccessions(long metadataId, Pageable pageable);
@Query("select distinct at.metadata from AccessionTrait at where at.accession = ?1")
List<Metadata> listMetadata(Accession accession);
@Query("select distinct at.method from AccessionTrait at where at.accession = ?1")
List<Method> listMethods(Accession accession);
@Query("select distinct at.accession from AccessionTrait at where at.metadata = ?1")
Page<Accession> listMetadataAccessions(Metadata metadata, Pageable pageable);
@Query("select distinct at.method from AccessionTrait at where at.metadata = ?1")
List<Method> listMetadataMethods(Metadata metadata);
} }
...@@ -47,6 +47,9 @@ public class TraitValueRepositoryImpl implements TraitValueRepository { ...@@ -47,6 +47,9 @@ public class TraitValueRepositoryImpl implements TraitValueRepository {
@Autowired @Autowired
private AccessionTraitRepository accessionTraitRepository; private AccessionTraitRepository accessionTraitRepository;
@Autowired
private MethodRepository methodRepository;
@Autowired @Autowired
public void setDataSource(final DataSource dataSource) { public void setDataSource(final DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource); this.jdbcTemplate = new JdbcTemplate(dataSource);
...@@ -90,7 +93,7 @@ public class TraitValueRepositoryImpl implements TraitValueRepository { ...@@ -90,7 +93,7 @@ public class TraitValueRepositoryImpl implements TraitValueRepository {
Map<Long, List<ExperimentTrait>> methodValues = new HashMap<Long, List<ExperimentTrait>>(); Map<Long, List<ExperimentTrait>> methodValues = new HashMap<Long, List<ExperimentTrait>>();
for (AccessionTrait at : accessionTraits) { for (AccessionTrait at : accessionTraits) {
Method method = at.getMethod(); Method method = methodRepository.findOne(at.getMethodId());
final List<Map<String, Object>> rows = this.jdbcTemplate.queryForList("select Meta_id as metadataId, `" + method.getFieldName() final List<Map<String, Object>> rows = this.jdbcTemplate.queryForList("select Meta_id as metadataId, `" + method.getFieldName()
+ "` as traitvalue from `" + method.getId() + "` where `ALIS_Id`=?", new Object[] { at.getAccession().getId() }); + "` as traitvalue from `" + method.getId() + "` where `ALIS_Id`=?", new Object[] { at.getAccession().getId() });
......
...@@ -17,7 +17,9 @@ ...@@ -17,7 +17,9 @@
package org.genesys2.server.service; package org.genesys2.server.service;
import java.util.List; import java.util.List;
import java.util.Map;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Metadata; import org.genesys2.server.model.genesys.Metadata;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
...@@ -34,4 +36,6 @@ public interface DatasetService { ...@@ -34,4 +36,6 @@ public interface DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#metadata, 'WRITE')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#metadata, 'WRITE')")
void touch(@Param("metadata") Metadata metadata); void touch(@Param("metadata") Metadata metadata);
void upsertAccessionData(Metadata metadata, Accession accession, Map<Long, List<Object>> methodValues);
} }
...@@ -74,7 +74,7 @@ public interface GenesysService { ...@@ -74,7 +74,7 @@ public interface GenesysService {
List<Method> listMethods(Metadata metadata); List<Method> listMethods(Metadata metadata);
Page<Accession> listMetadataAccessions(Metadata metadata, Pageable pageable); Page<Accession> listMetadataAccessions(long metadataId, Pageable pageable);
List<Method> listMethods(Accession accession); List<Method> listMethods(Accession accession);
...@@ -113,14 +113,4 @@ public interface GenesysService { ...@@ -113,14 +113,4 @@ public interface GenesysService {
void saveSvalbards(List<SvalbardData> svalbards); void saveSvalbards(List<SvalbardData> svalbards);
long countAvailable(Set<Long> accessionIds); long countAvailable(Set<Long> accessionIds);
/**
*
* @param metadata
* @param accession
* @param methodValues
* Map of method ids with List of objects
*/
void upsertAccessionData(Metadata metadata, Accession accession, Map<Long, List<Object>> methodValues);
} }
...@@ -26,7 +26,6 @@ import org.apache.commons.lang.StringUtils; ...@@ -26,7 +26,6 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionTrait;
import org.genesys2.server.model.genesys.AllAccnames; import org.genesys2.server.model.genesys.AllAccnames;
import org.genesys2.server.model.genesys.AllAcqBreeding; import org.genesys2.server.model.genesys.AllAcqBreeding;
import org.genesys2.server.model.genesys.AllAcqCollect; import org.genesys2.server.model.genesys.AllAcqCollect;
...@@ -224,12 +223,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -224,12 +223,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
public List<Metadata> listMetadata(Accession accession) { public List<Metadata> listMetadata(Accession accession) {
return accessionTraitRepository.listMetadata(accession); return metadataRepository.findByIds(accessionTraitRepository.listMetadataIds(accession));
} }
@Override @Override
public List<Method> listMethods(Accession accession) { public List<Method> listMethods(Accession accession) {
return accessionTraitRepository.listMethods(accession); return methodRepository.findByIds(accessionTraitRepository.listMethodIds(accession));
} }
@Override @Override
...@@ -248,8 +247,8 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -248,8 +247,8 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
} }
@Override @Override
public Page<Accession> listMetadataAccessions(Metadata metadata, Pageable pageable) { public Page<Accession> listMetadataAccessions(long metadataId, Pageable pageable) {
return accessionTraitRepository.listMetadataAccessions(metadata, pageable); return accessionTraitRepository.listMetadataAccessions(metadataId, pageable);
} }
@Override @Override
...@@ -477,18 +476,25 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -477,18 +476,25 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return methodRepository.findByIds(oids); return methodRepository.findByIds(oids);
} }
/**
* @param metadata
* @param accession
* @param methodValues
*/
@Override @Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#metadata, 'WRITE')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#metadata, 'WRITE')")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public void upsertAccessionData(Metadata metadata, Accession accession, Map<Long, List<Object>> methodValues) { public void upsertAccessionData(Metadata metadata, Accession accession, Map<Long, List<Object>> methodValues) {
// TODO Load all existing records for metadata+accession // TODO Load all existing records for metadata+accession
List<AccessionTrait> existingEntries = accessionTraitRepository.findByMetadataAndAccession(metadata, accession); // List<AccessionTrait> existingEntries =
// accessionTraitRepository.findByMetadataAndAccession(metadata,
// accession);
// Remove entries missing from map // Remove entries missing from map
for (AccessionTrait accessionTrait : existingEntries) { // for (AccessionTrait accessionTrait : existingEntries) {
LOG.debug("Existing trait " + accessionTrait); // LOG.debug("Existing trait " + accessionTrait);
// TODO Remove // // TODO Remove
} // }
// TODO Add new entries from map // TODO Add new entries from map
// just stick them to database // just stick them to database
......
...@@ -65,7 +65,7 @@ public class DatasetController extends BaseController { ...@@ -65,7 +65,7 @@ public class DatasetController extends BaseController {
model.addAttribute("metadata", metadata); model.addAttribute("metadata", metadata);
model.addAttribute("methods", genesysService.listMethods(metadata)); model.addAttribute("methods", genesysService.listMethods(metadata));
Page<Accession> accessions = genesysService.listMetadataAccessions(metadata, new PageRequest(page - 1, 50)); Page<Accession> accessions = genesysService.listMetadataAccessions(metadata.getId(), new PageRequest(page - 1, 50));
model.addAttribute("pagedData", accessions); model.addAttribute("pagedData", accessions);
// Map[accession.id][method.id] // Map[accession.id][method.id]
......
...@@ -159,6 +159,7 @@ public class DatasetController extends RestController { ...@@ -159,6 +159,7 @@ public class DatasetController extends RestController {
System.err.println(data); System.err.println(data);
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode json = null; JsonNode json = null;
try { try {
json = objectMapper.readTree(data); json = objectMapper.readTree(data);
System.err.println(json); System.err.println(json);
...@@ -190,7 +191,7 @@ public class DatasetController extends RestController { ...@@ -190,7 +191,7 @@ public class DatasetController extends RestController {
LOG.info("Upserting data for accession " + accession); LOG.info("Upserting data for accession " + accession);
genesysService.upsertAccessionData(metadata, accession, dataJson.methodValues); datasetService.upsertAccessionData(metadata, accession, dataJson.methodValues);
} }
private DataJson readDataJson(JsonNode json) { private DataJson readDataJson(JsonNode json) {
......
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