File Source: WeblogSharedTheme.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.business.themes;
20
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.weblogger.WebloggerException;
30 import org.apache.roller.weblogger.business.FileManager;
31 import org.apache.roller.weblogger.business.FileNotFoundException;
32 import org.apache.roller.weblogger.business.FilePathException;
33 import org.apache.roller.weblogger.business.WebloggerFactory;
34 import org.apache.roller.weblogger.business.UserManager;
35 import org.apache.roller.weblogger.pojos.ThemeResource;
36 import org.apache.roller.weblogger.pojos.ThemeTemplate;
37 import org.apache.roller.weblogger.pojos.WeblogTheme;
38 import org.apache.roller.weblogger.pojos.Weblog;
39
40
41 /**
42 * A WeblogTheme shared by many weblogs and backed by a SharedTheme.
43 */
44 public class WeblogSharedTheme extends WeblogTheme {
45
/*
P/P * Method: org.apache.roller.weblogger.business.themes.WeblogSharedTheme__static_init
*
* Postconditions:
* init'ed(log)
*/
46 private static Log log = LogFactory.getLog(WeblogSharedTheme.class);
47
48 private SharedTheme theme = null;
49
50
51 public WeblogSharedTheme(Weblog weblog, SharedTheme theme) {
/*
P/P * Method: void org.apache.roller.weblogger.business.themes.WeblogSharedTheme(Weblog, SharedTheme)
*
* Postconditions:
* this.theme == theme
* init'ed(this.theme)
*/
52 super(weblog);
53 this.theme = theme;
54 }
55
56
57 public String getId() {
/*
P/P * Method: String getId()
*
* Preconditions:
* this.theme != null
* init'ed(this.theme.id)
*
* Postconditions:
* return_value == this.theme.id
* init'ed(return_value)
*/
58 return this.theme.getId();
59 }
60
61 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* this.theme != null
* init'ed(this.theme.name)
*
* Postconditions:
* return_value == this.theme.name
* init'ed(return_value)
*/
62 return this.theme.getName();
63 }
64
65 public String getDescription() {
/*
P/P * Method: String getDescription()
*
* Preconditions:
* this.theme != null
* init'ed(this.theme.description)
*
* Postconditions:
* return_value == this.theme.description
* init'ed(return_value)
*/
66 return this.theme.getDescription();
67 }
68
69 public Date getLastModified() {
/*
P/P * Method: Date getLastModified()
*
* Preconditions:
* this.theme != null
* init'ed(this.theme.lastModified)
*
* Postconditions:
* return_value == this.theme.lastModified
* init'ed(return_value)
*/
70 return this.theme.getLastModified();
71 }
72
73 public boolean isEnabled() {
/*
P/P * Method: bool isEnabled()
*
* Preconditions:
* this.theme != null
* init'ed(this.theme.enabled)
*
* Postconditions:
* return_value == this.theme.enabled
* init'ed(return_value)
*/
74 return this.theme.isEnabled();
75 }
76
77
78 /**
79 * Get the collection of all templates associated with this Theme.
80 */
81 public List getTemplates() throws WebloggerException {
82
/*
P/P * Method: List getTemplates()
*
* Preconditions:
* (soft) log != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) this.theme != null
* (soft) this.weblog != null
*
* Presumptions:
* getWeblogger(...).userManager != null
* java.util.Iterator:next(...)@105 != null
* java.util.Iterator:next(...)@91 != null
* javax.persistence.Query:getResultList(...)@964 != null
* org.apache.roller.weblogger.business.themes.SharedTheme:getTemplates(...)@103 != null
* ...
*
* Postconditions:
* return_value == &new ArrayList(getTemplates#2)
* new ArrayList(getTemplates#2) num objects == 1
*/
83 Map pages = new TreeMap();
84
85 // first get the pages from the db
86 try {
87 ThemeTemplate template = null;
88 UserManager userMgr = WebloggerFactory.getWeblogger().getUserManager();
89 Iterator dbPages = userMgr.getPages(this.weblog).iterator();
90 while(dbPages.hasNext()) {
91 template = (ThemeTemplate) dbPages.next();
92 pages.put(template.getName(), template);
93 }
94 } catch(Exception e) {
95 // db error
96 log.error(e);
97 }
98
99
100 // now get theme pages if needed and put them in place of db pages
101 try {
102 ThemeTemplate template = null;
103 Iterator themePages = this.theme.getTemplates().iterator();
104 while(themePages.hasNext()) {
105 template = (ThemeTemplate) themePages.next();
106
107 // note that this will put theme pages over custom
108 // pages in the pages list, which is what we want
109 pages.put(template.getName(), template);
110 }
111 } catch(Exception e) {
112 // how??
113 log.error(e);
114 }
115
116 return new ArrayList(pages.values());
117 }
118
119
120 /**
121 * Lookup the stylesheet template for this theme.
122 * Returns null if no stylesheet can be found.
123 */
124 public ThemeTemplate getStylesheet() throws WebloggerException {
125 // stylesheet is handled differently than other templates because with
126 // the stylesheet we want to return the weblog custom version if it
127 // exists, otherwise we return the shared theme version
128
129 // load from theme first to see if we even support a stylesheet
/*
P/P * Method: ThemeTemplate getStylesheet()
*
* Preconditions:
* this.theme != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) this.weblog != null
*
* Presumptions:
* getWeblogger(...).userManager != null
* org.apache.roller.weblogger.pojos.ThemeTemplate:getLink(...)@134 != null
* umgr.strategy != null
* umgr.strategy.emf != null
* umgr.strategy.threadLocalEntityManager != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.roller.weblogger.business.themes.SharedTheme:getStylesheet(...)@130: Addr_Set{null}, Inverse{null}
*/
130 ThemeTemplate stylesheet = this.theme.getStylesheet();
131 if(stylesheet != null) {
132 // now try getting custom version from weblog
133 UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
134 ThemeTemplate override = umgr.getPageByLink(this.weblog, stylesheet.getLink());
135 if(override != null) {
136 stylesheet = override;
137 }
138 }
139
140 return stylesheet;
141 }
142
143
144 /**
145 * Lookup the default template.
146 */
147 public ThemeTemplate getDefaultTemplate() throws WebloggerException {
/*
P/P * Method: ThemeTemplate getDefaultTemplate()
*
* Preconditions:
* this.theme != null
*
* Postconditions:
* init'ed(return_value)
*/
148 return this.theme.getDefaultTemplate();
149 }
150
151
152 /**
153 * Lookup the specified template by action.
154 * Returns null if the template cannot be found.
155 */
156 public ThemeTemplate getTemplateByAction(String action) throws WebloggerException {
157
/*
P/P * Method: ThemeTemplate getTemplateByAction(String)
*
* Preconditions:
* (soft) this.theme != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* action: Inverse{null}, Addr_Set{null}
*/
158 if(action == null)
159 return null;
160
161 // NOTE: we specifically do *NOT* return templates by action from the
162 // weblog's custom templates if the weblog is using a theme because we
163 // don't want old templates to take effect when using a specific theme
164 return this.theme.getTemplateByAction(action);
165 }
166
167
168 /**
169 * Lookup the specified template by name.
170 * Returns null if the template cannot be found.
171 */
172 public ThemeTemplate getTemplateByName(String name) throws WebloggerException {
173
/*
P/P * Method: ThemeTemplate getTemplateByName(String)
*
* Preconditions:
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) this.theme != null
* (soft) this.weblog != null
*
* Presumptions:
* getWeblogger(...).userManager != null
* userMgr.strategy != null
* userMgr.strategy.emf != null
* userMgr.strategy.threadLocalEntityManager != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* name: Inverse{null}, Addr_Set{null}
* java.lang.String:equals(...)@181: {0}, {1}
* org.apache.roller.weblogger.business.themes.SharedTheme:getTemplateByName(...)@187: Inverse{null}, Addr_Set{null}
*/
174 if(name == null)
175 return null;
176
177 ThemeTemplate template = null;
178
179 // if name refers to the stylesheet then return result of getStylesheet()
180 ThemeTemplate stylesheet = getStylesheet();
181 if(stylesheet != null && name.equals(stylesheet.getName())) {
182 return stylesheet;
183 }
184
185 // first check if this user has selected a theme
186 // if so then return the proper theme template
187 template = this.theme.getTemplateByName(name);
188
189 // if we didn't get the Template from a theme then look in the db
190 if(template == null) {
191 UserManager userMgr = WebloggerFactory.getWeblogger().getUserManager();
192 template = userMgr.getPageByName(this.weblog, name);
193 }
194
195 return template;
196 }
197
198
199 /**
200 * Lookup the specified template by link.
201 * Returns null if the template cannot be found.
202 */
203 public ThemeTemplate getTemplateByLink(String link) throws WebloggerException {
204
/*
P/P * Method: ThemeTemplate getTemplateByLink(String)
*
* Preconditions:
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) this.theme != null
* (soft) this.weblog != null
*
* Presumptions:
* getWeblogger(...).userManager != null
* userMgr.strategy != null
* userMgr.strategy.emf != null
* userMgr.strategy.threadLocalEntityManager != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* link: Inverse{null}, Addr_Set{null}
* java.lang.String:equals(...)@212: {0}, {1}
* org.apache.roller.weblogger.business.themes.SharedTheme:getTemplateByLink(...)@218: Inverse{null}, Addr_Set{null}
*/
205 if(link == null)
206 return null;
207
208 ThemeTemplate template = null;
209
210 // if name refers to the stylesheet then return result of getStylesheet()
211 ThemeTemplate stylesheet = getStylesheet();
212 if(stylesheet != null && link.equals(stylesheet.getLink())) {
213 return stylesheet;
214 }
215
216 // first check if this user has selected a theme
217 // if so then return the proper theme template
218 template = this.theme.getTemplateByLink(link);
219
220 // if we didn't get the Template from a theme then look in the db
221 if(template == null) {
222 UserManager userMgr = WebloggerFactory.getWeblogger().getUserManager();
223 template = userMgr.getPageByLink(this.weblog, link);
224 }
225
226 return template;
227 }
228
229
230 /**
231 * Lookup the specified resource by path.
232 * Returns null if the resource cannot be found.
233 */
234 public ThemeResource getResource(String path) {
235
/*
P/P * Method: ThemeResource getResource(String)
*
* Preconditions:
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) this.theme != null
*
* Postconditions:
* init'ed(return_value)
* new File(getRealFile#6) num objects <= 1
* new FileManagerImpl$WeblogResourceFile(getFile#3) num objects <= 1
* new FileManagerImpl$WeblogResourceFile(getFile#3).relativePath == path
* new FileManagerImpl$WeblogResourceFile(getFile#3).relativePath != null
* new FileManagerImpl$WeblogResourceFile(getFile#3).resourceFile == &new File(getRealFile#6)
* new FileManagerImpl$WeblogResourceFile(getFile#3).weblog == this.weblog
* new FileManagerImpl$WeblogResourceFile(getFile#3).weblog != null
*
* Test Vectors:
* path: Inverse{null}, Addr_Set{null}
* org.apache.roller.weblogger.business.themes.SharedTheme:getResource(...)@242: Inverse{null}, Addr_Set{null}
*/
236 if(path == null)
237 return null;
238
239 ThemeResource resource = null;
240
241 // first check in our shared theme
242 resource = this.theme.getResource(path);
243
244 // if we didn't find it in our theme then look in weblog uploads
245 if(resource == null) {
246 try {
247 FileManager fileMgr = WebloggerFactory.getWeblogger().getFileManager();
+ 248 resource = fileMgr.getFile(this.weblog, path);
249 } catch (WebloggerException ex) {
250 // ignored, resource considered not found
251 }
252 }
253
254 return resource;
255 }
256
257 }
SofCheck Inspector Build Version : 2.18479
| WeblogSharedTheme.java |
2009-Jan-02 14:24:44 |
| WeblogSharedTheme.class |
2009-Sep-04 03:12:32 |