001: package ru.emdev.EmForge.wiki;
002:
003: import java.util.Properties;
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.BpmService;
009: import org.emforge.jbpm.web.bean.TaskController;
010: import org.emforge.jbpm.web.bean.WorkflowController;
011: import org.emforge.projectmanager.ProjectService;
012: import org.emforge.projectmanager.base.MilestoneDO;
013: import org.emforge.projectmanager.base.ProjectDO;
014: import org.emforge.projectmanager.web.bean.MilestoneController;
015: import org.emforge.projectmanager.web.bean.ProjectController;
016: import org.springframework.context.ApplicationContext;
017: import org.springframework.context.ApplicationContextAware;
018:
019: import ru.emdev.EmForge.util.Helper;
020:
021: import com.ecyrd.jspwiki.WikiContext;
022: import com.ecyrd.jspwiki.WikiEngine;
023: import com.ecyrd.jspwiki.plugin.WeblogEntryPlugin;
024: import com.ecyrd.jspwiki.providers.ProviderException;
025: import com.ecyrd.jspwiki.url.DefaultURLConstructor;
026:
027: /** EmForge URL Constructor used ShortURLCOnstructor
028: *
029: * But it also checked, is page related to some project, milestone or process
030: * If so - it included reference to specified object instead of reference to page
031: */
032: public class EmForgeURLConstructor extends DefaultURLConstructor
033: implements ApplicationContextAware {
034: protected final Log logger = LogFactory.getLog(getClass());
035:
036: private ProjectService m_projectService;
037: ApplicationContext m_appContext;
038:
039: public void setApplicationContext(ApplicationContext i_appContext) {
040: m_appContext = i_appContext;
041: }
042:
043: public void setProjectService(ProjectService i_projectService) {
044: m_projectService = i_projectService;
045: }
046:
047: public void initialize(WikiEngine engine, Properties properties) {
048: super .initialize(engine, properties);
049: }
050:
051: /** makes url to page by specified params
052: *
053: * @todo absolute and parameters is not processed yet
054: * @todo only VIEW, EDIT,INFO,ATTACH is supported
055: */
056: public String makeURL(String context, String name,
057: boolean absolute, String parameters) {
058: String url = processLinksToObjects(context, name, absolute,
059: parameters);
060: if (url != null) {
061: url = Helper.concatUrl(Helper.getPath(), url);
062: }
063:
064: if (url == null) {
065: // makes links to the specified wiki-page
066: if (StringUtils.equals(context, WikiContext.NONE)
067: || StringUtils.equals(context, WikiContext.VIEW)) {
068: // check - is it link to the image
069: if (StringUtils.contains(name, "images/")) {
070: return Helper.concatUrl(Helper.getPath(),
071: "resources/" + name);
072: }
073: //
074: // this is temporary code until it is not implemented own BlogEntry.
075: // Currently we use url like this "NewBlogEntry.htm?page=weblogName"
076: // to add a blog entry to News
077: //
078: if (StringUtils.equals(name, "NewBlogEntry.jsp")) {
079: // get blog name
080: // params shoudl begin from page=
081: String blogName = parameters.substring(5);
082:
083: WeblogEntryPlugin p = new WeblogEntryPlugin();
084: String newEntry = null;
085: try {
086: newEntry = p
087: .getNewEntryPage(m_engine, blogName);
088: } catch (ProviderException ex) {
089: logger.error("Cannot generate new blog entry",
090: ex);
091: }
092: return Helper.concatUrl(Helper.getPath(), "edit/"
093: + newEntry);
094: }
095:
096: url = "wiki";
097: } else if (context.equals(WikiContext.EDIT)) {
098: url = "edit";
099: } else if (context.equals(WikiContext.INFO)) {
100: url = "pageinfo";
101: } else if (context.equals(WikiContext.ATTACH)) {
102: url = "attach";
103: } else if (context.equals(WikiContext.DIFF)) {
104: return Helper.concatUrl(Helper.getPath(), "diff.faces?"
105: + parameters);
106: }
107:
108: if (url == null) {
109: logger.error("Unknown WikiContext State: " + context);
110: return null;
111: }
112:
113: url = Helper.concatUrl(Helper.getPath(), url + "/" + name);
114: }
115:
116: return url;
117: }
118:
119: private String processLinksToObjects(String context, String name,
120: boolean absolute, String parameters) {
121: // processing of special string
122: if (context.equals(WikiContext.VIEW)
123: || context.equals(WikiContext.EDIT)) {
124: // processing for links to the different objects in the emforge
125: // check - is it name of the project?
126: if (m_projectService != null) {
127: ProjectDO project = m_projectService.getProject(name);
128:
129: if (project != null) {
130: return ProjectController.PROJECT_PAGE_NAME + "/"
131: + project.getName();
132: }
133: }
134:
135: if (m_projectService != null) {
136: // check, is it name of the milestone?
137: MilestoneDO milestone = m_projectService
138: .getMilestone(name);
139:
140: if (milestone != null) {
141: /** @todo Place all link generation into one place */
142: return ProjectController.PROJECT_PAGE_NAME + "/"
143: + milestone.getProject().getName() + "/"
144: + MilestoneController.MILESTONE_PAGE_NAME
145: + "/" + milestone.getName();
146: }
147: }
148:
149: // check, is it name of the process definition
150: // try to get bpm context
151: //get repository factory
152: BpmService bpmService = null;
153: try {
154: bpmService = (BpmService) m_appContext
155: .getBean("bpmService");
156: } catch (Exception ex) {
157: // just ignore this exception
158: }
159:
160: if (bpmService != null && name != null) {
161: if (bpmService.hasWorkflow(name,
162: BpmService.WORKFLOW_LAST_VERSION)) {
163: return WorkflowController.WORKFLOW_PAGE_NAME + "/"
164: + name;
165: }
166:
167: // check, is it name of the process instance which should be a number
168: if (Helper.isNumber(name)) {
169: if (bpmService.hasTask(Long.parseLong(name))) {
170: return TaskController.TASK_PAGE_NAME + "/"
171: + name;
172: }
173: }
174: }
175: }
176:
177: return null;
178: }
179: }
|