1 | package org.expeditee.auth.tags;
|
---|
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 | EmailAgain ("txtEmailAgain", System.getProperty("user.email.again")),
|
---|
18 | ColleagueOne ("txtColleagueOne", System.getProperty("user.colleague.one")),
|
---|
19 | ColleagueTwo ("txtColleagueTwo", System.getProperty("user.colleague.two")),
|
---|
20 | Frameset ("txtFrameset", System.getProperty("user.toshare.frameset"));
|
---|
21 |
|
---|
22 | public String val;
|
---|
23 | public String def;
|
---|
24 |
|
---|
25 | private AuthenticationTag(String tag, String def) {
|
---|
26 | this.val = tag;
|
---|
27 | this.def = def;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static Optional<Map<AuthenticationTag, String>> fetchUserData(Collection<Text> from, boolean useDefaults, AuthenticationTag... tags) {
|
---|
31 | final Map<AuthenticationTag, String> fetchedData = new HashMap<AuthenticationTag, String>();
|
---|
32 |
|
---|
33 | for (AuthenticationTag tag: tags) {
|
---|
34 | for (Text canditate: from) {
|
---|
35 | // use the canditate if it exists
|
---|
36 | if (canditate.hasData(tag.val)) {
|
---|
37 | if (tag.val.equals("txtUsername")) {
|
---|
38 | fetchedData.put(tag, canditate.getText().toLowerCase());
|
---|
39 | } else {
|
---|
40 | fetchedData.put(tag, canditate.getText());
|
---|
41 | }
|
---|
42 | break;
|
---|
43 | }
|
---|
44 | // otherwise try use the default
|
---|
45 | String property = tag.def;
|
---|
46 | if (property != null && !property.isEmpty()) {
|
---|
47 | fetchedData.put(tag, useDefaults ? property : null);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (fetchedData.entrySet().size() == tags.length) {
|
---|
53 | return Optional.of(fetchedData);
|
---|
54 | } else {
|
---|
55 | return Optional.empty();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|