001: package ru.emdev.EmForge.wiki.web.bean;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import javax.faces.model.SelectItem;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.springframework.beans.factory.InitializingBean;
014:
015: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
016:
017: import com.ecyrd.jspwiki.PageManager;
018: import com.ecyrd.jspwiki.WikiContext;
019: import com.ecyrd.jspwiki.WikiException;
020: import com.ecyrd.jspwiki.WikiPage;
021: import com.ecyrd.jspwiki.plugin.ReferredPagesPlugin;
022: import com.ecyrd.jspwiki.plugin.ReferringPagesPlugin;
023:
024: /**
025: *
026: * @author spopov
027: *
028: */
029: public class PageInfoController extends BaseWikiControllerImpl
030: implements InitializingBean {
031: protected final Log logger = LogFactory.getLog(getClass());
032:
033: /** We need to wrap wiki pages in revision history
034: * To be able to get previous (in history list - next) version
035: * to compare
036: * @author akakunin
037: *
038: */
039: public class WikiPageWrapper {
040: public WikiPage m_wikiPage;
041: public Integer m_prevVersion;
042:
043: public Integer getPrevVersion() {
044: return m_prevVersion;
045: }
046:
047: public void setPrevVersion(Integer prevVersion) {
048: m_prevVersion = prevVersion;
049: }
050:
051: public WikiPage getWikiPage() {
052: return m_wikiPage;
053: }
054:
055: public void setWikiPage(WikiPage wikiPage) {
056: m_wikiPage = wikiPage;
057: }
058: }
059:
060: private String m_renameTo;
061: private Boolean m_isUpdateRefs = true;
062: private Integer m_currentRevisionIntervalIndex = 0;
063:
064: @Override
065: protected void init() {
066: m_pageName = getPageParam();
067: m_renameTo = m_pageName;
068: }
069:
070: @Override
071: public MainMenuItem getSelectionItemOnMainMenu() {
072: return null;
073: }
074:
075: @Override
076: public String getTitleImpl() {
077: return "Info:\"" + m_pageName + "\"";
078: }
079:
080: @Override
081: public Crumb getTrailCrumbInfo() {
082: return new Crumb(getTitleImpl(), "pageInfo.faces?page="
083: + m_pageName);
084: }
085:
086: public void setRenameTo(String i_renameTo) {
087: m_renameTo = i_renameTo;
088: }
089:
090: public String getRenameTo() {
091: return m_renameTo;
092: }
093:
094: public WikiPage getPage() {
095: return m_wikiEngine.getPage(m_pageName);
096: }
097:
098: public String getViewUrl() {
099: return m_wikiEngine.getViewURL(m_pageName);
100: }
101:
102: public void setUpdateRefs(Boolean isUpdateRef) {
103: m_isUpdateRefs = isUpdateRef;
104: }
105:
106: public Boolean getUpdateRefs() {
107: return m_isUpdateRefs;
108: }
109:
110: public String deleteEntirePage() {
111: logger.debug("try to delete entire page...");
112: try {
113: PageManager pageManager = m_wikiEngine.getPageManager();
114: pageManager.deletePage(m_wikiEngine.getPage(m_pageName));
115:
116: String url = m_wikiEngine.getViewURL(m_pageName);
117: redirect(url);
118: } catch (Exception ex) {
119: logger.error(ex.getMessage(), ex);
120: addErrorMessage("Cannot remove page: ", ex.getMessage());
121: return null;
122: }
123:
124: return "mainwiki";
125: }
126:
127: public String renamePage() {
128: logger.info("Page rename request for page '"
129: + m_pageName
130: + "' to new name '"
131: + m_renameTo
132: + "' by "
133: + m_appContext.getUserService().getCurrentUser()
134: .getUsername());
135:
136: try {
137: if (StringUtils.isNotEmpty(m_renameTo)) {
138: WikiContext wikiContext = new WikiContext(m_wikiEngine,
139: m_wikiEngine.getPage(m_pageName));
140: String renamedTo = m_wikiEngine.renamePage(wikiContext,
141: m_pageName, m_renameTo, m_isUpdateRefs);
142: logger.info("Page successfully renamed to '"
143: + renamedTo + "'");
144:
145: String url = m_wikiEngine.getViewURL(renamedTo);
146: redirect(url);
147: } else {
148: logger
149: .info("Page rename request failed because new page name was left blank");
150: }
151: } catch (WikiException we) {
152: if (we.getMessage().equals("Page exists")) {
153: if (m_renameTo.equals(m_pageName)) {
154: logger
155: .info("Page rename request failed because page names are identical");
156: } else {
157: logger
158: .info("Page rename request failed because new page name is already in use");
159: }
160: } else {
161: logger.info("An Unknown error occurred ("
162: + we.toString() + ")");
163: }
164: }
165:
166: return null;
167: }
168:
169: @SuppressWarnings("unchecked")
170: public WikiPageWrapper[] getRevisionList() {
171: Collection<WikiPage> pageHistory = new ArrayList<WikiPage>();
172: try {
173: PageManager pageManager = m_wikiEngine.getPageManager();
174: pageHistory = pageManager.getVersionHistory(m_pageName);
175: } catch (Exception ex) {
176: logger.error(ex.getMessage(), ex);
177: return null;
178: }
179:
180: // we need: 1. Reverse the list; 2. Insert information about previous version
181: WikiPageWrapper history[] = new WikiPageWrapper[pageHistory
182: .size()];
183: int index = pageHistory.size() - 1;
184: Integer prevVersion = null;
185: for (WikiPage page : pageHistory) {
186: WikiPageWrapper wrapper = new WikiPageWrapper();
187: wrapper.setWikiPage(page);
188: wrapper.setPrevVersion(prevVersion);
189:
190: history[index--] = wrapper;
191: prevVersion = page.getVersion();
192: }
193:
194: return history;
195: }
196:
197: public Collection<SelectItem> getRevisionIntervals() {
198: Collection<SelectItem> result = new ArrayList<SelectItem>();
199: result.add(new SelectItem(new Integer("0"),
200: "Show all revision..."));
201: return result;
202: }
203:
204: public void setCurrentRevisionIntervalIndex(Integer i_index) {
205: m_currentRevisionIntervalIndex = i_index;
206: }
207:
208: public Integer getCurrentRevisionIntervalIndex() {
209: return m_currentRevisionIntervalIndex;
210: }
211:
212: public String getIncomingLinks() {
213: String textOutput = "";
214: Map<String, String> p = new HashMap<String, String>();
215: p.put(ReferringPagesPlugin.PARAM_BEFORE, "*");
216: p.put(ReferringPagesPlugin.PARAM_AFTER, "\n");
217: p.put(ReferringPagesPlugin.PARAM_MAX, "5");
218: try {
219: WikiContext wikiContext = new WikiContext(m_wikiEngine,
220: m_wikiEngine.getPage(m_pageName, m_version));
221: textOutput = m_wikiEngine.getPluginManager().execute(
222: wikiContext, "ReferringPagesPlugin", p);
223: } catch (Exception e) {
224: logger.error(e);
225: return null;
226: }
227: return textOutput;
228: }
229:
230: public String getOutgoingLinks() {
231: String textOutput = "";
232: Map<String, String> p = new HashMap<String, String>();
233: p.put(ReferredPagesPlugin.PARAM_DEPTH, "depth=1");
234: try {
235: WikiContext wikiContext = new WikiContext(m_wikiEngine,
236: m_wikiEngine.getPage(m_pageName, m_version));
237: textOutput = m_wikiEngine.getPluginManager().execute(
238: wikiContext, "ReferredPagesPlugin", p);
239: } catch (Exception e) {
240: logger.error(e);
241: return null;
242: }
243:
244: return textOutput;
245: }
246: }
|