Skip to content

Commit

Permalink
Core Service: methods for deleting types (#607).
Browse files Browse the repository at this point in the history
2 methods added to Core Service:
{{{
Directives deleteTopicType(String topicTypeUri)
Directives deleteAssociationType(String assocTypeUri)
}}}
Deletes the respective type from the DB and the type cache.
The directives returned include a DELETE_TOPIC_TYPE resp. DELETE_ASSOCIATION_TYPE directive.

REST API:
{{{
DELETE /core/topictype/{uri}
DELETE /core/assoctype/{uri}
}}}

See #607.
  • Loading branch information
jri committed Jan 25, 2014
1 parent 11490e1 commit 7a7254c
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ void updateChildTopics(List<? extends TopicModel> newChildTopics, AssociationDef



// === Deletion ===

/**
* Deletes the DeepaMehta object in its entirety, that is
* - the object itself (the <i>parent</i>)
* - all child topics associated via "dm4.core.composition", recusively
* - all the remaining direct associations, e.g. "dm4.core.instantiation"
*/
void delete(Directives directives);



// === Traversal ===

// --- Topic Retrieval ---
Expand Down Expand Up @@ -123,18 +135,6 @@ Association getAssociation(String assocTypeUri, String myRoleTypeUri, String oth



// === Deletion ===

/**
* Deletes the DeepaMehta object in its entirety, that is
* - the object itself (the <i>parent</i>)
* - all child topics associated via "dm4.core.composition", recusively
* - all the remaining direct associations, e.g. "dm4.core.instantiation"
*/
void delete(Directives directives);



// === Properties ===

Object getProperty(String propUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ class AttachedAssociation extends AttachedDeepaMehtaObject implements Associatio



// === Updating ===

/**
* @param model The data to update.
* If the type URI is <code>null</code> it is not updated.
* If role 1 is <code>null</code> it is not updated.
* If role 2 is <code>null</code> it is not updated.
*/
@Override
public void update(AssociationModel model, ClientState clientState, Directives directives) {
// ### TODO: there is no possible POST_UPDATE_ASSOCIATION_REQUEST event to fire here (compare to
// AttachedTopic update()). It would be equivalent to POST_UPDATE_ASSOCIATION.
// Per request exactly one association is updated. Its childs are topics (never associations).
logger.info("Updating association " + getId() + " (new " + model + ")");
//
dms.fireEvent(CoreEvent.PRE_UPDATE_ASSOCIATION, this, model, directives);
//
AssociationModel oldModel = getModel().clone();
super.update(model, clientState, directives);
updateRole(model.getRoleModel1(), 1);
updateRole(model.getRoleModel2(), 2);
//
addUpdateDirective(directives);
//
dms.fireEvent(CoreEvent.POST_UPDATE_ASSOCIATION, this, oldModel, clientState, directives);
}



// === Deletion ===

@Override
Expand Down Expand Up @@ -170,32 +199,6 @@ public AssociationModel getModel() {



// === Updating ===

/**
* @param model The data to update.
* If the type URI is <code>null</code> it is not updated.
* If role 1 is <code>null</code> it is not updated.
* If role 2 is <code>null</code> it is not updated.
*/
@Override
public void update(AssociationModel model, ClientState clientState, Directives directives) {
logger.info("Updating association " + getId() + " (new " + model + ")");
//
dms.fireEvent(CoreEvent.PRE_UPDATE_ASSOCIATION, this, model, directives);
//
AssociationModel oldModel = getModel().clone();
super.update(model, clientState, directives);
updateRole(model.getRoleModel1(), 1);
updateRole(model.getRoleModel2(), 2);
//
addUpdateDirective(directives);
//
dms.fireEvent(CoreEvent.POST_UPDATE_ASSOCIATION, this, oldModel, clientState, directives);
}



// === Traversal ===

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ final void removeFromTypeCache() {
// ---

@Override
final void addDeleteTypeDirective(Directives directives, JSONEnabled arg) {
directives.add(Directive.DELETE_ASSOCIATION_TYPE, arg);
final Directive getDeleteTypeDirective() {
return Directive.DELETE_ASSOCIATION_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,35 @@ public void updateChildTopics(List<? extends TopicModel> newChildTopics, Associa



// === Deletion ===

/**
* Deletes all sub-topics of this DeepaMehta object (associated via "dm4.core.composition", recursively) and
* deletes all the remaining direct associations of this DeepaMehta object.
* <p>
* Note: deletion of the object itself is up to the subclasses.
*/
@Override
public void delete(Directives directives) {
// Note: directives must be not null.
// The subclass's delete() methods add DELETE_TOPIC and DELETE_ASSOCIATION directives to it respectively.
if (directives == null) {
throw new IllegalArgumentException("directives is null");
}
// 1) recursively delete sub-topics
ResultList<RelatedTopic> childTopics = getRelatedTopics("dm4.core.composition",
"dm4.core.parent", "dm4.core.child", null, false, false, 0);
for (Topic childTopic : childTopics) {
childTopic.delete(directives);
}
// 2) delete direct associations
for (Association assoc : getAssociations()) { // getAssociations() is abstract
assoc.delete(directives);
}
}



// === Traversal ===

// --- Topic Retrieval ---
Expand Down Expand Up @@ -238,35 +267,6 @@ public ResultList<RelatedTopic> getRelatedTopics(String assocTypeUri, String myR



// === Deletion ===

/**
* Deletes all sub-topics of this DeepaMehta object (associated via "dm4.core.composition", recursively) and
* deletes all the remaining direct associations of this DeepaMehta object.
* <p>
* Note: deletion of the object itself is up to the subclasses.
*/
@Override
public void delete(Directives directives) {
// Note: directives must be not null.
// The subclass's delete() methods add DELETE_TOPIC and DELETE_ASSOCIATION directives to it respectively.
if (directives == null) {
throw new IllegalArgumentException("directives is null");
}
// 1) recursively delete sub-topics
ResultList<RelatedTopic> childTopics = getRelatedTopics("dm4.core.composition",
"dm4.core.parent", "dm4.core.child", null, false, false, 0);
for (Topic childTopic : childTopics) {
childTopic.delete(directives);
}
// 2) delete direct associations
for (Association assoc : getAssociations()) { // getAssociations() is abstract
assoc.delete(directives);
}
}



// **********************************
// *** JSONEnabled Implementation ***
// **********************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ class AttachedTopic extends AttachedDeepaMehtaObject implements Topic {



// === Updating ===

@Override
public void update(TopicModel model, ClientState clientState, Directives directives) {
_update(model, clientState, directives);
//
dms.fireEvent(CoreEvent.POST_UPDATE_TOPIC_REQUEST, this);
}



// === Deletion ===

@Override
Expand Down Expand Up @@ -83,17 +94,6 @@ public TopicModel getModel() {



// === Updating ===

@Override
public void update(TopicModel model, ClientState clientState, Directives directives) {
_update(model, clientState, directives);
//
dms.fireEvent(CoreEvent.POST_UPDATE_TOPIC_REQUEST, this);
}



// === Traversal ===

// --- Association Retrieval ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ final void removeFromTypeCache() {
// ---

@Override
final void addDeleteTypeDirective(Directives directives, JSONEnabled arg) {
directives.add(Directive.DELETE_TOPIC_TYPE, arg);
final Directive getDeleteTypeDirective() {
return Directive.DELETE_TOPIC_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import de.deepamehta.core.model.RoleModel;
import de.deepamehta.core.model.TypeModel;
import de.deepamehta.core.service.ClientState;
import de.deepamehta.core.service.Directive;
import de.deepamehta.core.service.Directives;
import de.deepamehta.core.storage.spi.DeepaMehtaTransaction;

import org.codehaus.jettison.json.JSONObject;

Expand Down Expand Up @@ -44,6 +46,58 @@ abstract class AttachedType extends AttachedTopic implements Type {



// ******************************************
// *** AttachedDeepaMehtaObject Overrides ***
// ******************************************



// === Updating ===

@Override
public void update(TypeModel model, ClientState clientState, Directives directives) {
boolean uriChanged = hasUriChanged(model.getUri());
if (uriChanged) {
removeFromTypeCache(directives);
}
//
super.update(model, clientState, directives);
//
if (uriChanged) {
putInTypeCache(); // abstract
}
//
updateDataTypeUri(model.getDataTypeUri(), directives);
updateAssocDefs(model.getAssocDefs(), clientState, directives);
updateSequence(model.getAssocDefs());
updateLabelConfig(model.getLabelConfig(), directives);
}



// === Deletion ===

@Override
public void delete(Directives directives) {
DeepaMehtaTransaction tx = dms.beginTx();
try {
logger.info("Deleting " + className() + " \"" + getUri() + "\"");
//
super.delete(directives); // delete type topic
//
removeFromTypeCache(directives);
//
tx.success();
} catch (Exception e) {
logger.warning("ROLLBACK!");
throw new RuntimeException("Deleting " + className() + " \"" + getUri() + "\" failed", e);
} finally {
tx.finish();
}
}



// ***************************
// *** Type Implementation ***
// ***************************
Expand Down Expand Up @@ -172,28 +226,6 @@ public TypeModel getModel() {



// === Updating ===

@Override
public void update(TypeModel model, ClientState clientState, Directives directives) {
boolean uriChanged = hasUriChanged(model.getUri());
if (uriChanged) {
removeFromTypeCache(); // abstract
addDeleteTypeDirective(directives, new JSONWrapper("uri", getUri())); // abstract
}
//
super.update(model, clientState, directives);
//
if (uriChanged) {
putInTypeCache(); // abstract
}
//
updateDataTypeUri(model.getDataTypeUri(), directives);
updateAssocDefs(model.getAssocDefs(), clientState, directives);
updateSequence(model.getAssocDefs());
updateLabelConfig(model.getLabelConfig(), directives);
}

// ----------------------------------------------------------------------------------------- Package Private Methods

abstract void putInTypeCache();
Expand All @@ -202,7 +234,7 @@ public void update(TypeModel model, ClientState clientState, Directives directiv

// ---

abstract void addDeleteTypeDirective(Directives directives, JSONEnabled arg);
abstract Directive getDeleteTypeDirective();

// ------------------------------------------------------------------------------------------------- Private Methods

Expand Down Expand Up @@ -339,6 +371,21 @@ private void initViewConfig() {



// ===

/**
* Removes this type from type cache and adds a DELETE TYPE directive to the given set of directives.
*/
private void removeFromTypeCache(Directives directives) {
removeFromTypeCache(); // abstract
addDeleteTypeDirective(directives);
}

private void addDeleteTypeDirective(Directives directives) {
Directive dir = getDeleteTypeDirective(); // abstract
directives.add(dir, new JSONWrapper("uri", getUri()));
}

// ------------------------------------------------------------------------------------------------- Private Classes

private class JSONWrapper implements JSONEnabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public void deliver(EventListener listener, Object... params) {
}
};

// Note: a corresponding POST_UPDATE_ASSOCIATION_REQUEST event is not necessary.
// It would be equivalent to POST_UPDATE_ASSOCIATION.
// Per request exactly one association is updated. Its childs are topics (never associations).

// ---

static DeepaMehtaEvent PRE_DELETE_ASSOCIATION = new DeepaMehtaEvent(PreDeleteAssociationListener.class) {
Expand Down
Loading

0 comments on commit 7a7254c

Please sign in to comment.