Skip to content

Commit

Permalink
Add new exception dialog
Browse files Browse the repository at this point in the history
Add first name, last name
Add test for parsing project informatin
  • Loading branch information
Siedlerchr committed Jul 16, 2017
1 parent dd18d38 commit 49f0234
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import javafx.scene.control.TextField;

import org.jabref.gui.AbstractController;
import org.jabref.gui.DialogService;
import org.jabref.gui.FXDialog;
import org.jabref.gui.FXDialogService;
import org.jabref.logic.sharelatex.ShareLatexManager;

public class ShareLatexLoginDialogController extends AbstractController<ShareLatexLoginDialogViewModel> {
Expand All @@ -30,14 +32,9 @@ private void closeDialog() {

@FXML
private void signIn() {
System.out.println("sign in pressed");
System.out.println(tbAddress.getText());
System.out.println(tbUsername.getText());
System.out.println(pfPassword.getText());

String result;
try {
result = manager.login(tbAddress.getText(), tbUsername.getText(), pfPassword.getText());
String result = manager.login(tbAddress.getText(), tbUsername.getText(), pfPassword.getText());
if (result.contains("incorrect")) {
FXDialog dlg = new FXDialog(AlertType.ERROR);
dlg.setContentText("Your email or password is incorrect. Please try again");
Expand All @@ -48,9 +45,8 @@ private void signIn() {
closeDialog();
}
} catch (Exception e) {
FXDialog dlg = new FXDialog(AlertType.ERROR);
dlg.setContentText(e.getMessage());
dlg.showAndWait();
DialogService dlg = new FXDialogService();
dlg.showErrorDialogAndWait(e);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class ShareLatexProjectDialogController extends AbstractController<ShareL

@FXML private TableColumn<ShareLatexProjectViewModel, Boolean> colActive;
@FXML private TableColumn<ShareLatexProjectViewModel, String> colTitle;
@FXML private TableColumn<ShareLatexProjectViewModel, String> colOwner;
@FXML private TableColumn<ShareLatexProjectViewModel, String> colFirstName;
@FXML private TableColumn<ShareLatexProjectViewModel, String> colLastName;
@FXML private TableColumn<ShareLatexProjectViewModel, String> colLastModified;
@FXML private TableView<ShareLatexProjectViewModel> tblProjects;
@Inject private ShareLatexManager manager;
Expand All @@ -48,7 +49,8 @@ private void initialize() {

colActive.setCellValueFactory(cellData -> cellData.getValue().isActiveProperty());
colTitle.setCellValueFactory(cellData -> cellData.getValue().getProjectTitle());
colOwner.setCellValueFactory(cellData -> cellData.getValue().getOwner());
colFirstName.setCellValueFactory(cellData -> cellData.getValue().getFirstName());
colLastName.setCellValueFactory(cellData -> cellData.getValue().getLastName());
colLastModified.setCellValueFactory(cellData -> cellData.getValue().getLastUpdated());
setBindings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public class ShareLatexProjectViewModel {
private final SimpleBooleanProperty active = new SimpleBooleanProperty(false);
private final String projectId;
private final StringProperty projectTitle;
private final StringProperty owner;
private final StringProperty firstName;
private final StringProperty lastName;
private final StringProperty lastUpdated;

public ShareLatexProjectViewModel(ShareLatexProject project) {
this.projectId = project.getProjectId();
this.projectTitle = new SimpleStringProperty(project.getProjectTitle());
this.owner = new SimpleStringProperty(project.getOwner());
this.firstName = new SimpleStringProperty(project.getFirstName());
this.lastName = new SimpleStringProperty(project.getLastName());
this.lastUpdated = new SimpleStringProperty(project.getLastUpdated());
}

Expand All @@ -35,8 +37,12 @@ public StringProperty getProjectTitle() {
return projectTitle;
}

public StringProperty getOwner() {
return owner;
public StringProperty getFirstName() {
return firstName;
}

public StringProperty getLastName() {
return lastName;
}

public StringProperty getLastUpdated() {
Expand Down
27 changes: 6 additions & 21 deletions src/main/java/org/jabref/logic/sharelatex/ShareLatexManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jabref.JabRefExecutorService;
Expand All @@ -15,8 +15,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.sharelatex.ShareLatexProject;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -25,30 +23,17 @@ public class ShareLatexManager {
private static final Log LOGGER = LogFactory.getLog(ShareLatexManager.class);

private final SharelatexConnector connector = new SharelatexConnector();
private final List<ShareLatexProject> projects = new ArrayList<>();
private final ShareLatexParser parser = new ShareLatexParser();

public String login(String server, String username, String password) throws IOException {
return connector.connectToServer(server, username, password);
}

public List<ShareLatexProject> getProjects() throws IOException {
connector.getProjects().ifPresent(jsonResponse -> {
if (jsonResponse.has("projects")) {
JsonArray projectArray = jsonResponse.get("projects").getAsJsonArray();
System.out.println(projectArray);
for (JsonElement elem : projectArray) {

String id = elem.getAsJsonObject().get("id").getAsString();
String name = elem.getAsJsonObject().get("name").getAsString();
String lastUpdated = elem.getAsJsonObject().get("lastUpdated").getAsString();
String owner = elem.getAsJsonObject().get("owner_ref").getAsString();

ShareLatexProject project = new ShareLatexProject(id, name, owner, lastUpdated);
projects.add(project);
}
}
});
return projects;
if (connector.getProjects().isPresent()) {
return parser.getProjectFromJson(connector.getProjects().get());
}
return Collections.emptyList();
}

public void startWebSocketHandler(String projectID, BibDatabaseContext database, ImportFormatPreferences preferences) {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/jabref/logic/sharelatex/ShareLatexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.sharelatex.ShareLatexProject;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -104,6 +105,30 @@ public List<SharelatexDoc> generateDiffs(String before, String after) {

}

public List<ShareLatexProject> getProjectFromJson(JsonObject json) {

List<ShareLatexProject> projects = new ArrayList<>();
if (json.has("projects")) {
JsonArray projectArray = json.get("projects").getAsJsonArray();
for (JsonElement elem : projectArray) {

String id = elem.getAsJsonObject().get("id").getAsString();
String name = elem.getAsJsonObject().get("name").getAsString();
String lastUpdated = elem.getAsJsonObject().get("lastUpdated").getAsString();
//String owner = elem.getAsJsonObject().get("owner_ref").getAsString();

JsonObject owner = elem.getAsJsonObject().get("owner").getAsJsonObject();
String firstName = owner.get("first_name").getAsString();
String lastName = owner.get("last_name").getAsString();

ShareLatexProject project = new ShareLatexProject(id, name, firstName, lastName, lastUpdated);
projects.add(project);
}
}
return projects;

}

private List<BibEntry> parseBibEntryFromJsonArray(JsonArray arr, ImportFormatPreferences prefs)
throws ParseException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ public Optional<JsonObject> getProjects() throws IOException {
Connection.Response projectsResponse = Jsoup.connect(projectUrl)
.referrer(loginUrl).cookies(loginCookies).method(Method.GET).userAgent(userAgent).execute();

System.out.println("Project Response cookies " + projectsResponse.cookies());

Optional<Element> scriptContent = Optional
.of(projectsResponse.parse().select("script#data").first());

Expand Down
49 changes: 44 additions & 5 deletions src/main/java/org/jabref/model/sharelatex/ShareLatexProject.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package org.jabref.model.sharelatex;

import java.util.Objects;

public class ShareLatexProject {

private final String projectId;
private final String projectTitle;
private final String owner;
private final String firstName;
private final String lastName;
private final String lastUpdated;

public ShareLatexProject(String projectId, String projectTitle, String owner, String lastUpdated) {
public ShareLatexProject(String projectId, String projectTitle, String firstName, String lastName, String lastUpdated) {
this.projectId = projectId;
this.projectTitle = projectTitle;
this.lastUpdated = lastUpdated;
this.owner = owner;
this.firstName = firstName;
this.lastName = lastName;
}

public String getLastUpdated() {
Expand All @@ -26,7 +30,42 @@ public String getProjectTitle() {
return projectTitle;
}

public String getOwner() {
return owner;
public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

@Override
public String toString() {
return "[projectId =" + projectId + ", "
+ "projectTitle = " + projectTitle + ", "
+ "firstName = " + firstName + ", "
+ "lastName = " + lastName + ", "
+ "lastUpdated = " + lastUpdated + "";
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ShareLatexProject other = (ShareLatexProject) obj;

return Objects.equals(projectId, other.projectId) && Objects.equals(projectTitle, other.projectTitle) && Objects.equals(lastUpdated, other.lastUpdated) && Objects.equals(lastName, other.lastName) && Objects.equals(firstName, other.firstName);
}

@Override
public int hashCode() {
return Objects.hash(projectId, projectTitle, lastUpdated, lastName, firstName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<columns>
<TableColumn fx:id="colActive" prefWidth="75.0" text="Active" />
<TableColumn fx:id="colTitle" prefWidth="75.0" text="Title" />
<TableColumn fx:id="colOwner" prefWidth="75.0" text="Owner" />
<TableColumn fx:id="colFirstName" prefWidth="75.0" text="First Name" />
<TableColumn fx:id="colLastName" prefWidth="75.0" text="Last Name" />
<TableColumn fx:id="colLastModified" prefWidth="75.0" text="Last modified" />
</columns>
</TableView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import java.util.ArrayList;
import java.util.List;

import org.jabref.model.sharelatex.ShareLatexProject;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -10,6 +16,25 @@ public class ShareLatexParserTest {

private final ShareLatexParser parser = new ShareLatexParser();

@Test
public void testGetSharelatexProjects() {
JsonParser jsonParser = new JsonParser();
String jsonString = "{\"projects\":[{\"id\":\"58df8f5e27aa5281020536ea\",\"name\":\"PLoS one\",\"lastUpdated\":\"2017-06-07T08:42:14.632Z\",\"publicAccessLevel\":\"private\",\"accessLevel\":\"owner\",\"archived\":false,\"owner_ref\":\"58df8f3627aa5281020536e1\",\"owner\":{\"_id\":\"58df8f3627aa5281020536e1\",\"last_name\":\"\",\"first_name\":\"cschwentker\"}},{\"id\":\"5950e47621d8ee3e76616374\",\"name\":\"Example\",\"lastUpdated\":\"2017-07-16T09:49:16.241Z\",\"publicAccessLevel\":\"private\",\"accessLevel\":\"owner\",\"archived\":false,\"owner_ref\":\"58df8f3627aa5281020536e1\",\"owner\":{\"_id\":\"58df8f3627aa5281020536e1\",\"last_name\":\"\",\"first_name\":\"cschwentker\"}}],\"tags\":[{\"_id\":\"59353074a47d9c0eb124ed11\",\"user_id\":\"58df8f3627aa5281020536e1\",\"name\":\"TestFolder\",\"project_ids\":[]}],\"notifications\":[]}";
JsonElement jsonTree = jsonParser.parse(jsonString);
JsonObject obj = jsonTree.getAsJsonObject();

List<ShareLatexProject> actual = parser.getProjectFromJson(obj);

List<ShareLatexProject> expected = new ArrayList<>();
ShareLatexProject project = new ShareLatexProject("58df8f5e27aa5281020536ea", "PLoS one", "cschwentker", "", "2017-06-07T08:42:14.632Z");
expected.add(project);
project = new ShareLatexProject("5950e47621d8ee3e76616374", "Example", "cschwentker", "", "2017-07-16T09:49:16.241Z");
expected.add(project);

assertEquals(expected, actual);

}

@Test
public void testFixWrongUTF8IsoEncoded() {
String wrongEncoded = "asdf_Wrocławskiej";
Expand Down

0 comments on commit 49f0234

Please sign in to comment.