1   /*
2    * Copyright 2003 - 2013 The eFaps Team
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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.admin.datamodel;
22  
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.efaps.util.EFapsException;
28  import org.efaps.util.cache.CacheReloadException;
29  
30  /**
31   * Basic class for AttributeSets.
32   *
33   * @author The eFaps Team
34   * @version $Id$
35   */
36  public class AttributeSet
37      extends Type
38  {
39      /**
40       * Needed for serialization.
41       */
42      private static final long serialVersionUID = 1L;
43  
44      /**
45       * Type of the attribute.
46       */
47      private final AttributeType attributeType;
48  
49      /**
50       * Name of the attribute.
51       */
52      private final String attributeName;
53  
54      /**
55       * attributes of this set.
56       */
57      private final Set<String> setAttributes = new HashSet<String>();
58  
59      /**
60       * @param _id               id of this set
61       * @param _type             type of his set
62       * @param _name             name of this set
63       * @param _attributeType    type of the attribute
64       * @param _sqlColNames      name of the sql column
65       * @param _tableId          id of the table
66       * @param _typeLinkId       id of the type link
67       * @param _uuid             UUID of this AttributeSet as String
68       * @throws EFapsException on error
69       */
70      //CHECKSTYLE:OFF
71      protected AttributeSet(final long _id,
72                             final Type _type,
73                             final String _name,
74                             final AttributeType _attributeType,
75                             final String _sqlColNames,
76                             final long _tableId,
77                             final long _typeLinkId,
78                             final String _uuid)
79          throws EFapsException
80      {
81          //CHECKSTYLE:ON
82          super(_id, _uuid, AttributeSet.evaluateName(_type.getName(), _name));
83  
84          this.attributeName = (_name == null) ? null : _name.trim();
85  
86          readFromDB4Properties();
87  
88          this.attributeType = _attributeType;
89  
90          final Attribute attr = new Attribute(_id, getId(), _name, _sqlColNames, SQLTable.get(_tableId), AttributeType
91                          .get("Link"), null, null);
92          addAttributes(false, attr);
93          attr.setLink(_type.getId());
94          if (_typeLinkId > 0) {
95              setParentTypeID(_typeLinkId);
96          }
97          inheritAttributes();
98      }
99  
100     /**
101      * Getter method for instance variable {@link #attributeType}.
102      *
103      * @return value of instance variable {@link #attributeType}
104      */
105     public AttributeType getAttributeType()
106     {
107         return this.attributeType;
108     }
109 
110     /**
111      * This is the getter method for instance variable {@link #sqlColNames}.
112      *
113      * @return value of instance variable {@link #sqlColNames}
114      * @see #sqlColNames
115      */
116     public List<String> getSqlColNames()
117     {
118         return getAttribute(this.attributeName).getSqlColNames();
119     }
120 
121     /**
122      * Getter method for instance variable {@link #attributeName}.
123      *
124      * @return value of instance variable {@link #attributeName}
125      */
126     public String getAttributeName()
127     {
128         return this.attributeName;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     protected void addAttributes(final boolean _inherited,
136                                  final Attribute... _attributes)
137         throws CacheReloadException
138     {
139         super.addAttributes(_inherited, _attributes);
140         // in the superconstructur this method is called, so the <code>Set<code> might not
141         // be initialised
142         if (this.setAttributes != null) {
143             for (final Attribute attribute : _attributes) {
144                 this.setAttributes.add(attribute.getName());
145             }
146         }
147     }
148 
149     /**
150      * Getter method for instance variable {@link #setAttributes}.
151      *
152      * @return value of instance variable {@link #setAttributes}
153      */
154     public Set<String> getSetAttributes()
155     {
156         return this.setAttributes;
157     }
158 
159     /**
160      * Evaluate the name. (Build the name as the set is cached).
161      *
162      * @param _typeName name of the type
163      * @param _name name of the attribute
164      * @return String
165      */
166     public static String evaluateName(final String _typeName,
167                                       final String _name)
168     {
169         final StringBuilder ret = new StringBuilder();
170         ret.append(_typeName).append(":").append(_name).toString();
171         return ret.toString();
172     }
173 
174     /**
175      * Method to get the type from the cache.
176      *
177      * @param _typeName name of the type
178      * @param _name name of the attribute
179      * @return AttributeSet
180      * @throws CacheReloadException on error
181      */
182     public static AttributeSet get(final String _typeName,
183                                    final String _name)
184         throws CacheReloadException
185     {
186         return (AttributeSet) Type.get(AttributeSet.evaluateName(_typeName, _name));
187     }
188 
189     /**
190      * Method to get the type from the cache. Searches if not found in the type
191      * hierarchy.
192      *
193      * @param _typeName name of the type
194      * @param _name name of the attribute
195      * @return AttributeSet
196      * @throws CacheReloadException on error
197      */
198     public static AttributeSet find(final String _typeName,
199                                     final String _name)
200         throws CacheReloadException
201     {
202         AttributeSet ret = (AttributeSet) Type.get(AttributeSet.evaluateName(_typeName, _name));
203         if (ret == null) {
204             if (Type.get(_typeName).getParentType() != null) {
205                 ret = AttributeSet.find(Type.get(_typeName).getParentType().getName(), _name);
206             }
207         }
208         return ret;
209     }
210 }