View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.config;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.dom4j.Element;
26  import org.springframework.extensions.config.ConfigElement;
27  import org.springframework.extensions.config.element.ConfigElementAdapter;
28  
29  /**
30   * @author muzquiano
31   */
32  public class WebStudioConfigElement extends ConfigElementAdapter implements
33          WebStudioConfigProperties
34  {
35      public static final String CONFIG_ELEMENT_ID = "web-studio";
36  
37      protected HashMap<String, ApplicationDescriptor> applications = null;
38  
39      protected boolean developerMode = false;
40  
41      /**
42       * Default Constructor
43       */
44      public WebStudioConfigElement()
45      {
46          super(CONFIG_ELEMENT_ID);
47  
48          this.applications = new HashMap<String, ApplicationDescriptor>(16, 1.0f);
49          this.developerMode = false;
50      }
51  
52      /*
53       * (non-Javadoc)
54       * 
55       * @see org.alfresco.config.element.GenericConfigElement#combine(org.alfresco.config.ConfigElement)
56       */
57      public ConfigElement combine(ConfigElement element)
58      {
59          WebStudioConfigElement configElement = (WebStudioConfigElement) element;
60  
61          // new combined element
62          WebStudioConfigElement combinedElement = new WebStudioConfigElement();
63  
64          // copy in our things
65          combinedElement.applications.putAll(this.applications);
66  
67          // override with things from the merging object
68          combinedElement.applications.putAll(configElement.applications);
69  
70          combinedElement.developerMode = this.developerMode;
71          if (configElement.developerMode)
72          {
73              combinedElement.developerMode = configElement.developerMode;
74          }
75  
76          return combinedElement;
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see org.alfresco.web.config.WebStudioConfigProperties#isDeveloperMode()
83       */
84      public boolean isDeveloperMode()
85      {
86          return this.developerMode;
87      }
88  
89      /*
90       * (non-Javadoc)
91       * 
92       * @see org.alfresco.web.config.WebStudioConfigProperties#setDeveloperMode(boolean)
93       */
94      public void setDeveloperMode(boolean developerMode)
95      {
96          this.developerMode = developerMode;
97      }
98  
99      /*
100      * (non-Javadoc)
101      * 
102      * @see org.alfresco.web.config.WebStudioConfigProperties#getApplicationIds()
103      */
104     public String[] getApplicationIds()
105     {
106         return this.applications.keySet().toArray(
107                 new String[this.applications.size()]);
108     }
109 
110     /*
111      * (non-Javadoc)
112      * 
113      * @see org.alfresco.web.config.WebStudioConfigProperties#getApplication(java.lang.String)
114      */
115     public ApplicationDescriptor getApplication(String id)
116     {
117         return (ApplicationDescriptor) this.applications.get(id);
118     }
119 
120     // //////////////////////////
121 
122     public static class Descriptor
123     {
124         private static final String ID = "id";
125 
126         HashMap<String, Object> map;
127 
128         Descriptor(Element el)
129         {
130             List elements = el.elements();
131             for (int i = 0; i < elements.size(); i++)
132             {
133                 Element element = (Element) elements.get(i);
134                 put(element);
135             }
136         }
137 
138         public void put(Element el)
139         {
140             if (this.map == null)
141             {
142                 this.map = new HashMap<String, Object>();
143             }
144 
145             String key = el.getName();
146             Object value = (Object) el.getTextTrim();
147             if (value != null)
148             {
149                 this.map.put(key, value);
150             }
151         }
152 
153         public Object get(String key)
154         {
155             if (this.map == null)
156             {
157                 this.map = new HashMap<String, Object>();
158             }
159 
160             return (Object) this.map.get(key);
161         }
162 
163         public String getId()
164         {
165             return (String) get(ID);
166         }
167 
168         public Object getProperty(String key)
169         {
170             return get(key);
171         }
172 
173         public String getStringProperty(String key)
174         {
175             return (String) get(key);
176         }
177 
178         public String getStringPropertyEx(String key)
179         {
180             String value = getStringProperty(key);
181             if (value != null)
182             {
183                 if (value.indexOf("${id}") > -1)
184                 {
185                     value = value.replace("${id}", getId());
186                 }
187             }
188             return value;
189         }
190     }
191 
192     public static class ApplicationDescriptor extends Descriptor
193     {
194         private static final String TITLE = "title";
195         private static final String DESCRIPTION = "description";
196         private static final String IMAGEURL = "image-url";
197         private static final String CLASSNAME = "classname";
198 
199         private Map<String, PanelDescriptor> panelDescriptors;
200 
201         ApplicationDescriptor(Element el)
202         {
203             super(el);
204 
205             this.panelDescriptors = new HashMap<String, PanelDescriptor>();
206 
207             // parse out the panel descriptors
208             Element panelsElement = el.element("panels");
209             if (panelsElement != null)
210             {
211                 List panelElements = panelsElement.elements("panel");
212                 
213                 for (int z = 0; z < panelElements.size(); z++)
214                 {
215                     Element panelElement = (Element) panelElements.get(z);
216                     
217                     PanelDescriptor panelDescriptor = new PanelDescriptor(panelElement);
218                     panelDescriptors.put(panelDescriptor.getId(), panelDescriptor);
219                 }
220             }
221         }
222 
223         public String getTitle()
224         {
225             return getStringProperty(TITLE);
226         }
227 
228         public String getDescription()
229         {
230             return getStringProperty(DESCRIPTION);
231         }
232         
233         public String getImageUrl()
234         {
235             return getStringProperty(IMAGEURL);
236         }
237 
238         public String getClassName()
239         {
240             return getStringPropertyEx(CLASSNAME);
241         }
242 
243         public String[] getPanelIds()
244         {
245             return this.panelDescriptors.keySet().toArray(new String[this.panelDescriptors.keySet().size()]);
246         }
247         
248         public PanelDescriptor getPanelDescriptor(String panelId)
249         {
250             return this.panelDescriptors.get(panelId);
251         }
252     }
253 
254     public static class PanelDescriptor extends Descriptor
255     {
256         private static final String TITLE = "title";
257         private static final String CLASSNAME = "classname";
258 
259         PanelDescriptor(Element el)
260         {
261             super(el);
262         }
263 
264         public String getTitle()
265         {
266             return getStringProperty(TITLE);
267         }
268 
269         public String getClassName()
270         {
271             return getStringPropertyEx(CLASSNAME);
272         }
273     }
274 
275     protected static WebStudioConfigElement newInstance(Element elem)
276     {
277         WebStudioConfigElement configElement = new WebStudioConfigElement();
278 
279         // developer mode
280         String _developerMode = (String) elem.elementTextTrim("developer-mode");
281         if (_developerMode != null)
282         {
283             configElement.developerMode = ("true"
284                     .equalsIgnoreCase(_developerMode));
285         }
286 
287         // applications
288         Element applications = elem.element("applications");
289         if (applications != null)
290         {
291             List list = applications.elements("application");
292             for (int i = 0; i < list.size(); i++)
293             {
294                 Element el = (Element) list.get(i);
295 
296                 ApplicationDescriptor applicationDescriptor = new ApplicationDescriptor(
297                         el);
298                 configElement.applications.put(applicationDescriptor.getId(),
299                         applicationDescriptor);
300             }
301         }
302 
303         return configElement;
304     }
305 }