BrightSide Workbench Full Report + Source Code
VisualElements.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.visual;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.TreeSet;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.impl.util.FileUtil;
31 import org.turro.file.FileWatch;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.reflection.JSONSerializer;
34 
39 public abstract class VisualElements<F extends VisualElements, E, V extends VisualElement<F, V, E>> extends TreeSet<V> {
40 
41  public boolean allows(E entity, IContact contact, String name) {
42  for(V ve : this) {
43  if(ve.getName().equals(name) && ve.check(entity, contact)) return true;
44  }
45  return false;
46  }
47 
48  public TreeSet<V> allowed(E entity, IContact contact) {
49  TreeSet<V> allowed = new TreeSet<>();
50  for(V ve : this) {
51  if(ve.check(entity, contact)) {
52  allowed.add(ve);
53  }
54  }
55  return allowed;
56  }
57 
58  public V getElement(String name, E entity, IContact contact) {
59  for(V ve : allowed(entity, contact)) {
60  if(ve.getName().equals(name)) {
61  return ve;
62  }
63  }
64  return null;
65  }
66 
67  public V getElement(String name) {
68  for(V ve : this) {
69  if(ve.getName().equals(name)) {
70  return ve;
71  }
72  }
73  return null;
74  }
75 
76  @Override
77  public boolean add(V e) {
78  if(e.getOrder() == 0) {
79  e.setOrder(size() + 1);
80  }
81  return super.add(e);
82  }
83 
84  public V addElement(String name) {
85  V v = createElement(name);
86  return add(v) ? v : null;
87  }
88 
89  protected void setDefaults() {
90  addElement("Default");
91  }
92 
93  protected abstract F createSubElements();
94  protected abstract V createElement(String name);
95 
96  /* Visual Elements */
97 
98  private static final Map<String, VisualElements> _ves = new HashMap<>();
99  private static final Map<String, Long> _lastLoads = new HashMap<>();
100 
101  protected static <F extends VisualElements> F doLoad(String visualFile, F visuals) {
102  if(_ves.get(visualFile) == null || FileWatch.isNewer(convert(visualFile), _lastLoads.get(visualFile))
103  || !FileWatch.exists(convert(visualFile))) {
104  VisualElements ves = null;
105  try {
106  JSONSerializer ser = new JSONSerializer(true);
107  ves = ser.fromJson(FileUtil.getContent(new File(convert(visualFile))), visuals.getClass());
108  } catch (IOException ex) {
109  Logger.getLogger(VisualElements.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
110  }
111  if(ves == null) {
112  ves = visuals;
113  ves.setDefaults();
114  VisualElements.save(visualFile, ves);
115  }
116  _ves.put(visualFile, ves);
117  _lastLoads.put(visualFile, FileWatch.getTime(convert(visualFile)));
118  }
119  return (F) _ves.get(visualFile);
120  }
121 
122  private static <F extends VisualElements> void save(String visualFile, F ves) {
123  try (Writer writer = FileUtil.getFileWriter(convert(visualFile))) {
124  JSONSerializer ser = new JSONSerializer(ves, true);
125  writer.write(ser.toJson());
126  } catch (IOException ex) {
127  Logger.getLogger(VisualElements.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
128  }
129  }
130 
131  private static String convert(String visualFile) {
132  if(!visualFile.contains("/")) {
133  visualFile = "/WEB-INF/elephant/conf/" + visualFile + "-visual.json";
134  }
135  return ElephantContext.getRealPath(visualFile);
136  }
137 
138 }
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static String getContent(File file)
Definition: FileUtil.java:245