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 org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.Logger; |
21 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
22 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
23 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
24 | |
import org.kuali.rice.core.api.style.Style; |
25 | |
import org.kuali.rice.core.api.style.StyleRepositoryService; |
26 | |
import org.kuali.rice.core.impl.services.CoreImplServiceLocator; |
27 | |
import org.kuali.rice.core.util.RiceUtilities; |
28 | |
import org.kuali.rice.ksb.api.cache.RiceCacheAdministrator; |
29 | |
|
30 | |
import javax.xml.transform.Templates; |
31 | |
import java.io.IOException; |
32 | |
import java.io.InputStream; |
33 | |
import java.net.MalformedURLException; |
34 | |
import java.util.List; |
35 | |
|
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 | |
|
66 | |
@Override |
67 | |
public Style getStyle(String styleName) { |
68 | 5 | if (StringUtils.isBlank(styleName)) { |
69 | 3 | throw new RiceIllegalArgumentException("styleName was null or blank"); |
70 | |
} |
71 | |
|
72 | |
|
73 | 2 | StyleBo style = styleDao.getStyle(styleName); |
74 | |
|
75 | 2 | if (style == null) { |
76 | 0 | String propertyName = STYLE_CONFIG_PREFIX + "." + styleName; |
77 | 0 | String location = ConfigContext.getCurrentContextConfig().getProperty(propertyName); |
78 | 0 | if (location != null) { |
79 | |
|
80 | 0 | InputStream xml = null; |
81 | |
|
82 | |
try { |
83 | 0 | xml = RiceUtilities.getResourceAsStream(location); |
84 | 0 | } catch (MalformedURLException e) { |
85 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e); |
86 | 0 | } catch (IOException e) { |
87 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e); |
88 | 0 | } |
89 | |
|
90 | 0 | if (xml == null) { |
91 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location) + ", no such file"); |
92 | |
} |
93 | |
|
94 | 0 | LOG.info("Automatically loading style '" + styleName + "' from '" + location + "' as configured by " + propertyName); |
95 | 0 | List<Style> styles = CoreImplServiceLocator.getStyleXmlLoader().parseStyles(xml); |
96 | 0 | for (Style autoLoadedStyle : styles) { |
97 | 0 | if (autoLoadedStyle.getName().equals(styleName)) { |
98 | 0 | return autoLoadedStyle; |
99 | |
} |
100 | |
} |
101 | 0 | throw new RiceRuntimeException("Failed to locate auto-loaded style '" + styleName + "' after successful parsing of file from '" + location + "' as configured by " + propertyName); |
102 | |
} |
103 | |
} |
104 | 2 | return StyleBo.to(style); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
private String getUnableToLoadMessage(String propertyName, String location) { |
115 | 0 | return "unable to load resource at '" + location + |
116 | |
"' specified by configuration parameter '" + propertyName + "'"; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
private void removeStyleFromCache(String styleName) { |
123 | 2 | LOG.info("Removing Style " + styleName + " from the style cache"); |
124 | |
|
125 | 2 | cache.flushGroup(TEMPLATES_CACHE_GROUP_NAME); |
126 | |
|
127 | 2 | } |
128 | |
|
129 | |
@Override |
130 | |
public void saveStyle(Style data) { |
131 | 3 | if (data == null) { |
132 | 1 | throw new RiceIllegalArgumentException("The given style was null."); |
133 | |
} |
134 | 2 | StyleBo styleToUpdate = StyleBo.from(data); |
135 | 2 | saveStyleBo(styleToUpdate); |
136 | 2 | } |
137 | |
|
138 | |
protected void saveStyleBo(StyleBo styleBo) { |
139 | 2 | StyleBo existingData = styleDao.getStyle(styleBo.getName()); |
140 | 2 | if (existingData != null) { |
141 | 2 | existingData.setActive(false); |
142 | 2 | styleDao.saveStyle(existingData); |
143 | |
} |
144 | 2 | styleDao.saveStyle(styleBo); |
145 | 2 | removeStyleFromCache(styleBo.getName()); |
146 | 2 | } |
147 | |
|
148 | |
@Override |
149 | |
public List<String> getAllStyleNames() { |
150 | 0 | return styleDao.getAllStyleNames(); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
protected String getTemplatesCacheKey(String styleName) { |
159 | 0 | return TEMPLATES_CACHE_GROUP_NAME + ":" + styleName; |
160 | |
} |
161 | |
|
162 | |
protected Templates fetchTemplatesFromCache(String styleName) { |
163 | 0 | return (Templates) cache.getFromCache(getTemplatesCacheKey(styleName)); |
164 | |
} |
165 | |
|
166 | |
protected void putTemplatesInCache(String styleName, Templates templates) { |
167 | 0 | cache.putInCache(getTemplatesCacheKey(styleName), templates, TEMPLATES_CACHE_GROUP_NAME); |
168 | 0 | } |
169 | |
|
170 | |
} |