Commit 3677912b authored by Matija Obreza's avatar Matija Obreza

Merge branch '451-delete-metadata' into 'master'

Resolve "Delete Metadata"

Closes #451

See merge request genesys-pgr/genesys-server!408
parents b450b609 75454e16
......@@ -35,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -87,6 +88,20 @@ public class DatasetController extends BaseController {
return "/metadata/view";
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/view/{id}", method = { RequestMethod.POST }, params = { "action=delete" })
public String delete(ModelMap model, @PathVariable(value = "id") long metadataId) {
LOG.debug("Deleting data for {}", metadataId);
final Metadata metadata = genesysService.getMetadata(metadataId);
if (metadata == null) {
throw new NotFoundElement();
}
genesysService.deleteMetadata(metadata);
return "redirect:/wiews/" + metadata.getInstituteCode() + "/datasets";
}
@RequestMapping(value = "/view/{id}/dwca", method = RequestMethod.POST, produces = "application/zip")
public void downloadDwca(@PathVariable(value = "id") long metadataId, HttpServletResponse response) throws IOException {
LOG.debug("Downloading data for {}", metadataId);
......
......@@ -46,6 +46,7 @@ import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.PDCIStatistics;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -293,6 +294,25 @@ public class WiewsController extends BaseController {
return "/metadata/index";
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/{wiewsCode}/datasets", method = { RequestMethod.POST }, params = { "action=delete-all" })
public String deleteInstituteDatasets(ModelMap model, @PathVariable(value = "wiewsCode") String wiewsCode) {
LOG.warn("Deleting old datasets for instCode={}", wiewsCode);
final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new NotFoundElement();
}
Page<Metadata> datasets = genesysService.listDatasets(faoInstitute.getCode(), new PageRequest(0, Integer.MAX_VALUE));
LOG.warn("Will delete {} old datasets for {}", datasets.getSize(), faoInstitute.getCode());
for (Metadata metadata : datasets) {
LOG.warn("Deleting {}", metadata.getTitle());
genesysService.deleteMetadata(metadata);
}
return "redirect:/wiews/" + wiewsCode;
}
@RequestMapping("/{wiewsCode}/edit")
public String edit(ModelMap model, @PathVariable(value = "wiewsCode") String wiewsCode) {
view(model, wiewsCode);
......
......@@ -41,6 +41,10 @@ public interface AccessionTraitRepository extends JpaRepository<AccessionTrait,
@Query(value = "select distinct a from AccessionTrait at inner join at.accession a where at.metadataId = ?1", countQuery = "select count(ma.accessionId) from MetadataAccession ma where ma.metadata.id = ?1")
Page<AccessionId> listMetadataAccessions(long metadataId, Pageable pageable);
@Modifying
@Query("delete from AccessionTrait at where at.metadataId = ?1")
int deleteMetadataAccessions(long metadataId);
List<AccessionTrait> findByMetadataIdAndAccession(Long id, Accession accession);
@Query("select count(id) from AccessionTrait at where at.metadataId = ?1 and at.accession = ?2")
......
......@@ -32,6 +32,10 @@ public interface MetadataAccessionRepository extends JpaRepository<MetadataAcces
@Query("select distinct ma.accessionId from MetadataAccession ma where ma.metadata = ?1")
List<Long> listMetadataAccessions(Metadata metadata);
@Modifying
@Query("delete from MetadataAccession ma where ma.metadata = ?1")
int deleteMetadataAccessions(Metadata metadata);
@Query("select distinct ma.metadata from MetadataAccession ma where ma.accessionId = ?1")
List<Metadata> listMetadataByAccessionId(long accessionId);
......
......@@ -31,6 +31,10 @@ public interface MetadataMethodRepository extends JpaRepository<MetadataMethod,
@Query("select distinct mm.methodId from MetadataMethod mm where mm.metadata = ?1")
List<Long> listMetadataMethods(Metadata metadata);
@Modifying
@Query("delete from MetadataMethod mm where mm.metadata = ?1")
int deleteMetadataMethods(Metadata metadata);
@Query("select distinct mm.metadata from MetadataMethod mm where mm.methodId = ?1")
List<Metadata> listMetadataByMethodId(long methodId);
......
......@@ -46,5 +46,6 @@ public interface TraitValueRepository {
Map<String, Long> getStatistics(Method method);
int deleteValues(Metadata metadata, Method method);
}
......@@ -253,4 +253,6 @@ public interface GenesysService {
Page<Metadata> listDatasets(String instCode, Pageable pageable);
void deleteMetadata(Metadata metadata);
}
......@@ -81,6 +81,7 @@ import org.genesys2.server.persistence.AccessionTraitRepository;
import org.genesys2.server.persistence.CropTaxonomyRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.GenesysLowlevelRepository;
import org.genesys2.server.persistence.MetadataAccessionRepository;
import org.genesys2.server.persistence.MetadataMethodRepository;
import org.genesys2.server.persistence.MetadataRepository;
import org.genesys2.server.persistence.MethodRepository;
......@@ -162,6 +163,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Autowired
private MetadataMethodRepository metadataMethodRepository;
@Autowired
private MetadataAccessionRepository metadataAccessionRepository;
@Autowired
private TraitValueRepository traitValueRepository;
@Autowired
private MethodRepository methodRepository;
......@@ -600,6 +603,25 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
public Metadata getMetadata(long metadataId) {
return metadataRepository.findOne(metadataId);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional
public void deleteMetadata(Metadata metadata) {
for (Method method : listMethods(metadata)) {
// delete observations
LOG.warn("Deleted {} observations of metadata {} method={}", traitValueRepository.deleteValues(metadata, method), metadata.getId(), method.getMethod());
}
// delete MetadataMethods
LOG.warn("Deleted {} MetadataMethods of metadata {}", metadataMethodRepository.deleteMetadataMethods(metadata), metadata.getId());
// delete AccessionTrait
LOG.warn("Deleted {} MetadataAccessions of metadata {}", metadataAccessionRepository.deleteMetadataAccessions(metadata), metadata.getId());
// delete MetadataAccessions
LOG.warn("Deleted {} AccessionTraits of metadata {}", accessionTraitRepository.deleteMetadataAccessions(metadata.getId()), metadata.getId());
// Delete the record itself
metadataRepository.delete(metadata);
}
@Override
public List<Method> listMethods(Metadata metadata) {
......
......@@ -108,6 +108,11 @@ public class TraitValueRepositoryImpl implements TraitValueRepository {
// return ret;
// }
@Override
public int deleteValues(Metadata metadata, Method method) {
return this.jdbcTemplate.update("delete from `" + method.getId() + "` where metadataId=" + metadata.getId());
}
@Override
public List<ExperimentAccessionTrait> getValues(final Metadata metadata, final Method method) {
final long metadataId = metadata.getId();
......
......@@ -21,6 +21,19 @@
</div>
</form>
</div>
<div class="pull-right list-view-controls">
<c:if test="${faoInstitute ne null}">
<security:authorize access="hasRole('ADMINISTRATOR')">
<form class="form-horizontal" method="post" action="<c:url value="/wiews/${faoInstitute.code}/datasets" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="hidden" name="action" value="delete-all"/>
<div class="col-sm-4">
<button class="btn btn-primary" type="submit"><spring:message code="delete" /> all for ${faoInstitute.code}</button>
</div>
</form>
</security:authorize>
</c:if>
</div>
</div>
<ul class="funny-list">
......
......@@ -46,6 +46,15 @@
</div>
</form>
</security:authorize>
<security:authorize access="hasRole('ADMINISTRATOR')">
<form class="form-horizontal" method="post" action="<c:url value="/data/view/${metadata.id}" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="hidden" name="action" value="delete"/>
<div class="col-sm-4">
<button class="btn btn-primary" type="submit"><spring:message code="delete" /></button>
</div>
</form>
</security:authorize>
</div>
</div>
......
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