1 10 11 package org.mmbase.bridge.implementation; 12 13 import org.mmbase.security.*; 14 import org.mmbase.bridge.*; 15 import org.mmbase.module.core.*; 16 import org.mmbase.util.logging.*; 17 18 25 public class BasicRelation extends BasicNode implements Relation { 26 private static final Logger log = Logging.getLoggerInstance(BasicRelation.class); 27 28 private RelationManager relationManager = null; 29 protected int snum; 30 protected int dnum; 31 32 private static final int UNSET = -999; 33 34 private int sourceNodeType = UNSET; 35 private int destinationNodeType = UNSET; 36 37 protected boolean relationChanged = false; 39 42 BasicRelation(MMObjectNode node, BasicCloud cloud) { 43 super(node, cloud); 44 } 45 46 49 BasicRelation(MMObjectNode node, BasicRelationManager nodeManager) { 50 super(node, nodeManager); 51 } 52 53 56 BasicRelation(MMObjectNode node, BasicCloud cloud, int id) { 57 super(node, cloud, id); 58 } 59 60 public final boolean isRelation() { 61 return true; 62 } 63 public Relation toRelation() { 64 return this; 65 } 66 67 72 protected void init() { 73 super.init(); 74 if (nodeManager instanceof RelationManager) { 75 relationManager = (RelationManager)nodeManager; 76 } 77 snum = getIntValue("snumber"); 78 dnum = getIntValue("dnumber"); 79 80 } 81 82 83 public Node getSource() { 84 return nodeManager.getCloud().getNode(snum); 87 } 88 89 public Node getDestination() { 90 return nodeManager.getCloud().getNode(dnum); 93 } 94 95 public void setSource(Node node) { 96 if (node.getCloud() != cloud) { 97 throw new BridgeException("Source and relation are not in the same transaction or from different clouds."); 98 } 99 relationChanged = true; 100 int source=node.getIntValue("number"); 101 if (source==-1) { 102 getNode().setValue("_snumber", node.getValue(MMObjectBuilder.TMP_FIELD_NUMBER)); 104 } else { 105 getNode().setValue("snumber",source); 106 } 107 snum = node.getNumber(); 108 sourceNodeType = node.getIntValue("otype"); 109 } 110 111 public void setDestination(Node node) { 112 if (node.getCloud() != cloud) { 113 throw new BridgeException("Destination and relation are not in the same transaction or from different clouds."); 114 } 115 relationChanged = true; 116 int dest=node.getIntValue("number"); 117 if (dest==-1) { 118 getNode().setValue("_dnumber", node.getValue(MMObjectBuilder.TMP_FIELD_NUMBER)); 120 } else { 121 getNode().setValue("dnumber",dest); 122 } 123 dnum = node.getNumber(); 124 destinationNodeType = node.getIntValue("otype"); 125 } 126 127 public RelationManager getRelationManager() { 128 if (relationManager == null) { 129 int stypenum = BasicCloudContext.mmb.getTypeRel().getNodeType(snum); 130 int dtypenum = BasicCloudContext.mmb.getTypeRel().getNodeType(dnum); 131 if (log.isDebugEnabled()) { 132 log.debug(stypenum + ", " + dtypenum + ", " + getNode().getIntValue("rnumber")); 133 } 134 135 relationManager=cloud.getRelationManager(stypenum,dtypenum, getNode().getIntValue("rnumber")); 136 } 137 return relationManager; 138 } 139 140 141 145 void checkValid() { 146 if (log.isDebugEnabled()) { 147 log.debug("s : " + snum + " d: " + dnum); 148 } 149 152 if (sourceNodeType == UNSET) { 153 sourceNodeType = -1; 154 if (snum != -1) sourceNodeType = BasicCloudContext.mmb.getTypeDef().getNodeType(snum); 155 } 156 if (destinationNodeType == UNSET) { 157 destinationNodeType = -1; 158 if (dnum!=-1) destinationNodeType = BasicCloudContext.mmb.getTypeDef().getNodeType(dnum); 159 } 160 161 int rnumber = getNode().getIntValue("rnumber"); 162 if (!BasicCloudContext.mmb.getTypeRel().contains(sourceNodeType, destinationNodeType, rnumber)) { 163 if (!BasicCloudContext.mmb.getTypeRel().contains(destinationNodeType, sourceNodeType, rnumber)) { 164 if (! cloud.hasNode(sourceNodeType)) { 165 throw new BridgeException("Source type of relation " + this + ": " + sourceNodeType + " does not point to a valid node."); 166 } 167 if (! cloud.hasNode(destinationNodeType)) { 168 throw new BridgeException("Destination type of relation " + this + ": " + destinationNodeType + " does not point to a valid node."); 169 } 170 if (! cloud.hasNode(rnumber)) { 171 throw new BridgeException("Rnumber of relation " + this + ": " + rnumber + " does not point to a valid node."); 172 } 173 throw new BridgeException("Source and/or Destination node are not of the correct type, or relation not allowed (" 174 + cloud.getNode(sourceNodeType).getValue("name") + "," 175 + cloud.getNode(destinationNodeType).getValue("name") + "," 176 + cloud.getNode(rnumber).getValue("sname") + ")"); 177 } 178 } 179 180 } 181 182 public void setValueWithoutProcess(String fieldName, Object value) { 183 edit(ACTION_EDIT); 184 if ("rnumber".equals(fieldName)) { 185 throw new BridgeException("Not allowed to change field '" + fieldName + "'."); 186 } else if ("snumber".equals(fieldName) || "dnumber".equals(fieldName)) { 187 relationChanged = true; 188 } 189 super.setValueWithoutProcess(fieldName, value); 190 } 191 192 public void commit() { 193 204 checkValid(); 205 if (! (BasicCloud.isTemporaryId(snum) || BasicCloud.isTemporaryId(dnum))) { 206 if (isNew()) { 207 cloud.verify(Operation.CREATE, BasicCloudContext.mmb.getTypeDef().getIntValue(getNodeManager().getName()), snum, dnum); 208 relationChanged = false; 209 } else { 210 if (relationChanged) { 211 cloud.verify(Operation.CHANGE_RELATION, noderef.getNumber(), snum, dnum); 212 relationChanged = false; 213 } 214 } 215 } 216 super.commit(); 217 if (!(cloud instanceof Transaction)) { 218 snum = getNode().getIntValue("snumber"); 219 dnum = getNode().getIntValue("dnumber"); 220 } 221 } 222 223 } 224 | Popular Tags |