View Javadoc

1   /*
2    * Copyright 2004 Charles Blaxland
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 mnemosyne.core;
17  
18  import java.util.Collection;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.Set;
22  
23  /***
24   * @version $Id: StandardTransaction.java,v 1.1.1.1 2004/08/07 06:41:12 charlesblaxland Exp $
25   */
26  public class StandardTransaction implements Transaction
27  {
28      private Set modifiedObjects = new HashSet();
29      private Set addedObjects = new HashSet();
30      private boolean complete = false;
31  
32      public void addModifiedObject(PersistentObject obj)
33      {
34          modifiedObjects.add(obj);
35      }
36  
37      public boolean hasModifiedOrAddedObject(PersistentObject obj)
38      {
39          return modifiedObjects.contains(obj) || addedObjects.contains(obj);
40      }
41  
42      public void prepareForCommit()
43      {
44          for (Iterator iterator = modifiedObjects.iterator(); iterator.hasNext();)
45          {
46              PersistentObject persistentObject = (PersistentObject) iterator.next();
47              addedObjects.addAll(persistentObject.prepareForCommit());
48          }
49      }
50  
51      public void commit(Version versionToCommitAs)
52      {
53          commit(modifiedObjects, versionToCommitAs);
54          commit(addedObjects, versionToCommitAs);
55          complete = true;
56      }
57  
58      private static void commit(Collection persistentObjects, Version versionToCommitAs)
59      {
60          for (Iterator iterator = persistentObjects.iterator(); iterator.hasNext();)
61          {
62              PersistentObject persistentObject = (PersistentObject) iterator.next();
63              persistentObject.commit(versionToCommitAs);
64          }
65      }
66  
67      public void rollback()
68      {
69          rollback(modifiedObjects);
70          rollback(addedObjects);
71          complete = true;
72      }
73  
74      private static void rollback(Collection persistentObjects)
75      {
76          for (Iterator iterator = persistentObjects.iterator(); iterator.hasNext();)
77          {
78              PersistentObject persistentObject = (PersistentObject) iterator.next();
79              persistentObject.rollback();
80          }
81      }
82  
83      protected void finalize() throws Throwable
84      {
85          super.finalize();
86          if (!complete)
87          {
88              rollback();
89          }
90      }
91  }