KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > requestmodel > ShallowAnnotatedVersionRequest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.publisher.serverimpl.requestmodel;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.outerx.daisy.x10.VersionDocument;
20 import org.outerj.daisy.repository.user.UserManager;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.Document;
23 import org.outerj.daisy.repository.Version;
24 import org.outerj.daisy.repository.Repository;
25 import org.outerj.daisy.publisher.serverimpl.StripDocumentHandler;
26 import org.outerj.daisy.publisher.serverimpl.DummyLexicalHandler;
27 import org.apache.xmlbeans.XmlCursor;
28
29 import java.text.DateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 public class ShallowAnnotatedVersionRequest implements Request {
33     public void process(ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
34         Document document = publisherContext.getDocument();
35         Version version = publisherContext.getVersion();
36         if (version != null) {
37             Repository repository = publisherContext.getRepository();
38             UserManager userManager = repository.getUserManager();
39             DateFormat JavaDoc dateFormat = publisherContext.getTimestampFormat();
40
41             VersionDocument versionXml = version.getShallowXml();
42             Version liveVersion = document.getLiveVersion();
43             long liveVersionId = liveVersion != null ? liveVersion.getId() : -1;
44             annotateVersion(versionXml.getVersion(), dateFormat, userManager, liveVersionId);
45             versionXml.save(new StripDocumentHandler(contentHandler), new DummyLexicalHandler());
46         }
47     }
48
49     private void annotateVersion(VersionDocument.Version versionXml, DateFormat JavaDoc dateFormat, UserManager userManager, long liveVersionId) throws RepositoryException {
50         long creatorId = versionXml.getCreator();
51         Date JavaDoc created = versionXml.getCreated().getTime();
52         Date JavaDoc stateLastModified = versionXml.getStateLastModified().getTime();
53         boolean isLiveVersion = (versionXml.getId() == liveVersionId);
54         XmlCursor cursor = versionXml.newCursor();
55         cursor.toNextToken();
56         cursor.insertAttributeWithValue("createdFormatted", dateFormat.format(created));
57         cursor.insertAttributeWithValue("creatorDisplayName", userManager.getUserDisplayName(creatorId));
58         cursor.insertAttributeWithValue("stateLastModifiedFormatted", dateFormat.format(stateLastModified));
59         if (isLiveVersion)
60             cursor.insertAttributeWithValue("live", "true");
61         cursor.dispose();
62     }
63 }
64
Popular Tags