Skip to content

Commit 49f0234

Browse files
committed
Add new exception dialog
Add first name, last name Add test for parsing project informatin
1 parent dd18d38 commit 49f0234

File tree

9 files changed

+121
-44
lines changed

9 files changed

+121
-44
lines changed

src/main/java/org/jabref/gui/sharelatex/ShareLatexLoginDialogController.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import javafx.scene.control.TextField;
99

1010
import org.jabref.gui.AbstractController;
11+
import org.jabref.gui.DialogService;
1112
import org.jabref.gui.FXDialog;
13+
import org.jabref.gui.FXDialogService;
1214
import org.jabref.logic.sharelatex.ShareLatexManager;
1315

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

3133
@FXML
3234
private void signIn() {
33-
System.out.println("sign in pressed");
34-
System.out.println(tbAddress.getText());
35-
System.out.println(tbUsername.getText());
36-
System.out.println(pfPassword.getText());
3735

38-
String result;
3936
try {
40-
result = manager.login(tbAddress.getText(), tbUsername.getText(), pfPassword.getText());
37+
String result = manager.login(tbAddress.getText(), tbUsername.getText(), pfPassword.getText());
4138
if (result.contains("incorrect")) {
4239
FXDialog dlg = new FXDialog(AlertType.ERROR);
4340
dlg.setContentText("Your email or password is incorrect. Please try again");
@@ -48,9 +45,8 @@ private void signIn() {
4845
closeDialog();
4946
}
5047
} catch (Exception e) {
51-
FXDialog dlg = new FXDialog(AlertType.ERROR);
52-
dlg.setContentText(e.getMessage());
53-
dlg.showAndWait();
48+
DialogService dlg = new FXDialogService();
49+
dlg.showErrorDialogAndWait(e);
5450

5551
}
5652

src/main/java/org/jabref/gui/sharelatex/ShareLatexProjectDialogController.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class ShareLatexProjectDialogController extends AbstractController<ShareL
2525

2626
@FXML private TableColumn<ShareLatexProjectViewModel, Boolean> colActive;
2727
@FXML private TableColumn<ShareLatexProjectViewModel, String> colTitle;
28-
@FXML private TableColumn<ShareLatexProjectViewModel, String> colOwner;
28+
@FXML private TableColumn<ShareLatexProjectViewModel, String> colFirstName;
29+
@FXML private TableColumn<ShareLatexProjectViewModel, String> colLastName;
2930
@FXML private TableColumn<ShareLatexProjectViewModel, String> colLastModified;
3031
@FXML private TableView<ShareLatexProjectViewModel> tblProjects;
3132
@Inject private ShareLatexManager manager;
@@ -48,7 +49,8 @@ private void initialize() {
4849

4950
colActive.setCellValueFactory(cellData -> cellData.getValue().isActiveProperty());
5051
colTitle.setCellValueFactory(cellData -> cellData.getValue().getProjectTitle());
51-
colOwner.setCellValueFactory(cellData -> cellData.getValue().getOwner());
52+
colFirstName.setCellValueFactory(cellData -> cellData.getValue().getFirstName());
53+
colLastName.setCellValueFactory(cellData -> cellData.getValue().getLastName());
5254
colLastModified.setCellValueFactory(cellData -> cellData.getValue().getLastUpdated());
5355
setBindings();
5456

src/main/java/org/jabref/gui/sharelatex/ShareLatexProjectViewModel.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public class ShareLatexProjectViewModel {
1717
private final SimpleBooleanProperty active = new SimpleBooleanProperty(false);
1818
private final String projectId;
1919
private final StringProperty projectTitle;
20-
private final StringProperty owner;
20+
private final StringProperty firstName;
21+
private final StringProperty lastName;
2122
private final StringProperty lastUpdated;
2223

2324
public ShareLatexProjectViewModel(ShareLatexProject project) {
2425
this.projectId = project.getProjectId();
2526
this.projectTitle = new SimpleStringProperty(project.getProjectTitle());
26-
this.owner = new SimpleStringProperty(project.getOwner());
27+
this.firstName = new SimpleStringProperty(project.getFirstName());
28+
this.lastName = new SimpleStringProperty(project.getLastName());
2729
this.lastUpdated = new SimpleStringProperty(project.getLastUpdated());
2830
}
2931

@@ -35,8 +37,12 @@ public StringProperty getProjectTitle() {
3537
return projectTitle;
3638
}
3739

38-
public StringProperty getOwner() {
39-
return owner;
40+
public StringProperty getFirstName() {
41+
return firstName;
42+
}
43+
44+
public StringProperty getLastName() {
45+
return lastName;
4046
}
4147

4248
public StringProperty getLastUpdated() {

src/main/java/org/jabref/logic/sharelatex/ShareLatexManager.java

+6-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.io.IOException;
44
import java.net.URISyntaxException;
55
import java.nio.charset.StandardCharsets;
6-
import java.util.ArrayList;
6+
import java.util.Collections;
77
import java.util.List;
88

99
import org.jabref.JabRefExecutorService;
@@ -15,8 +15,6 @@
1515
import org.jabref.model.database.BibDatabaseContext;
1616
import org.jabref.model.sharelatex.ShareLatexProject;
1717

18-
import com.google.gson.JsonArray;
19-
import com.google.gson.JsonElement;
2018
import org.apache.commons.logging.Log;
2119
import org.apache.commons.logging.LogFactory;
2220

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

2725
private final SharelatexConnector connector = new SharelatexConnector();
28-
private final List<ShareLatexProject> projects = new ArrayList<>();
26+
private final ShareLatexParser parser = new ShareLatexParser();
2927

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

3432
public List<ShareLatexProject> getProjects() throws IOException {
35-
connector.getProjects().ifPresent(jsonResponse -> {
36-
if (jsonResponse.has("projects")) {
37-
JsonArray projectArray = jsonResponse.get("projects").getAsJsonArray();
38-
System.out.println(projectArray);
39-
for (JsonElement elem : projectArray) {
40-
41-
String id = elem.getAsJsonObject().get("id").getAsString();
42-
String name = elem.getAsJsonObject().get("name").getAsString();
43-
String lastUpdated = elem.getAsJsonObject().get("lastUpdated").getAsString();
44-
String owner = elem.getAsJsonObject().get("owner_ref").getAsString();
45-
46-
ShareLatexProject project = new ShareLatexProject(id, name, owner, lastUpdated);
47-
projects.add(project);
48-
}
49-
}
50-
});
51-
return projects;
33+
if (connector.getProjects().isPresent()) {
34+
return parser.getProjectFromJson(connector.getProjects().get());
35+
}
36+
return Collections.emptyList();
5237
}
5338

5439
public void startWebSocketHandler(String projectID, BibDatabaseContext database, ImportFormatPreferences preferences) {

src/main/java/org/jabref/logic/sharelatex/ShareLatexParser.java

+25
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.jabref.logic.importer.ParseException;
1212
import org.jabref.logic.importer.fileformat.BibtexParser;
1313
import org.jabref.model.entry.BibEntry;
14+
import org.jabref.model.sharelatex.ShareLatexProject;
1415

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

105106
}
106107

108+
public List<ShareLatexProject> getProjectFromJson(JsonObject json) {
109+
110+
List<ShareLatexProject> projects = new ArrayList<>();
111+
if (json.has("projects")) {
112+
JsonArray projectArray = json.get("projects").getAsJsonArray();
113+
for (JsonElement elem : projectArray) {
114+
115+
String id = elem.getAsJsonObject().get("id").getAsString();
116+
String name = elem.getAsJsonObject().get("name").getAsString();
117+
String lastUpdated = elem.getAsJsonObject().get("lastUpdated").getAsString();
118+
//String owner = elem.getAsJsonObject().get("owner_ref").getAsString();
119+
120+
JsonObject owner = elem.getAsJsonObject().get("owner").getAsJsonObject();
121+
String firstName = owner.get("first_name").getAsString();
122+
String lastName = owner.get("last_name").getAsString();
123+
124+
ShareLatexProject project = new ShareLatexProject(id, name, firstName, lastName, lastUpdated);
125+
projects.add(project);
126+
}
127+
}
128+
return projects;
129+
130+
}
131+
107132
private List<BibEntry> parseBibEntryFromJsonArray(JsonArray arr, ImportFormatPreferences prefs)
108133
throws ParseException {
109134

src/main/java/org/jabref/logic/sharelatex/SharelatexConnector.java

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

98-
System.out.println("Project Response cookies " + projectsResponse.cookies());
99-
10098
Optional<Element> scriptContent = Optional
10199
.of(projectsResponse.parse().select("script#data").first());
102100

src/main/java/org/jabref/model/sharelatex/ShareLatexProject.java

+44-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package org.jabref.model.sharelatex;
22

3+
import java.util.Objects;
4+
35
public class ShareLatexProject {
46

57
private final String projectId;
68
private final String projectTitle;
7-
private final String owner;
9+
private final String firstName;
10+
private final String lastName;
811
private final String lastUpdated;
912

10-
public ShareLatexProject(String projectId, String projectTitle, String owner, String lastUpdated) {
13+
public ShareLatexProject(String projectId, String projectTitle, String firstName, String lastName, String lastUpdated) {
1114
this.projectId = projectId;
1215
this.projectTitle = projectTitle;
1316
this.lastUpdated = lastUpdated;
14-
this.owner = owner;
17+
this.firstName = firstName;
18+
this.lastName = lastName;
1519
}
1620

1721
public String getLastUpdated() {
@@ -26,7 +30,42 @@ public String getProjectTitle() {
2630
return projectTitle;
2731
}
2832

29-
public String getOwner() {
30-
return owner;
33+
public String getFirstName() {
34+
return firstName;
35+
}
36+
37+
public String getLastName() {
38+
return lastName;
3139
}
40+
41+
@Override
42+
public String toString() {
43+
return "[projectId =" + projectId + ", "
44+
+ "projectTitle = " + projectTitle + ", "
45+
+ "firstName = " + firstName + ", "
46+
+ "lastName = " + lastName + ", "
47+
+ "lastUpdated = " + lastUpdated + "";
48+
}
49+
50+
@Override
51+
public boolean equals(Object obj) {
52+
if (this == obj) {
53+
return true;
54+
}
55+
if (obj == null) {
56+
return false;
57+
}
58+
if (getClass() != obj.getClass()) {
59+
return false;
60+
}
61+
ShareLatexProject other = (ShareLatexProject) obj;
62+
63+
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);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(projectId, projectTitle, lastUpdated, lastName, firstName);
69+
}
70+
3271
}

src/main/resources/org/jabref/gui/sharelatex/ShareLatexProjectDialog.fxml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<columns>
1616
<TableColumn fx:id="colActive" prefWidth="75.0" text="Active" />
1717
<TableColumn fx:id="colTitle" prefWidth="75.0" text="Title" />
18-
<TableColumn fx:id="colOwner" prefWidth="75.0" text="Owner" />
18+
<TableColumn fx:id="colFirstName" prefWidth="75.0" text="First Name" />
19+
<TableColumn fx:id="colLastName" prefWidth="75.0" text="Last Name" />
1920
<TableColumn fx:id="colLastModified" prefWidth="75.0" text="Last modified" />
2021
</columns>
2122
</TableView>

src/test/java/org/jabref/logic/sharelatex/ShareLatexParserTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
6+
import org.jabref.model.sharelatex.ShareLatexProject;
7+
8+
import com.google.gson.JsonElement;
9+
import com.google.gson.JsonObject;
10+
import com.google.gson.JsonParser;
511
import org.junit.Test;
612

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

1117
private final ShareLatexParser parser = new ShareLatexParser();
1218

19+
@Test
20+
public void testGetSharelatexProjects() {
21+
JsonParser jsonParser = new JsonParser();
22+
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\":[]}";
23+
JsonElement jsonTree = jsonParser.parse(jsonString);
24+
JsonObject obj = jsonTree.getAsJsonObject();
25+
26+
List<ShareLatexProject> actual = parser.getProjectFromJson(obj);
27+
28+
List<ShareLatexProject> expected = new ArrayList<>();
29+
ShareLatexProject project = new ShareLatexProject("58df8f5e27aa5281020536ea", "PLoS one", "cschwentker", "", "2017-06-07T08:42:14.632Z");
30+
expected.add(project);
31+
project = new ShareLatexProject("5950e47621d8ee3e76616374", "Example", "cschwentker", "", "2017-07-16T09:49:16.241Z");
32+
expected.add(project);
33+
34+
assertEquals(expected, actual);
35+
36+
}
37+
1338
@Test
1439
public void testFixWrongUTF8IsoEncoded() {
1540
String wrongEncoded = "asdf_Wrocławskiej";

0 commit comments

Comments
 (0)