Coverage Report - de.smartics.properties.impl.config.domain.key.rtaware.TenantUserKeyListBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
TenantUserKeyListBuilder
0%
0/57
0%
0/24
2.714
 
 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  0
   {
 47  0
   }
 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  0
     final List<ConfigurationKey<?>> list = new ArrayList<ConfigurationKey<?>>();
 63  0
     addNoDups(list, key);
 64  
 
 65  0
     final TenantUserConfigurationKey specific = addDynamicKeys(key, list);
 66  0
     addStaticKeys(list, specific);
 67  
 
 68  0
     return list;
 69  
   }
 70  
 
 71  
   private TenantUserConfigurationKey addDynamicKeys(
 72  
       final ConfigurationKey<?> key, final List<ConfigurationKey<?>> list)
 73  
   {
 74  0
     final TenantUserConfigurationKey specific =
 75  
         (TenantUserConfigurationKey) key;
 76  
 
 77  0
     final UserId userId = specific.getUserId();
 78  0
     if (!UserId.ANY_USER.equals(userId))
 79  
     {
 80  0
       addNoDups(list, new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
 81  
               ApplicationId.ANY_APP, TenantId.ANY_TENANT, userId));
 82  
     }
 83  
 
 84  0
     final TenantId tenantId = specific.getTenantId();
 85  0
     if (!TenantId.ANY_TENANT.equals(tenantId))
 86  
     {
 87  0
       addNoDups(list, new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
 88  
           ApplicationId.ANY_APP, tenantId, UserId.ANY_USER));
 89  
     }
 90  0
     return specific;
 91  
   }
 92  
 
 93  
   private void addStaticKeys(final List<ConfigurationKey<?>> list,
 94  
       final TenantUserConfigurationKey specific)
 95  
   {
 96  0
     final EnvironmentId envId = specific.getEnvironmentId();
 97  0
     final ApplicationId appId = specific.getApplicationId();
 98  
 
 99  0
     addAppKeys(list, envId, appId);
 100  
 
 101  0
     if (envId.getNode() != null)
 102  
     {
 103  0
       final EnvironmentId defaultEnv = new EnvironmentId(envId.getName());
 104  0
       if (!ApplicationId.ANY_APP.equals(appId))
 105  
       {
 106  0
         if (appId.getVersion() != null)
 107  
         {
 108  0
           final TenantUserConfigurationKey defaultKey =
 109  
               new TenantUserConfigurationKey(defaultEnv, appId);
 110  0
           addNoDups(list, defaultKey);
 111  
         }
 112  
 
 113  0
         addAppKeys(list, defaultEnv, appId);
 114  
       }
 115  
       else
 116  
       {
 117  0
         addNoDups(list, new TenantUserConfigurationKey(defaultEnv,
 118  
             ApplicationId.ANY_APP));
 119  
       }
 120  
     }
 121  
 
 122  0
     addAppKeyNoEnv(list, appId);
 123  
 
 124  0
     final TenantUserConfigurationKey defaultKey =
 125  
         new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
 126  
             ApplicationId.ANY_APP);
 127  0
     addNoDups(list, defaultKey);
 128  0
   }
 129  
 
 130  
   private static void addNoDups(final List<ConfigurationKey<?>> list,
 131  
       final ConfigurationKey<?> key)
 132  
   {
 133  0
     if (!list.contains(key))
 134  
     {
 135  0
       list.add(key);
 136  
     }
 137  0
   }
 138  
 
 139  
   private void addAppKeys(final List<ConfigurationKey<?>> list,
 140  
       final EnvironmentId envId, final ApplicationId appId)
 141  
     throws IllegalArgumentException, NullArgumentException
 142  
   {
 143  0
     if (appId.getVersion() != null)
 144  
     {
 145  0
       final ApplicationId defaultAppId =
 146  
           new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
 147  0
       final TenantUserConfigurationKey defaultKey =
 148  
           new TenantUserConfigurationKey(envId, defaultAppId);
 149  0
       addNoDups(list, defaultKey);
 150  
     }
 151  
 
 152  0
     if (appId.getArtifactId() != null)
 153  
     {
 154  0
       final ApplicationId defaultAppId =
 155  
           new ApplicationId(appId.getGroupId(), null, null);
 156  0
       final TenantUserConfigurationKey defaultKey =
 157  
           new TenantUserConfigurationKey(envId, defaultAppId);
 158  0
       addNoDups(list, defaultKey);
 159  
     }
 160  
 
 161  0
     if (appId.getGroupId() != null)
 162  
     {
 163  0
       final TenantUserConfigurationKey defaultKey =
 164  
           new TenantUserConfigurationKey(envId, ApplicationId.ANY_APP);
 165  0
       addNoDups(list, defaultKey);
 166  
     }
 167  0
   }
 168  
 
 169  
   private void addAppKeyNoEnv(final List<ConfigurationKey<?>> list,
 170  
       final ApplicationId appId) throws NullArgumentException,
 171  
     IllegalArgumentException
 172  
   {
 173  0
     if (appId.getVersion() != null)
 174  
     {
 175  0
       final TenantUserConfigurationKey defaultAppKey =
 176  
           new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, appId);
 177  0
       addNoDups(list, defaultAppKey);
 178  
     }
 179  
 
 180  0
     if (appId.getArtifactId() != null)
 181  
     {
 182  0
       final ApplicationId noTenantUserId =
 183  
           new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
 184  0
       final TenantUserConfigurationKey defaultAppKey =
 185  
           new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, noTenantUserId);
 186  0
       addNoDups(list, defaultAppKey);
 187  
     }
 188  
 
 189  0
     if (appId.getGroupId() != null)
 190  
     {
 191  0
       final ApplicationId noTenantUserId =
 192  
           new ApplicationId(appId.getGroupId(), null, null);
 193  0
       final TenantUserConfigurationKey defaultAppKey =
 194  
           new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, noTenantUserId);
 195  0
       addNoDups(list, defaultAppKey);
 196  
     }
 197  0
   }
 198  
 
 199  
   // --- object basics --------------------------------------------------------
 200  
 
 201  
 }