1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.impl.style; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
21 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
22 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
23 | |
import org.kuali.rice.core.api.style.Style; |
24 | |
import org.kuali.rice.core.api.style.StyleRepositoryService; |
25 | |
import org.kuali.rice.core.api.util.RiceUtilities; |
26 | |
import org.kuali.rice.core.impl.services.CoreImplServiceLocator; |
27 | |
|
28 | |
import java.io.IOException; |
29 | |
import java.io.InputStream; |
30 | |
import java.net.MalformedURLException; |
31 | |
import java.util.List; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class StyleRepositoryServiceImpl implements StyleRepositoryService { |
40 | 0 | private static final Logger LOG = Logger.getLogger(StyleRepositoryServiceImpl.class); |
41 | |
|
42 | |
private static final String STYLE_CONFIG_PREFIX = "edl.style"; |
43 | |
|
44 | |
private StyleDao styleDao; |
45 | |
|
46 | |
public void setStyleDao(StyleDao styleDao) { |
47 | 0 | this.styleDao = styleDao; |
48 | 0 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Override |
56 | |
public Style getStyle(String styleName) { |
57 | 0 | if (StringUtils.isBlank(styleName)) { |
58 | 0 | throw new RiceIllegalArgumentException("styleName was null or blank"); |
59 | |
} |
60 | |
|
61 | |
|
62 | 0 | StyleBo style = styleDao.getStyle(styleName); |
63 | |
|
64 | 0 | if (style == null) { |
65 | 0 | String propertyName = STYLE_CONFIG_PREFIX + "." + styleName; |
66 | 0 | String location = ConfigContext.getCurrentContextConfig().getProperty(propertyName); |
67 | 0 | if (location != null) { |
68 | |
|
69 | |
final InputStream xml; |
70 | |
|
71 | |
try { |
72 | 0 | xml = RiceUtilities.getResourceAsStream(location); |
73 | 0 | } catch (MalformedURLException e) { |
74 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e); |
75 | 0 | } catch (IOException e) { |
76 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e); |
77 | 0 | } |
78 | |
|
79 | 0 | if (xml == null) { |
80 | 0 | throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location) + ", no such file"); |
81 | |
} |
82 | |
|
83 | 0 | LOG.info("Automatically loading style '" + styleName + "' from '" + location + "' as configured by " + propertyName); |
84 | 0 | List<Style> styles = CoreImplServiceLocator.getStyleXmlLoader().parseStyles(xml); |
85 | 0 | for (Style autoLoadedStyle : styles) { |
86 | 0 | if (autoLoadedStyle.getName().equals(styleName)) { |
87 | 0 | return autoLoadedStyle; |
88 | |
} |
89 | |
} |
90 | 0 | throw new RiceRuntimeException("Failed to locate auto-loaded style '" + styleName + "' after successful parsing of file from '" + location + "' as configured by " + propertyName); |
91 | |
} |
92 | |
} |
93 | 0 | return StyleBo.to(style); |
94 | |
} |
95 | |
|
96 | |
private String getUnableToLoadMessage(String propertyName, String location) { |
97 | 0 | return "unable to load resource at '" + location + |
98 | |
"' specified by configuration parameter '" + propertyName + "'"; |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public void saveStyle(Style data) { |
103 | 0 | if (data == null) { |
104 | 0 | throw new RiceIllegalArgumentException("The given style was null."); |
105 | |
} |
106 | 0 | StyleBo styleToUpdate = StyleBo.from(data); |
107 | 0 | saveStyleBo(styleToUpdate); |
108 | 0 | } |
109 | |
|
110 | |
protected void saveStyleBo(StyleBo styleBo) { |
111 | 0 | StyleBo existingData = styleDao.getStyle(styleBo.getName()); |
112 | 0 | if (existingData != null) { |
113 | 0 | existingData.setActive(false); |
114 | 0 | styleDao.saveStyle(existingData); |
115 | |
} |
116 | 0 | styleDao.saveStyle(styleBo); |
117 | 0 | } |
118 | |
|
119 | |
@Override |
120 | |
public List<String> getAllStyleNames() { |
121 | 0 | return styleDao.getAllStyleNames(); |
122 | |
} |
123 | |
} |