1 | package org.expeditee.auth;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Optional;
|
---|
7 |
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 |
|
---|
10 | public enum AuthenticationTag {
|
---|
11 | Username ("txtUsername", System.getProperty("user.name")),
|
---|
12 | Password ("txtPassword", System.getProperty("user.password")),
|
---|
13 | PasswordAgain ("txtPasswordAgain", System.getProperty("user.password.again")),
|
---|
14 | NewPassword ("txtNewPassword", System.getProperty("user.new.password")),
|
---|
15 | NewPasswordAgain ("txtNewPasswordAgain", System.getProperty("user.new.password.again")),
|
---|
16 | Email ("txtEmail", System.getProperty("user.email")),
|
---|
17 | ColleagueOne ("txtColleagueOne", System.getProperty("user.colleague.one")),
|
---|
18 | ColleagueTwo ("txtColleagueTwo", System.getProperty("user.colleague.two"));
|
---|
19 |
|
---|
20 | public String val;
|
---|
21 | public String def;
|
---|
22 |
|
---|
23 | private AuthenticationTag(final String tag, final String def) {
|
---|
24 | this.val = tag;
|
---|
25 | this.def = def;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public static Optional<Map<AuthenticationTag, String>> fetchUserData(final Collection<Text> from, final AuthenticationTag... tags) {
|
---|
29 | final Map<AuthenticationTag, String> fetchedData = new HashMap<AuthenticationTag, String>();
|
---|
30 |
|
---|
31 | for (final AuthenticationTag tag: tags) {
|
---|
32 | for (final Text canditate: from) {
|
---|
33 | // use the canditate if it exists
|
---|
34 | if (canditate.hasData(tag.val)) {
|
---|
35 | fetchedData.put(tag, canditate.getText());
|
---|
36 | break;
|
---|
37 | }
|
---|
38 | // otherwise try use the default
|
---|
39 | final String property = tag.def;
|
---|
40 | if (property != null && !property.isEmpty()) {
|
---|
41 | fetchedData.put(tag, property);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (fetchedData.entrySet().size() == tags.length) {
|
---|
47 | return Optional.of(fetchedData);
|
---|
48 | } else {
|
---|
49 | return Optional.empty();
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|