KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.daisy.repository.Document;
20 import org.outerj.daisy.repository.Version;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.user.UserManager;
23 import org.outerj.daisy.publisher.serverimpl.StripDocumentHandler;
24 import org.outerj.daisy.publisher.serverimpl.DummyLexicalHandler;
25 import org.outerx.daisy.x10.VersionsDocument;
26 import org.outerx.daisy.x10.VersionDocument;
27 import org.apache.xmlbeans.XmlCursor;
28
29 import java.text.DateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 public class AnnotatedVersionListRequest implements Request {
33     public void process(ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
34         UserManager userManager = publisherContext.getRepository().getUserManager();
35         DateFormat JavaDoc dateFormat = publisherContext.getTimestampFormat();
36         Document document = publisherContext.getDocument();
37         VersionsDocument versionsDocument = document.getVersions().getXml();
38         Version liveVersion = document.getLiveVersion();
39         long liveVersionId = liveVersion != null ? liveVersion.getId() : -1;
40         VersionDocument.Version[] versions = versionsDocument.getVersions().getVersionArray();
41         for (int i = 0; i < versions.length; i++) {
42             annotateVersion(versions[i], dateFormat, userManager, liveVersionId);
43         }
44         versionsDocument.save(new StripDocumentHandler(contentHandler), new DummyLexicalHandler());
45     }
46
47     private void annotateVersion(VersionDocument.Version versionXml, DateFormat JavaDoc dateFormat, UserManager userManager, long liveVersionId) throws RepositoryException {
48         long creatorId = versionXml.getCreator();
49         Date JavaDoc created = versionXml.getCreated().getTime();
50         Date JavaDoc stateLastModified = versionXml.getStateLastModified().getTime();
51         boolean isLiveVersion = (versionXml.getId() == liveVersionId);
52         XmlCursor cursor = versionXml.newCursor();
53         cursor.toNextToken();
54         cursor.insertAttributeWithValue("createdFormatted", dateFormat.format(created));
55         cursor.insertAttributeWithValue("creatorDisplayName", userManager.getUserDisplayName(creatorId));
56         cursor.insertAttributeWithValue("stateLastModifiedFormatted", dateFormat.format(stateLastModified));
57         if (isLiveVersion)
58             cursor.insertAttributeWithValue("live", "true");
59         cursor.dispose();
60     }
61 }
62
Popular Tags