001: package ru.emdev.EmForge.wiki.web.bean;
002:
003: import java.util.Date;
004:
005: import org.apache.commons.lang.StringUtils;
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.emforge.jbpm.web.bean.TaskController;
009: import org.emforge.projectmanager.base.MilestoneDO;
010: import org.emforge.projectmanager.web.bean.MilestoneController;
011: import org.emforge.projectmanager.web.bean.ProjectController;
012: import org.emforge.report.web.bean.ReportViewBean;
013:
014: import ru.emdev.EmForge.util.Helper;
015: import ru.emdev.EmForge.web.bean.EditTextController;
016: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
017:
018: import com.ecyrd.jspwiki.WikiContext;
019: import com.ecyrd.jspwiki.WikiPage;
020:
021: /**
022: * Controller for Edit page. It can be used for description editing too
023: *
024: * @author spopov
025: */
026: public class WikiTextEditController extends BaseWikiControllerImpl {
027:
028: protected final Log logger = LogFactory.getLog(getClass());
029: public static final String EDIT_PAGE_NAME = "edit.faces";
030:
031: private EditTextController m_editTextController;
032:
033: /*
034: * @see ru.emdev.EmForge.web.bean.BaseControllerImpl#init()
035: */
036: @Override
037: protected void init() {
038:
039: setPageName(getPageParam());
040:
041: String version = getParam("version");
042: if (Helper.isNumber(version)) {
043: setVersion(Integer.valueOf(version));
044: }
045:
046: if (m_pageName != null) {
047: try {
048: // get wiki text without conversions
049: String content = m_wikiEngine.getPureText(m_pageName,
050: m_version);
051: if (content != null) {
052: m_editTextController.setWikiText(content);
053: }
054: } catch (Exception e) {
055: logger.error(e.getMessage());
056: }
057: }
058: }
059:
060: /**
061: * @param editTextController
062: */
063: public void setEditTextController(
064: EditTextController editTextController) {
065:
066: m_editTextController = editTextController;
067: }
068:
069: /*
070: * @see ru.emdev.EmForge.web.bean.BaseControllerImpl#getSelectionItemOnMainMenu()
071: */
072: @Override
073: public MainMenuItem getSelectionItemOnMainMenu() {
074:
075: return null;
076: }
077:
078: /*
079: * @see ru.emdev.EmForge.web.bean.BaseControllerImpl#getTitleImpl()
080: */
081: @Override
082: public String getTitleImpl() {
083:
084: if (isEditProject()) {
085: return "Edit Project \"" + getPageName() + "\" Description";
086: } else if (isEditMilestone()) {
087: return "Edit Milestone \"" + getPageName()
088: + "\" Description";
089: } else if (isEditTask()) {
090: return "Edit Task #" + getPageName() + " Description";
091: } else {
092: return "Edit Page \"" + getPageName() + "\"";
093: }
094: }
095:
096: /*
097: * @see ru.emdev.EmForge.web.bean.BaseControllerImpl#getTrailCrumbInfo()
098: */
099: @Override
100: public Crumb getTrailCrumbInfo() {
101:
102: return new Crumb(getTitleImpl(), m_urlConstructor.makeURL(
103: WikiContext.EDIT, getPageName(), false, null));
104: }
105:
106: /**
107: * @return
108: */
109: public String submit() {
110:
111: if (m_appContext != null && m_pageName != null) {
112:
113: if (StringUtils.isEmpty(m_pageName)) {
114: logger.info("page name is not specified as param");
115: return null;
116: }
117: try {
118: WikiPage wikiPage = m_wikiEngine.getPage(m_pageName,
119: m_version);
120:
121: if (wikiPage == null) {
122: wikiPage = new WikiPage(m_wikiEngine, m_pageName);
123: }
124: WikiContext wikiContext = new WikiContext(m_wikiEngine,
125: wikiPage);
126: String user = getCurrentUser().getUsername();
127:
128: wikiContext.getPage().setAuthor(user);
129: wikiContext.getPage().setLastModified(new Date());
130: m_wikiEngine.saveText(wikiContext,
131: getTextInWikiFormat());
132:
133: /*
134: * else { String comment = getTextInWikiFormat(); if (StringUtils.isNotEmpty(comment)) { String content =
135: * m_wikiEngine.getPureText(wikiPage); StringBuffer strbuf = new StringBuffer(content);
136: * strbuf.append("\n----"); strbuf.append(comment); strbuf.append("\n\n--[" +
137: * getCurrentUser().getUsername() + "|mailto:" + getCurrentUser().getEmail() + "], " + new Date() +
138: * "\n\n"); WikiContext wikiContext = new WikiContext(m_wikiEngine, wikiPage);
139: * m_wikiEngine.saveText(wikiContext, strbuf.toString()); } }
140: */
141:
142: if (isEditProject()) {
143: // if we edited project descriptoion - we should back to project page
144: redirect(ProjectController.PROJECT_PAGE_NAME + "/"
145: + getPageName());
146:
147: } else if (isEditMilestone()) {
148: MilestoneDO milestone = m_projectService
149: .getMilestone(getPageName());
150: redirect(ProjectController.PROJECT_PAGE_NAME + "/"
151: + milestone.getProject().getName() + "/"
152: + MilestoneController.MILESTONE_PAGE_NAME
153: + "/" + milestone.getName());
154:
155: } else if (isEditTask()) {
156: redirect(TaskController.TASK_PAGE_NAME + "/"
157: + getPageName());
158:
159: } else if (isEditReport()) {
160: redirect(ReportViewBean.PAGE_FOLDER + "/"
161: + getPageName());
162:
163: } else {
164: // redirect to previous view page after submit action
165: m_trail.backToPreviousUrl();
166: }
167:
168: } catch (Exception e) {
169: logger.error(e.getMessage(), e);
170: return null;
171: }
172: }
173: return null;
174: }
175:
176: /**
177: * @return
178: */
179: public String cancel() {
180:
181: m_trail.backToPreviousUrl();
182: return null;
183: }
184:
185: /**
186: * @return
187: */
188: public String getTextInWikiFormat() {
189:
190: return m_editTextController.getWikiText();
191: }
192: }
|