KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > revisions > RevisionEditorModule


1 /*
2  * Created on Dec 30, 2004
3  */

4 package com.openedit.modules.revisions;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.openedit.repository.ContentItem;
12 import org.openedit.repository.RepositoryException;
13
14 import com.openedit.OpenEditException;
15 import com.openedit.WebPageRequest;
16 import com.openedit.modules.edit.BaseEditorModule;
17 import com.openedit.page.Page;
18 import com.openedit.users.User;
19
20 /**
21  * A module containing commands required by the revision editor (repository
22  * history browser).
23  *
24  * @author Eric Galluzzo, egalluzzo@einnovation.com
25  */

26 public class RevisionEditorModule extends BaseEditorModule
27 {
28     private static Log log = LogFactory.getLog( RevisionEditorModule.class );
29
30     /**
31      * Places all the revisions for the path denoted by the "path" request
32      * parameter in the session as attribute "revisions".
33      *
34      * @param inContext The web page context
35      */

36     public RevisionSession getRevisions( WebPageRequest inContext ) throws OpenEditException
37     {
38         String JavaDoc path = inContext.getRequestParameter( "editPath" );
39         if ( path == null)
40         {
41             return null;
42         }
43         RevisionSession session = (RevisionSession)inContext.getPageValue("revisions");
44         if ( session == null || !session.getEditPath().equals(path))
45         {
46             Page editPage = getPageManager().getPage(path);
47             session = new RevisionSession();
48             session.setEditPage(editPage);
49             session.setOriginalUrl(inContext.getRequestParameter("origURL"));
50             session.setParentName(inContext.getRequestParameter("parentName"));
51         }
52         List JavaDoc revisions = getPageManager().getRepository().getVersions( path );
53         session.setRevisions(revisions);
54         inContext.putSessionValue( "revisions", session );
55         return session;
56     }
57     
58     public ContentItem getLatestRevision(WebPageRequest inReq) throws RepositoryException
59     {
60         String JavaDoc path = inReq.getRequestParameter( "editPath" );
61         if ( path == null)
62         {
63             return null;
64         }
65         ContentItem revision = getPageManager().getLatestVersion( path );
66         if (revision != null )
67         {
68             inReq.putPageValue("latest", revision);
69             
70             String JavaDoc author = revision.getAuthor();
71             User user = getUserManager().getUser(author);
72             if (user != null)
73             {
74                 inReq.putPageValue("latestemail", user.getEmail());
75             }
76             return revision;
77         }
78         return null;
79     }
80
81     /**
82      * Retrieves the content item for a revision. The <code>version</code>
83      * parameter must be set to the version number to retrieve, and the
84      * <code>path</code> parameter to the path to retrieve. The
85      * {@link Revision} corresponding to that version of that file will be
86      * placed in the session attribute <code>revision</code>, and the content
87      * of that revision in the session attribute <code>revisionContent</code>,
88      * if it is non-binary. This method assumes that there is already a list
89      * of {@link Revision}s in the session under the attribute name
90      * <code>revisions</code>.
91      *
92      * @param inContext The web page context
93      */

94     public void getRevisionContent( WebPageRequest inContext )
95         throws OpenEditException
96     {
97         RevisionSession session = getRevisions(inContext);
98         String JavaDoc version = inContext.getRequiredParameter( "version" );
99
100         for ( Iterator JavaDoc iter = session.getRevisions().iterator(); iter.hasNext(); )
101         {
102             ContentItem revision = (ContentItem) iter.next();
103             if ( revision.getVersion().equals( version ) )
104             {
105                 Page oldpage = getPageManager().getPage( revision.getActualPath() );
106                 inContext.putPageValue("oldPage", oldpage);
107                 if ( !session.getEditPage().isBinary() )
108                 {
109                     session.setRevisionContent( oldpage.getContent() ); //This is here in case of a merge
110
}
111                 session.setOldPage(oldpage);
112                 session.setSelectedRevision(revision);
113                 break;
114             }
115         }
116     }
117
118     /**
119      * This command writes the content in the session attribute
120      * <code>revisionContent</code> as a new revision of the page given by the
121      * <code>path</code> request parameter.
122      *
123      * @param inContext The web page context
124      */

125     public void writeRevisionContent( WebPageRequest inContext )
126         throws OpenEditException
127     {
128         /* TODO:Add this code Filter filter = page.getEditFilter();
129         boolean value= ((filter == null) || filter.passes( inContext.copy(page) ));
130         
131         if ( !value)
132         {
133             throw new OpenEditException("No permissions available");
134         }
135          */

136         if ( inContext.getUser() == null)
137         {
138             throw new OpenEditException("No permissions available");
139         }
140         RevisionSession session = getRevisions(inContext);
141         ContentItem revision = session.getSelectedRevision();
142         
143         String JavaDoc message = "Version " + revision.getVersion() + " restored.";
144         String JavaDoc content = session.getRevisionContent();
145         if ( content != null )
146         {
147             inContext.setRequestParameter( "message", message );
148             inContext.setRequestParameter( "content", content );
149             inContext.setRequestParameter( "editPath", session.getEditPath() );
150             writeContent( inContext );
151         }
152         else
153         {
154             Page old = getPageManager().getPage(revision.getActualPath());
155             Page current = session.getEditPage();
156             current.getContentItem().setMessage(message);
157             current.getContentItem().setAuthor(inContext.getUser().getUserName() );
158             getPageManager().copyPage(old, current);
159         }
160         log.info("restored revision " + session.getEditPath() );
161         inContext.removeSessionValue("revisions");
162         //inContext.redirect(inContext.getPath() + "#reload?reload=true");
163
}
164     
165     public void deleteAll(WebPageRequest inReq) throws OpenEditException
166     {
167         String JavaDoc path = inReq.getRequestParameter( "editPath" );
168         if ( path == null)
169         {
170             return;
171         }
172         getPageManager().getRepository().deleteOldVersions( path );
173     }
174 }
Popular Tags