BrightSide Workbench Full Report + Source Code
WebContext.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.context;
20 
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.Writer;
27 import java.nio.file.Path;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.TreeMap;
31 import java.util.TreeSet;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import org.turro.string.Strings;
35 import org.turro.configuration.JsonConfiguration;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.context.IElement;
38 import org.turro.i18n.I_;
39 
44 public class WebContext {
45 
46  private boolean
47  showInNavigators,
48  showAlways,
49  traversalNavigation,
50  noIndex,
51  noFollow,
52  printContext,
53  printVersion,
54  controlVersion,
55  icon;
56  private Set<String> langs = new TreeSet<>();
57  private Map<String, String> names = new TreeMap<>();
58  private String i18nKey, order, role, redirect, target, image, webTag;
59 
60  private WebLayout layout = new WebLayout();
61  private WebElement element = new WebElement();
62  private WebPage page = new WebPage();
63 
64  public boolean isShowInNavigators() {
65  return showInNavigators;
66  }
67 
68  public void setShowInNavigators(boolean showInNavigators) {
69  this.showInNavigators = showInNavigators;
70  }
71 
72  public boolean isShowAlways() {
73  return showAlways;
74  }
75 
76  public void setShowAlways(boolean showAlways) {
77  this.showAlways = showAlways;
78  }
79 
80  public boolean isTraversalNavigation() {
81  return traversalNavigation;
82  }
83 
84  public void setTraversalNavigation(boolean traversalNavigation) {
85  this.traversalNavigation = traversalNavigation;
86  }
87 
88  public boolean isNoIndex() {
89  return noIndex;
90  }
91 
92  public void setNoIndex(boolean noIndex) {
93  this.noIndex = noIndex;
94  }
95 
96  public boolean isNoFollow() {
97  return noFollow;
98  }
99 
100  public void setNoFollow(boolean noFollow) {
101  this.noFollow = noFollow;
102  }
103 
104  public boolean isPrintContext() {
105  return printContext;
106  }
107 
108  public void setPrintContext(boolean printContext) {
109  this.printContext = printContext;
110  }
111 
112  public boolean isPrintVersion() {
113  return printVersion;
114  }
115 
116  public void setPrintVersion(boolean printVersion) {
117  this.printVersion = printVersion;
118  }
119 
120  public boolean isControlVersion() {
121  return controlVersion;
122  }
123 
124  public void setControlVersion(boolean controlVersion) {
125  this.controlVersion = controlVersion;
126  }
127 
128  public boolean isIcon() {
129  return icon;
130  }
131 
132  public void setIcon(boolean icon) {
133  this.icon = icon;
134  }
135 
136  public Set<String> getLangs() {
137  return langs;
138  }
139 
140  public void setLangs(Set<String> langs) {
141  this.langs = langs;
142  }
143 
144  public String getName() {
145  if(!Strings.isBlank(i18nKey)) {
146  return I_.get(i18nKey);
147  }
148  if(!getNames().isEmpty()) {
149  return getNames().getOrDefault(
150  "_" + I_.api().used().getLanguage(),
151  getNames().values().iterator().next());
152  }
153  return null;
154  }
155 
156  public Map<String, String> getNames() {
157  return names;
158  }
159 
160  public void setNames(Map<String, String> names) {
161  this.names = names;
162  }
163 
164  public String getI18nKey() {
165  return i18nKey;
166  }
167 
168  public void setI18nKey(String i18nKey) {
169  this.i18nKey = i18nKey;
170  }
171 
172  public String getOrder() {
173  return order;
174  }
175 
176  public void setOrder(String order) {
177  this.order = order;
178  }
179 
180  public String getRole() {
181  return role;
182  }
183 
184  public void setRole(String role) {
185  this.role = role;
186  }
187 
188  public String getRedirect() {
189  return redirect;
190  }
191 
192  public void setRedirect(String redirect) {
193  this.redirect = redirect;
194  }
195 
196  public String getTarget() {
197  return target;
198  }
199 
200  public void setTarget(String target) {
201  this.target = target;
202  }
203 
204  public String getImage() {
205  return image;
206  }
207 
208  public void setImage(String image) {
209  this.image = image;
210  }
211 
212  public String getWebTag() {
213  return webTag;
214  }
215 
216  public void setWebTag(String webTag) {
217  this.webTag = webTag;
218  }
219 
220  public WebLayout getLayout() {
221  return layout;
222  }
223 
224  public void setLayout(WebLayout layout) {
225  this.layout = layout;
226  }
227 
228  /* Element */
229 
231  return element;
232  }
233 
234  public void setElement(WebElement element) {
235  this.element = element;
236  }
237 
239  return getElement().getInstance(this);
240  }
241 
242  /* Page */
243 
244  public WebPage getPage() {
245  return page;
246  }
247 
248  public void setPage(WebPage page) {
249  this.page = page;
250  }
251 
252  /* Utils */
253 
254  public String getLangsStr() {
255  return Strings.listToCvs(getLangs());
256  }
257 
258  public void setLangsStr(String langs) {
259  this.langs.clear();
260  this.langs.addAll(Strings.csvToList(langs));
261  }
262 
263  public boolean isWikiPage() {
264  return Strings.isBlank(redirect) && getElement().isEmpty();
265  }
266 
267  private void prepareSaving() {
268  getLangs().removeIf(l -> Strings.isBlank(l));
269  getNames().entrySet().removeIf(entry ->
270  (Strings.isBlank(entry.getValue()) || Strings.isBlank(entry.getKey())));
271  getElement().getAttributes().entrySet().removeIf(entry ->
272  (Strings.isBlank(entry.getValue()) || Strings.isBlank(entry.getKey())));
273  }
274 
275  /* Dynamics */
276 
277  private transient boolean dynamic;
278 
279  public boolean isDynamic() {
280  return dynamic;
281  }
282 
283  public void setDynamic(boolean dynamic) {
284  this.dynamic = dynamic;
285  }
286 
287  /* Factory */
288 
289  public static final String
290  CONTEXT_FOLDER = "_internal",
291  CONTEXT_REPOSITORY = "repository",
292  CONTEXT_FILE = "context.el";
293 
294  public static WebContext inheritFrom(WebContext webContext) {
295  WebContext wc = new WebContext();
296  wc.setShowInNavigators(webContext.isShowInNavigators());
297  wc.setPrintContext(webContext.isPrintContext());
298  wc.setRole(webContext.getRole());
299  wc.setOrder("000");
300  wc.setNoIndex(webContext.isNoIndex());
301  wc.setNoFollow(webContext.isNoFollow());
302  wc.setPrintVersion(webContext.isPrintVersion());
303  wc.setControlVersion(webContext.isControlVersion());
305  return wc;
306  }
307 
308  public static String relativeRepository() {
309  return Path.of("/", CONTEXT_FOLDER, CONTEXT_REPOSITORY).toString();
310  }
311 
312  public static Path repository(Path path) {
313  return Path.of(path.toString(), relativeRepository());
314  }
315 
316  public static boolean existsConf(Path path) {
317  Path real = Path.of(ElephantContext.getRealPath(path.toString()),
318  CONTEXT_FOLDER, CONTEXT_FILE);
319  return real.toFile().exists();
320  }
321 
322  public static boolean removeFrom(Path path) {
323  Path real = Path.of(ElephantContext.getRealPath(path.toString()),
324  CONTEXT_FOLDER, CONTEXT_FILE);
325  return real.toFile().delete();
326  }
327 
328  public static WebContext loadFrom(Path path) {
329  Path real = Path.of(ElephantContext.getRealPath(path.toString()),
330  CONTEXT_FOLDER, CONTEXT_FILE);
331  try(Reader reader = new FileReader(real.toFile())) {
332  return JsonConfiguration.read(reader, WebContext.class);
333  } catch (IOException ex) {
334  Logger.getLogger(WebContext.class.getName()).log(Level.SEVERE, null, ex);
335  return null;
336  }
337  }
338 
339  public static void saveTo(Path path, WebContext context) {
340  Path real = Path.of(ElephantContext.getRealPath(path.toString()),
341  CONTEXT_FOLDER, CONTEXT_FILE);
342  try(Writer writer = new FileWriter(real.toFile())) {
343  context.prepareSaving();
344  JsonConfiguration.write(writer, context);
345  } catch (IOException ex) {
346  Logger.getLogger(WebContext.class.getName()).log(Level.SEVERE, null, ex);
347  }
348  }
349 
350  public static boolean createFolders(Path relativePath) {
351  File folder = Path.of(ElephantContext.getRealPath("/"), relativePath.toString()).toFile();
352  if(!folder.exists()) {
353  folder.mkdir();
354  folder.toPath().resolve(CONTEXT_FOLDER).resolve(CONTEXT_REPOSITORY).toFile().mkdirs();
355  return true;
356  } else {
357  return false;
358  }
359  }
360 
361 }
static boolean createFolders(Path relativePath)
void setPrintContext(boolean printContext)
static boolean existsConf(Path path)
void setLangs(Set< String > langs)
static boolean removeFrom(Path path)
void setTraversalNavigation(boolean traversalNavigation)
Definition: WebContext.java:84
static WebContext inheritFrom(WebContext webContext)
void setPrintVersion(boolean printVersion)
static void saveTo(Path path, WebContext context)
void setNames(Map< String, String > names)
void setShowAlways(boolean showAlways)
Definition: WebContext.java:76
static WebContext loadFrom(Path path)
void setControlVersion(boolean controlVersion)
void setShowInNavigators(boolean showInNavigators)
Definition: WebContext.java:68
Map< String, String > getAttributes()
Definition: WebElement.java:56
IElement getInstance(WebContext context)
Definition: WebElement.java:72
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65