Commit 7a5d0113 authored by Vincent Horváth's avatar Vincent Horváth

created table preprocessor

parent 1d1f662b
......@@ -10,10 +10,10 @@ import sk.tuke.bfi.core.preprocessor.html.HtmlPreprocessor;
import java.util.Map;
public abstract class AngularReplacer implements HtmlPreprocessor {
protected final static Logger LOGGER = Logger.getLogger(AngularReplacer.class);
public abstract class AngularPreprocessor implements HtmlPreprocessor {
protected final static Logger LOGGER = Logger.getLogger(AngularPreprocessor.class);
protected Map<String, BaseEntity> entities;
private Map<String, BaseEntity> entities;
protected Node node;
@Override
......@@ -26,14 +26,14 @@ public abstract class AngularReplacer implements HtmlPreprocessor {
this.node = node;
}
public Element getElement() {
protected Element getElement() {
if (node instanceof Element) {
return (Element) node;
}
return null;
}
public Model getModel(String name) throws Exception {
protected Model getModel(String name) throws Exception {
BaseEntity entity = entities.get(name);
if (entity instanceof Model) {
return (Model) entity;
......@@ -49,7 +49,7 @@ public abstract class AngularReplacer implements HtmlPreprocessor {
}
private void copyAttributes(Element orgElement, Element dupElement) {
protected void copyAttributes(Element orgElement, Element dupElement) {
for (Attribute attribute : orgElement.attributes()) {
if (!attribute.getKey().startsWith("bfi-")) {
dupElement.attr(attribute.getKey(), attribute.getValue());
......
package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularReplacer;
import sk.tuke.bfi.angular.generator.AngularPreprocessor;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
@Preprocessor("create")
public class CreatePreprocessor extends AngularReplacer {
public class CreatePreprocessor extends AngularPreprocessor {
@Override
......
package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularReplacer;
import sk.tuke.bfi.angular.generator.AngularPreprocessor;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
import static sk.tuke.bfi.core.preprocessor.html.BfiTag.BFI;
import static sk.tuke.bfi.core.helper.constant.StringConstant.EMPTY_STRING;
@Preprocessor("form")
public class FormPreprocessor extends AngularReplacer {
public class FormPreprocessor extends AngularPreprocessor {
@Override
......
package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularReplacer;
import sk.tuke.bfi.core.entity.model.Editor;
import sk.tuke.bfi.angular.generator.AngularPreprocessor;
import sk.tuke.bfi.core.entity.model.Field;
import sk.tuke.bfi.core.entity.model.Model;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
......@@ -12,7 +10,7 @@ import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_FIELD;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_MODEL;
@Preprocessor("input")
public class InputPreprocessor extends AngularReplacer {
public class InputPreprocessor extends AngularPreprocessor {
@Override
public void replace() {
......@@ -31,17 +29,25 @@ public class InputPreprocessor extends AngularReplacer {
throw new RuntimeException("Field " + fieldName + " is not exist in model " + modelName);
}
Element inputElement;
if (field.getEditor() == Editor.STRING) {
inputElement = getInputText(field,model);
} else {
inputElement = getInputText(field, model);
Element inputElement = null;
switch (field.getEditor()) {
case STRING:
inputElement = getInputText(field, model);
case TEXTAREA:
inputElement = getInputTextarea(field, model);
case SELECT:
inputElement = getInputSelect(field, model);
case MULTI_SELECT:
inputElement = getInputMultiselect(field, model);
}
if (inputElement == null) {
LOGGER.error("Editor " + field.getEditor() + " is not supported.");
}
element.replaceWith(inputElement);
}
private Element getInputText(Field field,Model model) {
private Element getInputText(Field field, Model model) {
Element element = getElement();
Element inputElement = new Element("div");
......@@ -50,15 +56,30 @@ public class InputPreprocessor extends AngularReplacer {
input.attr("type", "text");
input.attr("name", field.getName());
input.attr("id", field.getName());
input.attr("[(ngModel)]",model.getName().toLowerCase()+"."+ field.getName());
input.attr("[(ngModel)]", model.getName().toLowerCase() + "." + field.getName());
copyAttributes(element, input);
for (Attribute attribute : element.attributes()) {
if (attribute.getKey().startsWith("bfi-")) {
continue;
}
input.attr(attribute.getKey(), attribute.getValue());
}
Element label = new Element("label");
label.attr("for", field.getName());
label.text(field.getLabel());
inputElement.appendChild(label);
inputElement.appendChild(input);
return inputElement;
}
private Element getInputTextarea(Field field, Model model) {
Element element = getElement();
Element inputElement = new Element("div");
Element input = new Element("textarea");
input.attr("name", field.getName());
input.attr("id", field.getName());
input.attr("[(ngModel)]", model.getName().toLowerCase() + "." + field.getName());
copyAttributes(element, input);
Element label = new Element("label");
label.attr("for", field.getName());
......@@ -69,6 +90,57 @@ public class InputPreprocessor extends AngularReplacer {
inputElement.appendChild(input);
return inputElement;
}
private Element getInputSelect(Field field, Model model) {
Element element = getElement();
Element inputElement = new Element("div");
Element input = new Element("select");
Element options = new Element("option");
options.attr("[value]", field.getName());
options.attr("*ngFor", "let " + field.getName() + " of " + model.getName() + "." + field.getName());
input.attr("name", field.getName());
input.attr("id", field.getName());
input.attr("[(ngModel)]", model.getName().toLowerCase() + "." + field.getName());
copyAttributes(element, input);
Element label = new Element("label");
label.attr("for", field.getName());
label.text(field.getLabel());
inputElement.appendChild(label);
inputElement.appendChild(input);
return inputElement;
}
private Element getInputMultiselect(Field field, Model model) {
Element element = getElement();
Element inputElement = new Element("div");
Element input = new Element("select");
Element options = new Element("option");
options.attr("[value]", field.getName());
options.attr("*ngFor", "let " + field.getName() + " of " + model.getName() + "." + field.getName());
input.attr("name", field.getName());
input.attr("multiple");
input.attr("id", field.getName());
input.attr("[(ngModel)]", model.getName().toLowerCase() + "." + field.getName());
copyAttributes(element, input);
Element label = new Element("label");
label.attr("for", field.getName());
label.text(field.getLabel());
inputElement.appendChild(label);
inputElement.appendChild(input);
return inputElement;
}
}
......@@ -2,7 +2,7 @@ package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularReplacer;
import sk.tuke.bfi.angular.generator.AngularPreprocessor;
import sk.tuke.bfi.core.entity.model.Editor;
import sk.tuke.bfi.core.entity.model.Field;
import sk.tuke.bfi.core.entity.model.Model;
......@@ -12,7 +12,7 @@ import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_FIELD;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_MODEL;
@Preprocessor("output")
public class OutputPreprocessor extends AngularReplacer {
public class OutputPreprocessor extends AngularPreprocessor {
@Override
public void replace() {
......@@ -47,13 +47,7 @@ public class OutputPreprocessor extends AngularReplacer {
Element outputElement = new Element("div");
outputElement.val("{" + model.getName().toLowerCase() + "." + field.getName() + "}");
for (Attribute attribute : element.attributes()) {
if (attribute.getKey().startsWith("bfi-")) {
continue;
}
outputElement.attr(attribute.getKey(), attribute.getValue());
}
copyAttributes(element, outputElement);
return outputElement;
......
package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularPreprocessor;
import sk.tuke.bfi.core.entity.model.Field;
import sk.tuke.bfi.core.entity.model.Model;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
import static sk.tuke.bfi.core.helper.constant.StringConstant.EMPTY_STRING;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_FIELD;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_MODEL;
import static sk.tuke.bfi.core.preprocessor.html.BfiTag.BFI;
@Preprocessor("table")
public class TablePreprocessor extends AngularPreprocessor {
@Override
public void replace() {
final String nodeName = node.nodeName().replaceAll(BFI.toString(), EMPTY_STRING);
Element element = getElement();
element.tagName(nodeName);
Element table = new Element("table");
table.appendChild(getHeader(element));
table.appendChild(getBody(element));
element.replaceWith(table);
}
private Element getHeader(final Element column) {
final Element tableHeader = new Element("thead");
final Element tableRow = new Element("tr");
tableHeader.appendChild(tableRow);
for (Element element : column.getAllElements()) {
final Element tableColumn = new Element("th");
try {
tableColumn.val(getField(element).getLabel());
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
continue;
}
copyAttributes(element, tableColumn);
}
return tableHeader;
}
private Element getBody(final Element column) {
final Element tableBody = new Element("tbody");
final Element tableRow = new Element("tr");
Model model = null;
tableBody.appendChild(tableRow);
for (Element element : column.getAllElements()) {
final Element tableColumn = new Element("td");
final Field field;
try {
field = getField(element);
if(model==null){
model = field.getModel();
}
tableColumn.val(String.format("{%s.%s}", field.getModel().getName(), field.getName()));
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
continue;
}
copyAttributes(element, tableColumn);
}
tableRow.attr("*ngFor",String.format("let item of %s",model.getName()));
return tableBody;
}
private Field getField(Element element) throws Exception {
Model model = null;
final String modelName = element.attr(BFI_MODEL.toString());
final String fieldName = element.attr(BFI_FIELD.toString());
model = getModel(modelName);
final Field field = model.getField(fieldName);
if (field == null) {
throw new Exception("Field " + fieldName + " is not exist in model " + modelName);
}
return field;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment