View Javadoc

1   package com.sourceforge.jpatterns.core.configuration.model;
2   
3   import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType;
4   import com.sourceforge.jpatterns.schema.CastorSectionType;
5   import com.zmicer.utils.InputArgumentUtils;
6   import com.zmicer.utils.ObjectStateUtils;
7   import com.zmicer.utils.model.ICheckable;
8   import org.apache.log4j.Logger;
9   
10  import java.util.ArrayList;
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  
15  /**
16   * This base class defines the fields are necessary for the defining the context of one/any combination of <code>JPatternsConfig</code>
17   * castor objects. Also it contains the utility methods could be used for the forming this fields (all of them are collections) with the
18   * appropriate information.
19   * <br/>
20   * Currently there are two types of the storages: sections and business items. Both of them are necessary for us as they could be use for
21   * the differenty purposes. E.g. A. the logically separate global configuration item (we would use the business items storage) and
22   * B. factory configuration is represented as storage - in this case for the retrieving the "factory" pattern we would find the appropriate
23   * section based castor object, and then the concrete implementation of the factory would retrieve the business items.
24   * <br/>
25   * The fields are introduced here could be extended (or modified) in the case the structure of the <code>JPatternsConfig</code> castor
26   * object would be changed (and we would need another storages for the configuration).
27   * <p/>
28   * note [zmicer]: be noticed some additional infrustructure methods may be required here.
29   * note [zmicer]: class is not done as abstract to allow the convenient testing. Also there is possibility it could be used as is somewhere.
30   *
31   * $Author:: zmicer             $<br/>
32   * $Rev:: 67                    $<br/>
33   * $Date:: 2007-08-28 21:37:07 #$<br/>
34   */
35  public class JPatternsConfigBaseBean implements ICheckable
36  {
37      /**
38       * Logger instance.
39       */
40      final public static Logger LOG = Logger.getLogger(JPatternsConfigBaseBean.class);
41  
42      /**
43       * The structure is necessary for the fast access to the necessary business items using the path. This path is as follows:
44       * Section Scope ID/ Section ID/ Item Scope ID/ Item Name
45       * <br/>
46       * 1st key is the scope of the section<br/>
47       * 2nd key is the name of the section castor object <br/>
48       * 3rd key is the scope of the appropriate CastorNameScopePriorityType object
49       * 4rd key is the name of the appropriate CastorNameScopePriorityType object
50       */
51      private Map<String, Map<String, Map<String, Map<String, CastorNameScopePriorityType>>>> m_businessItems =
52          new HashMap<String, Map<String, Map<String, Map<String, CastorNameScopePriorityType>>>>();
53  
54      /**
55       * This structure stores the configuration related to the "section based" castor objects used at the JPatterns framework (these types
56       * base on the <code>CastorSectionType</code> base castor type).
57       * <br/>
58       * This structure allows us to access the appropriate <code>CastorSectionType</code> object using two keys:<br/>
59       * 1st key is the scope name,<br/>
60       * 2nd kays is the name of the appropriate section
61       */
62      private Map<String, Map<String, CastorSectionType>> m_sectionItems =
63          new HashMap<String, Map<String, CastorSectionType>>();
64  
65      /**
66       * Default public constructor.
67       * <br/>
68       * Is tested at the setup method
69       */
70      public JPatternsConfigBaseBean()
71      {
72          super();
73      }
74  
75      /**
76       * Set the new value for the business items.
77       * <br/>
78       * Tested here <code>com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBeanTest#testSetGetBusinessItems()</code>
79       *
80       * @param businessItems the new value for the business items storage.
81       *                      Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
82       */
83      public void setBusinessItems(Map<String, Map<String, Map<String, Map<String, CastorNameScopePriorityType>>>> businessItems)
84      {
85          InputArgumentUtils.checkObjects(businessItems);
86          m_businessItems = businessItems;
87      }
88  
89      /**
90       * Tested here <code>com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBeanTest#testSetGetBusinessItems()</code>
91       *
92       * @return the business items storage
93       */
94      public Map<String, Map<String, Map<String, Map<String, CastorNameScopePriorityType>>>> getBusinessItems()
95      {
96          return m_businessItems;
97      }
98  
99      /**
100      * Set the new value for the section items
101      * <br/>
102      * Tested here: com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBeanTest#testSetGetSectionItems()
103      *
104      * @param sectionItems the section items storage
105      *                     Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
106      */
107     public void setSectionItems(Map<String, Map<String, CastorSectionType>> sectionItems)
108     {
109         InputArgumentUtils.checkObjects(sectionItems);
110         m_sectionItems = sectionItems;
111     }
112 
113     /**
114      * Tested here: com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBeanTest#testSetGetSectionItems()
115      *
116      * @return the section items storage
117      */
118     public Map<String, Map<String, CastorSectionType>> getSectionItems()
119     {
120         return m_sectionItems;
121     }
122 
123     /**
124      * @return another representation of the section items - the List of the <code>CastorSectionType</code> objects.
125      */
126     public List<CastorSectionType> getListOfSectionItems()
127     {
128         final List<CastorSectionType> result = new ArrayList<CastorSectionType>();
129         for (String scope : m_sectionItems.keySet())
130         {
131             for (String sectionName : m_sectionItems.get(scope).keySet())
132             {
133                 result.add(m_sectionItems.get(scope).get(sectionName));
134             }
135         }
136 
137         return result;
138     }
139 
140     /**
141      * @see com.zmicer.utils.model.ICheckable#check()
142      */
143     public boolean check()
144     {
145         return (null != m_businessItems && null != m_sectionItems);
146     }
147 
148     /**
149      * Get the business item - the instance of the CastorNameScopePriorityType class.
150      * <br/>
151      * Tested here: com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBeanTest#testSetGetBusinessItem()
152      *
153      * @param sectionScope section scope id. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
154      * @param sectionName  section name. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
155      * @param itemScope    item scope. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
156      * @param itemName     business item name. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
157      *
158      * @return the appropriate <code>CastorNameScopePriorityType</code> castor object or the <code>null</code> in the case
159      *         the provided path (these three keys) do not point to the real castor object.
160      */
161     public CastorNameScopePriorityType getBusinessItem(final String sectionScope, final String sectionName, final String itemScope,
162         final String itemName)
163     {
164         InputArgumentUtils.checkStrings(true, sectionScope, sectionName, itemScope, itemName);
165         ObjectStateUtils.strongCheck(this);
166         try
167         {
168             return m_businessItems.get(sectionScope).get(sectionName).get(itemScope).get(itemName);
169         }
170         catch (NullPointerException npe)
171         {
172             // just catch
173         }
174         return null;
175     }
176 
177     /**
178      * Set the provided business item using the provided path. In the case some parts of the path are not constructed yet - they would be
179      * constructed (new maps would be created). In the case another business items has been already stored under the same path -
180      * it would be replaced by this one.
181      * <br/>
182      * tested here:com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBeanTest#testSetGetBusinessItem()
183      *
184      * @param sectionScope sectionScope name. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
185      * @param sectionName  section name. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
186      * @param item         the castor object to be stored. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
187      *                     The item name would be get from this bean too, so <code>item.getName()</code> should not return null or empty
188      *                     String (<code>IllegalArgumentException</code> would appear in this wrong case)
189      */
190     public void setBusinessItem(final String sectionScope, final String sectionName, final CastorNameScopePriorityType item)
191     {
192         InputArgumentUtils.checkObjects(item);
193         InputArgumentUtils.checkStrings(true, sectionScope, sectionName, item.getName(), item.getScope());
194         ObjectStateUtils.strongCheck(this);
195         if (null == m_businessItems.get(sectionScope))
196         {
197             m_businessItems.put(sectionScope, new HashMap<String, Map<String, Map<String, CastorNameScopePriorityType>>>());
198         }
199         if (null == m_businessItems.get(sectionScope).get(sectionName))
200         {
201             m_businessItems.get(sectionScope).put(sectionName, new HashMap<String, Map<String, CastorNameScopePriorityType>>());
202         }
203         if (null == m_businessItems.get(sectionScope).get(sectionName).get(item.getScope()))
204         {
205             m_businessItems.get(sectionScope).get(sectionName).put(item.getScope(), new HashMap<String, CastorNameScopePriorityType>());
206         }
207         m_businessItems.get(sectionScope).get(sectionName).get(item.getScope()).put(item.getName(), item);
208     }
209 
210     /**
211      * Get the section type castor object by the pointed path (basing on the scope/section name string)
212      *
213      * @param scope       the scope name. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
214      * @param sectionName the section name. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
215      *
216      * @return the <code>CastorSectionType</code> castor object in the case of success or <code>null</code> otherwise..
217      */
218     public CastorSectionType getSection(final String scope, final String sectionName)
219     {
220         InputArgumentUtils.checkStrings(true, scope, sectionName);
221         ObjectStateUtils.strongCheck(this);
222         if (null != m_sectionItems.get(scope))
223         {
224             return m_sectionItems.get(scope).get(sectionName);
225         }
226 
227         return null;
228     }
229 
230     /**
231      * Set the pointed section <code>CastorSectionType</code> castor object to the map is used for this purposes.
232      *
233      * @param scope   the name of the scope the provided CastorSectionType castor object should belong.
234      *                Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
235      * @param section <code>CastorSectionType</code> castor object which is based for all the "section based" JPatterns configurations items
236      */
237     public void setSection(final String scope, final CastorSectionType section)
238     {
239         InputArgumentUtils.checkObjects(section);
240         InputArgumentUtils.checkStrings(true, scope, section.getName());
241         ObjectStateUtils.strongCheck(this);
242         if (null == m_sectionItems.get(scope))
243         {
244             m_sectionItems.put(scope, new HashMap<String, CastorSectionType>());
245         }
246         m_sectionItems.get(scope).put(section.getName(), section);
247     }
248 }