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.emforge.BpmService;
007: import org.emforge.EmForgeException;
008: import org.emforge.ReportService;
009: import org.emforge.projectmanager.ProjectService;
010: import org.emforge.projectmanager.base.MilestoneDO;
011: import org.emforge.projectmanager.base.ProjectDO;
012: import org.emforge.xfer.AttachmentTO;
013: import org.emforge.xfer.TaskTO;
014:
015: import ru.emdev.EmForge.security.EmForgeUserDetails;
016: import ru.emdev.EmForge.security.UserFactory;
017: import ru.emdev.EmForge.security.web.SiteRole;
018: import ru.emdev.EmForge.web.bean.BaseControllerImpl;
019:
020: import com.ecyrd.jspwiki.WikiContext;
021: import com.ecyrd.jspwiki.WikiException;
022: import com.ecyrd.jspwiki.WikiPage;
023: import com.ecyrd.jspwiki.providers.ProviderException;
024: import com.ecyrd.jspwiki.url.URLConstructor;
025:
026: /**
027: * Base wiki controller for controllers containing a wiki page
028: *
029: * @author spopov
030: */
031: public abstract class BaseWikiControllerImpl extends BaseControllerImpl {
032:
033: public static final String PAGE_PARAM_NAME = "page";
034:
035: protected String m_pageName = "";
036:
037: protected Integer m_version = -1;
038:
039: protected URLConstructor m_urlConstructor;
040: protected WikiPage m_page;
041:
042: protected UserFactory m_userFactory;
043: protected BpmService m_bpmService;
044: protected ProjectService m_projectService; // TODO: can be deleted?
045: protected ReportService m_reportService;
046:
047: /**
048: * @param userFactory
049: */
050: public void setUserFactory(UserFactory userFactory) {
051:
052: m_userFactory = userFactory;
053: }
054:
055: /**
056: * @param i_bpmService
057: */
058: public void setBpmService(BpmService i_bpmService) {
059:
060: m_bpmService = i_bpmService;
061: }
062:
063: /**
064: * @param projectService
065: * @todo Can be deleted
066: */
067: public void setProjectService(ProjectService projectService) {
068:
069: m_projectService = projectService;
070: }
071:
072: /**
073: * @param i_reportService
074: */
075: public void setReportService(ReportService i_reportService) {
076:
077: m_reportService = i_reportService;
078: }
079:
080: /**
081: * @return
082: */
083: public String getPageName() {
084:
085: return m_pageName;
086: }
087:
088: /**
089: * @param pageName
090: */
091: public void setPageName(String pageName) {
092:
093: m_pageName = pageName;
094: }
095:
096: /**
097: * @param i_version
098: */
099: public void setVersion(Integer i_version) {
100:
101: m_version = i_version;
102: }
103:
104: /**
105: * @param i_urlConstructor
106: */
107: public void setUrlConstructor(URLConstructor i_urlConstructor) {
108:
109: m_urlConstructor = i_urlConstructor;
110: }
111:
112: /**
113: * @return
114: */
115: protected boolean isEditProject() {
116:
117: return (m_projectService.getProject(getPageName()) != null);
118: }
119:
120: /**
121: * @return
122: */
123: protected boolean isEditMilestone() {
124:
125: return (m_projectService.getMilestone(getPageName()) != null);
126: }
127:
128: /**
129: * @return
130: */
131: protected boolean isEditReport() {
132:
133: return m_reportService.exists(getPageName());
134: }
135:
136: /**
137: * @return
138: */
139: protected boolean isEditTask() {
140:
141: return getEditedTask() != null;
142: }
143:
144: /**
145: * @return
146: */
147: protected TaskTO getEditedTask() {
148:
149: // try to get task
150: Long taskId = null;
151: TaskTO task = null;
152: try {
153: taskId = Long.valueOf(getPageName());
154: } catch (Exception ex) {
155: // just ignore it here
156: }
157: if (taskId != null) {
158: try {
159: task = m_bpmService.getTask(taskId);
160: } catch (EmForgeException ex) {
161: // just ignore it here
162: }
163: }
164:
165: return task;
166: }
167:
168: /**
169: * @return
170: */
171: public Integer getVersion() {
172:
173: return m_version;
174: }
175:
176: /**
177: * @return
178: */
179: public boolean isPageExists() {
180:
181: if (StringUtils.isEmpty(m_pageName)) {
182: return false;
183: }
184:
185: boolean isThere = false;
186: try {
187: isThere = m_wikiEngine.pageExists(m_pageName, m_version);
188: } catch (ProviderException e) {
189: logger.info("Cannot get info about page \"" + m_pageName
190: + "\" version " + m_version, e);
191: return false;
192: }
193: return isThere;
194: }
195:
196: /**
197: * @return
198: */
199: @SuppressWarnings("unchecked")
200: public AttachmentTO[] getAttachments() {
201: try {
202: return m_attachmentService.getAttachments(getPageName());
203: } catch (Exception e) {
204: logger.error("Cannot get attachment list: ", e);
205: return new AttachmentTO[0];
206: }
207: }
208:
209: /**
210: * @return
211: */
212: public WikiContext getWikiContext() {
213:
214: if (StringUtils.isEmpty(m_pageName)) {
215: return null;
216: }
217:
218: WikiPage page = m_wikiEngine.getPage(m_pageName, m_version);
219: if (page != null) {
220: return new WikiContext(m_wikiEngine, page);
221: } else {
222: logger
223: .info("\""
224: + m_pageName
225: + "\" page has not been found! Probably it has not been created yet.");
226: }
227: return null;
228: }
229:
230: /**
231: * Creates new wiki page with name i_newPageName and with a content; Sets curent user as author and current time as
232: * last modified
233: *
234: * @param i_newPageName
235: * @param i_text
236: * @return wiki context of created page if all OK
237: * @throws WikiException
238: */
239: public WikiContext createNewWikiPage(String i_newPageName,
240: String i_text) throws WikiException {
241:
242: WikiContext wikiContext = null;
243: // doesn't already exist the page that we want to create?
244: if (m_wikiEngine.getPage(i_newPageName) == null) {
245: WikiPage newpage = new WikiPage(m_wikiEngine, i_newPageName);
246: wikiContext = new WikiContext(m_wikiEngine, newpage);
247:
248: String user = getCurrentUser().getUsername();
249: wikiContext.getPage().setAuthor(user);
250: wikiContext.getPage().setLastModified(new Date());
251: m_wikiEngine.saveText(wikiContext, i_text);
252: } else {
253: logger
254: .info("Cannot create new page because the page with name"
255: + i_newPageName + "already exists");
256: }
257: return wikiContext;
258: }
259:
260: /**
261: * @return
262: */
263: public WikiPage getPage() {
264:
265: if (m_page == null && StringUtils.isNotEmpty(m_pageName)) {
266: WikiPage page = m_wikiEngine.getPage(m_pageName, m_version);
267:
268: return page;
269: }
270:
271: return m_page;
272: }
273:
274: /**
275: * @return
276: */
277: public String getPageAuthorEmail() {
278:
279: if (getPage() == null) {
280: return null;
281: }
282:
283: String author = getPage().getAuthor();
284:
285: if (author == null) {
286: return null;
287: }
288:
289: try {
290: EmForgeUserDetails userInfo = m_userFactory.getUser(author);
291: if (userInfo == null) {
292: return null;
293: }
294:
295: return userInfo.getEmail();
296: } catch (Exception ex) {
297: // ignore error - just return null
298: return null;
299: }
300: }
301:
302: /**
303: * @return
304: */
305: public String getFooter() {
306:
307: String result = "Page not created yet";
308:
309: if (StringUtils.isNotEmpty(m_pageName)) {
310: WikiPage page = m_wikiEngine.getPage(m_pageName, m_version);
311: if (page != null) {
312: result = "This page (revision-" + page.getVersion()
313: + ") last changed on " + page.getLastModified()
314: + " by " + page.getAuthor();
315: }
316: }
317:
318: return result;
319: }
320:
321: /**
322: * Same rules as for edit page
323: *
324: * @return <code>True</code> if the current user is a writer, otherwise <code>False</code>
325: */
326: public boolean isCanAddAttachment() {
327:
328: return m_attachmentService.canAddAttachment(getPageName());
329: }
330:
331: /**
332: * OK, rules for ability to edit page: - Admin can do everything. Addtionally to it: - Normal pages may be edited by
333: * WIKIWRITER users - Project and Milestone descriptions may be edited by Project Managers (ok, by users, allowed to
334: * edit project) - Task descriptions may be edited by owner of task or project manager
335: *
336: * @return <code>True</code> if the current user is a writer, otherwise <code>False</code>
337: */
338: public boolean isCanEditPage() {
339:
340: // admin can edit everything
341: if (isAdmin()) {
342: return true;
343: }
344:
345: // try to get project
346: ProjectDO project = m_projectService.getProject(getPageName());
347: if (project != null) {
348: // is current user allowed to edit project?
349: return m_projectService.canChangeProject(project);
350: }
351: // try to get milestone
352: MilestoneDO milestone = m_projectService
353: .getMilestone(getPageName());
354: if (milestone != null) {
355: // is current user allowed to edit milestone?
356: return m_projectService.canChangeMilestone(milestone);
357: }
358:
359: // try to get task
360: Long taskId = null;
361: TaskTO task = null;
362: try {
363: taskId = Long.valueOf(getPageName());
364: } catch (Exception ex) {
365: // just ignore it here
366: }
367: if (taskId != null) {
368: try {
369: task = m_bpmService.getTask(taskId);
370:
371: if (task != null) {
372: // only task owner - or project manager can change it
373: if (task.getOwner().equals(
374: getCurrentUser().getUsername())) {
375: return true;
376: }
377: project = m_projectService.getProject(task
378: .getProjectName());
379: return m_projectService.hasRole(project,
380: getCurrentUser(),
381: ProjectService.ROLE_MANAGER);
382: }
383: } catch (EmForgeException ex) {
384: // just ignore it here
385: }
386: }
387:
388: // ok - this is simple page - so, check for WRITER role
389: return getCurrentUser().hasRole(SiteRole.WRITER.getId());
390: }
391:
392: /**
393: * @return <code>True</code> if the page exists and the current user is not an anonymous, otherwise
394: * <code>False</code>
395: */
396: public boolean isCanAddComment() {
397:
398: return !m_appContext.getUserService().getCurrentUser()
399: .isAnonymous()
400: && isPageExists();
401: }
402:
403: /**
404: * @return
405: */
406: public String getPageParam() {
407:
408: String pageName = getParam(PAGE_PARAM_NAME);
409:
410: if (pageName == null) {
411: return "";
412: } else {
413: return pageName;
414: }
415: }
416:
417: }
|