BrightSide Workbench Full Report + Source Code
WebActions.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.elephant.web.actions;
20 
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.elephant.web.ElContext;
26 import org.turro.elephant.web.ElContextMap;
27 import org.turro.i18n.I_;
28 import org.turro.string.Strings;
29 
34 public class WebActions {
35 
36  public WebActions constructor(IConstructor constructor) {
37  this.constructor = constructor;
38  return this;
39  }
40 
41  public WebActions image(String image) {
42  this.image = image;
43  return this;
44  }
45 
46  public WebLink link() {
47  WebActionType type = WebActionType.typeOf(action);
48  if(type != null) {
49  String command = WebActionType.actionOf(action);
50  if(image == null) image = WebActionType.imageOf(action);
51  String caption = null, url = null, icon = null;
52  switch(type) {
53  case NAVIGATE -> {
54  ElContext elctx = ElContextMap.getContext(command);
55  if(elctx != null) {
56  caption = elctx.getName();
57  url = elctx.getFullPath();
58  }
59  }
60  case TAG -> {
62  if(elctx != null) {
63  caption = elctx.getName();
64  url = elctx.getFullPath();
65  }
66  }
67  case URL -> {
68  caption = command;
69  url = command;
70  }
71  case MAIL -> {
72  caption = I_.get("Email");
73  url = action;
74  }
75  case TEL -> {
76  caption = I_.get("Call");
77  url = action;
78  }
79  case ACTION -> {
80  return switch(command) {
81  case "signup" -> WebActions.of("nav:/user/signup").link();
82  case "tellsomeone" -> WebActions.of("nav:/user/tellsomeone").link();
83  default -> null;
84  };
85  }
86  case WHATS -> {
87  caption = "Whatsapp";
88  url = "https://api.whatsapp.com/send?phone=" + command;
89  }
90  case TGRAM -> {
91  caption = "Telegram";
92  url = "https://t.me/" + command;
93  }
94  }
95  if(!Strings.isBlank(url)) {
96  return new WebLink(type, caption, url, Strings.isBlank(image, icon));
97  }
98  }
99  return new WebLink(null, null, null);
100  }
101 
102  public void silentExecute() {
103  try {
104  execute();
105  } catch (IOException ex) {
106  Logger.getLogger(WebActions.class.getName()).log(Level.SEVERE, null, ex);
107  }
108  }
109 
110  public void execute() throws IOException {
111  WebLink wl = link();
112  if(!wl.isEmpty()) {
113  switch(wl.getType()) {
114  case NAVIGATE -> constructor.redirect(wl.getUrl());
115  case URL, MAIL, TEL -> constructor.redirect(wl.getUrl());
116  case ACTION -> constructor.redirect(wl.getUrl());
117  case TAG -> constructor.redirect(wl.getUrl());
118  }
119  }
120  }
121 
122  /* Factory */
123 
124  public static WebActions of(String action) {
125  return new WebActions(action);
126  }
127 
128  private final String action;
129 
130  private IConstructor constructor;
131  private String image;
132 
133  private WebActions(String action) {
134  this.action = action;
135  }
136 
137 }
static ElContext getContextFromWebTag(String webTag)
static ElContext getContext(IConstructor constructor)
static WebActions of(String action)
WebActions constructor(IConstructor constructor)
Definition: WebActions.java:36
static String get(String msg)
Definition: I_.java:41
static WebActionType typeOf(String action)