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 java.util.ArrayList;
19  import java.util.List;
20  
21  import de.smartics.properties.api.config.domain.key.ApplicationId;
22  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
23  import de.smartics.properties.api.config.domain.key.EnvironmentId;
24  import de.smartics.properties.api.config.domain.key.KeyListBuilder;
25  import de.smartics.util.lang.NullArgumentException;
26  
27  /**
28   * Explodes the key to its defaulting keys.
29   */
30  final class TenantUserKeyListBuilder implements KeyListBuilder
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    // --- members --------------------------------------------------------------
37  
38    // ****************************** Initializer *******************************
39  
40    // ****************************** Constructors ******************************
41  
42    /**
43     * Default constructor.
44     */
45    TenantUserKeyListBuilder()
46    {
47    }
48  
49    // ****************************** Inner Classes *****************************
50  
51    // ********************************* Methods ********************************
52  
53    // --- init -----------------------------------------------------------------
54  
55    // --- get&set --------------------------------------------------------------
56  
57    // --- business -------------------------------------------------------------
58  
59    @Override
60    public List<ConfigurationKey<?>> createKeyList(final ConfigurationKey<?> key)
61    {
62      final List<ConfigurationKey<?>> list = new ArrayList<ConfigurationKey<?>>();
63      addNoDups(list, key);
64  
65      final TenantUserConfigurationKey specific = addDynamicKeys(key, list);
66      addStaticKeys(list, specific);
67  
68      return list;
69    }
70  
71    private TenantUserConfigurationKey addDynamicKeys(
72        final ConfigurationKey<?> key, final List<ConfigurationKey<?>> list)
73    {
74      final TenantUserConfigurationKey specific =
75          (TenantUserConfigurationKey) key;
76  
77      final UserId userId = specific.getUserId();
78      if (!UserId.ANY_USER.equals(userId))
79      {
80        addNoDups(list, new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
81                ApplicationId.ANY_APP, TenantId.ANY_TENANT, userId));
82      }
83  
84      final TenantId tenantId = specific.getTenantId();
85      if (!TenantId.ANY_TENANT.equals(tenantId))
86      {
87        addNoDups(list, new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
88            ApplicationId.ANY_APP, tenantId, UserId.ANY_USER));
89      }
90      return specific;
91    }
92  
93    private void addStaticKeys(final List<ConfigurationKey<?>> list,
94        final TenantUserConfigurationKey specific)
95    {
96      final EnvironmentId envId = specific.getEnvironmentId();
97      final ApplicationId appId = specific.getApplicationId();
98  
99      addAppKeys(list, envId, appId);
100 
101     if (envId.getNode() != null)
102     {
103       final EnvironmentId defaultEnv = new EnvironmentId(envId.getName());
104       if (!ApplicationId.ANY_APP.equals(appId))
105       {
106         if (appId.getVersion() != null)
107         {
108           final TenantUserConfigurationKey defaultKey =
109               new TenantUserConfigurationKey(defaultEnv, appId);
110           addNoDups(list, defaultKey);
111         }
112 
113         addAppKeys(list, defaultEnv, appId);
114       }
115       else
116       {
117         addNoDups(list, new TenantUserConfigurationKey(defaultEnv,
118             ApplicationId.ANY_APP));
119       }
120     }
121 
122     addAppKeyNoEnv(list, appId);
123 
124     final TenantUserConfigurationKey defaultKey =
125         new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
126             ApplicationId.ANY_APP);
127     addNoDups(list, defaultKey);
128   }
129 
130   private static void addNoDups(final List<ConfigurationKey<?>> list,
131       final ConfigurationKey<?> key)
132   {
133     if (!list.contains(key))
134     {
135       list.add(key);
136     }
137   }
138 
139   private void addAppKeys(final List<ConfigurationKey<?>> list,
140       final EnvironmentId envId, final ApplicationId appId)
141     throws IllegalArgumentException, NullArgumentException
142   {
143     if (appId.getVersion() != null)
144     {
145       final ApplicationId defaultAppId =
146           new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
147       final TenantUserConfigurationKey defaultKey =
148           new TenantUserConfigurationKey(envId, defaultAppId);
149       addNoDups(list, defaultKey);
150     }
151 
152     if (appId.getArtifactId() != null)
153     {
154       final ApplicationId defaultAppId =
155           new ApplicationId(appId.getGroupId(), null, null);
156       final TenantUserConfigurationKey defaultKey =
157           new TenantUserConfigurationKey(envId, defaultAppId);
158       addNoDups(list, defaultKey);
159     }
160 
161     if (appId.getGroupId() != null)
162     {
163       final TenantUserConfigurationKey defaultKey =
164           new TenantUserConfigurationKey(envId, ApplicationId.ANY_APP);
165       addNoDups(list, defaultKey);
166     }
167   }
168 
169   private void addAppKeyNoEnv(final List<ConfigurationKey<?>> list,
170       final ApplicationId appId) throws NullArgumentException,
171     IllegalArgumentException
172   {
173     if (appId.getVersion() != null)
174     {
175       final TenantUserConfigurationKey defaultAppKey =
176           new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, appId);
177       addNoDups(list, defaultAppKey);
178     }
179 
180     if (appId.getArtifactId() != null)
181     {
182       final ApplicationId noTenantUserId =
183           new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
184       final TenantUserConfigurationKey defaultAppKey =
185           new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, noTenantUserId);
186       addNoDups(list, defaultAppKey);
187     }
188 
189     if (appId.getGroupId() != null)
190     {
191       final ApplicationId noTenantUserId =
192           new ApplicationId(appId.getGroupId(), null, null);
193       final TenantUserConfigurationKey defaultAppKey =
194           new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, noTenantUserId);
195       addNoDups(list, defaultAppKey);
196     }
197   }
198 
199   // --- object basics --------------------------------------------------------
200 
201 }