1 10 11 package org.mmbase.bridge.util; 12 13 import java.util.*; 14 import java.io.InputStream ; 15 import org.mmbase.bridge.*; 16 import org.mmbase.bridge.implementation.BasicFunctionValue; 17 import org.mmbase.bridge.implementation.BasicField; 18 import org.mmbase.core.CoreField; 19 import org.mmbase.core.util.Fields; 20 import org.mmbase.util.Casting; 21 import org.mmbase.util.SizeOf; 22 import org.mmbase.util.logging.*; 23 import org.mmbase.util.functions.*; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 27 35 36 public class MapNode extends AbstractNode implements Node { 37 38 private static final Logger log = Logging.getLoggerInstance(MapNode.class); 39 40 44 final protected NodeManager nodeManager; 45 final protected Map values; 46 final protected Map sizes = new HashMap(); 47 final protected Map originalValues = new HashMap(); 48 49 52 public MapNode(Map v, NodeManager nm) { 53 values = v; 54 originalValues.putAll(values); 55 nodeManager = nm; 56 } 57 61 public MapNode(Map v, Cloud cloud) { 62 this(v, createVirtualNodeManager(cloud, v)); 63 64 } 65 70 public MapNode(Map v) { 71 this(v, ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null)); 72 } 73 74 protected static NodeManager createVirtualNodeManager(Cloud cloud, final Map map) { 75 return new AbstractNodeManager(cloud) { 76 Map fieldTypes = new HashMap(); 77 { 78 Iterator i = map.entrySet().iterator(); 79 while (i.hasNext()) { 80 Map.Entry entry = (Map.Entry) i.next(); 81 String fieldName = (String ) entry.getKey(); 82 Object value = entry.getValue(); 83 CoreField fd = Fields.createField(fieldName, Fields.classToType(value == null ? Object .class : value.getClass()), 84 Field.TYPE_UNKNOWN, Field.STATE_VIRTUAL, null); 85 Field ft = new BasicField(fd, this); 86 fieldTypes.put(fieldName, ft); 87 } 88 } 89 protected Map getFieldTypes() { 90 return fieldTypes; 91 } 92 93 }; 94 } 95 96 public Cloud getCloud() { 97 return nodeManager.getCloud(); 98 } 99 100 public NodeManager getNodeManager() { 101 return nodeManager; 102 } 103 104 public int getNumber() { 105 return Casting.toInt(values.get("number")); 106 } 107 108 public boolean isNew() { 109 return false; 110 } 111 112 public boolean isChanged(String fieldName) { 113 Object originalValue = originalValues.get(fieldName); 114 Object value = values.get(fieldName); 115 return originalValue == null ? value == null : originalValue.equals(value); 116 } 117 public boolean isChanged() { 118 return ! values.equals(originalValues); 119 } 120 121 122 protected void edit(int i) { 123 } 125 public Object getValueWithoutProcess(String fieldName) { 126 return values.get(fieldName); 127 } 128 public void setValueWithoutProcess(String fieldName, Object value) { 129 values.put(fieldName, value); 130 } 131 public void setValueWithoutChecks(String fieldName, Object value) { 132 values.put(fieldName, value); 133 } 134 135 public boolean isNull(String fieldName) { 136 return values.get(fieldName) == null; 137 } 138 protected void setSize(String fieldName, long size) { 139 sizes.put(fieldName, new Long (size)); 140 } 141 142 public long getSize(String fieldName) { 143 Long size = (Long ) sizes.get(fieldName); 144 if (size != null) { 145 return size.longValue(); 146 } else { 147 int s = SizeOf.getByteSize(values.get(fieldName)); 148 sizes.put(fieldName, new Long (s)); 149 return s; 150 } 151 } 152 153 public void commit() { 154 throw new UnsupportedOperationException ("Cannot commit map node"); 155 } 156 157 public void cancel() { 158 } 159 160 161 public void delete(boolean deleteRelations) { 162 throw new UnsupportedOperationException ("Cannot delete map node"); 163 } 164 165 public String toString() { 166 return "Map Node" + values; 167 } 168 169 public void deleteRelations(String type) throws NotFoundException { 170 } 171 172 public RelationList getRelations(String role, NodeManager nodeManager, String searchDir) throws NotFoundException { 173 return BridgeCollections.EMPTY_RELATIONLIST; 174 } 175 public RelationList getRelations(String role, String nodeManager) throws NotFoundException { 176 return BridgeCollections.EMPTY_RELATIONLIST; 177 } 178 179 180 public boolean hasRelations() { 181 return false; 182 } 183 184 public int countRelatedNodes(NodeManager otherNodeManager, String role, String direction) { 185 return 0; 186 187 } 188 189 public NodeList getRelatedNodes(NodeManager nodeManager, String role, String searchDir) { 190 return BridgeCollections.EMPTY_NODELIST; 191 } 192 193 public int countRelatedNodes(String type) { 194 return 0; 195 } 196 197 public StringList getAliases() { 198 return BridgeCollections.EMPTY_STRINGLIST; 199 } 200 201 public void createAlias(String aliasName) { 202 throw new UnsupportedOperationException ("Map nodes have no aliases"); 203 } 204 205 public void deleteAlias(String aliasName) { 206 throw new UnsupportedOperationException ("Map nodes have no aliases"); 207 } 208 209 public Relation createRelation(Node destinationNode, RelationManager relationManager) { 210 throw new UnsupportedOperationException ("Map nodes have no relations"); 211 } 212 213 214 public void setContext(String context) { 215 throw new UnsupportedOperationException ("Map nodes have no security context"); 216 } 217 218 public String getContext() { 220 throw new UnsupportedOperationException ("Virtual nodes have no security context"); 221 } 222 223 224 public StringList getPossibleContexts() { 226 return BridgeCollections.EMPTY_STRINGLIST; 227 } 228 229 public boolean mayWrite() { 230 return true; 231 } 232 233 public boolean mayDelete() { 234 return false; 235 } 236 237 public boolean mayChangeContext() { 238 return false; 239 } 240 241 public Collection getFunctions() { 242 return nodeManager.getFunctions(); 243 } 244 245 246 protected Function getNodeFunction(String functionName) { 247 return nodeManager.getFunction(functionName); 248 } 249 } 250 251 | Popular Tags |