BrightSide Workbench Full Report + Source Code
AlliancePhaseDefinitions.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.alliance.content;
20 
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Set;
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.json.Jsons;
33 import org.turro.lock.Initializer;
34 import org.turro.log.WebLoggers;
35 import org.turro.phase.PhaseDefinition;
36 import org.turro.phase.PhaseType;
37 import org.turro.reflection.JSONSerializer;
38 import org.turro.security.ConceptPermission;
39 
44 public class AlliancePhaseDefinitions extends TreeSet<PhaseDefinition> {
45 
46  public PhaseDefinition get(int index) {
47  return stream().filter(pd -> pd.getIndex() == index).findFirst().orElse(null);
48  }
49 
50  public PhaseDefinition get(String name) {
51  return stream().filter(pd -> pd.getName().equals(name)).findFirst().orElse(null);
52  }
53 
54  public int getMapping(int index, String serverId) {
55  PhaseDefinition pd = get(index);
56  if(pd == null) return index;
57  return pd.getMapping().getOrDefault(serverId, index);
58  }
59 
60  /* Defaults */
61 
62  private void setDefaults() {
63  add(PhaseDefinition.create(0, "Idea", Set.of(PhaseType.CREATION))
64  .icon("lightbulb yellow")
65  .described("New idea")
66  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
67  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER)));
68  add(PhaseDefinition.create(1, "Incubator", Set.of(PhaseType.INCUBATOR))
69  .icon("egg orange")
70  .described("Project incubation")
71  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
72  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER)));
73  add(PhaseDefinition.create(2, "Proposals", Set.of(PhaseType.PROPOSALS))
74  .icon("user tag brown")
75  .described("Request for proposal")
76  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
77  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER))
78  .addPermission(ConceptPermission.allow(ConceptPermission.CONSORTIUM)));
79  add(PhaseDefinition.create(3, "Execution", Set.of(PhaseType.EXECUTION))
80  .icon("cogs teal")
81  .described("Being executed")
82  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
83  .addPermission(ConceptPermission.forbid(ConceptPermission.OFFERER).show())
84  .addPermission(ConceptPermission.forbid(ConceptPermission.CONSORTIUM).show()));
85  add(PhaseDefinition.create(4, "Resolution", Set.of(PhaseType.ARCHIVE))
86  .icon("check green")
87  .described("Project archive")
88  .addPermission(ConceptPermission.forbid(ConceptPermission.BENEFICIARY).show())
89  .addPermission(ConceptPermission.forbid(ConceptPermission.OFFERER).show())
90  .addPermission(ConceptPermission.forbid(ConceptPermission.CONSORTIUM).show()));
91  }
92 
93  /* Factory */
94 
95  private static final Initializer<AlliancePhaseDefinitions> INIT = new Initializer<>();
96  private static final FileWatch watch = new FileWatch(definitionsFile());
97 
99  return INIT.instance(() -> !watch.isNewer(), () -> load());
100  }
101 
102  public static void reset() {
103  INIT.reset();
104  }
105 
106  public static Jsons json() {
107  try(FileReader reader = new FileReader(definitionsFile())) {
108  return Jsons.read(reader);
109  } catch (IOException ex) {
111  }
112  return Jsons.array();
113  }
114 
115  private static final String PROJECT_PHASES_FILE = "/WEB-INF/elephant/conf/alliance-phases.json";
116 
117  protected static AlliancePhaseDefinitions load() {
118  AlliancePhaseDefinitions definitions = null;
119  if(watch.exists()) try {
120  watch.reset();
121  JSONSerializer ser = new JSONSerializer(true);
122  definitions = ser.fromJson(FileUtil.getContent(watch.getFile()), AlliancePhaseDefinitions.class);
123  } catch (IOException ex) {
124  Logger.getLogger(AlliancePhaseDefinitions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
125  }
126  if(definitions == null) {
127  definitions = new AlliancePhaseDefinitions();
128  definitions.setDefaults();
129  save(definitions);
130  }
131  return definitions;
132  }
133 
134  private static void save(AlliancePhaseDefinitions definitions) {
135  try (Writer writer = FileUtil.getFileWriter(watch.getFileSafe())) {
136  JSONSerializer ser = new JSONSerializer(definitions, true);
137  writer.write(ser.toJson());
138  } catch (IOException ex) {
139  Logger.getLogger(AlliancePhaseDefinitions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
140  }
141  }
142 
143  private static File definitionsFile() {
144  return new File(ElephantContext.getRealPath(PROJECT_PHASES_FILE));
145  }
146 
147 }
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static String getContent(File file)
Definition: FileUtil.java:245
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
Map< String, Integer > getMapping()
static PhaseDefinition create(int index, String name, Set< PhaseType > types)
static ConceptPermission forbid(String name)