Skip to content

Commit

Permalink
render a 'detail' view
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-rew committed Oct 24, 2023
1 parent 608c601 commit da6c56d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -24,6 +25,7 @@
import ch.ivyteam.ivy.process.model.Process;
import ch.ivyteam.ivy.process.model.element.activity.Script;
import ch.ivyteam.ivy.process.model.element.event.start.dialog.html.HtmlDialogMethodStart;
import ch.ivyteam.ivy.process.model.element.value.Mappings;
import ch.ivyteam.ivy.scripting.dataclass.IDataClass;
import ch.ivyteam.ivy.scripting.dataclass.IDataClassField;
import ch.ivyteam.ivy.scripting.dataclass.IEntityClass;
Expand Down Expand Up @@ -53,6 +55,8 @@ void createEntityDialog(@TempDir Path dir) throws IOException, CoreException {
assertData(dialog.getDataClass(null));
assertProcess(customer, dialog.getProcess(null).getModel());
assertView(read(dialog.getViewFile()));
var udRoot = (IFolder) dialog.getResource();
assertDetailView(read(udRoot.getFile("EntityDetail.xhtml")));

} finally {
customer.getResource().delete(true, new NullProgressMonitor());
Expand All @@ -70,10 +74,16 @@ private void assertData(IDataClass dataClass) {
private void assertProcess(IEntityClass customer, Process process) {
Script loader = process.search().type(Script.class).findOne();
assertThat(loader.getCode()).contains(customer.getName());
var delete = process.search().type(HtmlDialogMethodStart.class).findOne();

var delete = process.search().type(HtmlDialogMethodStart.class).name("delete(customer)").findOne();
String removal = delete.getOutput().getCode();
assertThat(removal)
.contains("testing.remove(");

var edit = process.search().type(HtmlDialogMethodStart.class).name("edit(customer)").findOne();
Mappings mappings = edit.getOutput().getMappings();
assertThat(mappings.asList())
.hasSize(1);
}

private void assertView(String view) {
Expand All @@ -84,6 +94,13 @@ private void assertView(String view) {
.doesNotContain("<!-- [entity.fields] -->");
}

private void assertDetailView(String view) {
assertThat(view)
.as("visualizes properties of the entity")
.contains("firstname")
.doesNotContain("<!-- [entity.fields] -->");
}

private static String read(IFile viewFile) throws IOException, CoreException {
try(InputStream in = viewFile.getContents()) {
var bos = new java.io.ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;

import ch.ivyteam.ivy.IvyConstants;
Expand Down Expand Up @@ -57,10 +59,19 @@ public IUserDialog createDialog(IEntityClass entity, String unit) {
processRdm.save();

extendView(userDialog.getViewFile(), entity);
detailView(userDialog, entity);

return userDialog;
}

private void detailView(IUserDialog userDialog, IEntityClass entity) {
String template = readTemplate("EntityDetail.xhtml");
String rendered = renderFields(entity, template, this::renderDetail);
var dir = (IFolder) userDialog.getResource();
var detailView = dir.getFile("EntityDetail.xhtml");
write(detailView, rendered);
}

private void prepareTemplate(IProject project, String template) {
try {
var view = ch.ivyteam.ivy.dialog.ui.ViewTechnologyDesignerUiRegistry.getInstance().getViewTechnology(IvyConstants.VIEW_TECHONOLOGY_JSF);
Expand All @@ -74,24 +85,34 @@ private void prepareTemplate(IProject project, String template) {
}

private void extendView(IFile viewFile, IEntityClass entity) {
try(InputStream is = DialogCreator.class.getResourceAsStream("/com/axonivy/util/excel/importer/EntityManager/EntityManager.xhtml")) {
String template = readTemplate("EntityManager.xhtml");
String rendered = renderFields(entity, template, this::renderColumn);
write(viewFile, rendered);
}

private static String readTemplate(String resource) {
try(InputStream is = DialogCreator.class.getResourceAsStream("/com/axonivy/util/excel/importer/EntityManager/"+resource)) {
var bos = new ByteArrayOutputStream();
is.transferTo(bos);
var template = new String(bos.toByteArray());
return template;
} catch (Exception ex) {
throw new RuntimeException("Failed to read template "+resource);
}
}

String rendered = renderFields(entity, template);

var bis = new ByteArrayInputStream(rendered.getBytes());
viewFile.setContents(bis, 0, null);
private static void write(IFile view, String content) {
try(var bis = new ByteArrayInputStream(content.getBytes())){
view.setContents(bis, 0, null);
} catch (Exception ex) {
throw new RuntimeException("Failed to extend view for "+viewFile, ex);
throw new RuntimeException("Failed to extend view for "+view, ex);
}
}

private String renderFields(IEntityClass entity, String template) {
private String renderFields(IEntityClass entity, String template, Function<IEntityClassField, String> renderer) {
String fieldXhtml = entity.getFields().stream()
.filter(fld -> !fld.getName().equals("id"))
.map(this::renderColumn)
.map(renderer)
.collect(Collectors.joining("\n"));
return template.replace("<!-- [entity.fields] -->", fieldXhtml);
}
Expand All @@ -105,6 +126,14 @@ private String renderColumn(IEntityClassField field) {
return fieldXhtml;
}

private String renderDetail(IEntityClassField field) {
String fieldXhtml = """
<p:outputLabel for="FIELD" value="FIELD" />
<p:inputText id="FIELD" value="#{data.edit.FIELD}"></p:inputText>
""".replaceAll("FIELD", field.getName());
return fieldXhtml;
}

public static UserDialogStart dialogStartFor(IEntityClass entity) {
var dialogId = UserDialogId.create(entity.getName()+"Manager");
var target = new UserDialogStart(dialogId, new CallSignature("start"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ch.ivyteam.ivy.process.model.element.event.start.dialog.html.HtmlDialogMethodStart;
import ch.ivyteam.ivy.process.model.element.event.start.dialog.html.HtmlDialogStart;
import ch.ivyteam.ivy.process.model.element.event.start.value.CallSignature;
import ch.ivyteam.ivy.process.model.element.value.Mappings;
import ch.ivyteam.ivy.process.model.value.MappingCode;
import ch.ivyteam.ivy.process.model.value.scripting.QualifiedType;
import ch.ivyteam.ivy.process.model.value.scripting.VariableDesc;
Expand All @@ -22,6 +23,9 @@ public class DialogProcess {
private final IEntityClass entity;
private final String unit;

private final int x = 96;
private int y = 248;

public DialogProcess(Process process, IEntityClass entity, String unit) {
this.process = process;
this.entity = entity;
Expand All @@ -31,6 +35,7 @@ public DialogProcess(Process process, IEntityClass entity, String unit) {
public void extendProcess() {
addDbLoaderScript();
addDeleteAction();
addEditAction();
}

private void addDbLoaderScript() {
Expand All @@ -53,10 +58,7 @@ private void addDbLoaderScript() {
}

private void addDeleteAction() {
int x = 96;
int y = 248;
var delete = process.add().element(HtmlDialogMethodStart.class);
delete.getShape().moveTo(new Position(x, y));
var delete = addMethod();
delete.setName("delete(" + entity.getSimpleName() + ")");
var param = new VariableDesc("entity", new QualifiedType(entity.getName()));
delete.setSignature(new CallSignature("delete").setInputParameters(List.of(param)));
Expand All @@ -75,4 +77,20 @@ private void addDeleteAction() {
delete.setOutput(new MappingCode(code));
}

private void addEditAction() {
var edit = addMethod();
edit.setName("edit(" + entity.getSimpleName() + ")");
var param = new VariableDesc("entity", new QualifiedType(entity.getName()));
edit.setSignature(new CallSignature("edit").setInputParameters(List.of(param)));

edit.setOutput(new MappingCode(Mappings.single("out.edit", "param.entity")));
}

private HtmlDialogMethodStart addMethod() {
var delete = process.add().element(HtmlDialogMethodStart.class);
delete.getShape().moveTo(new Position(x, y));
y += 120;
return delete;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
columnClasses="ui-g-12 ui-md-3 ui-lg-2, ui-g-12 ui-md-9 ui-lg-4, ui-g-12 ui-md-3 ui-lg-2, ui-g-12 ui-md-9 ui-lg-4">

<p:outputLabel for="id" value="Id" />
<p:inputText id="id" value="#{data.edit.id}"></p:inputText>
<p:inputText id="id" value="#{data.edit.id}" readonly="true"></p:inputText>

<!-- [entity.fields] -->

Expand Down

0 comments on commit da6c56d

Please sign in to comment.