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.portlet.randombibleverse.model.Bible;
30  import com.liferay.portlet.randombibleverse.model.Verse;
31  
32  import java.net.URL;
33  
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.Iterator;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.portlet.PortletPreferences;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.dom4j.Document;
48  import org.dom4j.DocumentException;
49  import org.dom4j.Element;
50  import org.dom4j.io.SAXReader;
51  
52  /**
53   * <a href="RBVUtil.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class RBVUtil {
59  
60      public static Bible getBible(PortletPreferences prefs, Locale locale) {
61          return _instance._getBible(prefs, locale);
62      }
63  
64      public static Map<String, Bible> getBibles() {
65          return _instance._bibles;
66      }
67  
68      public static Verse getVerse(PortletPreferences prefs, Locale locale) {
69          return _instance._getVerse(prefs, locale);
70      }
71  
72      private RBVUtil() {
73          Document doc = null;
74  
75          try {
76              SAXReader reader = new SAXReader();
77  
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 = reader.read(url);
85          }
86          catch (DocumentException de) {
87              _log.error(de);
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 = Collections.unmodifiableList(_verses);
120     }
121 
122     private Bible _getBible(PortletPreferences prefs, Locale locale) {
123         Bible bible = _bibles.get(prefs.getValue("language", StringPool.BLANK));
124 
125         if (bible == null) {
126             bible = _bibles.get(locale.getLanguage());
127         }
128 
129         if (bible == null) {
130             bible = _bibles.get("en");
131         }
132 
133         return bible;
134     }
135 
136     private Verse _getVerse(PortletPreferences prefs, Locale locale) {
137         Bible bible = _getBible(prefs, locale);
138 
139         int i = Randomizer.getInstance().nextInt(_verses.size());
140 
141         return _getVerse(_verses.get(i), bible.getVersionId());
142     }
143 
144     private Verse _getVerse(String location, String versionId) {
145         WebCacheItem wci = new VerseWebCacheItem(location, versionId);
146 
147         return (Verse)WebCachePoolUtil.get(
148             RBVUtil.class.getName() + StringPool.PERIOD + location +
149                 StringPool.PERIOD + versionId,
150             wci);
151     }
152 
153     private static Log _log = LogFactory.getLog(RBVUtil.class);
154 
155     private static RBVUtil _instance = new RBVUtil();
156 
157     private Map<String, Bible> _bibles;
158     private List<String> _verses;
159 
160 }