Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / DeleteRelationAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class DeleteRelationAction extends UndoableEntityAction {
6         private var setDeleted:Function;
7         private var effects:CompositeUndoableAction;
8         private var memberList:Array;
9         private var oldMemberList:Array;
10         
11         public function DeleteRelationAction(relation:Relation, setDeleted:Function, memberList:Array) {
12             super(relation, "Delete");
13             this.setDeleted = setDeleted;
14             this.memberList = memberList;
15         }
16             
17         public override function doAction():uint {
18             var relation:Relation = entity as Relation;
19             if ( relation.isDeleted() )
20                 return NO_CHANGE;
21
22             effects = new CompositeUndoableAction("Delete refs");
23                         relation.removeFromParents(effects.push);
24                         oldMemberList = memberList.slice();
25                         for each (var member:RelationMember in memberList) {
26                             member.entity.removeParent(relation);
27                         }
28                         memberList=[];
29                         effects.doAction();
30                         setDeleted(true);
31             
32             // see note in DeleteNodeAction
33             if (relation.id < 0) {
34               markClean();
35             } else {
36               markDirty();
37             }
38             relation.dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, relation));
39
40             return SUCCESS;
41         }
42             
43         public override function undoAction():uint {
44             var relation:Relation = entity as Relation;
45             setDeleted(false);
46             if (relation.id < 0) {
47               markDirty();
48             } else {
49               markClean();
50             }
51             relation.dispatchEvent(new EntityEvent(Connection.NEW_RELATION, relation));
52             effects.undoAction();
53             for each(var member:RelationMember in oldMemberList) {
54                 memberList.push(member);
55                 member.entity.addParent(relation);
56                 relation.dispatchEvent(new RelationMemberEvent(
57                         Connection.RELATION_MEMBER_ADDED, member.entity, relation, 0));
58             }
59             
60             return SUCCESS;
61         }
62     }
63 }
64