BrightSide Workbench Full Report + Source Code
PhaseDefinitions.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.phase;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.impl.util.FileUtil;
30 import org.turro.file.FileWatch;
31 import org.turro.lock.Initializer;
32 import org.turro.reflection.JSONSerializer;
33 import org.turro.security.ConceptPermission;
34 
39 public class PhaseDefinitions extends TreeSet<PhaseDefinition> {
40 
41  public PhaseDefinition get(int index) {
42  return stream().filter(pd -> pd.getIndex() == index).findFirst().orElse(null);
43  }
44 
45  public PhaseDefinition get(String name) {
46  return stream().filter(pd -> pd.getName().equals(name)).findFirst().orElse(null);
47  }
48 
49  public int getMapping(int index, String serverId) {
50  PhaseDefinition pd = get(index);
51  if(pd == null) return index;
52  return pd.getMapping().getOrDefault(serverId, index);
53  }
54 
55  /* Defaults */
56 
57  private void setDefaults() {
58  add(PhaseDefinition.create(0, "Idea", Set.of(PhaseType.CREATION))
59  .icon("lightbulb yellow")
60  .described("New idea")
61  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
62  .addPermission(ConceptPermission.allow(ConceptPermission.SUPPORT).max(1))
63  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
64  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER)));
65  add(PhaseDefinition.create(1, "Incubator", Set.of(PhaseType.INCUBATOR))
66  .icon("egg orange")
67  .described("Project incubation")
68  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
69  .addPermission(ConceptPermission.allow(ConceptPermission.SUPPORT).max(1))
70  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
71  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER)));
72  add(PhaseDefinition.create(2, "Votes", Set.of(PhaseType.VOTES))
73  .icon("vote yea violet")
74  .described("Internally voted")
75  .addMapping("domain/axserver", 2)
76  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
77  .addPermission(ConceptPermission.allow(ConceptPermission.VOTES))
78  .addPermission(ConceptPermission.allow(ConceptPermission.PROPOSALS))
79  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
80  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER))
81  .addPermission(ConceptPermission.allow(ConceptPermission.CONSORTIUM)));
82  add(PhaseDefinition.create(3, "Deciding", Set.of(PhaseType.DECISION))
83  .icon("users olive")
84  .described("Next step based on votes")
85  .addMapping("domain/axserver", 2)
86  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
87  .addPermission(ConceptPermission.forbid(ConceptPermission.VOTES).show())
88  .addPermission(ConceptPermission.allow(ConceptPermission.PROPOSALS))
89  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
90  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER))
91  .addPermission(ConceptPermission.allow(ConceptPermission.CONSORTIUM)));
92  add(PhaseDefinition.create(4, "Proposals", Set.of(PhaseType.PROPOSALS))
93  .icon("user tag brown")
94  .described("Request for proposal")
95  .addMapping("domain/axserver", 2)
96  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
97  .addPermission(ConceptPermission.allow(ConceptPermission.PROPOSALS))
98  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
99  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER))
100  .addPermission(ConceptPermission.allow(ConceptPermission.CONSORTIUM)));
101  add(PhaseDefinition.create(5, "Execution", Set.of(PhaseType.EXECUTION))
102  .icon("cogs teal")
103  .described("Being executed")
104  .addMapping("domain/axserver", 3)
105  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
106  .addPermission(ConceptPermission.allow(ConceptPermission.DEBATES))
107  .addPermission(ConceptPermission.allow(ConceptPermission.DELIVERABLES)
108  .showRole("@partner:on|@admin:is")
109  .actionScript("user.admin || dpl.isDriver(user)"))
110  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
111  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER))
112  .addPermission(ConceptPermission.allow(ConceptPermission.CONSORTIUM)));
113  add(PhaseDefinition.create(6, "Resolution", Set.of(PhaseType.ARCHIVE))
114  .icon("check green")
115  .described("Project archive")
116  .addMapping("domain/axserver", 4)
117  .addPermission(ConceptPermission.allow(ConceptPermission.TAGS))
118  .addPermission(ConceptPermission.allow(ConceptPermission.DEBATES))
119  .addPermission(ConceptPermission.forbid(ConceptPermission.DELIVERABLES).show()
120  .showRole("@partner:on|@admin:is")
121  .actionScript("user.admin || dpl.isDriver(user)"))
122  .addPermission(ConceptPermission.allow(ConceptPermission.BENEFICIARY))
123  .addPermission(ConceptPermission.allow(ConceptPermission.OFFERER))
124  .addPermission(ConceptPermission.allow(ConceptPermission.CONSORTIUM)));
125  }
126 
127  /* Factory */
128 
129  private static final Initializer<PhaseDefinitions> INIT = new Initializer<>();
130  private static final FileWatch watch = new FileWatch(definitionsFile());
131 
132  public static PhaseDefinitions instance() {
133  return INIT.instance(() -> !watch.isNewer(), () -> load());
134  }
135 
136  public static void reset() {
137  INIT.reset();
138  }
139 
140  private static final String PROJECT_PHASES_FILE = "/WEB-INF/elephant/conf/project-phases.json";
141 
142  protected static PhaseDefinitions load() {
143  PhaseDefinitions definitions = null;
144  if(watch.exists()) try {
145  watch.reset();
146  JSONSerializer ser = new JSONSerializer(true);
147  definitions = ser.fromJson(FileUtil.getContent(watch.getFile()), PhaseDefinitions.class);
148  } catch (IOException ex) {
149  Logger.getLogger(PhaseDefinitions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
150  }
151  if(definitions == null) {
152  definitions = new PhaseDefinitions();
153  definitions.setDefaults();
154  save(definitions);
155  }
156  return definitions;
157  }
158 
159  private static void save(PhaseDefinitions definitions) {
160  try (Writer writer = FileUtil.getFileWriter(watch.getFileSafe())) {
161  JSONSerializer ser = new JSONSerializer(definitions, true);
162  writer.write(ser.toJson());
163  } catch (IOException ex) {
164  Logger.getLogger(PhaseDefinitions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
165  }
166  }
167 
168  private static File definitionsFile() {
169  return new File(ElephantContext.getRealPath(PROJECT_PHASES_FILE));
170  }
171 
172 }
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static String getContent(File file)
Definition: FileUtil.java:245
Map< String, Integer > getMapping()
static PhaseDefinition create(int index, String name, Set< PhaseType > types)
static PhaseDefinitions load()
int getMapping(int index, String serverId)
static PhaseDefinitions instance()
static ConceptPermission forbid(String name)