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