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 mnemosyne.guid.Guid;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /***
26   * @version $Id: AggregatedTransaction.java,v 1.1.1.1 2004/08/07 06:40:56 charlesblaxland Exp $
27   */
28  public class AggregatedTransaction
29  {
30      private static final Log log = LogFactory.getLog(AggregatedTransaction.class);
31  
32      private Map modifiedObjects = new HashMap();
33      private Map unresolvedReferences = new HashMap();
34      private boolean complete = false;
35  
36      public PersistentObject addModifiedObject(final PersistentObject obj)
37      {
38          Guid guid = obj.getGuid();
39          PersistentObject result = obj;
40          PersistentObject modifiedObject = (PersistentObject) modifiedObjects.get(guid);
41          if (modifiedObject == null)
42          {
43              if (log.isTraceEnabled())
44                  log.trace("Adding modified object " + obj);
45  
46              result = resolveUnresolvedReferences(obj);
47              modifiedObjects.put(guid, result);
48          }
49          else
50          {
51              if (log.isTraceEnabled())
52                  log.trace("Modified object already loaded " + obj);
53              result = modifiedObject;
54          }
55  
56          return result;
57      }
58  
59      public PersistentObject addUnresolvedReference(final PersistentObject obj)
60      {
61          Guid guid = obj.getGuid();
62          PersistentObject result = obj;
63          PersistentObject modifiedObject = (PersistentObject) modifiedObjects.get(guid);
64          if (modifiedObject == null)
65          {
66              log.debug("Adding unresolved reference " + obj);
67              if (!unresolvedReferences.containsKey(guid))
68              {
69                  unresolvedReferences.put(guid, obj);
70              }
71          }
72          else
73          {
74              log.debug("Reference " + obj + " found in modified map");
75              result = modifiedObject;
76          }
77  
78          return result;
79      }
80  
81      public PersistentObject resolveObject(final PersistentObject obj)
82      {
83          Guid guid = obj.getGuid();
84          PersistentObject resolvedObject = (PersistentObject) modifiedObjects.get(guid);
85          if (resolvedObject == null)
86          {
87              resolvedObject = resolveUnresolvedReferences(obj);
88          }
89          else
90          {
91              log.debug("Found modified version of object " + obj);
92          }
93  
94          return resolvedObject;
95      }
96  
97      private PersistentObject resolveUnresolvedReferences(final PersistentObject obj)
98      {
99          Guid guid = obj.getGuid();
100         PersistentObject result = obj;
101         PersistentObject unresolvedRef = (PersistentObject) unresolvedReferences.remove(guid);
102         if (unresolvedRef != null)
103         {
104             log.debug("Resolved unresolved reference " + obj);
105             unresolvedRef.initialize(obj.findTargetObject());
106             result = unresolvedRef;
107         }
108 
109         return result;
110     }
111 
112     public boolean isComplete()
113     {
114         return complete;
115     }
116 
117     public void setComplete(boolean complete)
118     {
119         this.complete = complete;
120     }
121 
122 }