228 lines
7.2 KiB
Java
228 lines
7.2 KiB
Java
package org.gcube.informationsystem.utils.documentation.generator;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Constructor;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
|
import org.gcube.informationsystem.model.reference.entities.Entity;
|
|
import org.gcube.informationsystem.model.reference.properties.Property;
|
|
import org.gcube.informationsystem.model.reference.relations.Relation;
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
import org.gcube.informationsystem.types.reference.entities.FacetType;
|
|
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
|
import org.gcube.informationsystem.types.reference.properties.PropertyType;
|
|
import org.gcube.informationsystem.types.reference.relations.ConsistsOfType;
|
|
import org.gcube.informationsystem.types.reference.relations.IsRelatedToType;
|
|
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.entities.EntityDocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.entities.FacetDocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.entities.ResourceDocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.properties.PropertyDocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.relations.ConsistsOfDocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.relations.IsRelatedToDocumentationGenerator;
|
|
import org.gcube.informationsystem.utils.documentation.model.relations.RelationDocumentationGenerator;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class TypeListGenerator {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(TypeListGenerator.class);
|
|
|
|
public static final String PROPERTIES_FILENAME = "properties.rst";
|
|
public static final String ENTITIES_FILENAME = "entities.rst";
|
|
public static final String RELATIONS_FILENAME = "relations.rst";
|
|
|
|
protected List<PropertyType<Property>> propertyTypes;
|
|
protected List<ResourceType> resourceTypes;
|
|
protected List<FacetType> facetTypes;
|
|
protected List<IsRelatedToType> isRelatedToTypes;
|
|
protected List<ConsistsOfType> consistsOfTypes;
|
|
|
|
protected int offsetLevel;
|
|
|
|
public TypeListGenerator() {
|
|
this.propertyTypes = new ArrayList<>();
|
|
this.resourceTypes = new ArrayList<>();
|
|
this.facetTypes = new ArrayList<>();
|
|
this.isRelatedToTypes = new ArrayList<>();
|
|
this.consistsOfTypes = new ArrayList<>();
|
|
this.offsetLevel = 2;
|
|
}
|
|
|
|
public List<ResourceType> getResourceTypes() {
|
|
return resourceTypes;
|
|
}
|
|
|
|
public void setResourceTypes(List<ResourceType> resources) {
|
|
this.resourceTypes = resources;
|
|
}
|
|
|
|
public List<FacetType> getFacetTypes() {
|
|
return facetTypes;
|
|
}
|
|
|
|
public void setFacetTypes(List<FacetType> facets) {
|
|
this.facetTypes = facets;
|
|
}
|
|
|
|
public List<IsRelatedToType> getIsRelatedToTypes() {
|
|
return isRelatedToTypes;
|
|
}
|
|
|
|
public void setIsRelatedToTypes(List<IsRelatedToType> isRelatedToTypes) {
|
|
this.isRelatedToTypes = isRelatedToTypes;
|
|
}
|
|
|
|
public List<ConsistsOfType> getConsistsOfTypes() {
|
|
return consistsOfTypes;
|
|
}
|
|
|
|
public void setConsistsOfTypes(List<ConsistsOfType> consistsOfTypes) {
|
|
this.consistsOfTypes = consistsOfTypes;
|
|
}
|
|
|
|
public void addType(Type type) {
|
|
switch (type.getAccessType()) {
|
|
case PROPERTY:
|
|
@SuppressWarnings("unchecked")
|
|
PropertyType<Property> p = (PropertyType<Property>) type;
|
|
propertyTypes.add(p);
|
|
break;
|
|
|
|
case RESOURCE:
|
|
ResourceType r = (ResourceType) type;
|
|
resourceTypes.add(r);
|
|
break;
|
|
|
|
case FACET:
|
|
FacetType f = (FacetType) type;
|
|
facetTypes.add(f);
|
|
break;
|
|
|
|
case IS_RELATED_TO:
|
|
IsRelatedToType irt = (IsRelatedToType) type;
|
|
isRelatedToTypes.add(irt);
|
|
break;
|
|
|
|
case CONSISTS_OF:
|
|
ConsistsOfType co = (ConsistsOfType) type;
|
|
consistsOfTypes.add(co);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
public void checkList(List<? extends Type> list) throws Exception {
|
|
Type first = list.get(0);
|
|
AccessType accessType = first.getAccessType();
|
|
if(Objects.isNull(list) || list.size()==0) {
|
|
throw new Exception("Please add all " + accessType.getName() + " types before starting generation");
|
|
}
|
|
}
|
|
|
|
protected DocumentationGenerator getDocumentationGeneratorInstance(Class<? extends DocumentationGenerator> clz, Type type) throws Exception {
|
|
Constructor<? extends DocumentationGenerator> constructor = clz.getConstructor(Type.class);
|
|
return constructor.newInstance(type);
|
|
}
|
|
|
|
public void elaborateList(List<? extends Type> types) throws Exception {
|
|
Type first = types.get(0);
|
|
AccessType accessType = first.getAccessType();
|
|
Class<? extends DocumentationGenerator> clz;
|
|
File f;
|
|
switch (accessType) {
|
|
case PROPERTY:
|
|
clz = PropertyDocumentationGenerator.class;
|
|
f = getFile(PROPERTIES_FILENAME, true);
|
|
break;
|
|
|
|
case RESOURCE:
|
|
clz = ResourceDocumentationGenerator.class;
|
|
f = getFile(ENTITIES_FILENAME, false);
|
|
break;
|
|
|
|
case FACET:
|
|
clz = FacetDocumentationGenerator.class;
|
|
f = getFile(ENTITIES_FILENAME, false);
|
|
break;
|
|
|
|
case IS_RELATED_TO:
|
|
clz = IsRelatedToDocumentationGenerator.class;
|
|
f = getFile(RELATIONS_FILENAME, false);
|
|
break;
|
|
|
|
case CONSISTS_OF:
|
|
clz = ConsistsOfDocumentationGenerator.class;
|
|
f = getFile(RELATIONS_FILENAME, false);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception("List of types not recognized");
|
|
}
|
|
|
|
for(Type type : types) {
|
|
DocumentationGenerator dg = getDocumentationGeneratorInstance(clz, type);
|
|
dg.setOffsetLevel(offsetLevel);
|
|
StringBuffer sb = dg.generateSection();
|
|
Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
|
}
|
|
}
|
|
|
|
public void generate() throws Exception {
|
|
checkList(propertyTypes);
|
|
checkList(resourceTypes);
|
|
checkList(facetTypes);
|
|
checkList(isRelatedToTypes);
|
|
checkList(consistsOfTypes);
|
|
|
|
elaborateList(propertyTypes);
|
|
|
|
File f = getFile(ENTITIES_FILENAME, true);
|
|
DocumentationGenerator edg = new EntityDocumentationGenerator(TypeMapper.createTypeDefinition(Entity.class));
|
|
edg.setOffsetLevel(offsetLevel);
|
|
StringBuffer sb = edg.generateSection();
|
|
Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
|
|
|
elaborateList(resourceTypes);
|
|
elaborateList(facetTypes);
|
|
|
|
f = getFile(RELATIONS_FILENAME, true);
|
|
DocumentationGenerator rdg = new RelationDocumentationGenerator(TypeMapper.createTypeDefinition(Relation.class));
|
|
rdg.setOffsetLevel(offsetLevel);
|
|
sb = rdg.generateSection();
|
|
Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
|
|
|
elaborateList(isRelatedToTypes);
|
|
elaborateList(consistsOfTypes);
|
|
}
|
|
|
|
}
|