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 org.apache.commons.lang.StringUtils;
19  
20  import de.smartics.properties.api.config.domain.key.ApplicationId;
21  import de.smartics.properties.api.config.domain.key.EnvironmentId;
22  import de.smartics.properties.impl.config.domain.key.envapp.AbstractConfigurationKey;
23  import de.smartics.util.lang.Arg;
24  
25  /**
26   * The configuration key identifies a configuration with environment and
27   * application information. With this key all configuration values can be
28   * accessed by their local configuration keys.
29   */
30  public final class TenantUserConfigurationKey extends
31      AbstractConfigurationKey<TenantUserConfigurationKey>
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    /**
38     * The class version identifier.
39     * <p>
40     * The value of this constant is {@value}.
41     * </p>
42     */
43    private static final long serialVersionUID = 1L;
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The identifier of the tenant the configuration is for.
49     *
50     * @serial
51     */
52    private final TenantId tenantId;
53  
54    /**
55     * The identifier of the user the configuration is for.
56     *
57     * @serial
58     */
59    private final UserId userId;
60  
61    /**
62     * The pre-calculated hash code of this instance.
63     *
64     * @serial
65     */
66    private final int hashCodeValue;
67  
68    // ****************************** Initializer *******************************
69  
70    // ****************************** Constructors ******************************
71  
72    /**
73     * Convenience constructor for any tenant and any user..
74     *
75     * @param environmentId the identifier of the environment the application is
76     *          deployed in.
77     * @param applicationId the identifier of the application whose configuration
78     *          is requested.
79     * @throws NullPointerException if {@code environmentId} or
80     *           {@code applicationId} is <code>null</code>.
81     */
82    public TenantUserConfigurationKey(final EnvironmentId environmentId,
83        final ApplicationId applicationId) throws NullPointerException
84    {
85      this(environmentId, applicationId, TenantId.ANY_TENANT, UserId.ANY_USER);
86    }
87  
88    /**
89     * Default constructor.
90     *
91     * @param environmentId the identifier of the environment the application is
92     *          deployed in.
93     * @param applicationId the identifier of the application whose configuration
94     *          is requested.
95     * @param tenantId the identifier of the tenant the configuration is for.
96     * @param userId the identifier of the user the configuration is for.
97     * @throws NullPointerException if {@code environmentId},
98     *           {@code applicationId}, {@code tenantId}, or {@code userId} is
99     *           <code>null</code>.
100    */
101   public TenantUserConfigurationKey(final EnvironmentId environmentId,
102       final ApplicationId applicationId, final TenantId tenantId,
103       final UserId userId) throws NullPointerException
104   {
105     super(environmentId, applicationId);
106     this.tenantId = Arg.checkNotNull("tenantId", tenantId);
107     this.userId = Arg.checkNotNull("userId", userId);
108     this.hashCodeValue = calcHashCode();
109   }
110 
111   /**
112    * Copy constructor.
113    *
114    * @param key the key to copy.
115    * @param appId the appId to set into the copy.
116    * @throws NullPointerException if {@code key} or {@code appId} is
117    *           <code>null</code>.
118    */
119   public TenantUserConfigurationKey(final TenantUserConfigurationKey key,
120       final ApplicationId appId) throws NullPointerException
121   {
122     this(key.getEnvironmentId(), appId, key.tenantId, key.userId);
123   }
124 
125   // ****************************** Inner Classes *****************************
126 
127   // ********************************* Methods ********************************
128 
129   // --- init -----------------------------------------------------------------
130 
131   private int calcHashCode()
132   {
133     int result = super.hashCodeValue;
134     result = 37 * result + tenantId.hashCode();
135     result = 37 * result + userId.hashCode();
136 
137     return result;
138   }
139 
140   // --- get&set --------------------------------------------------------------
141 
142   /**
143    * Returns the identifier of the tenant the configuration is for.
144    *
145    * @return the identifier of the tenant the configuration is for.
146    */
147   public TenantId getTenantId()
148   {
149     return tenantId;
150   }
151 
152   /**
153    * Returns the identifier of the user the configuration is for.
154    *
155    * @return the identifier of the user the configuration is for.
156    */
157   public UserId getUserId()
158   {
159     return userId;
160   }
161 
162   // --- business -------------------------------------------------------------
163 
164   @Override
165   public boolean hasActiveDynamicParts()
166   {
167     return !(TenantId.ANY_TENANT.equals(this.tenantId) && UserId.ANY_USER
168         .equals(this.userId));
169   }
170 
171   // --- object basics --------------------------------------------------------
172 
173   /**
174    * Returns the hash code of the object.
175    *
176    * @return the hash code.
177    */
178   @Override
179   public int hashCode()
180   {
181     return hashCodeValue;
182   }
183 
184   /**
185    * Returns <code>true</code> if the given object is semantically equal to the
186    * given object, <code>false</code> otherwise.
187    *
188    * @param object the instance to compare to.
189    * @return <code>true</code> if the given object is semantically equal to the
190    *         given object, <code>false</code> otherwise.
191    */
192   @Override
193   public boolean equals(final Object object)
194   {
195     if (this == object)
196     {
197       return true;
198     }
199     else if (!super.equals(object))
200     {
201       return false;
202     }
203 
204     final TenantUserConfigurationKey other =
205         (TenantUserConfigurationKey) object;
206 
207     return (tenantId.equals(other.tenantId) && userId.equals(other.userId));
208   }
209 
210   @Override
211   public int compareTo(final TenantUserConfigurationKey o)
212   {
213     int result = super.compareTo(o);
214     if (result == 0)
215     {
216       result = tenantId.compareTo(o.tenantId);
217     }
218     if (result == 0)
219     {
220       result = userId.compareTo(o.userId);
221     }
222     return result;
223   }
224 
225   @Override
226   public String toString()
227   {
228     final StringBuilder buffer = new StringBuilder();
229 
230     final String userIdString = userId.toString();
231     if (StringUtils.isNotBlank(userIdString))
232     {
233       buffer.append(userIdString);
234     }
235     buffer.append('/');
236 
237     final String tenantIdString = tenantId.toString();
238     if (StringUtils.isNotBlank(tenantIdString))
239     {
240       buffer.append(tenantIdString);
241     }
242     buffer.append('/');
243 
244     buffer.append(super.toString());
245 
246     if (buffer.length() == 0)
247     {
248       buffer.append("<no named key>");
249     }
250     return buffer.toString();
251   }
252 }