From dda8edccf3d889b9a6bbf99d9caf8436e173ff89 Mon Sep 17 00:00:00 2001 From: Oleg Dudar Date: Tue, 16 Jan 2018 20:37:06 +0200 Subject: [PATCH 1/3] refactoring. read credentials from credentials.config file. store all data in HashMap --- pom.xml | 58 ++++++++ src/main/java/Main.java | 147 +++++++++++++++++++- src/main/java/TicketLoggedWork.java | 3 - src/main/java/com/dudar/ConnectionData.java | 25 ++++ src/main/java/com/dudar/JIRA_Accessor.java | 101 ++++++++++++++ src/main/java/com/dudar/Record.java | 38 +++++ 6 files changed, 362 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/dudar/ConnectionData.java create mode 100644 src/main/java/com/dudar/JIRA_Accessor.java create mode 100644 src/main/java/com/dudar/Record.java diff --git a/pom.xml b/pom.xml index a151e03..19da3b6 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,21 @@ JIRATimeParser 1.0-SNAPSHOT + + + atlassian-public + https://maven.atlassian.com/repository/public + + true + never + warn + + + true + warn + + + @@ -23,6 +38,49 @@ javacsv 2.0 + + + com.atlassian.jira + jira-rest-java-client-api + 4.0.0 + + + + com.atlassian.jira + jira-rest-java-client-core + 4.0.0 + + + com.atlassian.httpclient + atlassian-httpclient-apache-httpcomponents + 0.11.0 + + + com.atlassian.fugue + fugue + 2.2.1 + + + + com.sun.jersey + jersey-client + 1.8 + + + + org.json + json + 20090211 + + + + com.atlassian.jira + jira-api + 7.0.0 + provided + + + diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a003f9a..87cf389 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,17 +2,152 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Scanner; +import java.util.*; +import com.atlassian.jira.rest.client.api.domain.Issue; import com.csvreader.CsvReader; +import com.dudar.ConnectionData; +import com.dudar.JIRA_Accessor; -public class Main { +import javax.security.sasl.AuthenticationException; + +import javafx.application.Application; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.StackPane; +import javafx.scene.text.Font; +import javafx.scene.text.FontWeight; +import javafx.scene.text.Text; +import javafx.stage.Stage; + +import static javafx.application.Application.launch; + +public class Main extends Application{ public static void main (String[] args) { + launch(args); + +// JIRA_Accessor accessor = new JIRA_Accessor(); +// +// try { +// accessor.testResponse(PropertiesReader.readPropertiesToCredentiasData()); +// } catch (AuthenticationException e) { +// e.printStackTrace(); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } + +// +// doTheJob(args); + + + } + + @Override + public void start(Stage primaryStage) { + primaryStage.setTitle("JIRA Worklog Time Parser"); + + String url = ""; + String user = ""; + String pass = ""; + + Properties prop = new Properties(); + try { + //load a properties file from class path, inside static method + prop.load(Main.class.getClassLoader().getResourceAsStream("credentials.properties")); + + //get the property value and print it out + url = prop.getProperty("url"); + user = prop.getProperty("user_name"); + pass = prop.getProperty("user_pass"); + + } + catch (IOException ex) { + ex.printStackTrace(); + } + + + + GridPane grid = new GridPane(); + grid.setAlignment(Pos.CENTER); + grid.setHgap(10); + grid.setVgap(10); + grid.setPadding(new Insets(25, 25, 25, 25)); + + Text scenetitle = new Text("Welcome"); + scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); + grid.add(scenetitle, 0, 0, 2, 1); + + Label jiraUrl = new Label("JIRA URL:"); + grid.add(jiraUrl, 0, 1); + + TextField urlTextField = new TextField(); + urlTextField.setText(url); + grid.add(urlTextField, 1, 1); + + Label userName = new Label("User Name:"); + grid.add(userName, 0, 2); + + TextField userTextField = new TextField(); + userTextField.setText(user); + grid.add(userTextField, 1, 2); + + Label pw = new Label("Password:"); + grid.add(pw, 0, 3); + + PasswordField pwBox = new PasswordField(); + pwBox.setText(pass); + grid.add(pwBox, 1, 3); + + Label usersLog = new Label("Name of users (separateed by comma):"); + grid.add(usersLog, 0, 4); + + TextField usersTextField = new TextField(); + usersTextField.setText("Name of users (separateed by comma)"); + grid.add(usersTextField, 1, 4); + + Button btn = new Button("Sign in"); + HBox hbBtn = new HBox(10); + hbBtn.setAlignment(Pos.BOTTOM_RIGHT); + hbBtn.getChildren().add(btn); + grid.add(hbBtn, 1, 5); + btn.setOnAction(new EventHandler() { + + @Override + public void handle(ActionEvent event) { + System.out.println("Start"); + + JIRA_Accessor accessor = new JIRA_Accessor(); + + try { + accessor.testResponse(new ConnectionData(urlTextField.getText(), userTextField.getText(), pwBox.getText()), usersTextField.getText()); + + } catch (AuthenticationException e) { + e.printStackTrace(); + } + + System.out.println("Finish"); +// calculation(); + } + }); + +// StackPane root = new StackPane(); +// root.getChildren().add(btn); + primaryStage.setScene(new Scene(grid, 300, 250)); + primaryStage.show(); + } + + private static void doTheJob(String[] args) { String pathToFile; String pathToFileCreate; String reportMonth; @@ -136,8 +271,6 @@ public static void main (String[] args) { pw.close(); System.out.println("Transformation completed!"); } - - } } diff --git a/src/main/java/TicketLoggedWork.java b/src/main/java/TicketLoggedWork.java index b27441e..dfea0e1 100644 --- a/src/main/java/TicketLoggedWork.java +++ b/src/main/java/TicketLoggedWork.java @@ -3,9 +3,6 @@ import java.util.List; import java.util.Map; -/** - * Created by Oleg_Dudar on 21-Nov-17. - */ public class TicketLoggedWork { String number; String summary; diff --git a/src/main/java/com/dudar/ConnectionData.java b/src/main/java/com/dudar/ConnectionData.java new file mode 100644 index 0000000..c754464 --- /dev/null +++ b/src/main/java/com/dudar/ConnectionData.java @@ -0,0 +1,25 @@ +package com.dudar; + +public class ConnectionData { + private String url; + private String user; + private String pass; + + public ConnectionData(String url, String user, String pass){ + this.url = url; + this.user = user; + this.pass = pass; + } + + public String getUrl(){ + return this.url; + } + + public String getUser(){ + return this.user; + } + + public String getPass(){ + return this.pass; + } +} diff --git a/src/main/java/com/dudar/JIRA_Accessor.java b/src/main/java/com/dudar/JIRA_Accessor.java new file mode 100644 index 0000000..515a214 --- /dev/null +++ b/src/main/java/com/dudar/JIRA_Accessor.java @@ -0,0 +1,101 @@ +package com.dudar; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.*; + +import com.atlassian.jira.rest.client.api.JiraRestClient; +import com.atlassian.jira.rest.client.api.RestClientException; +import com.atlassian.jira.rest.client.api.domain.Issue; +import com.atlassian.jira.rest.client.api.domain.SearchResult; +import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory; +import com.atlassian.util.concurrent.Promise; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.core.util.Base64; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import javax.security.sasl.AuthenticationException; + +public class JIRA_Accessor { + + public Map getWorklogIssues(ConnectionData connectionData, String names){ + final URI jiraServerUri; + Map issuesList = new HashMap<>(); + ArrayList ids = new ArrayList<>(); + try { + jiraServerUri = new URI(connectionData.getUrl()); + + final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, connectionData.getUser(), connectionData.getPass()); + Set asd = new HashSet(); + Promise searchJqlPromise = restClient.getSearchClient().searchJql("worklogAuthor in (\"" + names + "\")", 1000, 0, asd); + + //TODO handle exception RestClientException for getIssues() + for (Issue issue : searchJqlPromise.claim().getIssues()) { + issuesList.put(issue.getId(), new Record(issue.getId(), issue.getKey(), issue.getSummary(), "", 0)); + ids.add(issue.getId()); + } + return issuesList; + } catch (URISyntaxException e) { + System.out.println("URI Syntax Exception"); + } + catch (RestClientException e){ + System.out.println("Rest Client Exception"); + } + return null; + } + + public void testResponse(ConnectionData connectionData, String users) throws AuthenticationException { + + String auth = new String(Base64.encode(connectionData.getUser()+":"+connectionData.getPass())); + Client client = Client.create(); + //TODO add '' for all items inside users list + String users1 = users.replaceAll(" ", "%20"); + String buff = "/rest/api/2/search?jql=worklogAuthor%20in%20(%27"+users1+"%27)&maxResults=1000"; + String url_request = connectionData.getUrl()+buff; + + WebResource webResource = client.resource(String.valueOf(url_request)); + + ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").get(ClientResponse.class); + + int statusCode = response.getStatus(); + if (statusCode == 401) { + throw new AuthenticationException("Invalid Username or Password"); + } + Map issues = getWorklogIssues(connectionData, users); + for(Long id : issues.keySet()){ + url_request = connectionData.getUrl()+"/rest/api/2/issue/"+id+"/worklog"; + webResource = client.resource(url_request); + response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").get(ClientResponse.class); + + statusCode = response.getStatus(); + if (statusCode == 401) { + throw new AuthenticationException("Invalid Username or Password"); + } + String response1 = response.getEntity(String.class); + + try { + JSONObject jsonVal = new JSONObject(response1); + + JSONArray arr = jsonVal.getJSONArray("worklogs"); + for (int i = 0; i < arr.length(); i++) + { + issues.get(id).updateAuthorAndTime(arr.getJSONObject(i).getJSONObject("author").getString("name"), arr.getJSONObject(i).getLong("timeSpentSeconds")); + } + + + } catch (JSONException e) { + //e.printStackTrace(); + } + } + + for(Long id : issues.keySet()){ + issues.get(id).print(); + } + + } + +} diff --git a/src/main/java/com/dudar/Record.java b/src/main/java/com/dudar/Record.java new file mode 100644 index 0000000..70784fa --- /dev/null +++ b/src/main/java/com/dudar/Record.java @@ -0,0 +1,38 @@ +package com.dudar; + +import org.apache.commons.lang.time.DurationFormatUtils; + +import java.util.HashMap; +import java.util.Map; + +public class Record { + Long id; + String number; + String summary; + Map logwork; + + public Record(Long id, String number, String summery, String workLogAuthor, long loggedTime){ + this.id = id; + this.number = number; + this.summary = summery; + logwork = new HashMap<>(); + if(!workLogAuthor.isEmpty()){ + logwork.put(workLogAuthor, loggedTime); + } + } + + public void updateAuthorAndTime(String author, long time){ + logwork.put(author, logwork.containsKey(author) ? logwork.get(author)+time : time); + } + + public void print(){ + System.out.println(this.id); + System.out.println(this.number); + System.out.println(this.summary); + for(String id : logwork.keySet()){ + System.out.println("Author: " + id); + System.out.println("Logged time: "+ DurationFormatUtils.formatDuration(logwork.get(id)*1000, "HH:mm:ss")); + } + System.out.println("***************************"); + } +} From 9776a24c2f1cb655df155267ecb361aca17ccf4e Mon Sep 17 00:00:00 2001 From: Oleg Dudar Date: Wed, 17 Jan 2018 17:26:51 +0200 Subject: [PATCH 2/3] small refactoring. fixed issues with searching records for many users. --- src/main/java/com/dudar/JIRA_Accessor.java | 27 ++++++++++++++++++---- src/main/java/com/dudar/Record.java | 22 ++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/dudar/JIRA_Accessor.java b/src/main/java/com/dudar/JIRA_Accessor.java index 515a214..b439e06 100644 --- a/src/main/java/com/dudar/JIRA_Accessor.java +++ b/src/main/java/com/dudar/JIRA_Accessor.java @@ -10,19 +10,29 @@ import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory; import com.atlassian.util.concurrent.Promise; +import com.google.common.collect.Lists; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.core.util.Base64; +import org.apache.http.util.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import webwork.util.TextUtil; import javax.security.sasl.AuthenticationException; public class JIRA_Accessor { public Map getWorklogIssues(ConnectionData connectionData, String names){ + +// List names1 = new ArrayList(Arrays.asList(names.split(","))); +// List resultNames = new ArrayList<>(); +// for(String a : names1){ +// resultNames.add(a.trim().replace(" ", ".")); +// } + final URI jiraServerUri; Map issuesList = new HashMap<>(); ArrayList ids = new ArrayList<>(); @@ -31,7 +41,7 @@ public Map getWorklogIssues(ConnectionData connectionData, String final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, connectionData.getUser(), connectionData.getPass()); Set asd = new HashSet(); - Promise searchJqlPromise = restClient.getSearchClient().searchJql("worklogAuthor in (\"" + names + "\")", 1000, 0, asd); + Promise searchJqlPromise = restClient.getSearchClient().searchJql("worklogAuthor in (" + names + ")", 1000, 0, asd); //TODO handle exception RestClientException for getIssues() for (Issue issue : searchJqlPromise.claim().getIssues()) { @@ -50,11 +60,18 @@ public Map getWorklogIssues(ConnectionData connectionData, String public void testResponse(ConnectionData connectionData, String users) throws AuthenticationException { + List names1 = new ArrayList(Arrays.asList(users.split(","))); + List resultNames = new ArrayList<>(); + for(String a : names1){ + resultNames.add(a.trim().replace(" ", ".")); + } + String auth = new String(Base64.encode(connectionData.getUser()+":"+connectionData.getPass())); Client client = Client.create(); //TODO add '' for all items inside users list - String users1 = users.replaceAll(" ", "%20"); - String buff = "/rest/api/2/search?jql=worklogAuthor%20in%20(%27"+users1+"%27)&maxResults=1000"; +// String users1 = users; +// users1.replaceAll(" ", "%20"); + String buff = "/rest/api/2/search?jql=worklogAuthor%20in%20("+String.join(",", resultNames)+")&maxResults=1000"; String url_request = connectionData.getUrl()+buff; WebResource webResource = client.resource(String.valueOf(url_request)); @@ -65,7 +82,7 @@ public void testResponse(ConnectionData connectionData, String users) throws Aut if (statusCode == 401) { throw new AuthenticationException("Invalid Username or Password"); } - Map issues = getWorklogIssues(connectionData, users); + Map issues = getWorklogIssues(connectionData, String.join(",", resultNames)); for(Long id : issues.keySet()){ url_request = connectionData.getUrl()+"/rest/api/2/issue/"+id+"/worklog"; webResource = client.resource(url_request); @@ -93,7 +110,7 @@ public void testResponse(ConnectionData connectionData, String users) throws Aut } for(Long id : issues.keySet()){ - issues.get(id).print(); + issues.get(id).print(String.join(",", resultNames)); } } diff --git a/src/main/java/com/dudar/Record.java b/src/main/java/com/dudar/Record.java index 70784fa..d9dcb35 100644 --- a/src/main/java/com/dudar/Record.java +++ b/src/main/java/com/dudar/Record.java @@ -35,4 +35,26 @@ public void print(){ } System.out.println("***************************"); } + + public void print(String names){ + boolean check = false; + for(String id : logwork.keySet()){ + if(names.contains(id)) + check = true; + break; + } + if(check) { + System.out.println(this.id); + System.out.println(this.number); + System.out.println(this.summary); + for (String id : logwork.keySet()) { + if(names.contains(id)) + { + System.out.println("Author: " + id); + System.out.println("Logged time: " + DurationFormatUtils.formatDuration(logwork.get(id) * 1000, "HH:mm:ss")); + } + } + System.out.println("***************************"); + } + } } From fd00445a25606440726041ce16bb7f170c0b4ece Mon Sep 17 00:00:00 2001 From: Oleg Dudar Date: Fri, 19 Jan 2018 15:17:31 +0200 Subject: [PATCH 3/3] Posting results data into table view. Handled issues while retrieving data for multiple users. --- src/main/java/Main.java | 128 +++++++++++++----- src/main/java/com/dudar/JIRA_Accessor.java | 66 +++++---- src/main/java/com/dudar/Record.java | 22 +++ src/main/java/com/dudar/TicketDataRecord.java | 50 +++++++ 4 files changed, 199 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/dudar/TicketDataRecord.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 87cf389..ac3d286 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,53 +4,38 @@ import java.io.PrintWriter; import java.util.*; -import com.atlassian.jira.rest.client.api.domain.Issue; import com.csvreader.CsvReader; import com.dudar.ConnectionData; import com.dudar.JIRA_Accessor; import javax.security.sasl.AuthenticationException; +import com.dudar.TicketDataRecord; import javafx.application.Application; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.control.PasswordField; -import javafx.scene.control.TextField; +import javafx.scene.control.*; +import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; +import javafx.stage.Modality; import javafx.stage.Stage; -import static javafx.application.Application.launch; - public class Main extends Application{ - public static void main (String[] args) { + public List records = new ArrayList<>(); + public static void main (String[] args) { launch(args); - -// JIRA_Accessor accessor = new JIRA_Accessor(); -// -// try { -// accessor.testResponse(PropertiesReader.readPropertiesToCredentiasData()); -// } catch (AuthenticationException e) { -// e.printStackTrace(); -// } catch (FileNotFoundException e) { -// e.printStackTrace(); -// } - -// -// doTheJob(args); - - } @Override @@ -76,8 +61,6 @@ public void start(Stage primaryStage) { ex.printStackTrace(); } - - GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); @@ -116,9 +99,23 @@ public void start(Stage primaryStage) { usersTextField.setText("Name of users (separateed by comma)"); grid.add(usersTextField, 1, 4); - Button btn = new Button("Sign in"); + Button btn_newWindows = new Button("Results"); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); + hbBtn.getChildren().add(btn_newWindows); + btn_newWindows.setDisable(true); + grid.add(hbBtn, 0, 5); + btn_newWindows.setOnAction(new EventHandler() { + + @Override + public void handle(ActionEvent event) { + showResultsView(primaryStage); + } + }); + + Button btn = new Button("Get data"); + hbBtn = new HBox(10); + hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().add(btn); grid.add(hbBtn, 1, 5); btn.setOnAction(new EventHandler() { @@ -130,23 +127,90 @@ public void handle(ActionEvent event) { JIRA_Accessor accessor = new JIRA_Accessor(); try { - accessor.testResponse(new ConnectionData(urlTextField.getText(), userTextField.getText(), pwBox.getText()), usersTextField.getText()); - + records = accessor.getResponseResult(new ConnectionData(urlTextField.getText(), userTextField.getText(), pwBox.getText()), usersTextField.getText()); } catch (AuthenticationException e) { e.printStackTrace(); } System.out.println("Finish"); -// calculation(); + if(!records.isEmpty()) + btn_newWindows.setDisable(false); } }); -// StackPane root = new StackPane(); -// root.getChildren().add(btn); - primaryStage.setScene(new Scene(grid, 300, 250)); + primaryStage.setScene(new Scene(grid, 660, 400)); + primaryStage.setX(100); + primaryStage.setY(100); primaryStage.show(); } + private void showResultsView(Stage primaryStage) { + final TableView table = new TableView(); + + Label secondLabel = new Label("I'm a Label on new Window"); + + StackPane secondaryLayout = new StackPane(); + secondaryLayout.getChildren().add(secondLabel); + + Scene secondScene = new Scene(secondaryLayout, 800, 600); + + // New window (Stage) + Stage newWindow = new Stage(); + newWindow.setTitle("Second Stage"); + newWindow.setScene(secondScene); + + // Specifies the modality for new window. + newWindow.initModality(Modality.APPLICATION_MODAL); + + // Specifies the owner Window (parent) for new window + newWindow.initOwner(primaryStage); + + // Set position of second window, related to primary window. + newWindow.setX(primaryStage.getX() + 10); + newWindow.setY(primaryStage.getY() + 10); + + table.setEditable(true); + + TableColumn authorNameCol = new TableColumn("Reporter"); + TableColumn ticketNameCol = new TableColumn("Ticket Name"); + TableColumn summaryCol = new TableColumn("Summary"); + TableColumn hoursCol = new TableColumn("Hours"); + + table.getColumns().addAll(authorNameCol, ticketNameCol, summaryCol, hoursCol); + + final ObservableList data1 = FXCollections.observableArrayList( + new TicketDataRecord("Jacob1", "ANS-1", "1h 15m", "1"), + new TicketDataRecord("Jacob2", "ANS-2", "Summary 2", "2h 15m"), + new TicketDataRecord("Jacob3", "ANS-3", "Summary 3", "3h 15m"), + new TicketDataRecord("Jacob4", "ANS-4", "Summary 4", "4h 15m"), + new TicketDataRecord("Jacob5", "ANS-5", "Summary 5", "5h 15m") + ); + + final ObservableList data = FXCollections.observableArrayList(); + for(TicketDataRecord rec : records){ + data.add(rec); + } + + authorNameCol.setCellValueFactory( + new PropertyValueFactory<>("authorName") + ); + ticketNameCol.setCellValueFactory( + new PropertyValueFactory<>("ticketNumber") + ); + summaryCol.setCellValueFactory( + new PropertyValueFactory<>("ticketSummary") + ); + hoursCol.setCellValueFactory( + new PropertyValueFactory<>("loggedHours") + ); + + table.setItems(data); + + secondaryLayout.getChildren().add(table); + + newWindow.show(); + } + private static void doTheJob(String[] args) { String pathToFile; String pathToFileCreate; diff --git a/src/main/java/com/dudar/JIRA_Accessor.java b/src/main/java/com/dudar/JIRA_Accessor.java index b439e06..9603cce 100644 --- a/src/main/java/com/dudar/JIRA_Accessor.java +++ b/src/main/java/com/dudar/JIRA_Accessor.java @@ -27,12 +27,6 @@ public class JIRA_Accessor { public Map getWorklogIssues(ConnectionData connectionData, String names){ -// List names1 = new ArrayList(Arrays.asList(names.split(","))); -// List resultNames = new ArrayList<>(); -// for(String a : names1){ -// resultNames.add(a.trim().replace(" ", ".")); -// } - final URI jiraServerUri; Map issuesList = new HashMap<>(); ArrayList ids = new ArrayList<>(); @@ -58,7 +52,9 @@ public Map getWorklogIssues(ConnectionData connectionData, String return null; } - public void testResponse(ConnectionData connectionData, String users) throws AuthenticationException { + public List getResponseResult(ConnectionData connectionData, String users) throws AuthenticationException { + + List records = new ArrayList<>(); List names1 = new ArrayList(Arrays.asList(users.split(","))); List resultNames = new ArrayList<>(); @@ -69,8 +65,6 @@ public void testResponse(ConnectionData connectionData, String users) throws Aut String auth = new String(Base64.encode(connectionData.getUser()+":"+connectionData.getPass())); Client client = Client.create(); //TODO add '' for all items inside users list -// String users1 = users; -// users1.replaceAll(" ", "%20"); String buff = "/rest/api/2/search?jql=worklogAuthor%20in%20("+String.join(",", resultNames)+")&maxResults=1000"; String url_request = connectionData.getUrl()+buff; @@ -83,36 +77,38 @@ public void testResponse(ConnectionData connectionData, String users) throws Aut throw new AuthenticationException("Invalid Username or Password"); } Map issues = getWorklogIssues(connectionData, String.join(",", resultNames)); - for(Long id : issues.keySet()){ - url_request = connectionData.getUrl()+"/rest/api/2/issue/"+id+"/worklog"; - webResource = client.resource(url_request); - response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").get(ClientResponse.class); - - statusCode = response.getStatus(); - if (statusCode == 401) { - throw new AuthenticationException("Invalid Username or Password"); - } - String response1 = response.getEntity(String.class); - - try { - JSONObject jsonVal = new JSONObject(response1); - - JSONArray arr = jsonVal.getJSONArray("worklogs"); - for (int i = 0; i < arr.length(); i++) - { - issues.get(id).updateAuthorAndTime(arr.getJSONObject(i).getJSONObject("author").getString("name"), arr.getJSONObject(i).getLong("timeSpentSeconds")); + if(!issues.isEmpty()) { + for (Long id : issues.keySet()) { + url_request = connectionData.getUrl() + "/rest/api/2/issue/" + id + "/worklog"; + webResource = client.resource(url_request); + response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").get(ClientResponse.class); + + statusCode = response.getStatus(); + if (statusCode == 401) { + throw new AuthenticationException("Invalid Username or Password"); + } + String response1 = response.getEntity(String.class); + try { + JSONObject jsonVal = new JSONObject(response1); + + JSONArray arr = jsonVal.getJSONArray("worklogs"); + for (int i = 0; i < arr.length(); i++) { + issues.get(id).updateAuthorAndTime(arr.getJSONObject(i).getJSONObject("author").getString("name"), arr.getJSONObject(i).getLong("timeSpentSeconds")); + } + } catch (JSONException e) { + //e.printStackTrace(); } - - - } catch (JSONException e) { - //e.printStackTrace(); } - } - for(Long id : issues.keySet()){ - issues.get(id).print(String.join(",", resultNames)); + for (Long id : issues.keySet()) { + issues.get(id).print(String.join(",", resultNames)); + List retrivedData = issues.get(id).convertToRecords(String.join(",", resultNames)); + if (retrivedData != null) + records.addAll(retrivedData); + } + return records; } - + return null; } } diff --git a/src/main/java/com/dudar/Record.java b/src/main/java/com/dudar/Record.java index d9dcb35..66c6b09 100644 --- a/src/main/java/com/dudar/Record.java +++ b/src/main/java/com/dudar/Record.java @@ -2,7 +2,9 @@ import org.apache.commons.lang.time.DurationFormatUtils; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; public class Record { @@ -57,4 +59,24 @@ public void print(String names){ System.out.println("***************************"); } } + + public List convertToRecords(String names){ + List ar = new ArrayList<>(); + boolean check = false; + for(String id : logwork.keySet()){ + if(names.contains(id)) + check = true; + break; + } + if(check) { + for (String name : logwork.keySet()) { + if(names.contains(name)) + { + ar.add(new TicketDataRecord(name, this.number, this.summary, DurationFormatUtils.formatDuration(logwork.get(name) * 1000, "HH:mm:ss"))); + } + } + return ar; + } + return null; + } } diff --git a/src/main/java/com/dudar/TicketDataRecord.java b/src/main/java/com/dudar/TicketDataRecord.java new file mode 100644 index 0000000..66a6ce6 --- /dev/null +++ b/src/main/java/com/dudar/TicketDataRecord.java @@ -0,0 +1,50 @@ +package com.dudar; + +import javafx.beans.property.SimpleStringProperty; + +public class TicketDataRecord { + private final SimpleStringProperty authorName; + private final SimpleStringProperty ticketNumber; + private final SimpleStringProperty ticketSummary; + private final SimpleStringProperty loggedHours; + + public TicketDataRecord(String name, String number, String summary, String hours) { + this.authorName = new SimpleStringProperty(name); + this.ticketNumber = new SimpleStringProperty(number); + this.ticketSummary = new SimpleStringProperty(summary); + this.loggedHours = new SimpleStringProperty(hours); + } + + public String getAuthorName() { + return authorName.get(); + } + + public void setAuthorName(String fName) { + authorName.set(fName); + } + + public String getTicketNumber() { + return ticketNumber.get(); + } + + public void setTicketNumber(String fName) { + ticketNumber.set(fName); + } + + public String getTicketSummary() { + return ticketSummary.get(); + } + + public void setTicketSummary(String fName) { + ticketSummary.set(fName); + } + + public String getLoggedHours() { + return loggedHours.get(); + } + + public void setLoggedHours(String fName) { + loggedHours.set(fName); + } +} +