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