Coverage Report - de.smartics.properties.impl.config.domain.key.rtaware.TenantUserConfigurationKeyFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TenantUserConfigurationKeyFactory
0%
0/27
0%
0/4
1.571
 
 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  0
 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  0
   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  0
   {
 52  0
   }
 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  0
     return DEFAULT;
 68  
   }
 69  
 
 70  
   @Override
 71  
   public ConfigurationKey<?> createKey(final ApplicationId applicationId)
 72  
   {
 73  0
     final RuntimeAdapter adapter = RuntimeAdapterManager.INSTANCE.adapter();
 74  0
     final ConfigurationKey<?> key =
 75  
         new TenantUserConfigurationKey(EnvironmentId.ANY_ENV, applicationId,
 76  
             adapter.getTenantId(), adapter.getUserId());
 77  0
     return key;
 78  
   }
 79  
 
 80  
   @Override
 81  
   public ConfigurationKey<?> createUniqueKey(final String id)
 82  
   {
 83  0
     final RuntimeAdapter adapter = RuntimeAdapterManager.INSTANCE.adapter();
 84  0
     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  0
     final ConfigurationKey<?> newKey =
 93  
         new TenantUserConfigurationKey((TenantUserConfigurationKey) key, appId);
 94  0
     return newKey;
 95  
   }
 96  
 
 97  
   @Override
 98  
   public ConfigurationKey<?> createKeyFromString(final String key)
 99  
     throws IllegalArgumentException
 100  
   {
 101  0
     if (key == null)
 102  
     {
 103  0
       throw new IllegalArgumentException(String.format(
 104  
           "Cannot parse key '%s', since key is null", key));
 105  
 
 106  
     }
 107  0
     final String[] keyArray = key.split("/", -1);
 108  0
     final int count = keyArray.length;
 109  0
     final int expectedCount = 4;
 110  0
     if (count != expectedCount)
 111  
     {
 112  0
       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  0
     final UserId userId = UserId.valueOf(defaultIfBlank(keyArray[0], null));
 118  0
     final TenantId tenantId =
 119  
         TenantId.valueOf(defaultIfBlank(keyArray[1], null));
 120  
 
 121  0
     final EnvironmentId environmentId =
 122  
         EnvironmentId.valueOf(defaultIfBlank(keyArray[2], null));
 123  0
     final ApplicationId applicationId =
 124  
         ApplicationId.valueOf(defaultIfBlank(keyArray[3], null));
 125  
 
 126  0
     final ConfigurationKey<?> newKey =
 127  
         new TenantUserConfigurationKey(environmentId, applicationId, tenantId,
 128  
             userId);
 129  0
     return newKey;
 130  
   }
 131  
 
 132  
   @Override
 133  
   public ConfigurationKey<?> createStaticKey(final ConfigurationKey<?> key)
 134  
   {
 135  0
     final TenantUserConfigurationKey typedKey =
 136  
         (TenantUserConfigurationKey) key;
 137  0
     return new TenantUserConfigurationKey(typedKey.getEnvironmentId(),
 138  
         typedKey.getApplicationId());
 139  
   }
 140  
 
 141  
   // --- object basics --------------------------------------------------------
 142  
 
 143  
 }