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