added view on mdstore and transaction tables

This commit is contained in:
Enrico Ottonello 2019-03-19 13:34:40 +01:00
parent cefcb38e62
commit 46bc3e1f53
9 changed files with 297 additions and 21 deletions

View File

@ -0,0 +1,30 @@
package eu.dnetlib.data.mdstore.manager;
public class MDStoreManagerException extends Exception{
/**
*
*/
private static final long serialVersionUID = -7503316126409002675L;
public MDStoreManagerException() {
super();
}
public MDStoreManagerException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public MDStoreManagerException(String message, Throwable cause) {
super(message, cause);
}
public MDStoreManagerException(String message) {
super(message);
}
public MDStoreManagerException(Throwable cause) {
super(cause);
}
}

View File

@ -5,56 +5,74 @@ import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.data.mdstore.manager.MDStoreManagerException;
import eu.dnetlib.data.mdstore.manager.model.MDStore; import eu.dnetlib.data.mdstore.manager.model.MDStore;
import eu.dnetlib.data.mdstore.manager.model.MDStoreWithInfo;
import eu.dnetlib.data.mdstore.manager.model.Transaction;
import eu.dnetlib.data.mdstore.manager.repository.MDStoreRepository; import eu.dnetlib.data.mdstore.manager.repository.MDStoreRepository;
import eu.dnetlib.data.mdstore.manager.repository.MDStoreWithInfoRepository;
import eu.dnetlib.data.mdstore.manager.repository.TransactionRepository;
@RestController @RestController
@RequestMapping("/api/mdstores") @RequestMapping("/api/mdstores")
public class MDStoreController { public class MDStoreController {
@Autowired @Autowired
private MDStoreRepository repo; private MDStoreRepository mdstoreRepository;
@Autowired
private TransactionRepository transactionRepository;
@Autowired
private MDStoreWithInfoRepository mdstoreWithInfoRepository;
@RequestMapping(value = "/", method = RequestMethod.GET) @RequestMapping(value = "/", method = RequestMethod.GET)
public final List<MDStore> find() { public final Iterable<MDStoreWithInfo> find() {
return repo.findAll(); return mdstoreWithInfoRepository.findAll();
} }
@RequestMapping(value = "/identifiers", method = RequestMethod.GET) @RequestMapping(value = "/identifiers", method = RequestMethod.GET)
public final List<String> findIdentifiers() { public final List<String> findIdentifiers() {
return repo.findAll().stream().map(MDStore::getId).collect(Collectors.toList()); return mdstoreRepository.findAll().stream().map(MDStore::getId).collect(Collectors.toList());
} }
@RequestMapping(value = "/{id}", method = RequestMethod.GET) @RequestMapping(value = "/byId/{id}", method = RequestMethod.GET)
public final MDStore get(@PathVariable final String id) { public final MDStoreWithInfo get(@PathVariable final String id) {
return repo.findById(id).orElse(null); return mdstoreWithInfoRepository.findById(id).orElse(null);
} }
@RequestMapping(value = "/count", method = RequestMethod.GET) @RequestMapping(value = "/count", method = RequestMethod.GET)
public final long count() { public final long count() {
return repo.count(); return mdstoreRepository.count();
} }
@RequestMapping(value = "/", method = RequestMethod.PUT) @RequestMapping(value = "/new/{format}/{layout}/{interpretation}", method = RequestMethod.PUT)
public final void save(@RequestBody final MDStore entity) { public MDStoreWithInfo createMDStore(
repo.save(entity); @PathVariable String format,
@PathVariable String layout,
@PathVariable String interpretation,
@RequestParam(required=false) String dsId,
@RequestParam(required=false) String apiId) throws MDStoreManagerException {
final MDStore md = MDStore.newInstance(dsId, apiId, format, layout, interpretation);
mdstoreRepository.save(md);
final Transaction t = Transaction.newInstance(md.getId());
t.setCurrent(true);
transactionRepository.save(t);
return mdstoreWithInfoRepository.findById(md.getId()).orElseThrow(() -> new MDStoreManagerException("MDStore not found"));
} }
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable final String id) { public void delete(@PathVariable final String id) throws MDStoreManagerException {
repo.deleteById(id); if (transactionRepository.countByMdstoreAndActive(id, true) == 0) {
} transactionRepository.deleteByMdstore(id);
mdstoreRepository.deleteById(id);
@RequestMapping(value = "/{id}", method = RequestMethod.POST) } else {
public void update(@PathVariable final String id, @RequestBody final MDStore entity) { throw new MDStoreManagerException("Active transactions found on mdstore : " + id);
if (repo.existsById(id)) {
entity.setId(id);
repo.save(entity);
} }
} }

View File

@ -1,6 +1,7 @@
package eu.dnetlib.data.mdstore.manager.model; package eu.dnetlib.data.mdstore.manager.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.UUID;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -85,6 +86,21 @@ public class MDStore implements Serializable {
this.apiId = apiId; this.apiId = apiId;
} }
public static MDStore newInstance(final String format, final String layout, final String interpretation) {
return newInstance(null, null, format, layout, interpretation);
}
public static MDStore newInstance(final String dsId, final String apiId, final String format, final String layout, final String interpretation) {
final MDStore md = new MDStore();
md.setId("md-" + UUID.randomUUID());
md.setDatasourceId(dsId);
md.setApiId(apiId);
md.setFormat(format);
md.setLayout(layout);
md.setInterpretation(interpretation);
return md;
}

