1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.style; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.net.MalformedURLException; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import javax.xml.transform.Templates; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
29 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
30 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
31 | |
import org.kuali.rice.core.api.style.Style; |
32 | |
import org.kuali.rice.core.api.style.StyleRepositoryService; |
33 | |
import org.kuali.rice.core.impl.services.CoreImplServiceLocator; |
34 | |
import org.kuali.rice.core.util.RiceUtilities; |
35 | |
import org.kuali.rice.ksb.cache.RiceCacheAdministrator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 7 | public class StyleRepositoryServiceImpl implements StyleRepositoryService { |
43 | 1 | private static final Logger LOG = Logger.getLogger(StyleRepositoryServiceImpl.class); |
44 | |
|
45 | |
static final String TEMPLATES_CACHE_GROUP_NAME = "Templates"; |
46 | |
private static final String STYLE_CONFIG_PREFIX = "edl.style"; |
47 | |
|
48 | |
private StyleDao styleDao; |
49 | |
private RiceCacheAdministrator cache; |
50 | |
|
51 | |
public void setStyleDao(StyleDao styleDao) { |
52 | 10 | this.styleDao = styleDao; |
53 | 10 | } |
54 | |
|
55 | |
public void setCache(RiceCacheAdministrator cache) { |
56 | 7 | this.cache = cache; |
57 | 7 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
@Override |
66 | |
public Style getStyle(String styleName) { |
67 | 5 | if (StringUtils.isBlank(styleName)) { |
68 | 3 | throw new RiceIllegalArgumentException("styleName was null or blank"); |
69 | |
} |
70 | |
|
71 | |
|
72 | 2 | StyleBo style = styleDao.getStyle(styleName); |
73 | |
|
74 | 2 | if (style == null) { |
75 | 0 | String propertyName = STYLE_CONFIG_PREFIX + "." + styleName; |
76 | 0 | String location = ConfigContext.getCurrentContextConfig().getProperty(propertyName); |
77 | 0 | if (location != null) { |
78 | |
|
79 | 0 | InputStream xml = null; |
80 | |
|
81 | |
try { |
82 | 0 | xml = RiceUtilities.getResourceAsStream(location); |
83 | 0 | } catch (MalformedURLException e) { |
84 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e); |
85 | 0 | } catch (IOException e) { |
86 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e); |
87 | 0 | } |
88 | |
|
89 | 0 | if (xml == null) { |
90 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location) + ", no such file"); |
91 | |
} |
92 | |
|
93 | 0 | LOG.info("Automatically importing style '" + styleName + "' from '" + location + "' as configured by "+ propertyName); |
94 | 0 | CoreImplServiceLocator.getStyleXmlLoader().loadXml(xml, null); |
95 | |
} |
96 | |
} |
97 | 2 | return StyleBo.to(style); |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private String getUnableToLoadMessage(String propertyName, String location) { |
108 | 0 | return "unable to load resource at '" + location + |
109 | |
"' specified by configuration parameter '" + propertyName + "'"; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private void removeStyleFromCache(String styleName) { |
116 | 2 | LOG.info("Removing Style " + styleName + " from the style cache"); |
117 | |
|
118 | 2 | cache.flushGroup(TEMPLATES_CACHE_GROUP_NAME); |
119 | |
|
120 | 2 | } |
121 | |
|
122 | |
@Override |
123 | |
public void saveStyle(Style data) { |
124 | 3 | if (data == null) { |
125 | 1 | throw new RiceIllegalArgumentException("The given style was null."); |
126 | |
} |
127 | 2 | StyleBo styleToUpdate = StyleBo.from(data); |
128 | 2 | saveStyleBo(styleToUpdate); |
129 | 2 | } |
130 | |
|
131 | |
protected void saveStyleBo(StyleBo styleBo) { |
132 | 2 | StyleBo existingData = styleDao.getStyle(styleBo.getName()); |
133 | 2 | if (existingData != null) { |
134 | 2 | existingData.setActive(false); |
135 | 2 | styleDao.saveStyle(existingData); |
136 | |
} |
137 | 2 | styleDao.saveStyle(styleBo); |
138 | 2 | removeStyleFromCache(styleBo.getName()); |
139 | 2 | } |
140 | |
|
141 | |
@Override |
142 | |
public List<String> getAllStyleNames() { |
143 | 0 | return styleDao.getAllStyleNames(); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
protected String getTemplatesCacheKey(String styleName) { |
152 | 0 | return TEMPLATES_CACHE_GROUP_NAME + ":" + styleName; |
153 | |
} |
154 | |
|
155 | |
protected Templates fetchTemplatesFromCache(String styleName) { |
156 | 0 | return (Templates) cache.getFromCache(getTemplatesCacheKey(styleName)); |
157 | |
} |
158 | |
|
159 | |
protected void putTemplatesInCache(String styleName, Templates templates) { |
160 | 0 | cache.putInCache(getTemplatesCacheKey(styleName), templates, TEMPLATES_CACHE_GROUP_NAME); |
161 | 0 | } |
162 | |
|
163 | |
} |