-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CommunityLifeCycle.java, CommunityEvent.java and CommunityEventType.java. With tests. Database connection is working, added persist/load as well as adjusted Database.java for the clc by storeCLC, and getCLC methods.
- Loading branch information
1 parent
0a5ce08
commit 0ec8b62
Showing
8 changed files
with
714 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
rest_ocd_services/src/main/java/i5/las2peer/services/ocd/utils/CommunityEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package i5.las2peer.services.ocd.utils; | ||
|
||
import com.arangodb.ArangoCollection; | ||
import com.arangodb.ArangoDatabase; | ||
import com.arangodb.entity.BaseDocument; | ||
import com.arangodb.model.DocumentCreateOptions; | ||
import com.arangodb.model.DocumentReadOptions; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import i5.las2peer.services.ocd.graphs.Community; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* The class corresponding to community events in the community life cycle. | ||
*/ | ||
public class CommunityEvent { | ||
public static final String clcKeyColumnName = "CLC_KEY"; | ||
public static final String collectionName = "communityEvent"; | ||
public static final String dateColumnName = "date"; | ||
public static final String eventTypeColumnName = "eventType"; | ||
public static final String communitiesColumnName = "communitiesInvolved"; | ||
public static final String nodesColumnName = "nodesInvolved"; | ||
/** | ||
* System generated persistence key | ||
*/ | ||
private String key; | ||
/** | ||
* The date of the event | ||
*/ | ||
private String date; | ||
/** | ||
* The event type by id | ||
*/ | ||
private int eventType; | ||
/** | ||
* The list of communities involved in the event | ||
*/ | ||
private List<Integer> communitiesInvolved = new ArrayList<>(); | ||
/** | ||
* The list of nodes involved in the event | ||
*/ | ||
private List<String> nodesInvolved = new ArrayList<>(); | ||
/** | ||
* The community life cycle the event is part of | ||
*/ | ||
private CommunityLifeCycle clc; | ||
public CommunityEvent(CommunityLifeCycle clc, String date, List<Integer> communitiesInvolved, List<String> nodesInvolved) { | ||
this.clc = clc; | ||
this.date = date; | ||
this.communitiesInvolved = communitiesInvolved; | ||
this.nodesInvolved = nodesInvolved; | ||
} | ||
|
||
public CommunityEvent(CommunityLifeCycle clc, String date, List<Integer> communitiesInvolved) { | ||
this.clc = clc; | ||
this.date = date; | ||
this.communitiesInvolved = communitiesInvolved; | ||
} | ||
|
||
public CommunityEvent() { | ||
} | ||
|
||
public int getEventType() { | ||
return eventType; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
public int getDateAsInt() { | ||
return Integer.parseInt(date); | ||
} | ||
|
||
public List<Integer> getCommunitiesInvolved() { | ||
return communitiesInvolved; | ||
} | ||
|
||
public List<String> getNodesInvolved() { | ||
return nodesInvolved; | ||
} | ||
|
||
/** | ||
* Adds an event type to the event as the id of the type. | ||
* @param type | ||
*/ | ||
public void addEventType(CommunityEventType type){ | ||
this.eventType = type.getId(); | ||
} | ||
|
||
public void persist(ArangoDatabase db, DocumentCreateOptions opt){ | ||
ArangoCollection collection = db.collection(collectionName); | ||
BaseDocument bd = new BaseDocument(); | ||
bd.addAttribute(dateColumnName, this.date); | ||
bd.addAttribute(eventTypeColumnName, this.eventType); | ||
bd.addAttribute(communitiesColumnName, this.communitiesInvolved); | ||
bd.addAttribute(nodesColumnName, this.nodesInvolved); | ||
bd.addAttribute(clcKeyColumnName, this.clc.getKey()); | ||
collection.insertDocument(bd, opt); | ||
this.key = bd.getKey(); | ||
} | ||
|
||
public static CommunityEvent load(String key, CommunityLifeCycle clc, ArangoDatabase db, DocumentReadOptions opt){ | ||
CommunityEvent ce = new CommunityEvent(); | ||
|
||
ArangoCollection collection = db.collection(collectionName); | ||
|
||
BaseDocument bd = collection.getDocument(key, BaseDocument.class, opt); | ||
if(bd != null){ | ||
ObjectMapper om = new ObjectMapper(); | ||
String eventTypeString = bd.getAttribute(eventTypeColumnName).toString(); | ||
Object objCommunities = bd.getAttribute(communitiesColumnName); | ||
Object objNodes = bd.getAttribute(nodesColumnName); | ||
|
||
ce.key = key; | ||
ce.clc = clc; | ||
ce.date = bd.getAttribute(dateColumnName).toString(); | ||
ce.eventType = Integer.parseInt(eventTypeString); | ||
ce.communitiesInvolved = om.convertValue(objCommunities, List.class); | ||
ce.nodesInvolved = om.convertValue(objNodes, List.class); | ||
}else{ | ||
System.out.println("empty event document"); | ||
} | ||
return ce; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CommunityEvent{" + | ||
"date='" + date + '\'' + | ||
", eventType=" + CommunityEventType.lookupType(eventType) + | ||
", communitiesInvolved=" + communitiesInvolved + | ||
", nodesInvolved=" + nodesInvolved + | ||
'}'; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
rest_ocd_services/src/main/java/i5/las2peer/services/ocd/utils/CommunityEventType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package i5.las2peer.services.ocd.utils; | ||
|
||
import java.util.Locale; | ||
|
||
import i5.las2peer.services.ocd.utils.EnumDisplayNames; | ||
|
||
/** | ||
* Used to indicate the type of event. | ||
*/ | ||
public enum CommunityEventType implements EnumDisplayNames { | ||
//event types, can be expanded | ||
GROWTH("Growth", 0), | ||
|
||
BIRTH("Birth", 1), | ||
|
||
CONTRACTION("Contraction", 2), | ||
|
||
DEATH("Death", 3), | ||
|
||
FUSION("Fusion", 4), | ||
|
||
SPLIT("Split", 5); | ||
|
||
|
||
/** | ||
* For persistence and other purposes. | ||
*/ | ||
private int id; | ||
|
||
/** | ||
* A display name for web frontends and more | ||
*/ | ||
private final String displayName; | ||
|
||
/** | ||
* Creates a new instance. | ||
* @param id Defines the id attribute. | ||
*/ | ||
private CommunityEventType(String displayName, int id) { | ||
this.displayName = displayName; | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Getter for the id. | ||
* @return The id. | ||
*/ | ||
public int getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Returns the display name of the type. | ||
* @return The name. | ||
*/ | ||
public String getDisplayName() { | ||
return displayName; | ||
} | ||
|
||
/** | ||
* Returns the type corresponding to an id. | ||
* @param id The id. | ||
* @return The type. | ||
*/ | ||
public static CommunityEventType lookupType(int id) { | ||
for (CommunityEventType type : CommunityEventType.values()) { | ||
if (id == type.getId()) { | ||
return type; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Returns the name of the type written in lower case letters and with any underscores replaced by space characters. | ||
*/ | ||
@Override | ||
public String toString() { | ||
String name = name(); | ||
name = name.replace('_', ' '); | ||
name = name.toLowerCase(Locale.ROOT); | ||
return name; | ||
} | ||
} |
Oops, something went wrong.