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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
@ -44,7 +45,7 @@ public class MainController {
if (restype.isPresent() && restype.get().isSimple()) {
map.addAttribute("type", restype.get());
} else {
map.addAttribute("type", new ResourceType("not_present", "???", 0));
map.addAttribute("type", new ResourceType("not_present", "???", MediaType.TEXT_PLAIN_VALUE, 0));
}
return "simpleResources";
}

View File

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

View File

@ -73,15 +73,14 @@ public class ResourcesRestController {
@GetMapping("/{id}/content")
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.setContentType(sr.getContentType());
simpleResourceRepository.getContentById(id);
res.setContentType(ctype);
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) {
throw new InformationServiceException("Error retrieving content", e);
}

View File

@ -30,7 +30,6 @@ public class DatabaseUtils {
res.setName(name);
res.setType(type);
res.setDescription(description);
res.setContentType(ctype);
res.setCreationDate(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-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>
<p class="card-text">{{r.description}}</p>
<p class="text-muted small">
@ -114,7 +114,7 @@
<div class="modal-body">
<form>
<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" />
</div>
</form>

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package eu.dnetlib.is.resource.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
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> {
@Query(value = "select content from resources where id = ?1", nativeQuery = true)
String getContentById(String id);
Optional<String> findContentById(String id);
@Modifying
@Query(value = "update resources set content = ?2 where id = ?1", nativeQuery = true)
void setContentById(String id, String content);
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
CREATE TABLE resource_types(id text PRIMARY KEY, name text);
INSERT INTO resource_types(id, name) VALUES
('transformation_rule', 'Transformation Rules'),
('cleaning_rule', 'Cleaning Rules'),
('hadoop_job_configuration', 'Hadoop Job Configurations');
CREATE TABLE resource_types(
id text PRIMARY KEY,
name text NOT NULL,
content_type text NOT NULL DEFAULT 'text/plain'
);
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 (
id text PRIMARY KEY,
name text NOT NULL,
description text,
content_type text NOT NULL DEFAULT 'text/plain',
content text NOT NULL DEFAULT '',
type text NOT NULL REFERENCES resource_types(id),
creation_date timestamp NOT NULL DEFAULT now(),
@ -99,10 +104,11 @@ CREATE TABLE resources (
CREATE VIEW resource_types_view AS (
SELECT
t.id AS id,
t.name AS name,
count(r.id) AS count,
true AS simple
t.id AS id,
t.name AS name,
t.content_type AS content_type,
count(r.id) AS count,
true AS simple
FROM resource_types t
LEFT OUTER JOIN resources r ON (r.type = t.id)
GROUP BY t.id, t.name
@ -111,14 +117,16 @@ CREATE VIEW resource_types_view AS (
SELECT
'vocabulary' AS id,
'Vocabularies' AS name,
'text/plain' AS content_type,
count(*) AS count,
false AS simple
FROM vocabularies
) UNION ALL (
SELECT
'context' AS id,
'Contexts' AS name,
count(*) AS count,
false AS simple
'context' AS id,
'Contexts' AS name,
'text/plain' AS content_type,
count(*) AS count,
false AS simple
FROM contexts
);