KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > variant > BranchImpl


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.repository.commonimpl.variant;
17
18 import org.outerj.daisy.repository.variant.Branch;
19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
20 import org.outerj.daisy.repository.commonimpl.Util;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerx.daisy.x10.BranchDocument;
23
24 import java.util.Date JavaDoc;
25 import java.util.GregorianCalendar JavaDoc;
26
27 public class BranchImpl implements Branch {
28     private long id = -1;
29     private String JavaDoc name;
30     private String JavaDoc description;
31     private boolean readOnly = false;
32     private long lastModifier;
33     private Date JavaDoc lastModified;
34     private long updateCount = 0;
35     private AuthenticatedUser currentUser;
36     private VariantStrategy strategy;
37     private IntimateAccess intimateAccess = new IntimateAccess();
38     private static final String JavaDoc READ_ONLY_MESSAGE = "This Branch object is read-only.";
39
40     public BranchImpl(VariantStrategy strategy, String JavaDoc name, AuthenticatedUser currentUser) {
41         Util.checkName(name);
42         this.strategy = strategy;
43         this.name = name;
44         this.currentUser = currentUser;
45     }
46
47     public long getId() {
48         return id;
49     }
50
51     public String JavaDoc getName() {
52         return name;
53     }
54
55     public String JavaDoc getDescription() {
56         return description;
57     }
58
59     public void setName(String JavaDoc name) {
60         if (readOnly)
61             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
62
63         Util.checkName(name);
64         this.name = name;
65     }
66
67     public void setDescription(String JavaDoc description) {
68         if (readOnly)
69             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
70
71         this.description = description;
72     }
73
74     public void save() throws RepositoryException {
75         strategy.storeBranch(this);
76     }
77
78     public long getLastModifier() {
79         return lastModifier;
80     }
81
82     public Date JavaDoc getLastModified() {
83         return lastModified;
84     }
85
86     public long getUpdateCount() {
87         return updateCount;
88     }
89
90     public BranchDocument getXml() {
91         BranchDocument branchDocument = BranchDocument.Factory.newInstance();
92         BranchDocument.Branch branch = branchDocument.addNewBranch();
93
94         branch.setName(name);
95         if (description != null)
96             branch.setDescription(description);
97
98         if (id != -1) {
99             branch.setId(id);
100
101             GregorianCalendar JavaDoc lastModifiedCalendar = new GregorianCalendar JavaDoc();
102             lastModifiedCalendar.setTime(lastModified);
103             branch.setLastModified(lastModifiedCalendar);
104
105             branch.setLastModifier(lastModifier);
106             branch.setUpdateCount(updateCount);
107         }
108
109         return branchDocument;
110     }
111
112     public void setAllFromXml(BranchDocument.Branch branchXml) {
113         if (readOnly)
114             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
115
116         setName(branchXml.getName());
117         setDescription(branchXml.getDescription());
118     }
119
120     public void makeReadOnly() {
121         this.readOnly = true;
122     }
123
124     public IntimateAccess getIntimateAccess(VariantStrategy strategy) {
125         if (readOnly)
126             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
127
128         if (this.strategy == strategy)
129             return intimateAccess;
130         else
131             return null;
132     }
133
134     public class IntimateAccess {
135         private IntimateAccess() {
136         }
137
138         public AuthenticatedUser getCurrentUser() {
139             return currentUser;
140         }
141
142         public void setId(long id) {
143             BranchImpl.this.id = id;
144         }
145
146         public void setLastModified(Date JavaDoc lastModified) {
147             BranchImpl.this.lastModified = lastModified;
148         }
149
150         public void setLastModifier(long lastModifier) {
151             BranchImpl.this.lastModifier = lastModifier;
152         }
153
154         public void setUpdateCount(long updateCount) {
155             BranchImpl.this.updateCount = updateCount;
156         }
157     }
158 }
159
Popular Tags