protocols

This commit is contained in:
Michele Artini 2023-01-04 15:49:56 +01:00
parent d54ae4ac64
commit dbb304a46a
14 changed files with 457 additions and 0 deletions

View File

@ -57,6 +57,8 @@ public class MainController {
return "redirect:vocs";
} else if (type.equalsIgnoreCase("context")) {
return "redirect:contexts";
} else if (type.equalsIgnoreCase("protocol")) {
return "redirect:protocols";
} else {
final Optional<ResourceType> restype = resourceTypeRepository.findById(type);
if (restype.isPresent() && restype.get().isSimple()) {
@ -74,6 +76,9 @@ public class MainController {
@GetMapping("/contexts")
public void contexts() {}
@GetMapping("/protocols")
public void protocols() {}
@GetMapping("/wf_history")
public void wfHistory(final ModelMap map,
@RequestParam(required = false, defaultValue = "-1") final Long from,

View File

@ -0,0 +1,20 @@
package eu.dnetlib.is.protocol;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import eu.dnetlib.common.controller.AbstractDnetController;
public class AbstractProtocolController extends AbstractDnetController {
@Autowired
protected ProtocolService protocolService;
@GetMapping("/")
public List<ProtocolDesc> listProtocols() {
return protocolService.listProtocols();
}
}

View File

@ -0,0 +1,27 @@
package eu.dnetlib.is.protocol;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ajax/protocols")
public class ProtocolAjaxController extends AbstractProtocolController {
@DeleteMapping("/{protocol}")
public List<ProtocolDesc> deleteProtocol(@PathVariable final String protocol) {
protocolService.deleteProtocols(protocol);
return protocolService.listProtocols();
}
@PostMapping("/{protocol}/terms")
public List<ProtocolDesc> saveTerm(@PathVariable final ProtocolDesc protocol) {
protocolService.saveProtocol(protocol);
return protocolService.listProtocols();
}
}

View File

@ -0,0 +1,10 @@
package eu.dnetlib.is.protocol;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/protocols")
public class ProtocolApiController extends AbstractProtocolController {
}

View File

@ -0,0 +1,40 @@
package eu.dnetlib.is.protocol;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import eu.dnetlib.data.is.protocol.model.ProtocolParam;
public class ProtocolDesc implements Serializable {
private static final long serialVersionUID = -5697339401309977230L;
private String id;
private List<ProtocolParam> params = new ArrayList<>();
public ProtocolDesc() {}
public ProtocolDesc(final String id, final List<ProtocolParam> params) {
this.id = id;
this.params = params;
}
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public List<ProtocolParam> getParams() {
return params;
}
public void setParams(final List<ProtocolParam> params) {
this.params = params;
}
}

View File

@ -0,0 +1,70 @@
package eu.dnetlib.is.protocol;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import eu.dnetlib.data.is.protocol.model.Protocol;
import eu.dnetlib.data.is.protocol.model.ProtocolParam;
import eu.dnetlib.data.is.protocol.repository.ProtocolParamRepository;
import eu.dnetlib.data.is.protocol.repository.ProtocolRepository;
@Service
public class ProtocolService {
@Autowired
private ProtocolRepository protocolRepository;
@Autowired
private ProtocolParamRepository protocolParamRepository;
private static final Log log = LogFactory.getLog(ProtocolService.class);
@Transactional
public List<ProtocolDesc> listProtocols() {
final Map<String, List<ProtocolParam>> params = protocolParamRepository.findAll()
.stream()
.collect(Collectors.groupingBy(ProtocolParam::getProtocol));
return protocolRepository.findAll()
.stream()
.map(Protocol::getId)
.map(id -> new ProtocolDesc(id, params.containsKey(id) ? params.get(id) : new ArrayList<>()))
.collect(Collectors.toList());
}
@Transactional
public void deleteProtocols(final String... ids) {
for (final String id : ids) {
protocolRepository.deleteById(id);
}
}
@Transactional
public void saveProtocol(final ProtocolDesc protocol) {
log.info("Saving protocol: " + protocol);
if (protocolRepository.existsById(protocol.getId())) {
protocolParamRepository.deleteByProtocol(protocol.getId());
} else {
protocolRepository.save(new Protocol(protocol.getId()));
}
for (final ProtocolParam p : protocol.getParams()) {
p.setProtocol(protocol.getId());
protocolParamRepository.save(p);
}
}
}

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head th:replace="fragments/mainParts.html :: htmlHeader('Harvesting Protocols')"></head>
<body ng-app="protocolsApp" ng-controller="protocolsController">
<nav th:replace="fragments/mainParts.html :: mainMenu('Harvesting Protocols')"></nav>
<div class="container-fluid">
<div class="row">
<div class="col">
{{protocols}}
</div>
</div>
</div>
</body>
<th:block th:replace="fragments/mainParts.html :: scripts"></th:block>
<script>
var app = angular.module('protocolsApp', []);
app.controller('protocolsController', function($scope, $http) {
$scope.protocols = [];
call_http_get($http, './ajax/protocols/?' + $.now(), function(res) {
$scope.protocols = res.data;
});
});
</script>
</html>

View File

@ -0,0 +1,34 @@
package eu.dnetlib.data.is.protocol.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "protocols")
public class Protocol implements Serializable {
private static final long serialVersionUID = -3448728311377943968L;
@Id
@Column(name = "id")
private String id;
public Protocol() {}
public Protocol(final String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
}

View File

@ -0,0 +1,89 @@
package eu.dnetlib.data.is.protocol.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name = "protocol_params")
@IdClass(ProtocolParamPK.class)
public class ProtocolParam implements Serializable {
private static final long serialVersionUID = -8190220047985766140L;
@Id
@Column(name = "protocol")
private String protocol;
@Id
@Column(name = "param_name")
private String name;
@Column(name = "param_label")
private String label;
@Enumerated(EnumType.STRING)
@Column(name = "param_type")
private ProtocolParamType type;
@Column(name = "optional")
private boolean optional;
@Column(name = "has_sel_function")
private boolean hasSelFunction;
public String getProtocol() {
return protocol;
}
public void setProtocol(final String protocol) {
this.protocol = protocol;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
public ProtocolParamType getType() {
return type;
}
public void setType(final ProtocolParamType type) {
this.type = type;
}
public boolean isOptional() {
return optional;
}
public void setOptional(final boolean optional) {
this.optional = optional;
}
public boolean isHasSelFunction() {
return hasSelFunction;
}
public void setHasSelFunction(final boolean hasSelFunction) {
this.hasSelFunction = hasSelFunction;
}
}

View File

@ -0,0 +1,47 @@
package eu.dnetlib.data.is.protocol.model;
import java.io.Serializable;
import java.util.Objects;
public class ProtocolParamPK implements Serializable {
private static final long serialVersionUID = 3126524893218480136L;
private String protocol;
private String name;
public String getProtocol() {
return protocol;
}
public void setProtocol(final String protocol) {
this.protocol = protocol;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hash(name, protocol);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof ProtocolParamPK)) { return false; }
final ProtocolParamPK other = (ProtocolParamPK) obj;
return Objects.equals(name, other.name) && Objects.equals(protocol, other.protocol);
}
@Override
public String toString() {
return String.format("ProtocolParamPK [protocol=%s, name=%s]", protocol, name);
}
}

View File

@ -0,0 +1,8 @@
package eu.dnetlib.data.is.protocol.model;
public enum ProtocolParamType {
TEXT,
LIST,
BOOLEAN,
INT
}

View File

@ -0,0 +1,14 @@
package eu.dnetlib.data.is.protocol.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.data.is.protocol.model.ProtocolParam;
import eu.dnetlib.data.is.protocol.model.ProtocolParamPK;
public interface ProtocolParamRepository extends JpaRepository<ProtocolParam, ProtocolParamPK> {
Iterable<ProtocolParam> findByProtocol(String protocol);
void deleteByProtocol(String protocol);
}

View File

@ -0,0 +1,9 @@
package eu.dnetlib.data.is.protocol.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.data.is.protocol.model.Protocol;
public interface ProtocolRepository extends JpaRepository<Protocol, String> {
}

View File

@ -17,6 +17,45 @@ CREATE TABLE vocabulary_terms (
CREATE INDEX ON vocabulary_terms (vocabulary);
CREATE TABLE protocols (
id text PRIMARY KEY
);
INSERT INTO protocols(id) VALUES ('oai'),('oai_sets'),('http'),('file'),('classpath'),('fileCSV'),('httpCSV'),('ftp'),('sftp'),('filesystem'),('files_from_metadata'),('files_from_mdstore'),('mongoDump'),('targz'),('zip'),('fileGzip'),('httpList'),('remoteMdstore');
CREATE TABLE protocol_params (
protocol text NOT NULL REFERENCES protocols(id) ON UPDATE CASCADE ON DELETE CASCADE,
param_name text NOT NULL,
param_label text NOT NULL,
param_type text NOT NULL DEFAULT 'TEXT',
optional boolean NOT NULL default false,
has_sel_function boolean NOT NULL default false,
PRIMARY KEY (protocol, param_name)
);
INSERT INTO protocol_params(protocol, param_name, param_label, param_type, optional, has_sel_function) VALUES
('oai', 'set', 'OAI set', 'LIST', true, true),
('oai', 'format', 'OAI Metadata Format', 'TEXT', false, false),
('http', 'splitOnElement', 'splitOnElement', 'TEXT', false, false),
('file', 'splitOnElement', 'splitOnElement', 'TEXT', false, false),
('classpath', 'splitOnElement', 'splitOnElement', 'TEXT', false, false),
('fileCSV', 'header', 'header', 'TEXT', false, false),
('fileCSV', 'separator', 'separator', 'TEXT', false, false),
('fileCSV', 'identifier', 'identifier', 'TEXT', false, false),
('fileCSV', 'quote', 'quote', 'TEXT', false, false),
('httpCSV', 'separator', 'separator', 'TEXT', false, false),
('httpCSV', 'identifier', 'identifier', 'TEXT', false, false),
('httpCSV', 'quote', 'quote', 'TEXT', false, false),
('ftp', 'username', 'username', 'TEXT', false, false),
('ftp', 'password', 'password', 'TEXT', false, false),
('ftp', 'recursive', 'recursive', 'BOOLEAN', false, false),
('ftp', 'extensions', 'extensions', 'LIST', false, false),
('sftp', 'username', 'username', 'TEXT', false, false),
('sftp', 'password', 'password', 'TEXT', true, false),
('sftp', 'authMethod', 'authMethod', 'TEXT', true, false),
('sftp', 'privateKeyPath', 'privateKeyPath', 'TEXT', true, false),
('sftp', 'port', 'port', 'TEXT', true, false),
('sftp', 'recursive', 'recursive', 'BOOLEAN', false, false),
('sftp', 'extensions', 'extensions', 'LIST', false, false);
-- Contexts
CREATE TABLE contexts (
@ -130,4 +169,12 @@ CREATE VIEW resource_types_view AS (
count(*) AS count,
false AS simple
FROM contexts
) UNION ALL (
SELECT
'protocol' AS id,
'Protocols' AS name,
'text/plain' AS content_type,
count(*) AS count,
false AS simple
FROM protocols
);