KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > core > TransactionResolver


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.core;
11
12 import java.util.*;
13 import org.mmbase.bridge.Field;
14 import org.mmbase.core.CoreField;
15 import org.mmbase.util.logging.*;
16
17 /**
18  * @javadoc
19  *
20  * @author Rico Jansen
21  * @version $Id: TransactionResolver.java,v 1.26 2006/04/21 16:12:30 michiel Exp $
22  */

23 public class TransactionResolver {
24     private static final Logger log = Logging.getLoggerInstance(TransactionResolver.class);
25     private final MMBase mmbase;
26
27     public TransactionResolver(MMBase mmbase) {
28         this.mmbase = mmbase;
29     }
30
31     public boolean resolve(final Collection nodes) {
32         Map numbers = new HashMap(); /* Temp key -> Real node number */
33         Map nnodes = new HashMap(); /* MMObjectNode --> List of changed fields */
34         boolean success = true;
35
36         // Find all unique keys and store them in a map to remap them later
37
// Also store the nodes with which fields uses them.
38
for (Iterator i = nodes.iterator(); i.hasNext();) {
39             MMObjectNode node = (MMObjectNode) i.next();
40             MMObjectBuilder bul = mmbase.getBuilder(node.getName());
41             if (log.isDebugEnabled()) {
42                 log.debug("TransactionResolver - builder " + node.getName() + " builder " + bul);
43             }
44             for (Iterator f = bul.getFields().iterator();f.hasNext();) {
45                 CoreField fd = (CoreField)f.next();
46                 int dbtype = fd.getType();
47                 if (log.isDebugEnabled()) {
48                     log.debug("TransactionResolver - type " + dbtype + "," + fd.getName() + "," + fd.getState());
49                 }
50                 if (dbtype == Field.TYPE_INTEGER || dbtype == Field.TYPE_NODE) {
51                     int state = fd.getState();
52                     if (fd.inStorage()) {
53                         // Database field of type integer
54
String JavaDoc field = fd.getName();
55                         String JavaDoc tmpField = "_" + field;
56                         if (node.getDBState(tmpField) == Field.STATE_VIRTUAL) {
57                             if (node.isNull(field)) {
58                                 if (! node.isNull(tmpField)) {
59                                     String JavaDoc key = node.getStringValue(tmpField);
60                                     log.debug("TransactionResolver - key,field " + field + " - " + key);
61                                     // keep fieldnumber key
62
if (! numbers.containsKey(key)) {
63                                         numbers.put(key, null);
64                                     }
65                                     // keep node + field to change
66
Collection changedFields = (Collection) nnodes.get(node);
67                                     if (changedFields != null) {
68                                         changedFields.add(field);
69                                     } else {
70                                         changedFields = new ArrayList();
71                                         changedFields.add(field);
72                                         nnodes.put(node, changedFields);
73                                     }
74                                 } else if (log.isDebugEnabled()) {
75                                     log.debug("TransactionResolver - Can't find key for field " + tmpField + " node " + node + " (warning)");
76                                 }
77                                 if (field.equals("number")) {
78                                     node.setValue(MMObjectBuilder.TMP_FIELD_EXISTS, TransactionManager.EXISTS_NO);
79                                 }
80                             } else {
81                                 // Key is already set
82
int ikey = node.getIntValue(field);
83                                 log.debug("TransactionResolver - Key for value " + field + " is already set " + ikey);
84                                 // Mark it as existing
85
if (field.equals("number")) {
86                                     // test for remove here
87
String JavaDoc exists = node.getStringValue(MMObjectBuilder.TMP_FIELD_EXISTS);
88                                     if (exists == null || !exists.equals(TransactionManager.EXISTS_NOLONGER)) {
89                                         node.setValue(MMObjectBuilder.TMP_FIELD_EXISTS, TransactionManager.EXISTS_YES);
90                                     }
91                                     String JavaDoc key = node.getStringValue(tmpField);
92                                     if (key != null) {
93                                         numbers.put(key, new Integer JavaDoc(ikey));
94                                     } else if (log.isDebugEnabled()) {
95                                         log.debug("TransactionResolver - Can't find key for field " + tmpField + " node " + node);
96                                     }
97                                 }
98                             }
99                         } else {
100                             log.debug("TransctionResolver - DBstate for " + tmpField + " is not VIRTUAL but is " + org.mmbase.core.util.Fields.getStateDescription(node.getDBState(tmpField)));
101                         }
102                     }
103                 }
104             }
105         }
106
107         if (log.isDebugEnabled()) {
108             log.debug("TransactionResolver - nnodes " + nnodes);
109         }
110
111         // Get the numbers
112
for (Iterator i = numbers.entrySet().iterator(); i.hasNext();) {
113             Map.Entry numberEntry = (Map.Entry)i.next();
114             Object JavaDoc key = numberEntry.getKey();
115             Integer JavaDoc num = (Integer JavaDoc)numberEntry.getValue();
116             if (num == null || num.intValue() == -1) {
117                 numbers.put(key, new Integer JavaDoc(mmbase.getStorageManager().createKey()));
118             }
119         }
120
121         if (log.isDebugEnabled()) {
122             log.debug("TransactionResolver - numbers: " + numbers);
123         }
124
125
126         // put numbers in the right place
127
for (Iterator i = nnodes.entrySet().iterator(); i.hasNext();) {
128             Map.Entry nnodeEntry = (Map.Entry)i.next();
129             MMObjectNode node = (MMObjectNode)nnodeEntry.getKey();
130             Collection changedFields = (Collection)nnodeEntry.getValue();
131             for (Iterator j = changedFields.iterator(); j.hasNext();) {
132                 String JavaDoc field = (String JavaDoc)j.next();
133                 String JavaDoc tmpField = "_" + field;
134                 String JavaDoc key = node.getStringValue(tmpField);
135                 int number = ((Integer JavaDoc)numbers.get(key)).intValue();
136                 node.setValue(field, number);
137             }
138         }
139
140         for (Iterator i = nodes.iterator(); i.hasNext();) {
141             MMObjectNode node = (MMObjectNode)i.next();
142             MMObjectBuilder bul = mmbase.getMMObject(node.getName());
143             for (Iterator j = bul.getFields().iterator();j.hasNext();) {
144                 CoreField fd = (CoreField)j.next();
145                 int dbtype = fd.getType();
146                 if ((dbtype == Field.TYPE_INTEGER)||
147                     (dbtype == Field.TYPE_NODE)) {
148                     
149                     String JavaDoc field = fd.getName();
150                     String JavaDoc tmpField = "_" + field;
151                     if (node.getDBState(tmpField) == Field.STATE_VIRTUAL) {
152                         int number = node.getIntValue(field);
153                         if (number == -1) {
154                             String JavaDoc key = node.getStringValue(tmpField);
155                             if (key != null && key.length() > 0) {
156                                 success = false;
157                             }
158                         }
159                     }
160                 }
161             }
162         }
163         return success;
164     }
165 }
166
Popular Tags