1   /**
2    * Copyright (c) 2000-2009 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.randombibleverse.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.Randomizer;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.UnmodifiableList;
30  import com.liferay.portal.kernel.webcache.WebCacheItem;
31  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
32  import com.liferay.portal.kernel.xml.Document;
33  import com.liferay.portal.kernel.xml.Element;
34  import com.liferay.portal.kernel.xml.SAXReaderUtil;
35  import com.liferay.portlet.randombibleverse.model.Bible;
36  import com.liferay.portlet.randombibleverse.model.Verse;
37  
38  import java.net.URL;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Iterator;
43  import java.util.LinkedHashMap;
44  import java.util.List;
45  import java.util.Locale;
46  import java.util.Map;
47  
48  import javax.portlet.PortletPreferences;
49  
50  /**
51   * <a href="RBVUtil.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class RBVUtil {
57  
58      public static Bible getBible(
59          PortletPreferences preferences, Locale locale) {
60  
61          return _instance._getBible(preferences, locale);
62      }
63  
64      public static Map<String, Bible> getBibles() {
65          return _instance._bibles;
66      }
67  
68      public static Verse getVerse(
69          PortletPreferences preferences, Locale locale) {
70  
71          return _instance._getVerse(preferences, locale);
72      }
73  
74      private RBVUtil() {
75          Document doc = null;
76  
77          try {
78              ClassLoader classLoader = getClass().getClassLoader();
79  
80              URL url = classLoader.getResource(
81                  "com/liferay/portlet/randombibleverse/dependencies/" +
82                      "random_bible_verse.xml");
83  
84              doc = SAXReaderUtil.read(url);
85          }
86          catch (Exception e) {
87              _log.error(e, e);
88          }
89  
90          _bibles = new LinkedHashMap<String, Bible>();
91          _verses = new ArrayList<String>();
92  
93          Element root = doc.getRootElement();
94  
95          Iterator<Element> itr = root.element("bibles").elements(
96              "bible").iterator();
97  
98          while (itr.hasNext()) {
99              Element bible = itr.next();
100 
101             _bibles.put(
102                 bible.attributeValue("language"),
103                 new Bible(
104                     bible.attributeValue("language"),
105                     bible.attributeValue("language-name"),
106                     bible.attributeValue("version-id")));
107         }
108 
109         _bibles = Collections.unmodifiableMap(_bibles);
110 
111         itr = root.element("verses").elements("verse").iterator();
112 
113         while (itr.hasNext()) {
114             Element verse = itr.next();
115 
116             _verses.add(verse.attributeValue("location"));
117         }
118 
119         _verses = new UnmodifiableList(_verses);
120     }
121 
122     private Bible _getBible(PortletPreferences preferences, Locale locale) {
123         Bible bible = _bibles.get(
124             preferences.getValue("language", StringPool.BLANK));
125 
126         if (bible == null) {
127             bible = _bibles.get(locale.getLanguage());
128         }
129 
130         if (bible == null) {
131             bible = _bibles.get("en");
132         }
133 
134         return bible;
135     }
136 
137     private Verse _getVerse(PortletPreferences preferences, Locale locale) {
138         Bible bible = _getBible(preferences, locale);
139 
140         int i = Randomizer.getInstance().nextInt(_verses.size());
141 
142         return _getVerse(_verses.get(i), bible.getVersionId());
143     }
144 
145     private Verse _getVerse(String location, String versionId) {
146         WebCacheItem wci = new VerseWebCacheItem(location, versionId);
147 
148         return (Verse)WebCachePoolUtil.get(
149             RBVUtil.class.getName() + StringPool.PERIOD + location +
150                 StringPool.PERIOD + versionId,
151             wci);
152     }
153 
154     private static Log _log = LogFactoryUtil.getLog(RBVUtil.class);
155 
156     private static RBVUtil _instance = new RBVUtil();
157 
158     private Map<String, Bible> _bibles;
159     private List<String> _verses;
160 
161 }