File Source: TemplateEdit.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.ui.struts2.editor;
20
21 import java.util.Arrays;
22 import java.util.Date;
23 import java.util.List;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.WebloggerException;
28 import org.apache.roller.weblogger.business.WebloggerFactory;
29 import org.apache.roller.weblogger.business.UserManager;
30 import org.apache.roller.weblogger.config.WebloggerConfig;
31 import org.apache.roller.weblogger.pojos.WeblogPermission;
32 import org.apache.roller.weblogger.pojos.WeblogTemplate;
33 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
34 import org.apache.roller.weblogger.util.Utilities;
35 import org.apache.roller.weblogger.util.cache.CacheManager;
36 import org.apache.struts2.interceptor.validation.SkipValidation;
37
38
39 /**
40 * Action which handles editing for a single WeblogTemplate.
41 */
42 public class TemplateEdit extends UIAction {
43
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.editor.TemplateEdit__static_init
*
* Postconditions:
* init'ed(log)
*/
44 private static Log log = LogFactory.getLog(TemplateEdit.class);
45
46 // form bean for collection all template properties
47 private TemplateEditBean bean = new TemplateEditBean();
48
49 // the template we are working on
50 private WeblogTemplate template = null;
51
52
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.editor.TemplateEdit()
*
* Presumptions:
* init'ed(org/apache/roller/weblogger/ui/struts2/editor/TemplateEditBean.java.lang.Boolean.TRUE)
*
* Postconditions:
* this.actionName == &"templateEdit"
* this.actionWeblog == null
* this.authenticatedUser == null
* this.template == null
* this.weblog == null
* this.bean.contents == null
* this.bean.description == null
* this.bean.id == null
* this.bean.link == null
* this.bean.manualContentType == null
* ...
*/
53 public TemplateEdit() {
54 this.actionName = "templateEdit";
55 this.desiredMenu = "editor";
56 this.pageTitle = "pagesForm.title";
57 }
58
59
60 @Override
61 public short requiredWeblogPermissions() {
/*
P/P * Method: short requiredWeblogPermissions()
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
*
* Postconditions:
* return_value == org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN
* (soft) init'ed(return_value)
*/
62 return WeblogPermission.ADMIN;
63 }
64
65
66 public void myPrepare() {
67 try {
/*
P/P * Method: void myPrepare()
*
* Preconditions:
* this.bean != null
* init'ed(this.bean.id)
* (soft) log != null
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@68 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@68 != null
*
* Postconditions:
* possibly_updated(this.template)
*/
68 UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
69 setTemplate(mgr.getPage(getBean().getId()));
70 } catch (WebloggerException ex) {
71 log.error("Error looking up template - "+getBean().getId(), ex);
72 }
73 }
74
75
76 /**
77 * Show template edit page.
78 */
79 @SkipValidation
80 public String execute() {
81
/*
P/P * Method: String execute()
*
* Preconditions:
* init'ed(this.template)
* (soft) this.bean != null
*
* Presumptions:
* init'ed(java.lang.Boolean.FALSE)
* init'ed(java.lang.Boolean.TRUE)
*
* Postconditions:
* return_value in Addr_Set{&"input",&"list"}
* this.bean.autoContentType == One-of{old this.bean.autoContentType, java.lang.Boolean.TRUE, java.lang.Boolean.FALSE}
* possibly_updated(this.bean.contents)
* possibly_updated(this.bean.description)
* possibly_updated(this.bean.hidden)
* possibly_updated(this.bean.id)
* possibly_updated(this.bean.link)
* possibly_updated(this.bean.manualContentType)
* possibly_updated(this.bean.name)
* possibly_updated(this.bean.navbar)
* ...
*
* Test Vectors:
* this.template: Inverse{null}, Addr_Set{null}
* org.apache.commons.lang.StringUtils:isEmpty(...)@92: {0}, {1}
*/
82 if(getTemplate() == null) {
83 // TODO: i18n
84 addError("Unable to locate specified template");
85 return LIST;
86 }
87
88 WeblogTemplate page = getTemplate();
+ 89 getBean().copyFrom(template);
90
91 // empty content-type indicates that page uses auto content-type detection
92 if (StringUtils.isEmpty(page.getOutputContentType())) {
93 getBean().setAutoContentType(Boolean.TRUE);
94 } else {
95 getBean().setAutoContentType(Boolean.FALSE);
96 getBean().setManualContentType(page.getOutputContentType());
97 }
98
99 return INPUT;
100 }
101
102
103 /**
104 * Save an existing template.
105 */
106 public String save() {
107
/*
P/P * Method: String save()
*
* Preconditions:
* init'ed(this.template)
* (soft) log != null
* (soft) init'ed(this.actionWeblog)
* (soft) this.bean != null
* (soft) init'ed(this.bean.autoContentType)
* (soft) init'ed(this.bean.contents)
* (soft) init'ed(this.bean.description)
* (soft) init'ed(this.bean.hidden)
* (soft) init'ed(this.bean.id)
* (soft) this.bean.link != null
* ...
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@132 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@132 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@134 != null
*
* Postconditions:
* return_value in Addr_Set{&"input",&"list"}
*
* Test Vectors:
* this.bean.autoContentType: Addr_Set{null}, Inverse{null}
* this.template: Inverse{null}, Addr_Set{null}
* java.lang.Boolean:booleanValue(...)@123: {1}, {0}
* org.apache.roller.weblogger.ui.struts2.editor.TemplateEdit:hasActionErrors(...)@117: {1}, {0}
*/
108 if(getTemplate() == null) {
109 // TODO: i18n
110 addError("Unable to locate specified template");
111 return LIST;
112 }
113
114 // validation
115 myValidate();
116
117 if(!hasActionErrors()) try {
118
119 WeblogTemplate template = getTemplate();
120 getBean().copyTo(template);
121 template.setLastModified(new Date());
122
123 if (getBean().getAutoContentType() == null ||
124 !getBean().getAutoContentType().booleanValue()) {
125 template.setOutputContentType(getBean().getManualContentType());
126 } else {
127 // empty content-type indicates that template uses auto content-type detection
128 template.setOutputContentType(null);
129 }
130
131 // save template and flush
132 UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
133 mgr.savePage(template);
134 WebloggerFactory.getWeblogger().flush();
135
136 // notify caches
137 CacheManager.invalidate(template);
138
139 // success message
140 addMessage("pageForm.save.success", template.getName());
141
142 } catch (WebloggerException ex) {
143 log.error("Error updating page - "+getBean().getId(), ex);
144 // TODO: i18n
145 addError("Error saving template");
146 }
147
148 return INPUT;
149 }
150
151
152 private void myValidate() {
153
154 // if name changed make sure there isn't a conflict
/*
P/P * Method: void myValidate()
*
* Preconditions:
* this.bean != null
* init'ed(this.bean.name)
* this.template != null
* (soft) log != null
* (soft) init'ed(this.actionWeblog)
* (soft) this.bean.link != null
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@157 != null
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@170 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@157 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@170 != null
* org.apache.roller.weblogger.pojos.WeblogTemplate:getName(...)@155 != null
*
* Test Vectors:
* java.lang.String:equals(...)@155: {1}, {0}
* java.lang.String:equals(...)@167: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@167: {1}, {0}
* org.apache.roller.weblogger.business.UserManager:getPageByLink(...)@171: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.business.UserManager:getPageByName(...)@158: Addr_Set{null}, Inverse{null}
*/
155 if(!getTemplate().getName().equals(getBean().getName())) {
156 try {
157 UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
158 if(umgr.getPageByName(getActionWeblog(), getBean().getName()) != null) {
159 addError("pagesForm.error.alreadyExists", getBean().getName());
160 }
161 } catch (WebloggerException ex) {
162 log.error("Error checking page name uniqueness", ex);
163 }
164 }
165
166 // if link changed make sure there isn't a conflict
167 if(!StringUtils.isEmpty(getBean().getLink()) &&
168 !getBean().getLink().equals(getTemplate().getLink())) {
169 try {
170 UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
171 if(umgr.getPageByLink(getActionWeblog(), getBean().getLink()) != null) {
172 addError("pagesForm.error.alreadyExists", getBean().getLink());
173 }
174 } catch (WebloggerException ex) {
175 log.error("Error checking page link uniqueness", ex);
176 }
177 }
178 }
179
180
181 public List getTemplateLanguages() {
/*
P/P * Method: List getTemplateLanguages()
*
* Postconditions:
* init'ed(return_value)
*/
182 String langs = WebloggerConfig.getProperty("rendering.templateLanguages","velocity");
183 String[] langsArray = Utilities.stringToStringArray(langs, ",");
184 return Arrays.asList(langsArray);
185 }
186
187
188 public TemplateEditBean getBean() {
/*
P/P * Method: TemplateEditBean getBean()
*
* Preconditions:
* init'ed(this.bean)
*
* Postconditions:
* return_value == this.bean
* init'ed(return_value)
*/
189 return bean;
190 }
191
192 public void setBean(TemplateEditBean bean) {
/*
P/P * Method: void setBean(TemplateEditBean)
*
* Postconditions:
* this.bean == bean
* init'ed(this.bean)
*/
193 this.bean = bean;
194 }
195
196 public WeblogTemplate getTemplate() {
/*
P/P * Method: WeblogTemplate getTemplate()
*
* Preconditions:
* init'ed(this.template)
*
* Postconditions:
* return_value == this.template
* init'ed(return_value)
*/
197 return template;
198 }
199
200 public void setTemplate(WeblogTemplate template) {
/*
P/P * Method: void setTemplate(WeblogTemplate)
*
* Postconditions:
* this.template == template
* init'ed(this.template)
*/
201 this.template = template;
202 }
203
204 }
SofCheck Inspector Build Version : 2.18479
| TemplateEdit.java |
2009-Jan-02 14:24:54 |
| TemplateEdit.class |
2009-Sep-04 03:12:45 |