001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.transaction;
019:
020: import org.apache.lenya.cms.repository.RepositoryException;
021:
022: public class MockTransactionable implements Transactionable {
023:
024: private String id;
025:
026: public MockTransactionable(String id, UnitOfWork unit) {
027: this .id = id;
028: this .unit = unit;
029: }
030:
031: public void write() {
032: MockRevisionController.getHistory(this ).newVersion();
033: }
034:
035: public void changed() {
036: }
037:
038: public void createTransactionable() throws TransactionException {
039: }
040:
041: public void deleteTransactionable() throws TransactionException {
042: }
043:
044: public void removed() {
045: }
046:
047: public void saveTransactionable() throws TransactionException {
048: }
049:
050: public void checkin() throws TransactionException {
051: MockRevisionController.getHistory(this ).checkIn();
052: }
053:
054: public void checkout() throws TransactionException {
055: MockRevisionController.getHistory(this ).checkOut(getUserId());
056: }
057:
058: public boolean hasChanged() throws TransactionException {
059: try {
060: int currentVersion = getLatestVersion();
061: int lockVersion = getLock().getVersion();
062: return currentVersion > lockVersion;
063: } catch (Exception e) {
064: throw new RepositoryException(e);
065: }
066: }
067:
068: protected int getLatestVersion() {
069: int currentVersion = MockRevisionController.getHistory(this )
070: .getLatestVersion();
071: return currentVersion;
072: }
073:
074: public boolean isCheckedOut() throws TransactionException {
075: return MockRevisionController.getHistory(this ).isCheckedOut();
076: }
077:
078: public boolean isCheckedOutBySession() throws TransactionException {
079: String user = MockRevisionController.getHistory(this )
080: .getCheckOutUser();
081: return user != null && user.equals(getUserId());
082: }
083:
084: private String getUserId() {
085: return ((UnitOfWorkImpl) this .unit).getIdentity().getUser()
086: .getId();
087: }
088:
089: private Lock lock;
090:
091: public Lock getLock() {
092: return this .lock;
093: }
094:
095: public boolean isLocked() throws TransactionException {
096: return this .lock != null;
097: }
098:
099: private UnitOfWork unit;
100:
101: public void lock() throws TransactionException {
102: this .lock = unit.createLock(this , getLatestVersion());
103: }
104:
105: public void unlock() throws TransactionException {
106: this .lock = null;
107: unit.removeLock(this );
108: }
109:
110: public String getId() {
111: return this.id;
112: }
113:
114: }
|