contentType

This commit is contained in:
Michele Artini 2022-11-24 10:19:36 +01:00
parent 4ff77964ca
commit 7e7f6ff746
9 changed files with 105 additions and 73 deletions

View File

@ -3,6 +3,7 @@ package eu.dnetlib.is;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -44,7 +45,7 @@ public class MainController {
if (restype.isPresent() && restype.get().isSimple()) { if (restype.isPresent() && restype.get().isSimple()) {
map.addAttribute("type", restype.get()); map.addAttribute("type", restype.get());
} else { } else {
map.addAttribute("type", new ResourceType("not_present", "???", 0)); map.addAttribute("type", new ResourceType("not_present", "???", MediaType.TEXT_PLAIN_VALUE, 0));
} }
return "simpleResources"; return "simpleResources";
} }

View File

@ -7,14 +7,15 @@ import javax.transaction.Transactional;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node; import org.dom4j.Node;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.resource.model.SimpleResource;
import eu.dnetlib.is.resource.repository.SimpleResourceRepository; import eu.dnetlib.is.resource.repository.SimpleResourceRepository;
import eu.dnetlib.is.util.InformationServiceException;
import eu.dnetlib.is.vocabulary.model.Synonym; import eu.dnetlib.is.vocabulary.model.Synonym;
import eu.dnetlib.is.vocabulary.model.Vocabulary; import eu.dnetlib.is.vocabulary.model.Vocabulary;
import eu.dnetlib.is.vocabulary.model.VocabularyTerm; import eu.dnetlib.is.vocabulary.model.VocabularyTerm;
@ -34,47 +35,65 @@ public class OldProfilesImporter {
private VocabularyTermRepository vocabularyTermRepository; private VocabularyTermRepository vocabularyTermRepository;
@Transactional @Transactional
public SimpleResource importSimpleResource(final String xml) throws Exception { public SimpleResource importSimpleResource(final String xml) throws InformationServiceException {
final SAXReader reader = new SAXReader(); final SAXReader reader = new SAXReader();
final Document doc = reader.read(new StringReader(xml));
final String id = StringUtils.substringBefore(doc.valueOf("//RESOURCE_IDENTIFIER/@value"), "_"); try {
final Date now = new Date(); final Document doc = reader.read(new StringReader(xml));
final SimpleResource res = new SimpleResource(); final String id = StringUtils.substringBefore(doc.valueOf("//RESOURCE_IDENTIFIER/@value"), "_");
res.setId(id); final Date now = new Date();
res.setCreationDate(now);
res.setModificationDate(now);
res.setContentType(MediaType.APPLICATION_XML_VALUE);
String resContent; final SimpleResource res = new SimpleResource();
switch (doc.valueOf("//RESOURCE_TYPE/@value")) { res.setId(id);
case "CleanerDSResourceType": res.setCreationDate(now);
res.setType("cleaning_rule"); res.setModificationDate(now);
res.setName(doc.valueOf("//CLEANER_NAME"));
res.setDescription(doc.valueOf("//CLEANER_DESCRIPTION")); String resContent;
resContent = doc.selectSingleNode("//CLEANER_RULES").asXML(); switch (doc.valueOf("//RESOURCE_TYPE/@value")) {
break; case "CleanerDSResourceType":
case "TransformationRuleDSResourceType": res.setType("cleaning_rule");
res.setType("transformation_rule"); res.setName(doc.valueOf("//CLEANER_NAME"));
res.setName(doc.valueOf("//SCRIPT/TITLE")); res.setDescription(doc.valueOf("//CLEANER_DESCRIPTION"));
res.setDescription(""); resContent = doc.selectSingleNode("//CLEANER_RULES").asXML();
resContent = doc.selectSingleNode("//SCRIPT/CODE").asXML(); break;
break; case "TransformationRuleDSResourceType":
case "HadoopJobConfigurationDSResourceType": res.setName(doc.valueOf("//SCRIPT/TITLE"));
res.setType("hadoop_job_configuration"); res.setDescription("");
res.setName(doc.valueOf("//HADOOP_JOB/@name")); if (doc.selectNodes("//*[local-name() = 'stylesheet']").size() > 0) {
res.setDescription(doc.valueOf("//HADOOP_JOB/DESCRIPTION")); res.setType("transformation_rule_xslt");
resContent = doc.selectSingleNode("//HADOOP_JOB").asXML(); resContent = doc.selectSingleNode("//*[local-name() = 'stylesheet']").asXML();
break; } else {
default: final String code = doc.valueOf("//SCRIPT/CODE").trim();
throw new Exception("Invalid resource type: " + doc.valueOf("//RESOURCE_TYPE/@value")); try {
final Document xsltDoc = reader.read(new StringReader(code));
res.setType("transformation_rule_xslt");
resContent = xsltDoc.asXML();
} catch (final DocumentException e) {
e.printStackTrace();
res.setType("transformation_rule_legacy");
resContent = code;
}
}
break;
case "HadoopJobConfigurationDSResourceType":
res.setType("hadoop_job_configuration");
res.setName(doc.valueOf("//HADOOP_JOB/@name"));
res.setDescription(doc.valueOf("//HADOOP_JOB/DESCRIPTION"));
resContent = doc.selectSingleNode("//HADOOP_JOB").asXML();
break;
default:
throw new InformationServiceException("Invalid resource type: " + doc.valueOf("//RESOURCE_TYPE/@value"));
}
simpleResourceRepository.save(res);
simpleResourceRepository.setContentById(id, resContent);
return res;
} catch (final Exception e) {
throw new InformationServiceException("Error parsing file", e);
} }
simpleResourceRepository.save(res);
simpleResourceRepository.setContentById(id, resContent);
return res;
} }
@Transactional @Transactional

View File

@ -73,15 +73,14 @@ public class ResourcesRestController {
@GetMapping("/{id}/content") @GetMapping("/{id}/content")
public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException { public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
final SimpleResource sr = simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found")); final String ctype = simpleResourceRepository.findContentTypeById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
final String content = simpleResourceRepository.findContentById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
res.setCharacterEncoding(StandardCharsets.UTF_8.name()); res.setCharacterEncoding(StandardCharsets.UTF_8.name());
res.setContentType(sr.getContentType()); res.setContentType(ctype);
simpleResourceRepository.getContentById(id);
try { try {
IOUtils.write(simpleResourceRepository.getContentById(id), res.getOutputStream(), StandardCharsets.UTF_8.name()); IOUtils.write(content, res.getOutputStream(), StandardCharsets.UTF_8.name());
} catch (final IOException e) { } catch (final IOException e) {
throw new InformationServiceException("Error retrieving content", e); throw new InformationServiceException("Error retrieving content", e);
} }

View File

@ -30,7 +30,6 @@ public class DatabaseUtils {
res.setName(name); res.setName(name);
res.setType(type); res.setType(type);
res.setDescription(description); res.setDescription(description);
res.setContentType(ctype);
res.setCreationDate(now); res.setCreationDate(now);
res.setModificationDate(now); res.setModificationDate(now);

View File

@ -25,7 +25,7 @@
<div class="card mb-4" ng-repeat="r in resources|filter:resFilter"> <div class="card mb-4" ng-repeat="r in resources|filter:resFilter">
<div class="card-body small"> <div class="card-body small">
<span class="badge badge-primary float-right">{{r.contentType}}</span> <span class="badge badge-primary float-right" th:text="${type.contentType}"></span>
<h5 class="card-title" title="{{r.id}}">{{r.name}}</h5> <h5 class="card-title" title="{{r.id}}">{{r.name}}</h5>
<p class="card-text">{{r.description}}</p> <p class="card-text">{{r.description}}</p>
<p class="text-muted small"> <p class="text-muted small">
@ -114,7 +114,7 @@
<div class="modal-body"> <div class="modal-body">
<form> <form>
<div class="form-group"> <div class="form-group">
<label>Suggested format: <i>{{tmpRes.contentType}}</i></label> <label>Format: <i th:text="${type.contentType}"></i></label>
<input type="file" class="form-control-file" /> <input type="file" class="form-control-file" />
</div> </div>
</form> </form>

View File

@ -20,6 +20,9 @@ public class ResourceType implements Serializable {
@Column(name = "name") @Column(name = "name")
private String name; private String name;
@Column(name = "content_type")
private String contentType;
@Column(name = "count") @Column(name = "count")
private long count; private long count;
@ -28,10 +31,11 @@ public class ResourceType implements Serializable {
public ResourceType() {} public ResourceType() {}
public ResourceType(final String id, final String name, final long count) { public ResourceType(final String id, final String name, final String contentType, final long count) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.count = count; this.count = count;
this.contentType = contentType;
this.simple = true; this.simple = true;
} }
@ -51,6 +55,14 @@ public class ResourceType implements Serializable {
this.name = name; this.name = name;
} }
public String getContentType() {
return contentType;
}
public void setContentType(final String contentType) {
this.contentType = contentType;
}
public long getCount() { public long getCount() {
return count; return count;
} }

View File

@ -26,9 +26,6 @@ public class SimpleResource implements Serializable {
@Column(name = "type") @Column(name = "type")
private String type; private String type;
@Column(name = "content_type")
private String contentType;
@Column(name = "description") @Column(name = "description")
private String description; private String description;
@ -64,14 +61,6 @@ public class SimpleResource implements Serializable {
this.type = type; this.type = type;
} }
public String getContentType() {
return contentType;
}
public void setContentType(final String contentType) {
this.contentType = contentType;
}
public String getDescription() { public String getDescription() {
return description; return description;
} }

View File

@ -1,6 +1,7 @@
package eu.dnetlib.is.resource.repository; package eu.dnetlib.is.resource.repository;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
@ -11,11 +12,15 @@ import eu.dnetlib.is.resource.model.SimpleResource;
public interface SimpleResourceRepository extends JpaRepository<SimpleResource, String> { public interface SimpleResourceRepository extends JpaRepository<SimpleResource, String> {
@Query(value = "select content from resources where id = ?1", nativeQuery = true) @Query(value = "select content from resources where id = ?1", nativeQuery = true)
String getContentById(String id); Optional<String> findContentById(String id);
@Modifying @Modifying
@Query(value = "update resources set content = ?2 where id = ?1", nativeQuery = true) @Query(value = "update resources set content = ?2 where id = ?1", nativeQuery = true)
void setContentById(String id, String content); void setContentById(String id, String content);
List<SimpleResource> findByType(String type); List<SimpleResource> findByType(String type);
@Query(value = "select t.content_type from resources r join resource_types t on (r.type = t.id) where r.id = ?1", nativeQuery = true)
Optional<String> findContentTypeById(String id);
} }

View File

@ -80,17 +80,22 @@ CREATE TABLE wf_history (
-- Other Resources -- Other Resources
CREATE TABLE resource_types(id text PRIMARY KEY, name text); CREATE TABLE resource_types(
INSERT INTO resource_types(id, name) VALUES id text PRIMARY KEY,
('transformation_rule', 'Transformation Rules'), name text NOT NULL,
('cleaning_rule', 'Cleaning Rules'), content_type text NOT NULL DEFAULT 'text/plain'
('hadoop_job_configuration', 'Hadoop Job Configurations'); );
INSERT INTO resource_types(id, name, content_type) VALUES
('transformation_rule_xslt', 'Transformation Rules (xslt)', 'application/xml'),
('transformation_rule_legacy', 'Transformation Rules (legacy)', 'text/plain'),
('cleaning_rule', 'Cleaning Rules', 'application/xml'),
('hadoop_job_configuration', 'Hadoop Job Configurations', 'application/xml');
CREATE TABLE resources ( CREATE TABLE resources (
id text PRIMARY KEY, id text PRIMARY KEY,
name text NOT NULL, name text NOT NULL,
description text, description text,
content_type text NOT NULL DEFAULT 'text/plain',
content text NOT NULL DEFAULT '', content text NOT NULL DEFAULT '',
type text NOT NULL REFERENCES resource_types(id), type text NOT NULL REFERENCES resource_types(id),
creation_date timestamp NOT NULL DEFAULT now(), creation_date timestamp NOT NULL DEFAULT now(),
@ -99,10 +104,11 @@ CREATE TABLE resources (
CREATE VIEW resource_types_view AS ( CREATE VIEW resource_types_view AS (
SELECT SELECT
t.id AS id, t.id AS id,
t.name AS name, t.name AS name,
count(r.id) AS count, t.content_type AS content_type,
true AS simple count(r.id) AS count,
true AS simple
FROM resource_types t FROM resource_types t
LEFT OUTER JOIN resources r ON (r.type = t.id) LEFT OUTER JOIN resources r ON (r.type = t.id)
GROUP BY t.id, t.name GROUP BY t.id, t.name
@ -111,14 +117,16 @@ CREATE VIEW resource_types_view AS (
SELECT SELECT
'vocabulary' AS id, 'vocabulary' AS id,
'Vocabularies' AS name, 'Vocabularies' AS name,
'text/plain' AS content_type,
count(*) AS count, count(*) AS count,
false AS simple false AS simple
FROM vocabularies FROM vocabularies
) UNION ALL ( ) UNION ALL (
SELECT SELECT
'context' AS id, 'context' AS id,
'Contexts' AS name, 'Contexts' AS name,
count(*) AS count, 'text/plain' AS content_type,
false AS simple count(*) AS count,
false AS simple
FROM contexts FROM contexts
); );