1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.themegallery.util;
24  
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.ReleaseInfo;
28  import com.liferay.portlet.themegallery.model.ThemeEntry;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  
36  import org.dom4j.Document;
37  import org.dom4j.Element;
38  import org.dom4j.io.SAXReader;
39  
40  /**
41   * <a href="ThemeGalleryUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ThemeGalleryUtil {
47  
48      public static String[] getCategories() {
49          return _instance._categories;
50      }
51  
52      public static List getEntries(String categoryId) {
53          List entries = null;
54  
55          if (Validator.isNotNull(categoryId)) {
56              entries = (List)_instance._entriesByCategory.get(categoryId);
57          }
58  
59          if (entries == null) {
60              entries = (List)_instance._entriesByCategory.get("ALL");
61          }
62  
63          return entries;
64      }
65  
66      public static List getEntries(String categoryId, int begin, int end) {
67          List entries = getEntries(categoryId);
68  
69          if (end > entries.size()) {
70              end = entries.size();
71          }
72  
73          return entries.subList(begin, end);
74      }
75  
76      public static int getEntriesSize(String categoryId) {
77          List entries = getEntries(categoryId);
78  
79          return entries.size();
80      }
81  
82      private ThemeGalleryUtil() {
83          try {
84              ClassLoader classLoader = getClass().getClassLoader();
85  
86              SAXReader reader = new SAXReader();
87  
88              Document doc = reader.read(classLoader.getResourceAsStream(
89                  "com/liferay/portlet/themegallery/dependencies/gallery.xml"));
90  
91              Element root = doc.getRootElement();
92  
93              _categories = StringUtil.split(
94                  root.elementText("available-categories"));
95  
96              for (int i = 0; i < _categories.length; i++) {
97                  _entriesByCategory.put(_categories[i], new ArrayList());
98              }
99  
100             List allEntries = new ArrayList();
101 
102             _entriesByCategory.put("ALL", allEntries);
103 
104             Iterator itr = root.elements("entry").iterator();
105 
106             while (itr.hasNext()) {
107                 Element entryEl = (Element)itr.next();
108 
109                 String download = entryEl.elementText("download");
110 
111                 String version = ReleaseInfo.getVersion();
112 
113                 version = StringUtil.replace(version, " ", "");
114                 version = version.toLowerCase();
115 
116                 download = StringUtil.replace(
117                     download, ".war", "-" + version + ".war");
118 
119                 ThemeEntry entry = new ThemeEntry(
120                     entryEl.attributeValue("name"),
121                     entryEl.elementText("description"),
122                     download, entryEl.elementText("image-large"),
123                     entryEl.elementText("image-medium"),
124                     entryEl.elementText("image-small"));
125 
126                 String[] categories = StringUtil.split(
127                     entryEl.elementText("categories"));
128 
129                 for (int i = 0; i < categories.length; i++) {
130                     List entries = (List)_entriesByCategory.get(categories[i]);
131 
132                     entries.add(entry);
133                 }
134 
135                 allEntries.add(entry);
136             }
137         }
138         catch (Exception e) {
139             e.printStackTrace();
140         }
141     }
142 
143     private static ThemeGalleryUtil _instance = new ThemeGalleryUtil();
144 
145     private String[] _categories = null;
146     private Map _entriesByCategory = new HashMap();
147 
148 }