1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.utils;
20
21 import fr.cnes.doi.db.model.DOIProject;
22 import fr.cnes.doi.exception.DOIDbException;
23 import fr.cnes.doi.exception.DoiRuntimeException;
24 import fr.cnes.doi.plugin.PluginFactory;
25 import fr.cnes.doi.utils.spec.Requirement;
26 import java.util.List;
27 import java.util.Random;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30
31
32
33
34
35 @Requirement(reqId = Requirement.DOI_INTER_030, reqName = Requirement.DOI_INTER_030_NAME)
36 public final class UniqueProjectName {
37
38
39
40
41 private static final Logger LOGGER = LogManager.getLogger(UniqueProjectName.class.getName());
42
43
44
45
46 private UniqueProjectName() {
47 }
48
49
50
51
52
53
54 public static UniqueProjectName getInstance() {
55 return UniqueProjectNameHolder.INSTANCE;
56 }
57
58
59
60
61
62
63
64 public List<DOIProject> getProjects() throws DOIDbException {
65 LOGGER.traceEntry();
66 final List<DOIProject> projects = PluginFactory.getProjectSuffix().getProjects();
67 return LOGGER.traceExit("List of projects", projects);
68 }
69
70
71
72
73
74
75
76
77 public List<DOIProject> getProjectsFromUser(final String userName) throws DOIDbException {
78 LOGGER.traceEntry("Parameter\n userName:{}", userName);
79 final List<DOIProject> projects = PluginFactory.getProjectSuffix().getProjectsFromUser(
80 userName);
81 return LOGGER.traceExit("Projects for userName", projects);
82 }
83
84
85
86
87
88
89
90
91 private int generateId(final int maxNumber) {
92 LOGGER.traceEntry("Parameter\n maxNumber: {}", maxNumber);
93 final Random rand = new Random();
94 final int identifier = Math.abs(rand.nextInt(maxNumber));
95 return LOGGER.traceExit("ID", identifier);
96 }
97
98
99
100
101
102
103
104
105
106 private int convert(final long input, final String projectName, final int maxNumber) {
107 LOGGER.traceEntry("Parameters\n input:{}\n projectName:{}\n maxNumer:{}", input,
108 projectName, maxNumber);
109 int result = (int) input;
110 do {
111 result = (int) (result ^ (projectName.hashCode() % maxNumber));
112 } while (result > maxNumber || result < 0 || !isIdUnique(result, projectName));
113 return LOGGER.traceExit("ID", result);
114 }
115
116
117
118
119
120
121
122
123
124
125 public int getShortName(final String project, final int length) throws DOIDbException {
126 LOGGER.traceEntry("Parameters\n project:{}\n length:{}", project, length);
127 int suffixID;
128 if (length > 9) {
129 final DoiRuntimeException doiEx = new DoiRuntimeException(
130 "The short name cannot be build because the length requested is too big");
131 throw LOGGER.throwing(doiEx);
132 } else if (PluginFactory.getProjectSuffix().isExistProjectName(project)) {
133
134 suffixID = PluginFactory.getProjectSuffix().getIDFrom(project);
135 LOGGER.warn("The project {} has already an id : {}", project, suffixID);
136 } else {
137 final int maxNumber = (int) Math.pow(10.0, length);
138 final long idRandom = generateId(maxNumber);
139 suffixID = convert(idRandom, project, maxNumber);
140 LOGGER.warn("The project {} has an id : {}", project, suffixID);
141 }
142 return LOGGER.traceExit(suffixID);
143 }
144
145
146
147
148
149
150
151
152
153 private synchronized boolean isIdUnique(final int idToCheck, final String projectName) {
154 LOGGER.traceEntry("Parameters\n idToCheck:{}\n projectName:{}\n", idToCheck, projectName);
155 final boolean result;
156 if (PluginFactory.getProjectSuffix().isExistID(idToCheck)) {
157 result = false;
158 } else {
159 result = PluginFactory.getProjectSuffix().addProjectSuffix(idToCheck, projectName);
160 }
161 return LOGGER.traceExit(result);
162 }
163
164
165
166
167
168 private static class UniqueProjectNameHolder {
169
170
171
172
173 private static final UniqueProjectName INSTANCE = new UniqueProjectName();
174 }
175
176 }