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