001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl.variant;
017:
018: import org.outerj.daisy.repository.variant.Branch;
019: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
020: import org.outerj.daisy.repository.commonimpl.Util;
021: import org.outerj.daisy.repository.RepositoryException;
022: import org.outerx.daisy.x10.BranchDocument;
023:
024: import java.util.Date;
025: import java.util.GregorianCalendar;
026:
027: public class BranchImpl implements Branch {
028: private long id = -1;
029: private String name;
030: private String description;
031: private boolean readOnly = false;
032: private long lastModifier;
033: private Date lastModified;
034: private long updateCount = 0;
035: private AuthenticatedUser currentUser;
036: private VariantStrategy strategy;
037: private IntimateAccess intimateAccess = new IntimateAccess();
038: private static final String READ_ONLY_MESSAGE = "This Branch object is read-only.";
039:
040: public BranchImpl(VariantStrategy strategy, String name,
041: AuthenticatedUser currentUser) {
042: Util.checkName(name);
043: this .strategy = strategy;
044: this .name = name;
045: this .currentUser = currentUser;
046: }
047:
048: public long getId() {
049: return id;
050: }
051:
052: public String getName() {
053: return name;
054: }
055:
056: public String getDescription() {
057: return description;
058: }
059:
060: public void setName(String name) {
061: if (readOnly)
062: throw new RuntimeException(READ_ONLY_MESSAGE);
063:
064: Util.checkName(name);
065: this .name = name;
066: }
067:
068: public void setDescription(String description) {
069: if (readOnly)
070: throw new RuntimeException(READ_ONLY_MESSAGE);
071:
072: this .description = description;
073: }
074:
075: public void save() throws RepositoryException {
076: strategy.storeBranch(this );
077: }
078:
079: public long getLastModifier() {
080: return lastModifier;
081: }
082:
083: public Date getLastModified() {
084: return lastModified;
085: }
086:
087: public long getUpdateCount() {
088: return updateCount;
089: }
090:
091: public BranchDocument getXml() {
092: BranchDocument branchDocument = BranchDocument.Factory
093: .newInstance();
094: BranchDocument.Branch branch = branchDocument.addNewBranch();
095:
096: branch.setName(name);
097: if (description != null)
098: branch.setDescription(description);
099:
100: if (id != -1) {
101: branch.setId(id);
102:
103: GregorianCalendar lastModifiedCalendar = new GregorianCalendar();
104: lastModifiedCalendar.setTime(lastModified);
105: branch.setLastModified(lastModifiedCalendar);
106:
107: branch.setLastModifier(lastModifier);
108: branch.setUpdateCount(updateCount);
109: }
110:
111: return branchDocument;
112: }
113:
114: public void setAllFromXml(BranchDocument.Branch branchXml) {
115: if (readOnly)
116: throw new RuntimeException(READ_ONLY_MESSAGE);
117:
118: setName(branchXml.getName());
119: setDescription(branchXml.getDescription());
120: }
121:
122: public void makeReadOnly() {
123: this .readOnly = true;
124: }
125:
126: public IntimateAccess getIntimateAccess(VariantStrategy strategy) {
127: if (readOnly)
128: throw new RuntimeException(READ_ONLY_MESSAGE);
129:
130: if (this .strategy == strategy)
131: return intimateAccess;
132: else
133: return null;
134: }
135:
136: public class IntimateAccess {
137: private IntimateAccess() {
138: }
139:
140: public AuthenticatedUser getCurrentUser() {
141: return currentUser;
142: }
143:
144: public void setId(long id) {
145: BranchImpl.this .id = id;
146: }
147:
148: public void setLastModified(Date lastModified) {
149: BranchImpl.this .lastModified = lastModified;
150: }
151:
152: public void setLastModifier(long lastModifier) {
153: BranchImpl.this .lastModifier = lastModifier;
154: }
155:
156: public void setUpdateCount(long updateCount) {
157: BranchImpl.this.updateCount = updateCount;
158: }
159: }
160: }
|