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

346 lines
10 KiB
Java

package org.gcube.informationsystem.utils.documentation.model;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
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 abstract class DocumentationGenerator {
private static final Logger logger = LoggerFactory.getLogger(DocumentationGenerator.class);
private static final int DEFAULT_NUMBER_OF_COLUMNS = 5;
protected final Type type;
protected final AccessType requiredType;
protected final String superClassToBeExcluded;
protected final int requiredNumberOfColumns;
protected DocumentationGenerator(Type type, AccessType requiredType) {
this(type, requiredType, DEFAULT_NUMBER_OF_COLUMNS, null);
}
protected DocumentationGenerator(Type type, AccessType requiredType, int requiredNumberOfColumns) {
this(type, requiredType, requiredNumberOfColumns, null);
}
protected DocumentationGenerator(Type type, AccessType requiredType, String superClassToBeExcluded) {
this(type, requiredType, DEFAULT_NUMBER_OF_COLUMNS, superClassToBeExcluded);
}
protected DocumentationGenerator(Type type, AccessType requiredType, int requiredNumberOfColumns, String superClassToBeExcluded) {
this.type = type;
this.requiredType = requiredType;
AccessType accessType = type.getAccessType();
if(accessType!=requiredType) {
throw new RuntimeException(type.getName() + " is not a " + requiredType.getName() + " type");
}
this.requiredNumberOfColumns = requiredNumberOfColumns;
this.superClassToBeExcluded = superClassToBeExcluded;
}
protected Row getPropertyFieldsBoldRow() {
Row row = new Row(RowType.NORMAL);
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);
for(int i=row.getCells().size(); i<requiredNumberOfColumns; i++) {
row.appendCell(description);
}
return row;
}
protected Row getPropertyFieldsHeadingRow() {
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);
for(int i=row.getCells().size(); i<requiredNumberOfColumns; i++) {
row.appendCell(description);
}
return row;
}
protected Row getSourceTargetBoldRow() {
Row row = new Row(RowType.NORMAL);
Cell source = new Cell();
source.setText("**Source**");
row.appendCell(source);
Cell relation = new Cell();
relation.setText("**Relation**");
row.appendCell(relation);
Cell multiplicity = new Cell();
multiplicity.setText("**Multiplicity**");
row.appendCell(multiplicity);
Cell target = new Cell();
target.setText("**Target**");
row.appendCell(target);
Cell description = new Cell();
description.setText("**Description**");
row.appendCell(description);
for(int i=row.getCells().size(); i<requiredNumberOfColumns; i++) {
row.appendCell(description);
}
return row;
}
protected Row getSourceTargetHeadingRow() {
Row row = new Row(RowType.HEADING);
Cell source = new Cell();
source.setText("Source");
row.appendCell(source);
Cell relation = new Cell();
relation.setText("Relation");
row.appendCell(relation);
Cell multiplicity = new Cell();
multiplicity.setText("Multiplicity");
row.appendCell(multiplicity);
Cell target = new Cell();
target.setText("Target");
row.appendCell(target);
Cell description = new Cell();
description.setText("Description");
row.appendCell(description);
for(int i=row.getCells().size(); i<requiredNumberOfColumns; i++) {
row.appendCell(description);
}
return row;
}
protected Row getKnownUsageBoldRow() {
Row row = new Row(RowType.NORMAL);
row = addCellSpanToRow(row, "**Known Usage**", requiredNumberOfColumns);
return row;
}
protected Row getPropertiesBoldRow() {
Row row = new Row(RowType.NORMAL);
row = addCellSpanToRow(row, "**Properties**", requiredNumberOfColumns);
return row;
}
protected Table addLinkedEntities(Table table, List<LinkedEntity> entities) {
if(entities!=null) {
for(LinkedEntity entity : entities) {
Row row = new Row(RowType.NORMAL);
Cell source = new Cell();
source.setText(entity.getSource());
row.appendCell(source);
Cell relation = new Cell();
relation.setText(entity.getRelation());
row.appendCell(relation);
Cell multiplicity = new Cell();
StringBuffer buffer = new StringBuffer();
Integer min = entity.getMin();
buffer.append(min==null? "0" : min.toString());
buffer.append("..");
Integer max = entity.getMax();
buffer.append(max==null? "n" : max.toString());
multiplicity.setText(buffer.toString());
row.appendCell(multiplicity);
Cell target = new Cell();
target.setText(entity.getTarget());
row.appendCell(target);
Cell description = new Cell();
description.setText(entity.getDescription());
row.appendCell(description);
for(int i=row.getCells().size(); i<requiredNumberOfColumns; i++) {
row.appendCell(description);
}
table.appendRow(row);
}
}
return table;
}
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);
for(int i=row.getCells().size(); i<requiredNumberOfColumns; i++) {
row.appendCell(description);
}
return row;
}
protected Row getNoPropertyRow() {
return getRowCellSpan("This type does not define any additional attributes.", requiredNumberOfColumns);
}
protected Row addCellSpanToRow(Row row, String content, int cellSpan) {
Cell cellToSpan = new Cell();
cellToSpan.setText(content);
for(int i=0; i<cellSpan; i++) {
row.appendCell(cellToSpan);
}
return row;
}
protected Row getRowCellSpan(String content, int cellSpan) {
Row row = new Row(RowType.NORMAL);
return addCellSpanToRow(row, content, cellSpan);
}
/*
* E.g. GCubeProperty extends Property
*/
protected StringBuffer getTypeDeclaration() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("**");
stringBuffer.append(type.getName());
stringBuffer.append("**");
Set<String> superClasses = type.getSuperClasses();
if(superClassToBeExcluded!=null) {
superClasses.remove(superClassToBeExcluded);
}
if(superClasses.size()>0){
stringBuffer.append(" ``extends`` ");
boolean first = true;
for(String superClass : superClasses) {
if(!first) {
stringBuffer.append(", ");
}else {
first = false;
}
stringBuffer.append("**");
stringBuffer.append(superClass);
stringBuffer.append("**");
}
}
return stringBuffer;
}
protected StringBuffer getChangelog() {
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<String, String> changelog = type.getChangelogWithVersionAsString();
Set<String> 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 abstract Table getTable();
public StringBuffer generateSection() {
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().toString();
Table table = getTable();
stringBuffer.append(table.generateTable(tableTitle));
stringBuffer.append(getChangelog());
logger.info(stringBuffer.toString());
return stringBuffer;
}
}