KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > RevisionControllerAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: RevisionControllerAction.java 151165 2005-02-03 15:34:19Z michi $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.acting.AbstractAction;
27 import org.apache.cocoon.environment.ObjectModelHelper;
28 import org.apache.cocoon.environment.Redirector;
29 import org.apache.cocoon.environment.Request;
30 import org.apache.cocoon.environment.Session;
31 import org.apache.cocoon.environment.SourceResolver;
32 import org.apache.lenya.ac.Identity;
33 import org.apache.lenya.ac.User;
34 import org.apache.lenya.cms.publication.Document;
35 import org.apache.lenya.cms.publication.DocumentBuilder;
36 import org.apache.lenya.cms.publication.PageEnvelope;
37 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
38 import org.apache.lenya.cms.publication.Publication;
39 import org.apache.lenya.cms.rc.RCEnvironment;
40 import org.apache.lenya.cms.rc.RevisionController;
41 import org.apache.log4j.Category;
42
43 public class RevisionControllerAction extends AbstractAction {
44     Category log = Category.getInstance(RevisionControllerAction.class);
45
46     private String JavaDoc rcmlDirectory = null;
47     private String JavaDoc backupDirectory = null;
48     private RevisionController rc = null;
49     private String JavaDoc username = null;
50     private String JavaDoc filename = null;
51
52     /**
53      * DOCUMENT ME!
54      *
55      * @param redirector DOCUMENT ME!
56      * @param resolver DOCUMENT ME!
57      * @param objectModel DOCUMENT ME!
58      * @param src DOCUMENT ME!
59      * @param parameters DOCUMENT ME!
60      *
61      * @return DOCUMENT ME!
62      *
63      * @throws Exception DOCUMENT ME!
64      */

65     public Map JavaDoc act(
66         Redirector redirector,
67         SourceResolver resolver,
68         Map JavaDoc objectModel,
69         String JavaDoc src,
70         Parameters parameters)
71         throws Exception JavaDoc {
72         // Get request object
73
Request request = ObjectModelHelper.getRequest(objectModel);
74
75         if (request == null) {
76             getLogger().error(".act(): No request object");
77
78             return null;
79         }
80
81         PageEnvelope envelope = null;
82         Publication publication = null;
83         Document document = null;
84
85         try {
86             envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
87             publication = envelope.getPublication();
88             document = envelope.getDocument();
89         } catch (Exception JavaDoc e) {
90             getLogger().error("Resolving page envelope failed: ", e);
91             throw e;
92         }
93
94         //get Parameters for RC
95
String JavaDoc publicationPath = publication.getDirectory().getAbsolutePath();
96         RCEnvironment rcEnvironment =
97             RCEnvironment.getInstance(publication.getServletContext().getAbsolutePath());
98         rcmlDirectory = rcEnvironment.getRCMLDirectory();
99         rcmlDirectory = publicationPath + File.separator + rcmlDirectory;
100         backupDirectory = rcEnvironment.getBackupDirectory();
101         backupDirectory = publicationPath + File.separator + backupDirectory;
102
103         // Initialize Revision Controller
104
rc = new RevisionController(rcmlDirectory, backupDirectory, publicationPath);
105         getLogger().debug("revision controller" + rc);
106
107         // /Initialize Revision Controller
108
// Get session
109
Session session = request.getSession(false);
110
111         if (session == null) {
112             getLogger().error(".act(): No session object");
113
114             return null;
115         }
116
117         Identity identity = (Identity) session.getAttribute(Identity.class.getName());
118         getLogger().debug(".act(): Identity: " + identity);
119
120         //FIXME: hack because of the uri for the editor bitflux. The filename cannot be get from the page-envelope
121

122         String JavaDoc documentid = document.getId();
123         int bx = documentid.lastIndexOf("-bxeng");
124
125         if (bx > 0) {
126             String JavaDoc language = document.getLanguage();
127
128             int l = documentid.length();
129             int bxLength = "-bxeng".length();
130             int lang = documentid.lastIndexOf("_", bx);
131             int langLength = bx - lang;
132
133             if (bx > 0 && bx + bxLength <= l) {
134                 documentid = documentid.substring(0, bx) + documentid.substring(bx + bxLength, l);
135
136                 if (lang > 0 && langLength + lang < l) {
137                     language = documentid.substring(lang + 1, lang + langLength);
138                     documentid =
139                         documentid.substring(0, lang)
140                             + documentid.substring(lang + langLength, l - bxLength);
141                 }
142             }
143
144             DocumentBuilder builder = publication.getDocumentBuilder();
145
146             String JavaDoc srcUrl =
147                 builder.buildCanonicalUrl(publication, document.getArea(), documentid, language);
148             Document srcDoc = builder.buildDocument(publication, srcUrl);
149             File JavaDoc newFile = srcDoc.getFile();
150             filename = newFile.getAbsolutePath();
151
152         } else {
153             filename = document.getFile().getAbsolutePath();
154         }
155
156         filename = filename.substring(publicationPath.length());
157         log.debug("Filename: " + filename);
158
159         username = null;
160
161         if (identity != null) {
162             User user = identity.getUser();
163             if (user != null) {
164                 username = user.getId();
165             }
166         } else {
167             getLogger().error(".act(): No identity yet");
168         }
169
170         getLogger().debug(".act(): Username: " + username);
171
172         return null;
173     }
174
175     /**
176      * Get the filename.
177      *
178      * @return the filename
179      */

180     protected String JavaDoc getFilename() {
181         return filename;
182     }
183
184     /**
185      * Get the revision controller.
186      *
187      * @return the revision controller
188      */

189     protected RevisionController getRc() {
190         return rc;
191     }
192
193     /**
194      * Get the user name.
195      *
196      * @return the user name
197      */

198     protected String JavaDoc getUsername() {
199         return username;
200     }
201
202 }
203
Popular Tags