package org.gcube.informationsystem.utils.documentation; import java.io.File; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.properties.PropertyDefinition; import org.gcube.informationsystem.utils.discovery.Discovery; import org.gcube.informationsystem.utils.documentation.rst.Section; import org.gcube.informationsystem.utils.documentation.rst.Section.SectionType; import org.gcube.informationsystem.utils.documentation.rst.table.Cell; import org.gcube.informationsystem.utils.documentation.rst.table.Row; import org.gcube.informationsystem.utils.documentation.rst.table.RowType; import org.gcube.informationsystem.utils.documentation.rst.table.Table; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ public class DocumentationGenerator { private static final Logger logger = LoggerFactory.getLogger(DocumentationGenerator.class); protected File baseDirectory; public DocumentationGenerator(File baseDirectory) { this.baseDirectory = baseDirectory; } /* * E.g. GCubeProperty extends Property */ protected StringBuffer getTypeDeclaration(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("**"); stringBuffer.append(type.getName()); stringBuffer.append("**"); stringBuffer.append(" ``extends`` "); boolean first = true; for(String superClass : type.getSuperClasses()) { if(!first) { stringBuffer.append(", "); }else { first = false; } stringBuffer.append("**"); stringBuffer.append(superClass); stringBuffer.append("**"); } return stringBuffer; } protected StringBuffer getTypeChangelog(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("The **"); stringBuffer.append(type.getName()); stringBuffer.append("** current version is "); stringBuffer.append(type.getVersionAsString()); stringBuffer.append(".\n\n"); Map changelog = type.getChangelogWithVersionAsString(); Set keySet = changelog.keySet(); stringBuffer.append("Changelog:\n\n"); int i=0; for(String version : keySet) { stringBuffer.append("* **"); stringBuffer.append(version); stringBuffer.append("**: "); stringBuffer.append(changelog.get(version)); if(i==keySet.size()-1) { stringBuffer.append("."); }else { stringBuffer.append(";"); } } stringBuffer.append("\n\n"); return stringBuffer; } protected Row getPropertyHeadingRow() { Row row = new Row(RowType.HEADING); Cell name = new Cell(); name.setText("Name"); row.appendCell(name); Cell type = new Cell(); type.setText("Type"); row.appendCell(type); Cell attributes = new Cell(); attributes.setText("Attributes"); row.appendCell(attributes); Cell description = new Cell(); description.setText("Description"); row.appendCell(description); return row; } protected String getPropertyAttributes(PropertyDefinition propertyDefinition) { StringBuffer stringBuffer = new StringBuffer(); boolean mandatory = propertyDefinition.isMandatory(); stringBuffer.append("``Mandatory:"); stringBuffer.append(mandatory); boolean readOnly = propertyDefinition.isReadonly(); stringBuffer.append("`` ``ReadOnly:"); stringBuffer.append(readOnly); boolean notNull = propertyDefinition.isNotnull(); stringBuffer.append("`` ``NotNull:"); stringBuffer.append(notNull); String regex = propertyDefinition.getRegexp(); if(regex!=null && regex.compareTo("")!=0) { stringBuffer.append("`` ``Regex:"); stringBuffer.append(regex); } Integer max = propertyDefinition.getMax(); if(max!=null) { stringBuffer.append("`` ``Max:"); stringBuffer.append(max); } Integer min = propertyDefinition.getMin(); if(min!=null) { stringBuffer.append("`` ``Min:"); stringBuffer.append(min); } stringBuffer.append("``"); return stringBuffer.toString(); } protected Row getPropertyRow(PropertyDefinition propertyDefinition) { Row row = new Row(RowType.NORMAL); Cell name = new Cell(); name.setText(propertyDefinition.getName()); row.appendCell(name); Cell type = new Cell(); type.setText(propertyDefinition.getType()); row.appendCell(type); Cell attributes = new Cell(); attributes.setText(getPropertyAttributes(propertyDefinition)); row.appendCell(attributes); Cell description = new Cell(); description.setText(propertyDefinition.getDescription()); row.appendCell(description); return row; } protected Table getTable(Type type) { Table table = new Table(); table.appendRow(getPropertyHeadingRow()); Set properties = type.getProperties(); if(properties!=null && properties.size()>0) { Set notManadatoryProperties = new HashSet<>(); for(PropertyDefinition propertyDefinition : properties) { if(propertyDefinition.isMandatory()) { /* * Adding Mandatory properties first in the table */ table.appendRow(getPropertyRow(propertyDefinition)); }else { notManadatoryProperties.add(propertyDefinition); } } for(PropertyDefinition propertyDefinition : notManadatoryProperties) { table.appendRow(getPropertyRow(propertyDefinition)); } }else { table.appendRow(getNoPropertyRow()); } return table; } protected Row getNoPropertyRow() { Row row = new Row(RowType.NORMAL); Cell noProp = new Cell(); noProp.setText("This type does not define any additional attributes."); row.appendCell(noProp); row.appendCell(noProp); row.appendCell(noProp); row.appendCell(noProp); return row; } public StringBuffer generatePropertySection(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\n"); String name = type.getName(); Section section = new Section(); stringBuffer.append(section.generateSection(SectionType.HEADING_3, name)); stringBuffer.append(type.getDescription()); stringBuffer.append("\n"); String tableTitle = getTypeDeclaration(type).toString(); Table table = getTable(type); stringBuffer.append(table.generateTable(tableTitle)); stringBuffer.append(getTypeChangelog(type)); logger.info(stringBuffer.toString()); return stringBuffer; } public StringBuffer generateResourceSection(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\n"); String name = type.getName(); Section section = new Section(); stringBuffer.append(section.generateSection(SectionType.HEADING_3, name)); stringBuffer.append(type.getDescription()); stringBuffer.append("\n"); String tableTitle = getTypeDeclaration(type).toString(); Table table = getTable(type); stringBuffer.append(table.generateTable(tableTitle)); stringBuffer.append(getTypeChangelog(type)); logger.info(stringBuffer.toString()); return stringBuffer; } public StringBuffer generateFacetSection(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\n"); String name = type.getName(); Section section = new Section(); stringBuffer.append(section.generateSection(SectionType.HEADING_3, name)); stringBuffer.append(type.getDescription()); stringBuffer.append("\n"); String tableTitle = getTypeDeclaration(type).toString(); Table table = getTable(type); stringBuffer.append(table.generateTable(tableTitle)); stringBuffer.append(getTypeChangelog(type)); logger.info(stringBuffer.toString()); return stringBuffer; } public StringBuffer generateISRelatedToSection(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\n"); String name = type.getName(); Section section = new Section(); stringBuffer.append(section.generateSection(SectionType.HEADING_3, name)); stringBuffer.append(type.getDescription()); stringBuffer.append("\n"); String tableTitle = getTypeDeclaration(type).toString(); Table table = getTable(type); stringBuffer.append(table.generateTable(tableTitle)); stringBuffer.append(getTypeChangelog(type)); logger.info(stringBuffer.toString()); return stringBuffer; } public StringBuffer generateConsistsOfSection(Type type) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\n"); String name = type.getName(); Section section = new Section(); stringBuffer.append(section.generateSection(SectionType.HEADING_3, name)); stringBuffer.append(type.getDescription()); stringBuffer.append("\n"); String tableTitle = getTypeDeclaration(type).toString(); Table table = getTable(type); stringBuffer.append(table.generateTable(tableTitle)); stringBuffer.append(getTypeChangelog(type)); logger.info(stringBuffer.toString()); return stringBuffer; } public void generate(List pkgs) throws Exception { DocumentationSchemaAction schemaAction = new DocumentationSchemaAction(pkgs); Discovery.discover(schemaAction); Map propertyElements = schemaAction.getPropertyElement(); Set propertyNames = propertyElements.keySet(); for(String name : propertyNames) { Type type = propertyElements.get(name); generatePropertySection(type); } /* Map relationElements = schemaAction.getRelationElements(); Set relationNames = relationElements.keySet(); for(String name : relationNames) { Type type = propertyElements.get(name); } Map entityElements = schemaAction.getEntityElement(); Set entityNames = entityElements.keySet(); for(String name : entityNames) { Type type = propertyElements.get(name); } */ } }