BrightSide Workbench Full Report + Source Code
Formulas.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.indicator;
20 
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.Properties;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.file.FileWatch;
29 
34 public class Formulas extends Properties {
35 
36  /* From classes */
37 
38  public String getFormula(Class javaClass) {
39  return getFormula(javaClass.getSimpleName());
40  }
41 
42  public String getFormula(Class javaClass, String defaultValue) {
43  return getFormula(javaClass.getSimpleName(), defaultValue);
44  }
45 
46  public String getFormula(String classContext) {
47  return getProperty(classContext);
48  }
49 
50  public String getFormula(String classContext, String defaultValue) {
51  return getProperty(classContext, defaultValue);
52  }
53 
54  public void setFormula(String classContext, String value) {
55  setProperty(classContext, value);
56  store();
57  }
58 
59  /* From classes with attributes */
60 
61  public String getFormulaAttr(Class javaClass, String attribute) {
62  return getFormulaAttr(javaClass.getSimpleName(), attribute);
63  }
64 
65  public String getFormulaAttr(Class javaClass, String attribute, String defaultValue) {
66  return getFormulaAttr(javaClass.getSimpleName(), attribute, defaultValue);
67  }
68 
69  public String getFormulaAttr(String classContext, String attribute) {
70  return getProperty(createProperty(classContext, attribute));
71  }
72 
73  public String getFormulaAttr(String classContext, String attribute, String defaultValue) {
74  return getProperty(createProperty(classContext, attribute), defaultValue);
75  }
76 
77  /* Class formula */
78 
79  private String createProperty(String classContext, String attribute) {
80  return classContext + "#" + attribute;
81  }
82 
83  /* Formula's context */
84 
85  private static final String formulaFile = "/WEB-INF/elephant/conf/formula-context.properties";
86 
87  private static Formulas _formulas;
88  private static long _lastLoad;
89 
90  public static Formulas load() {
91  boolean exists = FileWatch.exists(ElephantContext.getRealPath(formulaFile));
92  if(_formulas == null || FileWatch.isNewer(ElephantContext.getRealPath(formulaFile), _lastLoad) || !exists) {
93  if(exists) {
94  try(FileInputStream fis = new FileInputStream(ElephantContext.getRealPath(formulaFile))) {
95  _formulas = new Formulas();
96  _formulas.load(fis);
97  } catch (IOException ex) {
98  Logger.getLogger(Formulas.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
99  }
100  } else {
101  _formulas = new Formulas();
102  }
103  _lastLoad = FileWatch.getTime(ElephantContext.getRealPath(formulaFile));
104  }
105  return _formulas;
106  }
107 
108  private static void store() {
109  try(FileOutputStream fos = new FileOutputStream(ElephantContext.getRealPath(formulaFile))) {
110  _formulas.store(fos, null);
111  } catch (IOException ex) {
112  Logger.getLogger(Formulas.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
113  }
114  }
115 
116 }
String getFormulaAttr(String classContext, String attribute, String defaultValue)
Definition: Formulas.java:73
String getFormulaAttr(Class javaClass, String attribute, String defaultValue)
Definition: Formulas.java:65
String getFormula(String classContext, String defaultValue)
Definition: Formulas.java:50
static Formulas load()
Definition: Formulas.java:90
String getFormula(String classContext)
Definition: Formulas.java:46
String getFormulaAttr(Class javaClass, String attribute)
Definition: Formulas.java:61
void setFormula(String classContext, String value)
Definition: Formulas.java:54
String getFormulaAttr(String classContext, String attribute)
Definition: Formulas.java:69
String getFormula(Class javaClass, String defaultValue)
Definition: Formulas.java:42
String getFormula(Class javaClass)
Definition: Formulas.java:38