KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > UniqueDocumentId


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: UniqueDocumentId.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.publication;
21
22 /**
23  * class to compute an unique document id for a document, if there is
24  * already a node in the sitetree for a document with this id. It will
25  * documentid_"number of version"
26  */

27 public class UniqueDocumentId {
28
29     /** compute an unique document id
30      * @param publication The publication the document belongs to.
31      * @param area The area the document belongs to.
32      * @param documentid The documentid .
33      * @return the unique documentid
34      */

35     public String JavaDoc computeUniqueDocumentId(
36         Publication publication,
37         String JavaDoc area,
38         String JavaDoc documentid) {
39         SiteTree tree;
40         try {
41             tree = publication.getTree(area);
42             SiteTreeNode node = tree.getNode(documentid);
43             String JavaDoc suffix = null;
44             int version = 0;
45             String JavaDoc idwithoutsuffix = null;
46
47             if (node != null) {
48                 int n = documentid.lastIndexOf("/");
49                 String JavaDoc lastToken = "";
50                 String JavaDoc substring = documentid;
51                 if ((n < documentid.length()) && (n > 0)) {
52                     lastToken = documentid.substring(n);
53                     substring = documentid.substring(0, n);
54                 }
55
56                 int l = lastToken.length();
57                 int index = lastToken.lastIndexOf("-");
58                 if (0 < index && index < l) {
59                     suffix = lastToken.substring(index + 1);
60                     idwithoutsuffix = substring + lastToken.substring(0, index);
61                     version = Integer.parseInt(suffix);
62                 } else {
63                     idwithoutsuffix = substring + lastToken;
64                 }
65
66                 while (node != null) {
67                     version = version + 1;
68                     suffix = (new Integer JavaDoc(version)).toString();
69                     documentid = idwithoutsuffix + "-" + suffix;
70                     node = tree.getNode(documentid);
71                 }
72             }
73         } catch (SiteTreeException e) {
74             e.printStackTrace();
75         }
76
77         return documentid;
78     }
79 }
80
Popular Tags