1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.plugin.impl.db;
20
21 import fr.cnes.doi.plugin.impl.db.service.DatabaseSingleton;
22 import fr.cnes.doi.plugin.impl.db.service.DOIDbDataAccessService;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28
29 import fr.cnes.doi.exception.DOIDbException;
30 import fr.cnes.doi.db.model.DOIProject;
31 import fr.cnes.doi.plugin.AbstractProjectSuffixPluginHelper;
32 import fr.cnes.doi.db.model.DOIUser;
33 import fr.cnes.doi.exception.DoiRuntimeException;
34 import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_MAX_ACTIVE_CONNECTIONS;
35 import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_MAX_IDLE_CONNECTIONS;
36 import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_MIN_IDLE_CONNECTIONS;
37 import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_PWD;
38 import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_URL;
39 import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_USER;
40 import fr.cnes.doi.utils.Utils;
41 import java.util.ArrayList;
42 import java.util.concurrent.ConcurrentHashMap;
43 import java.util.stream.Collectors;
44
45
46
47
48
49
50 public final class DefaultProjectSuffixImpl extends AbstractProjectSuffixPluginHelper {
51
52
53
54
55 private static final Logger LOG = LogManager.getLogger(DefaultProjectSuffixImpl.class.getName());
56
57
58
59
60 private static final String DESCRIPTION = "Provides a pre-defined list of users and groups";
61
62
63
64 private static final String VERSION = "1.0.0";
65
66
67
68 private static final String OWNER = "CNES";
69
70
71
72 private static final String AUTHOR = "Jean-Christophe Malapert";
73
74
75
76 private static final String LICENSE = "LGPLV3";
77
78
79
80 private final String NAME = this.getClass().getName();
81
82
83
84 private DOIDbDataAccessService das;
85
86
87
88
89 private Map<String, String> conf;
90
91
92
93
94 private final Map<String, Integer> options = new ConcurrentHashMap<>();
95
96
97
98
99 private boolean configured = false;
100
101
102
103
104 @Override
105 public void setConfiguration(final Object configuration) {
106 this.conf = (Map<String, String>) configuration;
107 final String dbUrl = this.conf.get(DB_URL);
108 final String dbUser = this.conf.get(DB_USER);
109 final String dbPwd = this.conf.get(DB_PWD);
110 if (this.conf.containsKey(DB_MIN_IDLE_CONNECTIONS)) {
111 this.options.put(DB_MIN_IDLE_CONNECTIONS,
112 Integer.valueOf(this.conf.get(DB_MIN_IDLE_CONNECTIONS)));
113 }
114 if (this.conf.containsKey(DB_MAX_IDLE_CONNECTIONS)) {
115 this.options.put(DB_MAX_IDLE_CONNECTIONS,
116 Integer.valueOf(this.conf.get(DB_MAX_IDLE_CONNECTIONS)));
117 }
118 if (this.conf.containsKey(DB_MAX_ACTIVE_CONNECTIONS)) {
119 this.options.put(DB_MAX_ACTIVE_CONNECTIONS,
120 Integer.valueOf(this.conf.get(DB_MAX_ACTIVE_CONNECTIONS)));
121 }
122 LOG.info("[CONF] Plugin database URL : {}", dbUrl);
123 LOG.info("[CONF] Plugin database user : {}", dbUser);
124 LOG.info("[CONF] Plugin database password : {}", Utils.transformPasswordToStars(dbPwd));
125 LOG.info("[CONF] Plugin options : {}", this.options);
126
127 this.configured = true;
128 }
129
130 @Override
131 public void initConnection() throws DoiRuntimeException {
132 DatabaseSingleton.getInstance().init(
133 this.conf.get(DB_URL), this.conf.get(DB_USER), this.conf.get(DB_PWD), this.options);
134 this.das = DatabaseSingleton.getInstance().getDatabaseAccess();
135 }
136
137
138
139
140 @Override
141 public synchronized boolean addProjectSuffix(final int projectID, final String projectName) {
142 boolean isAdded = false;
143 try {
144 das.addDOIProject(projectID, projectName);
145 LOG.info("Add projectSuffix in the database {} / {}", projectID, projectName);
146 isAdded = true;
147 } catch (DOIDbException e) {
148 LOG.fatal("The id " + projectID + " of the project " + projectName
149 + "cannot be saved in the database", e);
150 }
151 return isAdded;
152
153 }
154
155
156
157
158 @Override
159 public synchronized boolean deleteProject(final int projectID) {
160 boolean isDeleted = false;
161 try {
162 das.removeDOIProject(projectID);
163 LOG.info("Delete projectSuffix in the database {} ", projectID);
164 isDeleted = true;
165 } catch (DOIDbException e) {
166 LOG.fatal("The id " + projectID + " cannot be deleted or doest not exist", e);
167 }
168 return isDeleted;
169 }
170
171
172
173
174 @Override
175 public boolean isExistID(final int projectID) {
176 boolean isExist;
177 try {
178 final List<DOIProject> projects = getProjects();
179 if (projects.isEmpty()) {
180 isExist = false;
181 } else {
182 final Map<String, Integer> map = projects.stream().collect(
183 Collectors.toMap(DOIProject::getProjectname, DOIProject::getSuffix));
184 isExist = map.containsValue(projectID);
185 }
186 } catch (DOIDbException ex) {
187 isExist = false;
188 LOG.fatal(ex);
189 }
190 return isExist;
191 }
192
193
194
195
196 @Override
197 public boolean isExistProjectName(final String projectName) {
198 boolean isExist;
199 try {
200 final List<DOIProject> projects = getProjects();
201
202 if (projects.isEmpty()) {
203 isExist = false;
204 } else {
205 final Map<String, Integer> map = projects.stream().collect(
206 Collectors.toMap(DOIProject::getProjectname, DOIProject::getSuffix));
207 isExist = map.containsKey(projectName);
208 }
209 } catch (DOIDbException ex) {
210 isExist = false;
211 LOG.fatal(ex);
212 }
213 return isExist;
214 }
215
216
217
218
219 @Override
220 public String getProjectFrom(final int projectID) throws DOIDbException {
221 return das.getDOIProjectName(projectID);
222 }
223
224
225
226
227 @Override
228 public int getIDFrom(final String projectName) throws DOIDbException {
229 final List<DOIProject> projects = getProjects();
230 if (projects.isEmpty()) {
231 throw new DoiRuntimeException("The projects list is empty");
232 }
233 final Map<String, Integer> map = projects.stream().collect(
234 Collectors.toMap(DOIProject::getProjectname, DOIProject::getSuffix));
235 return map.get(projectName);
236 }
237
238
239
240
241 @Override
242 public List<DOIProject> getProjects() throws DOIDbException {
243 return das.getAllDOIProjects();
244 }
245
246
247
248
249 @Override
250 public List<DOIProject> getProjectsFromUser(final String userName) throws DOIDbException {
251 return das.getAllDOIProjectsForUser(userName);
252 }
253
254
255
256
257 @Override
258 public boolean renameProject(final int projectId, final String newProjectName) {
259 boolean isRenamed = false;
260 try {
261 das.renameDOIProject(projectId, newProjectName);
262 LOG.info("Rename project in the database {} to {}", projectId, newProjectName);
263 isRenamed = true;
264 } catch (DOIDbException e) {
265 LOG.fatal("An error occured while trying to rename the project " + projectId, e);
266 }
267 return isRenamed;
268 }
269
270
271
272
273 @Override
274 public String getName() {
275 return NAME;
276 }
277
278
279
280
281 @Override
282 public String getDescription() {
283 return DESCRIPTION;
284 }
285
286
287
288
289 @Override
290 public String getVersion() {
291 return VERSION;
292 }
293
294
295
296
297 @Override
298 public String getAuthor() {
299 return AUTHOR;
300 }
301
302
303
304
305 @Override
306 public String getOwner() {
307 return OWNER;
308 }
309
310
311
312
313 @Override
314 public String getLicense() {
315 return LICENSE;
316 }
317
318
319
320
321 @Override
322 public List<DOIUser> getAllDOIUsersForProject(final int doiSuffix) {
323 final List<DOIUser> doiUsers = new ArrayList<>();
324 try {
325 doiUsers.addAll(this.das.getAllDOIUsersForProject(doiSuffix));
326 } catch (DOIDbException ex) {
327 LOG.fatal("An error occured while trying to get all "
328 + "DOI users from project " + doiSuffix, ex);
329 }
330 return doiUsers;
331 }
332
333
334
335
336 @Override
337 public StringBuilder validate() {
338 return new StringBuilder();
339 }
340
341
342
343
344
345
346
347 public static boolean isPassword(final String key) {
348 return false;
349 }
350
351
352
353
354 @Override
355 public void release() {
356 try {
357 if (this.das != null) {
358 this.das.close();
359 }
360 } catch (DOIDbException ex) {
361 }
362 this.configured = false;
363 }
364
365 @Override
366 public boolean isConfigured() {
367 return this.configured;
368 }
369
370 }