001package de.deepamehta.core.impl; 002 003import de.deepamehta.core.model.AssociationModel; 004import de.deepamehta.core.model.ChildTopicsModel; 005import de.deepamehta.core.model.DeepaMehtaObjectModel; 006import de.deepamehta.core.model.IndexMode; 007import de.deepamehta.core.model.RoleModel; 008import de.deepamehta.core.model.TopicRoleModel; 009import de.deepamehta.core.model.TypeModel; 010import de.deepamehta.core.service.DeepaMehtaEvent; 011import de.deepamehta.core.service.Directive; 012 013import org.codehaus.jettison.json.JSONObject; 014 015import java.util.List; 016 017 018 019/** 020 * Collection of the data that makes up an {@link Association}. 021 * 022 * @author <a href="mailto:jri@deepamehta.de">Jörg Richter</a> 023 */ 024class AssociationModelImpl extends DeepaMehtaObjectModelImpl implements AssociationModel { 025 026 // ---------------------------------------------------------------------------------------------- Instance Variables 027 028 private RoleModelImpl roleModel1; // may be null in models used for an update operation 029 private RoleModelImpl roleModel2; // may be null in models used for an update operation 030 031 // ---------------------------------------------------------------------------------------------------- Constructors 032 033 AssociationModelImpl(DeepaMehtaObjectModelImpl object, RoleModelImpl roleModel1, RoleModelImpl roleModel2) { 034 super(object); 035 this.roleModel1 = roleModel1; 036 this.roleModel2 = roleModel2; 037 } 038 039 AssociationModelImpl(AssociationModelImpl assoc) { 040 super(assoc); 041 this.roleModel1 = assoc.roleModel1; 042 this.roleModel2 = assoc.roleModel2; 043 } 044 045 // -------------------------------------------------------------------------------------------------- Public Methods 046 047 @Override 048 public RoleModelImpl getRoleModel1() { 049 return roleModel1; 050 } 051 052 @Override 053 public RoleModelImpl getRoleModel2() { 054 return roleModel2; 055 } 056 057 // --- 058 059 @Override 060 public void setRoleModel1(RoleModel roleModel1) { 061 this.roleModel1 = (RoleModelImpl) roleModel1; 062 } 063 064 @Override 065 public void setRoleModel2(RoleModel roleModel2) { 066 this.roleModel2 = (RoleModelImpl) roleModel2; 067 } 068 069 // --- Convenience Methods --- 070 071 @Override 072 public RoleModelImpl getRoleModel(String roleTypeUri) { 073 boolean rm1 = roleModel1.getRoleTypeUri().equals(roleTypeUri); 074 boolean rm2 = roleModel2.getRoleTypeUri().equals(roleTypeUri); 075 if (rm1 && rm2) { 076 throw new RuntimeException("Ambiguous getRoleModel() call: both players occupy role \"" + roleTypeUri + 077 "\" (" + this + ")"); 078 } 079 return rm1 ? roleModel1 : rm2 ? roleModel2 : null; 080 } 081 082 @Override 083 public boolean hasSameRoleTypeUris() { 084 return roleModel1.getRoleTypeUri().equals(roleModel2.getRoleTypeUri()); 085 } 086 087 @Override 088 public boolean matches(String roleTypeUri1, long playerId1, String roleTypeUri2, long playerId2) { 089 if (roleTypeUri1.equals(roleTypeUri2)) { 090 throw new IllegalArgumentException("matches() was called with 2 identical role type URIs (\"" + 091 roleTypeUri1 + "\")"); 092 } 093 if (!hasSameRoleTypeUris()) { 094 RoleModel r1 = getRoleModel(roleTypeUri1); 095 RoleModel r2 = getRoleModel(roleTypeUri2); 096 if (r1 != null && r1.getPlayerId() == playerId1 && 097 r2 != null && r2.getPlayerId() == playerId2) { 098 return true; 099 } 100 } 101 return false; 102 } 103 104 @Override 105 public long getOtherPlayerId(long id) { 106 long id1 = roleModel1.getPlayerId(); 107 long id2 = roleModel2.getPlayerId(); 108 if (id1 == id) { 109 return id2; 110 } else if (id2 == id) { 111 return id1; 112 } else { 113 throw new IllegalArgumentException("ID " + id + " doesn't refer to a player in " + this); 114 } 115 } 116 117 118 119 // === Implementation of the abstract methods === 120 121 @Override 122 public RoleModel createRoleModel(String roleTypeUri) { 123 return mf.newAssociationRoleModel(id, roleTypeUri); 124 } 125 126 127 128 // === Serialization === 129 130 @Override 131 public JSONObject toJSON() { 132 try { 133 return super.toJSON() 134 .put("role_1", roleModel1.toJSON()) 135 .put("role_2", roleModel2.toJSON()); 136 } catch (Exception e) { 137 throw new RuntimeException("Serialization failed (" + this + ")", e); 138 } 139 } 140 141 142 143 // === Java API === 144 145 @Override 146 public AssociationModel clone() { 147 try { 148 AssociationModel model = (AssociationModel) super.clone(); 149 model.setRoleModel1(roleModel1.clone()); 150 model.setRoleModel2(roleModel2.clone()); 151 return model; 152 } catch (Exception e) { 153 throw new RuntimeException("Cloning an AssociationModel failed", e); 154 } 155 } 156 157 @Override 158 public String toString() { 159 return "association (" + super.toString() + ", " + roleModel1 + ", " + roleModel2 + ")"; 160 } 161 162 163 164 // ----------------------------------------------------------------------------------------- Package Private Methods 165 166 @Override 167 String className() { 168 return "association"; 169 } 170 171 @Override 172 AssociationImpl instantiate() { 173 return new AssociationImpl(this, pl); 174 } 175 176 @Override 177 AssociationModelImpl createModelWithChildTopics(ChildTopicsModel childTopics) { 178 return mf.newAssociationModel(childTopics); 179 } 180 181 // --- 182 183 @Override 184 final AssociationTypeModelImpl getType() { 185 return pl.typeStorage.getAssociationType(typeUri); 186 } 187 188 @Override 189 final List<AssociationModelImpl> getAssociations() { 190 return pl.fetchAssociationAssociations(id); 191 } 192 193 // --- 194 195 @Override 196 final RelatedTopicModelImpl getRelatedTopic(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri, 197 String othersTopicTypeUri) { 198 return pl.fetchAssociationRelatedTopic(id, assocTypeUri, myRoleTypeUri, othersRoleTypeUri, 199 othersTopicTypeUri); 200 } 201 202 @Override 203 final List<RelatedTopicModelImpl> getRelatedTopics(String assocTypeUri, String myRoleTypeUri, 204 String othersRoleTypeUri, 205 String othersTopicTypeUri) { 206 return pl.fetchAssociationRelatedTopics(id, assocTypeUri, myRoleTypeUri, othersRoleTypeUri, 207 othersTopicTypeUri); 208 } 209 210 @Override 211 final List<RelatedTopicModelImpl> getRelatedTopics(List assocTypeUris, String myRoleTypeUri, 212 String othersRoleTypeUri, 213 String othersTopicTypeUri) { 214 return pl.fetchAssociationRelatedTopics(id, assocTypeUris, myRoleTypeUri, othersRoleTypeUri, 215 othersTopicTypeUri); 216 } 217 218 // --- 219 220 @Override 221 final void storeUri() { 222 pl.storeAssociationUri(id, uri); 223 } 224 225 @Override 226 final void storeTypeUri() { 227 reassignInstantiation(); 228 pl.storeAssociationTypeUri(id, typeUri); 229 } 230 231 @Override 232 final void storeSimpleValue() { 233 TypeModel type = getType(); 234 pl.storeAssociationValue(id, value, type.getIndexModes(), type.getUri(), getIndexValue()); 235 } 236 237 @Override 238 final void indexSimpleValue(IndexMode indexMode) { 239 pl.indexAssociationValue(id, indexMode, typeUri, getIndexValue()); 240 } 241 242 @Override 243 final void storeProperty(String propUri, Object propValue, boolean addToIndex) { 244 pl.storeAssociationProperty(id, propUri, propValue, addToIndex); 245 } 246 247 @Override 248 final void removeProperty(String propUri) { 249 pl.removeAssociationProperty(id, propUri); 250 } 251 252 // --- 253 254 @Override 255 final void _delete() { 256 pl._deleteAssociation(id); 257 } 258 259 // --- 260 261 @Override 262 final void checkReadAccess() { 263 pl.checkAssociationReadAccess(id); 264 } 265 266 @Override 267 final void checkWriteAccess() { 268 pl.checkAssociationWriteAccess(id); 269 } 270 271 // --- 272 273 @Override 274 final DeepaMehtaEvent getPreUpdateEvent() { 275 return CoreEvent.PRE_UPDATE_ASSOCIATION; 276 } 277 278 @Override 279 final DeepaMehtaEvent getPostUpdateEvent() { 280 return CoreEvent.POST_UPDATE_ASSOCIATION; 281 } 282 283 @Override 284 final DeepaMehtaEvent getPreDeleteEvent() { 285 return CoreEvent.PRE_DELETE_ASSOCIATION; 286 } 287 288 @Override 289 final DeepaMehtaEvent getPostDeleteEvent() { 290 return CoreEvent.POST_DELETE_ASSOCIATION; 291 } 292 293 // --- 294 295 @Override 296 final Directive getUpdateDirective() { 297 return Directive.UPDATE_ASSOCIATION; 298 } 299 300 @Override 301 final Directive getDeleteDirective() { 302 return Directive.DELETE_ASSOCIATION; 303 } 304 305 306 307 // === Core Internal Hooks === 308 309 @Override 310 void preCreate() { 311 duplicateCheck(); 312 } 313 314 @Override 315 void postUpdate(DeepaMehtaObjectModel updateModel, DeepaMehtaObjectModel oldObject) { 316 // update association specific parts: the 2 roles 317 updateRoles((AssociationModel) updateModel); 318 // 319 duplicateCheck(); 320 // 321 // Type Editor Support 322 if (isAssocDef(this)) { 323 if (isAssocDef((AssociationModel) oldObject)) { 324 updateAssocDef((AssociationModel) oldObject); 325 } else { 326 createAssocDef(); 327 } 328 } else if (isAssocDef((AssociationModel) oldObject)) { 329 removeAssocDef(); 330 } 331 } 332 333 @Override 334 void preDelete() { 335 // Type Editor Support 336 if (isAssocDef(this)) { 337 // Note: we listen to the PRE event here, not the POST event. At POST time the assocdef sequence might be 338 // interrupted, which would result in a corrupted sequence once rebuild. (Due to the interruption, while 339 // rebuilding not all segments would be catched for deletion and recreated redundantly -> ambiguity.) 340 // ### FIXDOC 341 removeAssocDef(); 342 } 343 } 344 345 346 347 // === 348 349 /** 350 * @return this association's player which plays the given role. 351 * If there is no such player, null is returned. 352 * <p> 353 * If there are 2 such players an exception is thrown. 354 */ 355 DeepaMehtaObjectModelImpl getPlayer(String roleTypeUri) { 356 RoleModelImpl role = getRoleModel(roleTypeUri); 357 return role != null ? role.getPlayer(this) : null; 358 } 359 360 TopicModelImpl getTopicByType(String topicTypeUri) { 361 TopicModelImpl topic1 = filterTopic(roleModel1, topicTypeUri); 362 TopicModelImpl topic2 = filterTopic(roleModel2, topicTypeUri); 363 if (topic1 != null && topic2 != null) { 364 throw new RuntimeException("Ambiguous getTopicByType() call: both topics are of type \"" + topicTypeUri + 365 "\" (" + this + ")"); 366 } 367 return topic1 != null ? topic1 : topic2 != null ? topic2 : null; 368 } 369 370 // --- 371 372 void updateRoleTypeUri(RoleModelImpl role, String roleTypeUri) { 373 role.setRoleTypeUri(roleTypeUri); // update memory 374 pl.storeRoleTypeUri(id, role.playerId, role.roleTypeUri); // update DB 375 } 376 377 // ------------------------------------------------------------------------------------------------- Private Methods 378 379 380 381 private void duplicateCheck() { 382 // ### FIXME: the duplicate check is supported only for topic players, and if they are identified by-ID. 383 // Note: we can't call roleModel.getPlayer() as this would build an entire object model, but its "value" 384 // is not yet available in case this association is part of the player's composite structure. 385 // Compare to DeepaMehtaUtils.associationAutoTyping() 386 if (!(roleModel1 instanceof TopicRoleModel) || ((TopicRoleModel) roleModel1).topicIdentifiedByUri() || 387 !(roleModel2 instanceof TopicRoleModel) || ((TopicRoleModel) roleModel2).topicIdentifiedByUri()) { 388 return; 389 } 390 // Note: only readable assocs (access control) are considered 391 for (AssociationModelImpl assoc : pl._getAssociations(typeUri, roleModel1.playerId, roleModel2.playerId, 392 roleModel1.roleTypeUri, roleModel2.roleTypeUri)) { 393 if (assoc.id != id && assoc.value.equals(value)) { 394 throw new RuntimeException("Duplicate: such an association exists already (ID=" + assoc.id + 395 ", typeUri=\"" + typeUri + "\", value=\"" + value + "\")"); 396 } 397 } 398 } 399 400 401 402 // === Update (memory + DB) === 403 404 /** 405 * @param updateModel The data to update. 406 * If role 1 is <code>null</code> it is not updated. 407 * If role 2 is <code>null</code> it is not updated. 408 */ 409 private void updateRoles(AssociationModel updateModel) { 410 updateRole(updateModel.getRoleModel1(), 1); 411 updateRole(updateModel.getRoleModel2(), 2); 412 } 413 414 /** 415 * @param nr used only for logging 416 */ 417 private void updateRole(RoleModel updateModel, int nr) { 418 if (updateModel != null) { // abort if no update is requested 419 // Note: We must lookup the roles individually. 420 // The role order (getRole1(), getRole2()) is undeterministic and not fix. 421 RoleModelImpl role = getRole(updateModel); 422 String newRoleTypeUri = updateModel.getRoleTypeUri(); // new value 423 String roleTypeUri = role.getRoleTypeUri(); // current value 424 if (!roleTypeUri.equals(newRoleTypeUri)) { // has changed? 425 logger.info("### Changing role type " + nr + " from \"" + roleTypeUri + "\" -> \"" + newRoleTypeUri + 426 "\""); 427 updateRoleTypeUri(role, newRoleTypeUri); 428 } 429 } 430 } 431 432 433 434 // === Roles (memory access) === 435 436 /** 437 * Returns this association's role which refers to the same object as the given role model. 438 * The role returned is found by comparing topic IDs, topic URIs, or association IDs. 439 * The role types are <i>not</i> compared. 440 * <p> 441 * If the object refered by the given role model is not a player in this association an exception is thrown. 442 */ 443 private RoleModelImpl getRole(RoleModel roleModel) { 444 if (roleModel1.refsSameObject(roleModel)) { 445 return roleModel1; 446 } else if (roleModel2.refsSameObject(roleModel)) { 447 return roleModel2; 448 } 449 throw new RuntimeException("Role is not part of association (role=" + roleModel + ", association=" + this); 450 } 451 452 // --- 453 454 private TopicModelImpl filterTopic(RoleModelImpl role, String topicTypeUri) { 455 if (role instanceof TopicRoleModel) { 456 TopicModelImpl topic = ((TopicRoleModelImpl) role).getPlayer(this); 457 if (topic.getTypeUri().equals(topicTypeUri)) { 458 return topic; 459 } 460 } 461 return null; 462 } 463 464 465 466 // === Store (DB only) === 467 468 private void reassignInstantiation() { 469 // remove current assignment 470 fetchInstantiation().delete(); 471 // create new assignment 472 pl.createAssociationInstantiation(id, typeUri); 473 } 474 475 private AssociationModelImpl fetchInstantiation() { 476 RelatedTopicModelImpl assocType = getRelatedTopic("dm4.core.instantiation", "dm4.core.instance", 477 "dm4.core.type", "dm4.core.assoc_type"); 478 // 479 if (assocType == null) { 480 throw new RuntimeException("Association " + id + " is not associated to an association type"); 481 } 482 // 483 return assocType.getRelatingAssociation(); 484 } 485 486 487 488 // === Type Editor Support === 489 490 // ### TODO: explain 491 492 private void createAssocDef() { 493 TypeModelImpl parentType = fetchParentType(); 494 logger.info("##### Adding association definition " + id + " to type \"" + parentType.getUri() + "\""); 495 // 496 parentType._addAssocDef(this); 497 } 498 499 private void updateAssocDef(AssociationModel oldAssoc) { 500 TypeModelImpl parentType = fetchParentType(); 501 logger.info("##### Updating association definition " + id + " of type \"" + parentType.getUri() + "\""); 502 // 503 parentType._updateAssocDef(this, oldAssoc); 504 } 505 506 private void removeAssocDef() { 507 TypeModelImpl parentType = fetchParentType(); 508 logger.info("##### Removing association definition " + id + " from type \"" + parentType.getUri() + "\""); 509 // 510 parentType._removeAssocDefFromMemoryAndRebuildSequence(this); 511 } 512 513 // --- 514 515 private boolean isAssocDef(AssociationModel assoc) { 516 String typeUri = assoc.getTypeUri(); 517 if (!typeUri.equals("dm4.core.aggregation_def") && 518 !typeUri.equals("dm4.core.composition_def")) { 519 return false; 520 } 521 // 522 if (assoc.hasSameRoleTypeUris()) { 523 return false; 524 } 525 if (assoc.getRoleModel("dm4.core.parent_type") == null || 526 assoc.getRoleModel("dm4.core.child_type") == null) { 527 return false; 528 } 529 // 530 return true; 531 } 532 533 private TypeModelImpl fetchParentType() { 534 return pl.typeStorage.fetchParentType(this); 535 } 536}