Skip to content

Commit

Permalink
Implementing backend support for issues #3 and #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jlcanovas committed Jul 11, 2014
1 parent 2455df6 commit 9e1c6d9
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 297 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package fr.inria.atlanmod.collaboro.backend;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;

import fr.inria.atlanmod.collaboro.history.Collaboration;
import fr.inria.atlanmod.collaboro.history.Comment;
import fr.inria.atlanmod.collaboro.history.History;
import fr.inria.atlanmod.collaboro.history.HistoryFactory;
import fr.inria.atlanmod.collaboro.history.Proposal;
import fr.inria.atlanmod.collaboro.history.Solution;
import fr.inria.atlanmod.collaboro.history.User;
import fr.inria.atlanmod.collaboro.history.Version;
import fr.inria.atlanmod.collaboro.history.VersionHistory;
import fr.inria.atlanmod.collaboro.notation.Definition;

/**
Expand All @@ -22,27 +29,27 @@
public class CollaboroBackend {
private static CollaboroBackend instance;
public static String PATH_TO_HISTORY = "C:\\Users\\useradm\\git\\collaboro\\plugins\\fr.inria.atlanmod.collaboro.web.servlets\\WebContent\\WEB-INF\\model\\ModiscoWorkflow.ecore";

// Variables to control the state of the collaboration
private int historyTracked = 0;
private int versionTracked = 0;
private int lastIndex = 0;

ModelManagerFactory modelManagerFactory = new ModelManagerFactory();
ModelManager modelManager = modelManagerFactory.createEmptyModelManager();

private CollaboroBackend() {
File historyFile = new File(PATH_TO_HISTORY);
loadHistory(historyFile);
}

public static CollaboroBackend getInstance() {
if(instance == null) {
instance = new CollaboroBackend();
}
return instance;
}

/**
* Sets the user logged in the application.
*
Expand All @@ -52,7 +59,7 @@ public static CollaboroBackend getInstance() {
public User loginUser(String email, String password, String dsl) {
if(email == null || password == null || dsl == null)
throw new IllegalArgumentException("Parameters cannot be null");

User found = null;

if(getHistory() != null)
Expand All @@ -62,7 +69,7 @@ public User loginUser(String email, String password, String dsl) {

return found;
}

/**
* Loads a new History model
*
Expand All @@ -75,7 +82,7 @@ public void loadHistory(Object resource) {
if(getHistory() != null)
versionTracked = getHistory().getHistories().get(historyTracked).getVersions().size() - 1;
}

/**
* Testing method to reset the information
*/
Expand All @@ -84,7 +91,7 @@ public void reset() {
historyTracked = 0;
versionTracked = 0;
}

private void calculateLastIndexProposal() {
lastIndex = 0;

Expand Down Expand Up @@ -115,7 +122,7 @@ private void calculateLastIndexProposal() {
}
}
}


public History getHistory() {
return modelManager.getHistory();
Expand All @@ -128,18 +135,114 @@ public EPackage getEcoreModel() {
public Definition getNotation() {
return modelManager.getNotation();
}

public ModelManager getModelManager() {
return modelManager;
}

public int getHistoryTracked() {
return historyTracked;
}

public int getVersionTracked() {
return versionTracked;
}

public void createProposalPlain(String userId, String rationale) {
Proposal newProposal = HistoryFactory.eINSTANCE.createProposal();

// Locating the user
User userProposing = null;
if(getHistory() != null)
for (User user : getHistory().getUsers())
if(user.getId() != null && user.getId().equals(userId))
userProposing = user;

if(userProposing != null) {
newProposal.setProposedBy(userProposing);
newProposal.setRationale(rationale);
createProposal(newProposal);
}
}

public void createProposal(Proposal newProposal) {
newProposal.setId("n" + ++lastIndex);
getProposals().add(newProposal);
Version version = getHistory().getHistories().get(getHistoryTracked()).getVersions().get(getVersionTracked());
version.getProposals().add(newProposal);
modelManager.saveHistory();
modelManager.saveNotation();
System.out.println("Se guardo!");
}

public List<Proposal> getProposals() {
List<Proposal> result = new ArrayList<Proposal>();

if(getHistory() != null && getHistory().getHistories() != null) {
VersionHistory versionHistory = getHistory().getHistories().get(getHistoryTracked());
if(versionHistory != null && getHistory().getHistories().get(getHistoryTracked()).getVersions() != null) {
Version version = getHistory().getHistories().get(getHistoryTracked()).getVersions().get(getVersionTracked());
if(version != null) {
result = getHistory().getHistories().get(getHistoryTracked()).getVersions().get(getVersionTracked()).getProposals();
}
}
}
return result;
}

private Collaboration initCollaborationPlain(Collaboration collaboration, String userId, String rationale) {
// Locating the user
User userProposing = null;
if(getHistory() != null)
for (User user : getHistory().getUsers())
if(user.getId() != null && user.getId().equals(userId))
userProposing = user;

if(userProposing != null) {
collaboration.setProposedBy(userProposing);
collaboration.setRationale(rationale);
}
return collaboration;
}

private Collaboration locateCollaborationById(Collaboration collaboration, String id) {
if(collaboration == null ) {
for(Proposal proposal : getProposals())
return locateCollaborationById(proposal, id);
} else {
if(collaboration.getId().equals(id))
return collaboration;
else {
if(collaboration instanceof Proposal)
for(Solution solution : ((Proposal) collaboration).getSols())
return locateCollaborationById(solution, id);
for(Comment comment : collaboration.getComments())
return locateCollaborationById(comment, id);
}
}
return null;
}

public void createCommentPlain(String parentCollaboration, String userId, String rationale) {
Comment newComment = HistoryFactory.eINSTANCE.createComment();
initCollaborationPlain(newComment, userId, rationale);

Collaboration parent = locateCollaborationById(null, parentCollaboration);
if(parent != null)
createComment(parent, newComment);
}

public void createComment(Collaboration collaboration, Comment comment) {
Comment newComment = HistoryFactory.eINSTANCE.createComment();
newComment.setId("n" + ++lastIndex);
collaboration.getComments().add(newComment);
modelManager.saveHistory();
modelManager.saveNotation();
}



public void saveHistory() {
modelManager.saveHistory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@
*
*/
public class JsonCollaborationSimplified {

private String proposedBy;

private String rationale;

private String type;

private String parent_id;

public JsonCollaborationSimplified()
{

}

public String getProposedBy() {
return proposedBy;
}
Expand Down Expand Up @@ -58,7 +49,4 @@ public String getParent_id() {
public void setType(String type) {
this.type = type;
}



}
Loading

0 comments on commit 9e1c6d9

Please sign in to comment.