some refactoring
This commit is contained in:
parent
7ac3d73299
commit
abc91beaf6
|
@ -1,215 +1,132 @@
|
||||||
package eu.dnetlib.data.mdstore.manager.controller;
|
package eu.dnetlib.data.mdstore.manager.controller;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import javax.transaction.Transactional;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
|
|
||||||
import eu.dnetlib.data.mdstore.manager.common.model.MDStore;
|
|
||||||
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreCurrentVersion;
|
|
||||||
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
|
||||||
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo;
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo;
|
||||||
import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException;
|
import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException;
|
||||||
import eu.dnetlib.data.mdstore.manager.exceptions.NoContentException;
|
import eu.dnetlib.data.mdstore.manager.utils.DatabaseUtils;
|
||||||
import eu.dnetlib.data.mdstore.manager.repository.MDStoreCurrentVersionRepository;
|
|
||||||
import eu.dnetlib.data.mdstore.manager.repository.MDStoreRepository;
|
|
||||||
import eu.dnetlib.data.mdstore.manager.repository.MDStoreVersionRepository;
|
|
||||||
import eu.dnetlib.data.mdstore.manager.repository.MDStoreWithInfoRepository;
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/mdstores")
|
@RequestMapping("/mdstores")
|
||||||
@Api(tags = { "Metadata Stores" })
|
@Api(tags = {
|
||||||
|
"Metadata Stores"
|
||||||
|
})
|
||||||
public class MDStoreController {
|
public class MDStoreController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MDStoreRepository mdstoreRepository;
|
private DatabaseUtils databaseUtils;
|
||||||
@Autowired
|
|
||||||
private MDStoreVersionRepository mdstoreVersionRepository;
|
|
||||||
@Autowired
|
|
||||||
private MDStoreCurrentVersionRepository mdstoreCurrentVersionRepository;
|
|
||||||
@Autowired
|
|
||||||
private MDStoreWithInfoRepository mdstoreWithInfoRepository;
|
|
||||||
@Autowired
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
@ApiOperation(value = "Return all the mdstores")
|
@ApiOperation("Return all the mdstores")
|
||||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
@GetMapping("/")
|
||||||
public List<MDStoreWithInfo> find() {
|
public Iterable<MDStoreWithInfo> find() {
|
||||||
return Lists.newArrayList(mdstoreWithInfoRepository.findAll());
|
return databaseUtils.listMdStores();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "Return all the mdstore identifiers")
|
@ApiOperation("Return all the mdstore identifiers")
|
||||||
@RequestMapping(value = "/ids", method = RequestMethod.GET)
|
@GetMapping("/ids")
|
||||||
public List<String> findIdentifiers() {
|
public List<String> findIdentifiers() {
|
||||||
return mdstoreRepository.findAll().stream().map(MDStore::getId).collect(Collectors.toList());
|
return databaseUtils.listMdStoreIDs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "Return a mdstores by id")
|
@ApiOperation("Return a mdstores by id")
|
||||||
@RequestMapping(value = "/mdstore/{mdId}", method = RequestMethod.GET)
|
@GetMapping("/mdstore/{mdId}")
|
||||||
public MDStoreWithInfo get(@ApiParam("the mdstore identifier") @PathVariable final String mdId) throws NoContentException {
|
public MDStoreWithInfo get(@ApiParam("the mdstore identifier") @PathVariable final String mdId) throws MDStoreManagerException {
|
||||||
return mdstoreWithInfoRepository.findById(mdId).orElseThrow(() -> new NoContentException("Missing mdstore: " + mdId));
|
return databaseUtils.findMdStore(mdId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Increase the read count of the current mdstore")
|
||||||
@ApiOperation(value = "Increase the read count of the current mdstore")
|
@GetMapping("/mdstore/{mdId}/startReading")
|
||||||
@RequestMapping(value = "/mdstore/{mdId}/startReading", method = RequestMethod.GET)
|
public MDStoreVersion startReading(@ApiParam("the mdstore identifier") @PathVariable final String mdId) throws MDStoreManagerException {
|
||||||
public MDStoreVersion startReading(@ApiParam("the mdstore identifier") @PathVariable final String mdId) throws NoContentException {
|
return databaseUtils.startReading(mdId);
|
||||||
final MDStoreCurrentVersion cv = mdstoreCurrentVersionRepository.findById(mdId).orElseThrow(() -> new NoContentException("Missing mdstore: " + mdId));
|
|
||||||
final MDStoreVersion v = mdstoreVersionRepository.findById(cv.getCurrentVersion())
|
|
||||||
.orElseThrow(() -> new NoContentException("Missing version: " + cv.getCurrentVersion()));
|
|
||||||
v.setReadCount(v.getReadCount() + 1);
|
|
||||||
mdstoreVersionRepository.save(v);
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "Return the number of mdstores")
|
@ApiOperation("Return the number of mdstores")
|
||||||
@RequestMapping(value = "/count", method = RequestMethod.GET)
|
@GetMapping("/count")
|
||||||
public long count() {
|
public SizeResponse count() {
|
||||||
return mdstoreRepository.count();
|
return new SizeResponse(databaseUtils.countMdStores());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "Create a new mdstore")
|
@ApiOperation("Create a new mdstore")
|
||||||
@RequestMapping(value = "/new/{format}/{layout}/{interpretation}", method = RequestMethod.GET)
|
@GetMapping("/new/{format}/{layout}/{interpretation}")
|
||||||
public MDStoreWithInfo createMDStore(
|
public MDStoreWithInfo createMDStore(
|
||||||
@ApiParam("mdstore format") @PathVariable final String format,
|
@ApiParam("mdstore format") @PathVariable final String format,
|
||||||
@ApiParam("mdstore layout") @PathVariable final String layout,
|
@ApiParam("mdstore layout") @PathVariable final String layout,
|
||||||
@ApiParam("mdstore interpretation") @PathVariable final String interpretation,
|
@ApiParam("mdstore interpretation") @PathVariable final String interpretation,
|
||||||
@ApiParam("datasource name") @RequestParam(required = false) final String dsName,
|
@ApiParam("datasource name") @RequestParam(required = false) final String dsName,
|
||||||
@ApiParam("datasource id") @RequestParam(required = false) final String dsId,
|
@ApiParam("datasource id") @RequestParam(required = false) final String dsId,
|
||||||
@ApiParam("api id") @RequestParam(required = false) final String apiId) throws MDStoreManagerException {
|
@ApiParam("api id") @RequestParam(required = false) final String apiId) throws MDStoreManagerException {
|
||||||
final String id = _createMDStore(format, layout, interpretation, dsName, dsId, apiId);
|
final String id = databaseUtils.createMDStore(format, layout, interpretation, dsName, dsId, apiId);
|
||||||
return mdstoreWithInfoRepository.findById(id).orElseThrow(() -> new MDStoreManagerException("MDStore not found"));
|
return databaseUtils.findMdStore(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Delete a mdstore by id")
|
||||||
private String _createMDStore(final String format,
|
@DeleteMapping("/mdstore/{mdId}")
|
||||||
final String layout,
|
public StatusResponse delete(@ApiParam("the id of the mdstore that will be deleted") @PathVariable final String mdId) throws MDStoreManagerException {
|
||||||
final String interpretation,
|
databaseUtils.deleteMdStore(mdId);
|
||||||
final String dsName,
|
return StatusResponse.DELETED;
|
||||||
final String dsId,
|
|
||||||
final String apiId) {
|
|
||||||
final MDStore md = MDStore.newInstance(format, layout, interpretation, dsName, dsId, apiId);
|
|
||||||
mdstoreRepository.save(md);
|
|
||||||
|
|
||||||
final MDStoreVersion v = MDStoreVersion.newInstance(md.getId(), false);
|
|
||||||
v.setLastUpdate(new Date());
|
|
||||||
mdstoreVersionRepository.save(v);
|
|
||||||
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
|
|
||||||
|
|
||||||
return md.getId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Return all the versions of a mdstore")
|
||||||
@ApiOperation(value = "Delete a mdstore by id")
|
@GetMapping("/mdstore/{mdId}/versions")
|
||||||
@RequestMapping(value = "/mdstore/{mdId}", method = RequestMethod.DELETE)
|
public Iterable<MDStoreVersion> listVersions(@PathVariable final String mdId) throws MDStoreManagerException {
|
||||||
public void delete(@ApiParam("the id of the mdstore that will be deleted") @PathVariable final String mdId) throws MDStoreManagerException {
|
return databaseUtils.listVersions(mdId);
|
||||||
if (!mdstoreRepository.existsById(mdId)) { throw new NoContentException("On delete MDStore not found " + "[" + mdId + "]"); }
|
|
||||||
|
|
||||||
if (mdstoreVersionRepository.countByMdstoreAndReadCountGreaterThan(mdId,
|
|
||||||
0) > 0) { throw new MDStoreManagerException("Read transactions found on mdstore : " + mdId); }
|
|
||||||
|
|
||||||
if (mdstoreVersionRepository.countByMdstoreAndWriting(mdId,
|
|
||||||
true) > 0) { throw new MDStoreManagerException("Write transactions found on mdstore : " + mdId); }
|
|
||||||
|
|
||||||
mdstoreCurrentVersionRepository.deleteById(mdId);
|
|
||||||
mdstoreVersionRepository.deleteByMdstore(mdId);
|
|
||||||
mdstoreRepository.deleteById(mdId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Create a new preliminary version of a mdstore")
|
||||||
@ApiOperation(value = "Return all the versions of a mdstore")
|
@GetMapping("/mdstore/{mdId}/newVersion")
|
||||||
@RequestMapping(value = "/mdstore/{mdId}/versions", method = RequestMethod.GET)
|
|
||||||
public List<MDStoreVersion> listVersions(@PathVariable final String mdId) throws MDStoreManagerException {
|
|
||||||
return Lists.newArrayList(mdstoreVersionRepository.findByMdstore(mdId));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
@ApiOperation(value = "Create a new preliminary version of a mdstore")
|
|
||||||
@RequestMapping(value = "/mdstore/{mdId}/newVersion", method = RequestMethod.GET)
|
|
||||||
public MDStoreVersion prepareNewVersion(@ApiParam("the id of the mdstore for which will be created a new version") @PathVariable final String mdId) {
|
public MDStoreVersion prepareNewVersion(@ApiParam("the id of the mdstore for which will be created a new version") @PathVariable final String mdId) {
|
||||||
final MDStoreVersion v = MDStoreVersion.newInstance(mdId, true);
|
return databaseUtils.prepareMdStoreVersion(mdId);
|
||||||
mdstoreVersionRepository.save(v);
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Promote a preliminary version to current")
|
||||||
@ApiOperation(value = "Promote a preliminary version to current")
|
@GetMapping("/version/{versionId}/commit/{size}")
|
||||||
@RequestMapping(value = "/version/{versionId}/commit/{size}", method = RequestMethod.GET)
|
public MDStoreVersion commitVersion(@ApiParam("the id of the version that will be promoted to the current version") @PathVariable final String versionId,
|
||||||
public void commitVersion(@ApiParam("the id of the version that will be promoted to the current version") @PathVariable final String versionId,
|
@ApiParam("the size of the new current mdstore") @PathVariable final long size) throws MDStoreManagerException {
|
||||||
@ApiParam(value = "the size of the new current mdstore") @PathVariable final long size) throws NoContentException {
|
return databaseUtils.commitMdStoreVersion(versionId, size);
|
||||||
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new NoContentException("Invalid version: " + versionId));
|
|
||||||
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
|
|
||||||
v.setWriting(false);
|
|
||||||
v.setSize(size);
|
|
||||||
v.setLastUpdate(new Date());
|
|
||||||
mdstoreVersionRepository.save(v);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "Return all the expired versions of all the mdstores")
|
@ApiOperation("Return all the expired versions of all the mdstores")
|
||||||
@RequestMapping(value = "/versions/expired", method = RequestMethod.GET)
|
@GetMapping("/versions/expired")
|
||||||
public List<String> listExpiredVersions() {
|
public List<String> listExpiredVersions() {
|
||||||
return jdbcTemplate.queryForList(
|
return databaseUtils.listExpiredVersions();
|
||||||
"select v.id from mdstore_versions v left outer join mdstore_current_versions cv on (v.id = cv.current_version) where v.writing = false and v.readcount = 0 and cv.mdstore is null;",
|
|
||||||
String.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Delete a mdstore version")
|
||||||
@ApiOperation(value = "Delete a mdstore version")
|
@DeleteMapping("/version/{versionId}")
|
||||||
@RequestMapping(value = "/version/{versionId}", method = RequestMethod.DELETE)
|
public StatusResponse deleteVersion(@ApiParam("the id of the version that has to be deleted") @PathVariable final String versionId,
|
||||||
public void deleteVersion(@ApiParam("the id of the version that has to be deleted") @PathVariable final String versionId,
|
@ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force)
|
||||||
@ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force)
|
throws MDStoreManagerException {
|
||||||
throws MDStoreManagerException {
|
databaseUtils.deleteMdStoreVersions(force, versionId);
|
||||||
_deleteVersion(versionId, force);
|
return StatusResponse.DELETED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Decrease the read count of a mdstore version")
|
||||||
@ApiOperation(value = "Decrease the read count of a mdstore version")
|
@GetMapping("/version/{versionId}/endReading")
|
||||||
@RequestMapping(value = "/version/{versionId}/endReading", method = RequestMethod.GET)
|
|
||||||
public MDStoreVersion endReading(@ApiParam("the id of the version that has been completely read") @PathVariable final String versionId)
|
public MDStoreVersion endReading(@ApiParam("the id of the version that has been completely read") @PathVariable final String versionId)
|
||||||
throws MDStoreManagerException {
|
throws MDStoreManagerException {
|
||||||
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
|
return databaseUtils.endReading(versionId);
|
||||||
v.setReadCount(v.getReadCount() - 1);
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@ApiOperation("Delete multiple mdstore versions")
|
||||||
@ApiOperation(value = "Delete multiple mdstore versions")
|
@PostMapping("/versions/deleteList")
|
||||||
@RequestMapping(value = "/versions/deleteList", method = RequestMethod.POST)
|
public StatusResponse deleteVersions(@ApiParam("the list of ids of the versions that have to be deleted") @RequestBody final String[] versions,
|
||||||
public void deleteVersions(@ApiParam("the list of ids of the versions that have to be deleted") @RequestBody final List<String> versions,
|
@ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force)
|
||||||
@ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force)
|
throws MDStoreManagerException {
|
||||||
throws MDStoreManagerException {
|
databaseUtils.deleteMdStoreVersions(force, versions);
|
||||||
for (final String v : versions) {
|
return StatusResponse.DELETED;
|
||||||
_deleteVersion(v, force);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _deleteVersion(final String versionId, final boolean force) throws MDStoreManagerException {
|
|
||||||
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
|
|
||||||
|
|
||||||
if (mdstoreCurrentVersionRepository
|
|
||||||
.countByCurrentVersion(versionId) > 0) { throw new MDStoreManagerException("I cannot delete this version because it is the current version"); }
|
|
||||||
|
|
||||||
if (!force) {
|
|
||||||
if (v.isWriting()) { throw new MDStoreManagerException("I cannot delete this version because it is in write mode"); }
|
|
||||||
if (v.getReadCount() > 0) { throw new MDStoreManagerException("I cannot delete this version because it is in read mode"); }
|
|
||||||
}
|
|
||||||
|
|
||||||
mdstoreVersionRepository.delete(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package eu.dnetlib.data.mdstore.manager.controller;
|
||||||
|
|
||||||
|
public class SizeResponse {
|
||||||
|
|
||||||
|
private long size = 0;
|
||||||
|
|
||||||
|
public SizeResponse() {}
|
||||||
|
|
||||||
|
public SizeResponse(final long size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSize(final long size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package eu.dnetlib.data.mdstore.manager.controller;
|
||||||
|
|
||||||
|
public class StatusResponse {
|
||||||
|
|
||||||
|
public static final StatusResponse DELETED = new StatusResponse("DELETED");
|
||||||
|
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
public StatusResponse() {}
|
||||||
|
|
||||||
|
public StatusResponse(final String status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(final String status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,36 +0,0 @@
|
||||||
package eu.dnetlib.data.mdstore.manager.exceptions;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
|
|
||||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
|
||||||
public class NoContentException extends MDStoreManagerException{
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = -278683963172303773L;
|
|
||||||
|
|
||||||
public NoContentException() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public NoContentException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
|
||||||
super(message, cause, enableSuppression, writableStackTrace);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NoContentException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NoContentException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NoContentException(Throwable cause) {
|
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -10,9 +10,9 @@ public interface MDStoreVersionRepository extends JpaRepository<MDStoreVersion,
|
||||||
|
|
||||||
void deleteByMdstore(String id);
|
void deleteByMdstore(String id);
|
||||||
|
|
||||||
int countByMdstoreAndWriting(String id, boolean b);
|
long countByMdstoreAndWriting(String id, boolean b);
|
||||||
|
|
||||||
int countByMdstoreAndReadCountGreaterThan(String id, int count);
|
long countByMdstoreAndReadCountGreaterThan(String id, int count);
|
||||||
|
|
||||||
Iterable<MDStoreVersion> findByMdstore(String mdId);
|
Iterable<MDStoreVersion> findByMdstore(String mdId);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,154 @@
|
||||||
|
package eu.dnetlib.data.mdstore.manager.utils;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStore;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreCurrentVersion;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.repository.MDStoreCurrentVersionRepository;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.repository.MDStoreRepository;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.repository.MDStoreVersionRepository;
|
||||||
|
import eu.dnetlib.data.mdstore.manager.repository.MDStoreWithInfoRepository;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class DatabaseUtils {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MDStoreRepository mdstoreRepository;
|
||||||
|
@Autowired
|
||||||
|
private MDStoreVersionRepository mdstoreVersionRepository;
|
||||||
|
@Autowired
|
||||||
|
private MDStoreCurrentVersionRepository mdstoreCurrentVersionRepository;
|
||||||
|
@Autowired
|
||||||
|
private MDStoreWithInfoRepository mdstoreWithInfoRepository;
|
||||||
|
@Autowired
|
||||||
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public Iterable<MDStoreWithInfo> listMdStores() {
|
||||||
|
return mdstoreWithInfoRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> listMdStoreIDs() {
|
||||||
|
return mdstoreRepository.findAll().stream().map(MDStore::getId).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public long countMdStores() {
|
||||||
|
return mdstoreRepository.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterable<MDStoreVersion> listVersions(final String mdId) {
|
||||||
|
return mdstoreVersionRepository.findByMdstore(mdId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> listExpiredVersions() {
|
||||||
|
return jdbcTemplate
|
||||||
|
.queryForList("select v.id from mdstore_versions v left outer join mdstore_current_versions cv on (v.id = cv.current_version) where v.writing = false and v.readcount = 0 and cv.mdstore is null;", String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MDStoreWithInfo findMdStore(final String mdId) throws MDStoreManagerException {
|
||||||
|
return mdstoreWithInfoRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore: " + mdId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public String createMDStore(final String format,
|
||||||
|
final String layout,
|
||||||
|
final String interpretation,
|
||||||
|
final String dsName,
|
||||||
|
final String dsId,
|
||||||
|
final String apiId) {
|
||||||
|
|
||||||
|
final MDStore md = MDStore.newInstance(format, layout, interpretation, dsName, dsId, apiId);
|
||||||
|
mdstoreRepository.save(md);
|
||||||
|
|
||||||
|
final MDStoreVersion v = MDStoreVersion.newInstance(md.getId(), false);
|
||||||
|
v.setLastUpdate(new Date());
|
||||||
|
mdstoreVersionRepository.save(v);
|
||||||
|
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
|
||||||
|
|
||||||
|
return md.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteMdStore(final String mdId) throws MDStoreManagerException {
|
||||||
|
if (!mdstoreRepository.existsById(mdId)) { throw new MDStoreManagerException("On delete MDStore not found " + "[" + mdId + "]"); }
|
||||||
|
|
||||||
|
if (mdstoreVersionRepository.countByMdstoreAndReadCountGreaterThan(mdId, 0) > 0) {
|
||||||
|
throw new MDStoreManagerException("Read transactions found on mdstore : " + mdId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mdstoreVersionRepository.countByMdstoreAndWriting(mdId, true) > 0) {
|
||||||
|
throw new MDStoreManagerException("Write transactions found on mdstore : " + mdId);
|
||||||
|
}
|
||||||
|
|
||||||
|
mdstoreCurrentVersionRepository.deleteById(mdId);
|
||||||
|
mdstoreVersionRepository.deleteByMdstore(mdId);
|
||||||
|
mdstoreRepository.deleteById(mdId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public MDStoreVersion startReading(final String mdId) throws MDStoreManagerException {
|
||||||
|
final MDStoreCurrentVersion cv =
|
||||||
|
mdstoreCurrentVersionRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore: " + mdId));
|
||||||
|
final MDStoreVersion v = mdstoreVersionRepository.findById(cv.getCurrentVersion())
|
||||||
|
.orElseThrow(() -> new MDStoreManagerException("Missing version: " + cv.getCurrentVersion()));
|
||||||
|
v.setReadCount(v.getReadCount() + 1);
|
||||||
|
mdstoreVersionRepository.save(v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public MDStoreVersion endReading(final String versionId) throws MDStoreManagerException {
|
||||||
|
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
|
||||||
|
v.setReadCount(v.getReadCount() - 1);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public MDStoreVersion prepareMdStoreVersion(final String mdId) {
|
||||||
|
final MDStoreVersion v = MDStoreVersion.newInstance(mdId, true);
|
||||||
|
mdstoreVersionRepository.save(v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public MDStoreVersion commitMdStoreVersion(final String versionId, final long size) throws MDStoreManagerException {
|
||||||
|
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Invalid version: " + versionId));
|
||||||
|
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
|
||||||
|
v.setWriting(false);
|
||||||
|
v.setSize(size);
|
||||||
|
v.setLastUpdate(new Date());
|
||||||
|
mdstoreVersionRepository.save(v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteMdStoreVersions(final boolean force, final String... versions) throws MDStoreManagerException {
|
||||||
|
|
||||||
|
for (final String versionId : versions) {
|
||||||
|
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
|
||||||
|
|
||||||
|
if (mdstoreCurrentVersionRepository
|
||||||
|
.countByCurrentVersion(versionId) > 0) {
|
||||||
|
throw new MDStoreManagerException("I cannot delete this version because it is the current version");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!force) {
|
||||||
|
if (v.isWriting()) { throw new MDStoreManagerException("I cannot delete this version because it is in write mode"); }
|
||||||
|
if (v.getReadCount() > 0) { throw new MDStoreManagerException("I cannot delete this version because it is in read mode"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
mdstoreVersionRepository.delete(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue