1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.domain.key.rtaware;
17
18 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
19 import de.smartics.properties.api.config.domain.key.ApplicationId;
20 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
21 import de.smartics.properties.api.config.domain.key.ConfigurationKeyFactory;
22 import de.smartics.properties.api.config.domain.key.EnvironmentId;
23
24
25
26
27 final class TenantUserConfigurationKeyFactory implements
28 ConfigurationKeyFactory<TenantUserConfigurationKey>
29 {
30
31
32
33
34
35
36
37 private static final TenantUserConfigurationKey DEFAULT =
38 new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
39 ApplicationId.ANY_APP, TenantId.ANY_TENANT, UserId.ANY_USER);
40
41
42
43
44
45
46
47
48
49
50 TenantUserConfigurationKeyFactory()
51 {
52 }
53
54
55
56
57
58
59
60
61
62
63
64 @Override
65 public TenantUserConfigurationKey createDefaultKey()
66 {
67 return DEFAULT;
68 }
69
70 @Override
71 public ConfigurationKey<?> createKey(final ApplicationId applicationId)
72 {
73 final RuntimeAdapter adapter = RuntimeAdapterManager.INSTANCE.adapter();
74 final ConfigurationKey<?> key =
75 new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, applicationId,
76 adapter.getTenantId(), adapter.getUserId());
77 return key;
78 }
79
80 @Override
81 public ConfigurationKey<?> createUniqueKey(final String id)
82 {
83 final RuntimeAdapter adapter = RuntimeAdapterManager.INSTANCE.adapter();
84 return new TenantUserConfigurationKey(new EnvironmentId(id),
85 ApplicationId.ANY_APP, adapter.getTenantId(), adapter.getUserId());
86 }
87
88 @Override
89 public ConfigurationKey<?> createKey(final ConfigurationKey<?> key,
90 final ApplicationId appId)
91 {
92 final ConfigurationKey<?> newKey =
93 new TenantUserConfigurationKey((TenantUserConfigurationKey) key, appId);
94 return newKey;
95 }
96
97 @Override
98 public ConfigurationKey<?> createKeyFromString(final String key)
99 throws IllegalArgumentException
100 {
101 if (key == null)
102 {
103 throw new IllegalArgumentException(String.format(
104 "Cannot parse key '%s', since key is null", key));
105
106 }
107 final String[] keyArray = key.split("/", -1);
108 final int count = keyArray.length;
109 final int expectedCount = 4;
110 if (count != expectedCount)
111 {
112 throw new IllegalArgumentException(String.format(
113 "Cannot parse key '%s', since %s slash-separated elements are expected,"
114 + " but found only %s.", key, expectedCount, count));
115 }
116
117 final UserId userId = UserId.valueOf(defaultIfBlank(keyArray[0], null));
118 final TenantId tenantId =
119 TenantId.valueOf(defaultIfBlank(keyArray[1], null));
120
121 final EnvironmentId environmentId =
122 EnvironmentId.valueOf(defaultIfBlank(keyArray[2], null));
123 final ApplicationId applicationId =
124 ApplicationId.valueOf(defaultIfBlank(keyArray[3], null));
125
126 final ConfigurationKey<?> newKey =
127 new TenantUserConfigurationKey(environmentId, applicationId, tenantId,
128 userId);
129 return newKey;
130 }
131
132 @Override
133 public ConfigurationKey<?> createStaticKey(final ConfigurationKey<?> key)
134 {
135 final TenantUserConfigurationKey typedKey =
136 (TenantUserConfigurationKey) key;
137 return new TenantUserConfigurationKey(typedKey.getEnvironmentId(),
138 typedKey.getApplicationId());
139 }
140
141
142
143 }