File Source: CategoryRemove.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.Collections;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.weblogger.WebloggerException;
29 import org.apache.roller.weblogger.business.WebloggerFactory;
30 import org.apache.roller.weblogger.business.WeblogManager;
31 import org.apache.roller.weblogger.pojos.WeblogPermission;
32 import org.apache.roller.weblogger.pojos.WeblogCategory;
33 import org.apache.roller.weblogger.pojos.WeblogCategoryPathComparator;
34 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
35 import org.apache.roller.weblogger.util.cache.CacheManager;
36
37
38 /**
39 * Remove a category.
40 */
41 public class CategoryRemove extends UIAction {
42
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.editor.CategoryRemove__static_init
*
* Postconditions:
* init'ed(log)
*/
43 private static Log log = LogFactory.getLog(CategoryRemove.class);
44
45 // id of category to remove
46 private String removeId = null;
47
48 // category object that we will remove
49 private WeblogCategory category = null;
50
51 // category id of the category to move to
52 private String targetCategoryId = null;
53
54 // all categories from the action weblog
55 private Set allCategories = Collections.EMPTY_SET;
56
57
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.editor.CategoryRemove()
*
* Presumptions:
* init'ed(java.util.Collections.EMPTY_SET)
*
* Postconditions:
* this.actionName == &"categoryRemove"
* this.allCategories == java.util.Collections.EMPTY_SET
* (soft) init'ed(this.allCategories)
* this.category == null
* this.removeId == null
* this.targetCategoryId == null
* this.desiredMenu == &"editor"
* this.pageTitle == &"categoriesForm.rootTitle"
*/
58 public CategoryRemove() {
59 this.actionName = "categoryRemove";
60 this.desiredMenu = "editor";
61 this.pageTitle = "categoriesForm.rootTitle";
62 }
63
64
65 public short requiredWeblogPermissions() {
/*
P/P * Method: short requiredWeblogPermissions()
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
*
* Postconditions:
* return_value == org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR
* (soft) init'ed(return_value)
*/
66 return WeblogPermission.AUTHOR;
67 }
68
69
70 public void myPrepare() {
71 try {
/*
P/P * Method: void myPrepare()
*
* Preconditions:
* (soft) log != null
* (soft) init'ed(this.removeId)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@72 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@72 != null
*
* Postconditions:
* possibly_updated(this.category)
*
* Test Vectors:
* org.apache.commons.lang.StringUtils:isEmpty(...)@73: {1}, {0}
*/
72 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
73 if(!StringUtils.isEmpty(getRemoveId())) {
74 setCategory(wmgr.getWeblogCategory(getRemoveId()));
75 }
76 } catch (WebloggerException ex) {
77 log.error("Error looking up category", ex);
78 }
79 }
80
81
82 /**
83 * Display the remove template confirmation.
84 */
85 public String execute() {
86
87 // build list of categories for display
/*
P/P * Method: String execute()
*
* Preconditions:
* (soft) log != null
* (soft) init'ed(this.removeId)
*
* Presumptions:
* java.util.Iterator:next(...)@94 != null
* org.apache.roller.weblogger.business.WeblogManager:getWeblogCategories(...)@93 != null
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@92 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@92 != null
* org.apache.roller.weblogger.pojos.WeblogCategory:getId(...)@95 != null
*
* Postconditions:
* return_value == &"input"
* this.allCategories == One-of{old this.allCategories, &new TreeSet(execute#1)}
* new TreeSet(execute#1) num objects == 1
*
* Test Vectors:
* java.lang.String:equals(...)@95: {1}, {0}
* java.util.TreeSet:size(...)@105: {-231..0}, {1..232-1}
*/
88 TreeSet allCategories = new TreeSet(new WeblogCategoryPathComparator());
89
90 try {
91 // Build list of all categories, except for current one, sorted by path.
92 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
93 List<WeblogCategory> cats = wmgr.getWeblogCategories(getActionWeblog(), true);
94 for(WeblogCategory cat : cats) {
95 if (!cat.getId().equals(getRemoveId())) {
96 allCategories.add(cat);
97 }
98 }
99 } catch (WebloggerException ex) {
100 log.error("Error building categories list", ex);
101 // TODO: i18n
102 addError("Error building categories list");
103 }
104
105 if (allCategories.size() > 0) {
106 setAllCategories(allCategories);
107 }
108
109 return INPUT;
110 }
111
112
113 /**
114 * Remove a new template.
115 */
116 public String remove() {
117
/*
P/P * Method: String remove()
*
* Preconditions:
* init'ed(this.category)
* (soft) log != null
* (soft) init'ed(this.removeId)
* (soft) init'ed(this.targetCategoryId)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@119 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@119 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@124 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@132 != null
*
* Postconditions:
* return_value in Addr_Set{&"success",&"input"}
* this.allCategories == One-of{old this.allCategories, &new TreeSet(execute#1)}
* init'ed(this.removeId)
* new TreeSet(execute#1) num objects <= 1
*
* Test Vectors:
* this.category: Addr_Set{null}, Inverse{null}
* this.targetCategoryId: Addr_Set{null}, Inverse{null}
*/
118 if(getCategory() != null) try {
119 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
120
121 if(getTargetCategoryId() != null) {
122 WeblogCategory target = wmgr.getWeblogCategory(getTargetCategoryId());
123 wmgr.moveWeblogCategoryContents(getCategory(), target);
124 WebloggerFactory.getWeblogger().flush();
125 }
126
127 // notify cache
128 String id = getCategory().getId();
129 CacheManager.invalidate(getCategory());
130
131 wmgr.removeWeblogCategory(getCategory());
132 WebloggerFactory.getWeblogger().flush();
133
134 // set category id to parent for next page
135 setRemoveId(id);
136
137 return SUCCESS;
138
139 } catch(Exception ex) {
140 log.error("Error removing category - "+getRemoveId(), ex);
141 // TODO: i18n
142 addError("Error removing category");
143 }
144
145 return execute();
146 }
147
148
149 public String getRemoveId() {
/*
P/P * Method: String getRemoveId()
*
* Preconditions:
* init'ed(this.removeId)
*
* Postconditions:
* return_value == this.removeId
* init'ed(return_value)
*/
150 return removeId;
151 }
152
153 public void setRemoveId(String categoryId) {
/*
P/P * Method: void setRemoveId(String)
*
* Postconditions:
* this.removeId == categoryId
* init'ed(this.removeId)
*/
154 this.removeId = categoryId;
155 }
156
157 public WeblogCategory getCategory() {
/*
P/P * Method: WeblogCategory getCategory()
*
* Preconditions:
* init'ed(this.category)
*
* Postconditions:
* return_value == this.category
* init'ed(return_value)
*/
158 return category;
159 }
160
161 public void setCategory(WeblogCategory category) {
/*
P/P * Method: void setCategory(WeblogCategory)
*
* Postconditions:
* this.category == category
* init'ed(this.category)
*/
162 this.category = category;
163 }
164
165 public String getTargetCategoryId() {
/*
P/P * Method: String getTargetCategoryId()
*
* Preconditions:
* init'ed(this.targetCategoryId)
*
* Postconditions:
* return_value == this.targetCategoryId
* init'ed(return_value)
*/
166 return targetCategoryId;
167 }
168
169 public void setTargetCategoryId(String targetCategoryId) {
/*
P/P * Method: void setTargetCategoryId(String)
*
* Postconditions:
* this.targetCategoryId == targetCategoryId
* init'ed(this.targetCategoryId)
*/
170 this.targetCategoryId = targetCategoryId;
171 }
172
173 public Set getAllCategories() {
/*
P/P * Method: Set getAllCategories()
*
* Preconditions:
* init'ed(this.allCategories)
*
* Postconditions:
* return_value == this.allCategories
* init'ed(return_value)
*/
174 return allCategories;
175 }
176
177 public void setAllCategories(Set allCategories) {
/*
P/P * Method: void setAllCategories(Set)
*
* Postconditions:
* this.allCategories == allCategories
* init'ed(this.allCategories)
*/
178 this.allCategories = allCategories;
179 }
180
181 }
SofCheck Inspector Build Version : 2.18479
| CategoryRemove.java |
2009-Jan-02 14:25:30 |
| CategoryRemove.class |
2009-Sep-04 03:12:45 |