View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Factory for configuration keys.
26   */
27  final class TenantUserConfigurationKeyFactory implements
28      ConfigurationKeyFactory<TenantUserConfigurationKey>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The default key referencing any environment and any application.
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    // --- members --------------------------------------------------------------
42  
43    // ****************************** Initializer *******************************
44  
45    // ****************************** Constructors ******************************
46  
47    /**
48     * Default constructor.
49     */
50    TenantUserConfigurationKeyFactory()
51    {
52    }
53  
54    // ****************************** Inner Classes *****************************
55  
56    // ********************************* Methods ********************************
57  
58    // --- init -----------------------------------------------------------------
59  
60    // --- get&set --------------------------------------------------------------
61  
62    // --- business -------------------------------------------------------------
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   // --- object basics --------------------------------------------------------
142 
143 }