001: package ru.emdev.EmForge.wiki.web.bean;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.List;
006:
007: import javax.faces.component.UIComponent;
008: import javax.faces.context.FacesContext;
009: import javax.faces.model.SelectItem;
010:
011: import org.apache.commons.lang.StringUtils;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
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.WikiPage;
020: import com.ecyrd.jspwiki.providers.ProviderException;
021:
022: /** Controller for Wiki pages
023: *
024: * @author spopov
025: *
026: */
027: public class DiffController extends BaseWikiControllerImpl {
028: protected final Log logger = LogFactory.getLog(getClass());
029:
030: public static String WIKIVIEW_PAGE = "diff.faces";
031:
032: protected Integer m_rev1;
033: protected Integer m_rev2;
034:
035: protected List<WikiPage> m_pageHistory;
036: protected Collection<SelectItem> m_selectVersions;
037:
038: @Override
039: protected void init() {
040:
041: String pageName = getPageParam();
042: setPageName(pageName);
043: }
044:
045: @Override
046: public Crumb getTrailCrumbInfo() {
047: // TODO
048: return new Crumb("Diff", m_urlConstructor.makeURL(
049: WikiContext.DIFF, getPageName(), false, "page="
050: + getPageName() + "&r1=" + getRev1() + "&r2="
051: + getRev2()));
052: }
053:
054: @Override
055: public MainMenuItem getSelectionItemOnMainMenu() {
056: return MainMenuItem.WIKI;
057: }
058:
059: @Override
060: public String getTitleImpl() {
061:
062: return "Compare revisions for page " + getPageName();
063: }
064:
065: public String getSubTitle() {
066: return "Revision " + getRev1() + " and revision " + getRev2();
067: }
068:
069: public String getDiff() {
070: return m_wikiEngine.getDiff(getWikiContext(), getRev2(),
071: getRev1());
072: }
073:
074: @SuppressWarnings("unchecked")
075: @Override
076: public void setPageName(String pageName) {
077: if (!StringUtils.isEmpty(pageName)) {
078: // making initialization
079: PageManager pageManager = m_wikiEngine.getPageManager();
080:
081: try {
082: m_pageHistory = pageManager.getVersionHistory(pageName);
083: } catch (ProviderException ex) {
084: logger.error("Cannot get page history");
085: }
086:
087: //clear select Versions
088: m_selectVersions = null;
089: }
090:
091: super .setPageName(pageName);
092: }
093:
094: public void setRev1(Integer i_rev) {
095: m_rev1 = i_rev;
096: }
097:
098: public Integer getRev1() {
099: if (m_rev1 == null) {
100: try {
101: m_rev1 = Integer.valueOf(getParam("r1"));
102: } catch (Exception ex) {
103: // ignore all exceptoins
104: }
105:
106: if (m_rev1 == null && m_pageHistory != null) {
107: // get latest version
108: if (m_pageHistory.size() > 0) {
109: m_rev1 = m_pageHistory
110: .get(m_pageHistory.size() - 1).getVersion();
111: } else {
112: m_rev1 = -1;
113: }
114: }
115: }
116:
117: return m_rev1;
118: }
119:
120: public void setRev2(Integer i_rev) {
121:
122: m_rev2 = i_rev;
123: }
124:
125: public Integer getRev2() {
126: if (m_rev2 == null) {
127: try {
128: m_rev2 = Integer.valueOf(getParam("r2"));
129: } catch (Exception ex) {
130: // ignore all exceptoins
131: }
132:
133: if (m_rev2 == null && m_pageHistory != null) {
134: // get latest version
135: if (m_pageHistory.size() > 1) {
136: m_rev2 = m_pageHistory
137: .get(m_pageHistory.size() - 2).getVersion();
138: } else {
139: m_rev2 = -1;
140: }
141: }
142: }
143:
144: return m_rev2;
145: }
146:
147: public Collection<SelectItem> getSelectVersions() {
148: if (m_selectVersions == null && m_pageHistory != null) {
149: // build select versions
150: m_selectVersions = new ArrayList<SelectItem>();
151: for (WikiPage page : m_pageHistory) {
152: m_selectVersions.add(new SelectItem(page.getVersion()));
153: }
154: }
155:
156: // to avoid lot of error messages in logs (we will need to clarify why they happens)
157: // it is better to return empty array instead of null
158: return m_selectVersions == null ? new ArrayList<SelectItem>()
159: : m_selectVersions;
160: }
161:
162: /** It is a trick.
163: * JSF Required selectVersions during validation step - but in this phase it is not yet passed page name
164: * Stored in t:saveState - so, we do not have pageName - and as result - we do not have pageVersions...
165: *
166: * Very stupid hack - during validation stage first called custom validators - so, in this validator we just
167: * add values into selectVersions array to return them on validation stage later
168: * @param i_context
169: * @param ui_component
170: * @param value
171: */
172: public void validateVersion(FacesContext i_context,
173: UIComponent ui_component, Object value) {
174: if (m_selectVersions == null) {
175: m_selectVersions = new ArrayList<SelectItem>();
176: }
177:
178: m_selectVersions.add(new SelectItem(value));
179: }
180: }
|