BrightSide Workbench Full Report + Source Code
FunnelDashboard.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.web.funnel;
20 
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.List;
24 import java.util.Set;
25 import javax.servlet.ServletException;
26 import org.turro.elephant.entities.web.WebGoal;
27 import org.turro.elephant.entities.web.WebItem;
28 import org.turro.elephant.impl.abstracts.AbstractElement;
29 import org.turro.elephant.impl.util.Parser;
30 import org.turro.elephant.web.actions.WebLink;
31 import org.turro.elephant.web.tags.SessionTags;
32 import org.turro.elephant.web.tags.TagsServlet;
33 import org.turro.marker.ElephantMarker;
34 import org.turro.marker.construct.Markers;
35 import org.turro.string.Nullable;
36 import org.turro.string.Strings;
37 import org.turro.web.funnel.graph.FunnelVertex;
38 import org.turro.web.funnel.model.FunnelVertices;
39 
44 public class FunnelDashboard extends AbstractElement {
45 
46  private FunnelVertices vertices;
47  private List<String> goalActions;
48 
49  private boolean test;
50 
51  public boolean isTest() {
52  return test;
53  }
54 
55  @Override
56  public void startConstruction() throws ServletException, IOException {
57  goalActions = Strings.csvToList(getAttributes().getAttributeValue("attrib:goals", null));
58  if(!goalActions.isEmpty()) SessionGoals.get(constructor).goals(goalActions);
59  test = getAttributes().getAttributeBooleanValue("attrib:test", false);
60  checkClean();
61  vertices = FunnelVertices.load(constructor);
62  FunnelVertex vertex = vertices.current();
63  if(vertex == null) {
64  constructTargets();
65  } else {
66  constructVertex(vertex);
67  }
68  }
69 
70  private void constructTargets() throws IOException, ServletException {
71  processVertices(vertices.bySelection(false).targets(), "hlist");
72  if(test) constructTest(null);
73  }
74 
75  private void constructVertex(FunnelVertex vertex) throws IOException, ServletException {
76  PrintWriter out = getConstructor().getOut();
77  WebItem webItem = vertex.getItem();
78  Parser.processMacros(constructor, out, Strings.isBlank(webItem.getContent(), "NO_CONTENT"));
79  if(test) constructTest(vertex);
80  }
81 
82  private void constructTest(FunnelVertex vertex) throws IOException, ServletException {
83  Markers.divider().cssClass("hidden").render(constructor);
84  Markers.segment().start().render(constructor);
85  Markers.header().level(3).cssClass("dividing")
86  .caption(Nullable.of(vertex).get(v -> v.getItem().getCaption(), () -> "[TEST FUNNEL]"))
87  .render(constructor);
88  Markers.header().level(4).caption(SessionTags.get(constructor).path()).render(constructor);
89  if(vertex != null) {
90  vertices.bySelection(true);
91  Set<FunnelVertex> nexts = vertices.nexts();
92  if(nexts != null && !nexts.isEmpty()) {
93  Markers.header().level(4).caption("Next steps").render(constructor);
94  processVertices(nexts, "vlist");
95  }
96  constructBackTo(vertices.previous());
97  Set<WebGoal> goals = vertices.goals();
98  if(goals != null && !goals.isEmpty()) {
99  Markers.header().level(4).caption("Goals").render(constructor);
100  processGoals(goals, "gsummary");
101  }
102  Set<FunnelVertex> siblings = vertices.siblings();
103  if(siblings != null && !siblings.isEmpty()) {
104  Markers.header().level(4).caption("Also interesting").render(constructor);
105  processVertices(siblings, "vlist");
106  }
107  Set<WebLink> externals = vertices.externals();
108  if(externals != null && !externals.isEmpty()) {
109  Markers.header().level(4).caption("Learn more").render(constructor);
110  linkWebLinks(externals);
111  }
112  }
113  constructCleanner();
114  Markers.segment().end().render(constructor);
115  }
116 
117  private void constructCleanner() {
118  ElephantMarker marker = new ElephantMarker(constructor);
119  marker.put("backLink", constructor.getCurrentContext().getFullPath() + "?clean=true");
120  marker.put("dashboard", this);
121  marker.process("web/funnel", "cleanner");
122  }
123 
124  private void constructBackTo(FunnelVertex vertex) throws IOException, ServletException {
125  ElephantMarker marker = new ElephantMarker(constructor);
126  marker.put("vertex", vertex);
127  marker.put("backLink", TagsServlet.navigateBack(constructor, constructor.getCurrentContext().getPath()));
128  marker.put("dashboard", this);
129  marker.process("web/funnel", "back");
130  }
131 
132  private void processVertices(Set<FunnelVertex> vertices, String template) {
133  ElephantMarker marker = new ElephantMarker(constructor);
134  marker.put("vertices", vertices);
135  marker.put("dashboard", this);
136  marker.process("web/funnel", template);
137  }
138 
139  private void processGoals(Set<WebGoal> goals, String template) {
140  ElephantMarker marker = new ElephantMarker(constructor);
141  marker.put("goals", goals);
142  marker.put("dashboard", this);
143  marker.process("web/funnel", template);
144  }
145 
146  private void linkWebLinks(Set<WebLink> links) {
147  ElephantMarker marker = new ElephantMarker(constructor);
148  marker.put("links", links);
149  marker.put("dashboard", this);
150  marker.process("web/funnel", "webLinks");
151  }
152 
153  /* Test clean */
154 
155  private void checkClean() {
156  if(constructor.getParameter("clean") != null) {
157  SessionTags.remove(constructor);
158  }
159  }
160 
161 }
static FunnelVertices load(IConstructor constructor)
FunnelVertices bySelection(boolean value)