View File

@ -0,0 +1,124 @@
package eu.dnetlib.data.mdstore.manager.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "mdstores_with_info")
public class MDStoreWithInfo implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8445784770687571492L;
@Id
@Column(name = "id")
private String id;
@Column(name = "format")
private String format;
@Column(name = "layout")
private String layout;
@Column(name = "interpretation")
private String interpretation;
@Column(name = "datasource_id")
private String datasourceId;
@Column(name = "api_id")
private String apiId ;
@Column(name = "current_version")
private String currentVersion;
@Column(name = "lastupdate")
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;
@Column(name = "size")
private int size;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getLayout() {
return layout;
}
public void setLayout(String layout) {
this.layout = layout;
}
public String getInterpretation() {
return interpretation;
}
public void setInterpretation(String interpretation) {
this.interpretation = interpretation;
}
public String getDatasourceId() {
return datasourceId;
}
public void setDatasourceId(String datasourceId) {
this.datasourceId = datasourceId;
}
public String getApiId() {
return apiId;
}
public void setApiId(String apiId) {
this.apiId = apiId;
}
public String getCurrentVersion() {
return currentVersion;
}
public void setCurrentVersion(String currentVersion) {
this.currentVersion = currentVersion;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}

View File

@ -25,9 +25,15 @@ public class Transaction implements Serializable{
@Column(name = "mdstore") @Column(name = "mdstore")
private String mdstore; private String mdstore;
@Column(name = "current")
private boolean current;
@Column(name = "active") @Column(name = "active")
private boolean active; private boolean active;
@Column(name = "readcount")
private int readCount;
@Column(name = "lastupdate") @Column(name = "lastupdate")
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate; private Date lastUpdate;
@ -51,6 +57,14 @@ public class Transaction implements Serializable{
this.mdstore = mdstore; this.mdstore = mdstore;
} }
public boolean isCurrent() {
return current;
}
public void setCurrent(boolean current) {
this.current = current;
}
public boolean isActive() { public boolean isActive() {
return active; return active;
} }
@ -59,6 +73,14 @@ public class Transaction implements Serializable{
this.active = active; this.active = active;
} }
public int getReadCount() {
return readCount;
}
public void setReadCount(int readCount) {
this.readCount = readCount;
}
public Date getLastUpdate() { public Date getLastUpdate() {
return lastUpdate; return lastUpdate;
} }
@ -75,5 +97,18 @@ public class Transaction implements Serializable{
this.size = size; this.size = size;
} }
public static Transaction newInstance(final String mdId) {
final Transaction t = new Transaction();
t.setId(mdId + "-" + new Date().getTime());
t.setMdstore(mdId);
t.setLastUpdate(null);
t.setActive(false);
t.setCurrent(false);
t.setReadCount(0);
t.setSize(0);
return t;
}
} }

View File

@ -0,0 +1,10 @@
package eu.dnetlib.data.mdstore.manager.repository;
import org.springframework.stereotype.Repository;
import eu.dnetlib.data.mdstore.manager.model.MDStoreWithInfo;
@Repository
public interface MDStoreWithInfoRepository extends ReadOnlyRepository<MDStoreWithInfo, String> {
}

View File

@ -0,0 +1,18 @@
package eu.dnetlib.data.mdstore.manager.repository;
import java.util.Optional;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
@NoRepositoryBean
public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
long count();
}

View File

@ -8,4 +8,7 @@ import eu.dnetlib.data.mdstore.manager.model.Transaction;
@Repository @Repository
public interface TransactionRepository extends JpaRepository<Transaction, String> { public interface TransactionRepository extends JpaRepository<Transaction, String> {
void deleteByMdstore(String id);
int countByMdstoreAndActive(String id, boolean active);
} }

View File

@ -1,3 +1,7 @@
DROP VIEW IF EXISTS mdstores_with_info;
DROP TABLE IF EXISTS transactions;
DROP TABLE IF EXISTS mdstores;
CREATE TABLE mdstores ( CREATE TABLE mdstores (
id text PRIMARY KEY, id text PRIMARY KEY,
format text, format text,
@ -10,7 +14,25 @@ CREATE TABLE mdstores (
CREATE TABLE transactions ( CREATE TABLE transactions (
id text PRIMARY KEY, id text PRIMARY KEY,
mdstore text REFERENCES mdstores(id), mdstore text REFERENCES mdstores(id),
current boolean,
active boolean, active boolean,
readcount int,
lastupdate timestamp, lastupdate timestamp,
size int size int
); );
CREATE VIEW mdstores_with_info AS SELECT
md.id AS id,
md.format AS format,
md.layout AS layout,
md.interpretation AS interpretation,
md.datasource_id AS datasource_id,
md.api_id AS api_id,
t.id AS current_version,
t.lastupdate AS lastupdate,
t.size AS size
FROM
mdstores md
LEFT OUTER JOIN transactions t ON (md.id = t.mdstore)
WHERE
t.current = TRUE;