1 10 11 package org.mmbase.bridge.implementation; 12 13 import java.util.*; 14 15 import javax.servlet.*; 16 import javax.servlet.http.*; 17 18 import org.mmbase.bridge.*; 19 import org.mmbase.bridge.util.Queries; 20 import org.mmbase.core.CoreField; 21 import org.mmbase.storage.search.*; 22 import org.mmbase.module.core.*; 23 import org.mmbase.module.corebuilders.*; 24 import org.mmbase.security.Operation; 25 import org.mmbase.util.PageInfo; 26 import org.mmbase.util.StringTagger; 27 import org.mmbase.util.functions.Function; 28 import org.mmbase.util.logging.*; 29 30 44 public class BasicNodeManager extends BasicNode implements NodeManager, Comparable { 45 private static final Logger log = Logging.getLoggerInstance(BasicNodeManager.class); 46 47 50 private long internalVersion = -1; 51 52 protected MMObjectBuilder builder; 54 55 protected Map fieldTypes = new HashMap(); 57 58 68 BasicNodeManager(MMObjectNode node, BasicCloud cloud, int nodeid) { 69 super(node, cloud, nodeid); 70 } 72 73 81 BasicNodeManager(MMObjectNode node, BasicCloud cloud) { 82 super(node, cloud); 83 initManager(); 84 } 85 86 93 BasicNodeManager(MMObjectBuilder builder, BasicCloud cloud) { 94 super(cloud); 95 noderef = getNodeForBuilder(builder); 96 this.builder = builder; 97 TypeDef typeDef = BasicCloudContext.mmb.getTypeDef(); 98 if (builder == typeDef) { 99 nodeManager = this; 100 } else { 101 nodeManager = cloud.getBasicNodeManager(typeDef); 102 } 103 sync(); 104 } 105 106 110 private static MMObjectNode getNodeForBuilder(MMObjectBuilder builder) { 111 if (builder.isVirtual()) { 112 return new org.mmbase.module.core.VirtualNode(BasicCloudContext.mmb.getTypeDef()); 113 } else { 114 MMObjectNode typedefNode = builder.getNode(builder.getNumber()); 115 if (typedefNode == null) { 116 throw new RuntimeException ("Could not find typedef node for builder " + builder + " with otype " + builder.getNumber()); 117 } 118 return typedefNode; 119 } 120 } 121 122 protected void setNodeManager(MMObjectNode node) { 123 int nodeNumber = node.getNumber(); 124 if (nodeNumber >= 0 && nodeNumber == node.getBuilder().getNumber()) { nodeManager = this; 126 } else { 127 log.debug("Setting node manager for nodeManager, but no typedef " + node); 128 super.setNodeManager(node); 129 } 130 } 131 132 public final boolean isNodeManager() { 133 return true; 134 } 135 136 public final NodeManager toNodeManager() { 137 return this; 138 } 139 140 144 protected void initManager() { 145 if (builder == null) { 146 if(noderef == null) { 147 throw new BridgeException("node reference was null, could not continue"); 148 } 149 TypeDef typedef = (TypeDef) noderef.getBuilder(); 153 builder = typedef.getBuilder(noderef); 154 if(builder == null) { 155 log.warn ("could not find nodemanager for node #" + noderef.getNumber() + " (" + noderef.getGUIIndicator() + ")"); 160 return; 161 } 162 } 163 sync(); 164 } 165 166 167 170 protected static void sync(MMObjectBuilder builder, Map fieldTypes, NodeManager nodeManager) { 171 Collection fields = builder.getFields(); 172 if (fields != null) { fieldTypes.clear(); 174 for(Iterator i = fields.iterator(); i.hasNext();){ 175 CoreField f = (CoreField) i.next(); 176 Field ft = new BasicField(f, nodeManager); 177 if (f.getStoragePosition() > 0) { 178 fieldTypes.put(ft.getName().toLowerCase(), ft); 179 } 180 } 181 } 182 } 183 184 185 190 synchronized private void sync() { 191 long builderVersion = builder.getInternalVersion(); 192 if (internalVersion < builderVersion) { 193 internalVersion = builderVersion; 194 sync(builder, fieldTypes, this); 195 } 196 } 197 198 202 protected Map getFieldTypes() { 203 sync(); 204 return fieldTypes; 205 } 206 207 211 protected final class NodeAndId { 212 final MMObjectNode node; 213 final int id; 214 NodeAndId(MMObjectNode n, int i) { 215 node = n; id = i; 216 } 217 } 218 219 220 225 protected NodeAndId createMMObjectNode() { 226 int id = BasicCloud.uniqueId(); 228 { 229 String currentObjectContext = BasicCloudContext.tmpObjectManager.createTmpNode(getMMObjectBuilder().getTableName(), cloud.getAccount(), ""+id); 230 cloud.add(currentObjectContext); 232 } 233 234 MMObjectNode node = BasicCloudContext.tmpObjectManager.getNode(cloud.getAccount(), "" + id); 235 237 node.setValue("owner", cloud.getUser().getOwnerField()); 240 241 return new NodeAndId(node, id); 242 } 243 244 248 protected BasicNode createBasicNode() { 249 NodeAndId n = createMMObjectNode(); 250 MMObjectBuilder bul = getMMObjectBuilder(); 251 if (bul instanceof TypeDef) { 252 return new BasicNodeManager(n.node, cloud, n.id); 254 } else if (bul instanceof TypeRel) { 255 return new BasicRelationManager(n.node, cloud, n.id); 256 } else { 257 return new BasicNode(n.node, cloud, n.id); 258 } 259 } 260 public final Node createNode() { 261 return createBasicNode(); 262 } 263 264 265 public NodeManager getParent() throws NotFoundException { 266 MMObjectBuilder bul = getMMObjectBuilder().getParentBuilder(); 267 if (bul == null) { 268 throw new NotFoundException("Parent of nodemanager " + getName() + "does not exist"); 269 } else { 270 return cloud.getNodeManager(bul.getTableName()); 271 } 272 } 273 274 public NodeManagerList getDescendants() { 275 List descs = getMMObjectBuilder().getDescendants(); 276 return new BasicNodeManagerList(descs, cloud); 277 } 278 279 public String getName() { 280 if (builder != null) { 281 return builder.getTableName(); 282 } else { 283 return getStringValue("name"); 284 } 285 } 286 287 public String getProperty(String name) { 288 if (builder != null) { 289 return builder.getInitParameter(name); 290 } else { 291 return null; 292 } 293 } 294 295 public Map getProperties() { 296 if (builder != null) { 297 return new HashMap(builder.getInitParameters()); 298 } else { 299 return Collections.EMPTY_MAP; 300 } 301 } 302 303 public String getGUIName() { 304 return getGUIName(NodeManager.GUI_SINGULAR); 305 } 306 307 public String getGUIName(int plurality) { 308 return getGUIName(plurality, null); 309 } 310 311 public String getGUIName(int plurality, Locale locale) { 312 if (locale==null) locale = cloud.getLocale(); 313 if (builder!=null) { 314 if (plurality == NodeManager.GUI_SINGULAR) { 315 return builder.getSingularName(locale.getLanguage()); 316 } else { 317 return builder.getPluralName(locale.getLanguage()); 318 } 319 } else { 320 return getName(); 321 } 322 } 323 324 public String getDescription() { 325 return getDescription(null); 326 } 327 328 public String getDescription(Locale locale) { 329 if (locale == null) locale = cloud.getLocale(); 330 if (builder != null) { 331 return builder.getDescription(locale.getLanguage()); 332 } else { 333 return ""; 334 } 335 } 336 337 public FieldList getFields() { 338 return getFields(NodeManager.ORDER_NONE); 339 } 340 341 public FieldList getFields(int order) { 342 if (builder != null) { 343 return new BasicFieldList(builder.getFields(order), this); 344 } else { 345 return new BasicFieldList(getFieldTypes().values(), this); 346 } 347 } 348 349 public Field getField(String fieldName) throws NotFoundException { 350 Field f = (Field) getFieldTypes().get(fieldName.toLowerCase()); 351 if (f == null) throw new NotFoundException("Field '" + fieldName + "' does not exist in NodeManager '" + getName() + "'.(" + getFieldTypes() + ")"); 352 return f; 353 } 354 355 public boolean hasField(String fieldName) { 356 return fieldName != null && getFieldTypes().containsKey(fieldName.toLowerCase()); 357 } 358 359 360 public NodeQuery createQuery() { 362 return new BasicNodeQuery(this); 363 } 364 365 public NodeList getList(NodeQuery query) { 366 try { 367 boolean checked = cloud.setSecurityConstraint(query); 368 List resultList = builder.getStorageConnector().getNodes(query); 369 BasicNodeList resultNodeList; 370 NodeManager nm = query.getNodeManager(); 371 if (nm instanceof RelationManager || (nm == this && builder instanceof InsRel)) { 372 resultNodeList = new BasicRelationList(resultList, cloud); 373 } else { 374 resultNodeList = new BasicNodeList(resultList, cloud); 375 } 376 377 resultNodeList.setProperty(NodeList.QUERY_PROPERTY, query); 378 379 if (! checked) { 380 cloud.checkNodes(resultNodeList, query); 381 } 382 383 return resultNodeList; 384 } catch (SearchQueryException sqe) { 385 throw new BridgeException(sqe); 386 } 387 } 388 389 390 public NodeList getList(String constraints, String sorted, String directions) { 391 393 402 404 405 NodeQuery query = createQuery(); 406 Queries.addConstraints(query, constraints); 407 Queries.addSortOrders(query, sorted, directions); 408 NodeList list = getList(query); 409 list.setProperty("constraints", constraints); 410 list.setProperty("orderby", sorted); 411 list.setProperty("directions", directions); 412 return list; 413 } 414 415 416 public RelationManagerList getAllowedRelations() { 417 return getAllowedRelations((NodeManager) null, null, null); 418 } 419 420 public RelationManagerList getAllowedRelations(String nodeManager, String role, String direction) { 421 if (nodeManager==null) { 422 return getAllowedRelations((NodeManager)null, role, direction); 423 } else { 424 return getAllowedRelations(cloud.getNodeManager(nodeManager), role, direction); 425 } 426 } 427 428 public RelationManagerList getAllowedRelations(NodeManager nodeManager, String role, String direction) { 429 int thisOType = getMMObjectBuilder().getNumber(); 430 int requestedRole = -1; 431 if (role != null) { 432 requestedRole = BasicCloudContext.mmb.getRelDef().getNumberByName(role); 433 if (requestedRole == -1) { 434 throw new NotFoundException("Could not get role '" + role + "'"); 435 } 436 } 437 438 int dir = ClusterBuilder.getSearchDir(direction); 439 440 Enumeration typerelNodes; 441 if (nodeManager != null) { 442 int otherOType = nodeManager.getNumber(); 443 typerelNodes = BasicCloudContext.mmb.getTypeRel().getAllowedRelations(thisOType, otherOType); 444 } else { 445 typerelNodes = BasicCloudContext.mmb.getTypeRel().getAllowedRelations(thisOType); 446 } 447 448 List nodes = new ArrayList(); 449 while (typerelNodes.hasMoreElements()) { 450 MMObjectNode n = (MMObjectNode) typerelNodes.nextElement(); 451 if ((requestedRole == -1) || (requestedRole == n.getIntValue("rnumber"))) { 452 int snumber = n.getIntValue("snumber"); 453 int dnumber = n.getIntValue("dnumber"); 454 if (snumber != dnumber) { if (thisOType == dnumber) { 456 if (dir == RelationStep.DIRECTIONS_DESTINATION) { 457 continue; 458 } 459 } else { 460 if (dir == RelationStep.DIRECTIONS_SOURCE) { 461 continue; 462 } 463 } 464 } 465 nodes.add(n); 466 } 467 } 468 return new BasicRelationManagerList(nodes, cloud); 469 } 470 471 public String getInfo(String command) { 472 return getInfo(command, null,null); 473 } 474 475 public String getInfo(String command, ServletRequest req, ServletResponse resp){ 476 MMObjectBuilder builder = getMMObjectBuilder(); 477 StringTokenizer tokens = new StringTokenizer(command,"-"); 478 return builder.replace(new PageInfo((HttpServletRequest)req, (HttpServletResponse)resp, getCloud()),tokens); 479 } 480 481 public NodeList getList(String command, Map parameters){ 482 return getList(command,parameters,null,null); 483 } 484 485 public NodeList getList(String command, Map parameters, ServletRequest req, ServletResponse resp){ 486 MMObjectBuilder builder = getMMObjectBuilder(); 487 StringTagger params= new StringTagger(""); 488 if (parameters!=null) { 489 for (Iterator entries = parameters.entrySet().iterator(); entries.hasNext(); ) { 490 Map.Entry entry = (Map.Entry) entries.next(); 491 String key=(String ) entry.getKey(); 492 Object o = entry.getValue(); 493 if (o instanceof Vector) { 494 params.setValues(key,(Vector)o); 495 } else { 496 params.setValue(key,""+o); 497 } 498 } 499 } 500 try { 501 StringTokenizer tokens= new StringTokenizer(command,"-"); 502 List v = builder.getList(new PageInfo((HttpServletRequest)req, (HttpServletResponse)resp, getCloud()), params, tokens); 503 if (v == null) { 504 v = new ArrayList(); 505 } 506 int items=1; 507 try { 508 items=Integer.parseInt(params.Value("ITEMS")); 509 } catch (Exception e) { 510 log.warn("parameter 'ITEMS' must be a int value, it was :" + params.Value("ITEMS")); 511 } 512 Vector fieldlist = params.Values("FIELDS"); 513 Vector res = new Vector(v.size() / items); 514 for (int i= 0; i<v.size(); i+=items) { 515 MMObjectNode node = new org.mmbase.module.core.VirtualNode(builder); 516 for(int j= 0; (j<items) && (j<v.size()); j++) { 517 if ((fieldlist!=null) && (j<fieldlist.size())) { 518 node.setValue((String )fieldlist.get(j),v.get(i+j)); 519 } else { 520 node.setValue("item"+(j+1),v.get(i+j)); 521 } 522 } 523 res.add(node); 524 } 525 if (res.size()>0) { 526 NodeManager tempNodeManager = new VirtualNodeManager((org.mmbase.module.core.VirtualNode)res.get(0), cloud); 527 return new BasicNodeList(res, tempNodeManager); 528 } 529 return createNodeList(); 530 } catch (Exception e) { 531 log.error(Logging.stackTrace(e)); 532 throw new BridgeException(e); 533 } 534 } 535 536 public boolean mayCreateNode() { 537 if (builder == null) return false; 538 return cloud.check(Operation.CREATE, builder.getNumber()); 539 } 540 541 MMObjectBuilder getMMObjectBuilder() { 542 if (builder == null) { 543 throw new IllegalStateException ("No functional instantiation exists (yet/any more) for this NodeManager."); 544 } 545 return builder; 546 } 547 548 550 public void commit() { 551 super.commit(); initManager(); 554 } 555 556 public void delete(boolean deleteRelations) { 557 super.delete(deleteRelations); 558 builder=null; } 560 561 public Collection getFunctions() { 562 return builder.getFunctions(); 563 } 564 565 protected Function getNodeFunction(String functionName) { 566 if (log.isDebugEnabled()) { 567 log.debug("Getting function '" + functionName + "' for " + this); 568 } 569 570 Function function = getNode().getFunction(functionName); 573 if (function == null || functionName.equals("info") || functionName.equals("getFunctions")) { 574 function = builder != null ? builder.getFunction(functionName) : null; 575 } 576 if (function == null) { 577 throw new NotFoundException("Function with name " + functionName + " does not exist in " + builder.getFunctions()); 578 } 579 return function; 580 581 } 582 583 public FieldList createFieldList() { 584 return new BasicFieldList(Collections.EMPTY_LIST, this); 585 } 586 587 public NodeList createNodeList() { 588 return new BasicNodeList(Collections.EMPTY_LIST, this); 589 } 590 591 public RelationList createRelationList() { 592 return new BasicRelationList(Collections.EMPTY_LIST, this); 593 } 594 595 } 596 597 | Popular Tags |