information-system-model-do.../src/main/java/org/gcube/informationsystem/utils/documentation/generator/DocumentationGenerator.java

164 lines
4.5 KiB
Java
Raw Normal View History

2023-01-24 16:54:00 +01:00
package org.gcube.informationsystem.utils.documentation.generator;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import org.gcube.informationsystem.base.reference.AccessType;
2023-02-07 14:34:44 +01:00
import org.gcube.informationsystem.discovery.knowledge.ModelKnowledge;
import org.gcube.informationsystem.discovery.knowledge.UsageKnowledge;
2023-01-24 16:54:00 +01:00
import org.gcube.informationsystem.types.reference.Type;
2023-02-08 16:53:29 +01:00
import org.gcube.informationsystem.utils.documentation.model.Documentation;
import org.gcube.informationsystem.utils.documentation.model.entities.EntityDocumentation;
import org.gcube.informationsystem.utils.documentation.model.entities.FacetDocumentation;
import org.gcube.informationsystem.utils.documentation.model.entities.ResourceDocumentation;
import org.gcube.informationsystem.utils.documentation.model.properties.PropertyDocumentation;
import org.gcube.informationsystem.utils.documentation.model.relations.ConsistsOfDocumentation;
import org.gcube.informationsystem.utils.documentation.model.relations.IsRelatedToDocumentation;
import org.gcube.informationsystem.utils.documentation.model.relations.RelationDocumentation;
2023-01-24 16:54:00 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2023-02-08 16:57:32 +01:00
public class DocumentationGenerator {
2023-01-24 16:54:00 +01:00
2023-02-08 16:57:32 +01:00
private static final Logger logger = LoggerFactory.getLogger(DocumentationGenerator.class);
2023-01-24 16:54:00 +01:00
2023-02-07 14:34:44 +01:00
protected ModelKnowledge modelKnowledge;
2023-02-08 16:57:32 +01:00
public DocumentationGenerator() {
2023-02-07 14:34:44 +01:00
this.modelKnowledge = new ModelKnowledge();
2023-01-24 16:54:00 +01:00
}
2023-02-08 16:46:44 +01:00
public ModelKnowledge getModelKnowledge() {
return modelKnowledge;
}
2023-02-08 16:54:24 +01:00
public Class<? extends Documentation> getDocumentationClass(AccessType accessType)
2023-01-24 16:54:00 +01:00
throws Exception {
2023-02-08 16:53:29 +01:00
Class<? extends Documentation> clz;
2023-01-24 16:54:00 +01:00
switch (accessType) {
case PROPERTY:
2023-02-08 16:53:29 +01:00
clz = PropertyDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
case ENTITY:
2023-02-08 16:53:29 +01:00
clz = EntityDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
case RESOURCE:
2023-02-08 16:53:29 +01:00
clz = ResourceDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
case FACET:
2023-02-08 16:53:29 +01:00
clz = FacetDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
case RELATION:
2023-02-08 16:53:29 +01:00
clz = RelationDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
case IS_RELATED_TO:
2023-02-08 16:53:29 +01:00
clz = IsRelatedToDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
case CONSISTS_OF:
2023-02-08 16:53:29 +01:00
clz = ConsistsOfDocumentation.class;
2023-01-24 16:54:00 +01:00
break;
default:
throw new Exception("List of types not recognized");
}
return clz;
}
2023-02-08 16:54:24 +01:00
protected Documentation getDocumentation(Type type) throws Exception {
Documentation d = null;
2023-01-24 16:54:00 +01:00
switch (type.getAccessType()) {
case PROPERTY:
2023-02-08 16:54:24 +01:00
d = new PropertyDocumentation(type);
d.setLevel(0);
2023-01-24 16:54:00 +01:00
break;
case ENTITY:
2023-02-08 16:54:24 +01:00
d = new EntityDocumentation(type);
d.setLevel(0);
2023-01-24 16:54:00 +01:00
break;
case RESOURCE:
2023-02-08 16:54:24 +01:00
d = new ResourceDocumentation(type);
d.setLevel(1);
2023-01-24 16:54:00 +01:00
break;
case FACET:
2023-02-08 16:54:24 +01:00
d = new FacetDocumentation(type);
d.setLevel(1);
2023-01-24 16:54:00 +01:00
break;
case RELATION:
2023-02-08 16:54:24 +01:00
d = new RelationDocumentation(type);
d.setLevel(0);
2023-01-24 16:54:00 +01:00
break;
case IS_RELATED_TO:
2023-02-08 16:54:24 +01:00
d = new IsRelatedToDocumentation(type);
d.setLevel(1);
2023-01-24 16:54:00 +01:00
break;
case CONSISTS_OF:
2023-02-08 16:54:24 +01:00
d = new ConsistsOfDocumentation(type);
d.setLevel(1);
2023-01-24 16:54:00 +01:00
break;
default:
throw new Exception("I'm not developed to manage the " + AccessType.class.getSimpleName() + " " + type.getName());
}
2023-02-07 14:34:44 +01:00
UsageKnowledge facetKnowledge = modelKnowledge.getUsageKnowledge(AccessType.FACET);
2023-02-08 16:54:24 +01:00
d.setFacetKnowledge(facetKnowledge);
2023-02-07 14:34:44 +01:00
UsageKnowledge resourceKnowledge = modelKnowledge.getUsageKnowledge(AccessType.RESOURCE);
2023-02-08 16:54:24 +01:00
d.setResourceKnowledge(resourceKnowledge);
return d;
2023-01-24 16:54:00 +01:00
}
protected File getFile(String fileName, boolean newFile) throws IOException {
File file = new File(fileName);
if(file.exists() && newFile) {
file.delete();
}
if(!file.exists()) {
logger.info("Going to create {}", file.getAbsolutePath());
file.createNewFile();
}
return file;
}
2023-02-07 14:09:25 +01:00
protected void writeSectionOnly(Type type, File file, Integer level) throws Exception {
2023-02-08 16:54:24 +01:00
Documentation dg = getDocumentation(type);
2023-02-07 14:09:25 +01:00
if(level!=null) {
dg.setLevel(level);
}
StringBuffer sb = dg.generateSection();
Files.write(file.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
}
2023-01-24 16:54:00 +01:00
protected void writeTypeToFile(Type type, File file, Integer level) throws Exception {
2023-02-08 16:54:24 +01:00
Documentation dg = getDocumentation(type);
2023-01-24 16:54:00 +01:00
if(level!=null) {
dg.setLevel(level);
}
StringBuffer sb = dg.generate();
Files.write(file.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
}
protected void writeTypeToFile(Type type, File file) throws Exception {
writeTypeToFile(type, file, null);
}
}