Промежуточный коммит

This commit is contained in:
2017-12-06 12:43:08 +07:00
parent d9beeab2cd
commit bdbb585d68
23 changed files with 333 additions and 53 deletions

View File

@ -79,4 +79,8 @@ public class ConfigController {
Stage stage = (Stage) cancel.getScene().getWindow();
stage.close();
}
public static String getConfigFile() {
return CONF_FILE;
}
}

View File

@ -5,6 +5,9 @@ public class EmailRecord {
private String name;
private String email;
public EmailRecord() {
}
public EmailRecord(long id, String name, String email) {
this.id = id;
this.name = name;

View File

@ -3,7 +3,9 @@ package ru.cft.task.restClient;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
@ -36,7 +38,7 @@ public class MainController {
System.exit(0);
}
private Stage showModal(String fxml, String title) {
private void showModal(String fxml, String title) {
Stage stage = null;
try {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource(fxml)));
@ -47,36 +49,29 @@ public class MainController {
stage.setTitle(title);
stage.initOwner(mainStage);
stage.initModality(Modality.APPLICATION_MODAL);
return stage;
stage.showAndWait();
} catch (IOException e) {
Utils.showAlert("error", e.getMessage());
} finally {
return stage;
}
}
public void showServerConfig() {
Stage config = showModal("/config.fxml", srv_config.getText());
config.showAndWait();
showModal("/config.fxml", srv_config.getText());
}
public void createAction() {
Stage cr = showModal("/form.fxml", create.getText());
cr.showAndWait();
showModal("/form_create.fxml", create.getText());
}
public void readAction() {
Stage rd = showModal("/form.fxml", read.getText());
rd.showAndWait();
showModal("/form_read.fxml", read.getText());
}
public void updateAction() {
Stage upd = showModal("/form.fxml", update.getText());
upd.showAndWait();
showModal("/form_update.fxml", update.getText());
}
public void deleteAction() {
Stage dlt = showModal("/form.fxml", delete.getText());
dlt.showAndWait();
showModal("/form_delete.fxml", delete.getText());
}
}

View File

@ -4,6 +4,14 @@ import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.springframework.http.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class RestActionsController {
@FXML
@ -18,11 +26,81 @@ public class RestActionsController {
@FXML
private Button cancel;
@FXML
private Button doAction;
private String getRestServerUrl() {
Properties props = new Properties();
InputStream conf_file = null;
String url = "";
try {
conf_file = new FileInputStream(ConfigController.getConfigFile());
props.load(conf_file);
url = props.getProperty("url", "http://localhost");
url += ":" + props.getProperty("port", "8080");
} catch (IOException ex) {
Utils.showAlert("error", ex.getMessage());
} finally {
if (conf_file != null) {
try {
conf_file.close();
} catch (IOException ex) {
Utils.showAlert("error", ex.getMessage());
}
}
return url;
}
}
public void cancelAction() {
Stage stage = (Stage) cancel.getScene().getWindow();
stage.close();
}
public void createAction() {
EmailRecord emailRecord = new EmailRecord();
emailRecord.setName("Test");
emailRecord.setEmail("aaa");
RestTemplate restTemplate = new RestTemplate();
HttpEntity<EmailRecord> request = new HttpEntity<>(emailRecord);
try {
ResponseEntity<EmailRecord> response = restTemplate.exchange(getRestServerUrl(), HttpMethod.POST, request, EmailRecord.class);
System.out.println(response.getStatusCode());
if (response.getStatusCode() == HttpStatus.OK) {
EmailRecord er = response.getBody();
id.setText(String.valueOf(er.getId()));
}
} catch (RestClientException ex) {
Utils.showAlert("error", ex.getMessage());
}
}
public void readAction() {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("id", id.getText());
headers.set("name", name.getText());
headers.set("email", email.getText());
RestTemplate restTemplate = new RestTemplate();
HttpEntity<EmailRecord> entity = new HttpEntity<>(headers);
try {
ResponseEntity<EmailRecord> result = restTemplate.exchange(getRestServerUrl(), HttpMethod.GET, entity, EmailRecord.class);
if (result.getStatusCode() == HttpStatus.OK) {
EmailRecord emailRecord = result.getBody();
id.setText(String.valueOf(emailRecord.getId()));
name.setText(emailRecord.getName());
email.setText(emailRecord.getEmail());
}
} catch (RestClientException ex) {
Utils.showAlert("error", ex.getMessage());
}
}
public void updateAction() {
}
public void deleteAction() {
}
}

View File

@ -0,0 +1,12 @@
package ru.cft.task.restClient;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import java.io.IOException;
public class RestErrorHandler extends DefaultResponseErrorHandler {
public void handleError(ClientHttpResponse response) throws IOException {
Utils.showAlert("error", response.getStatusText());
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="147.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.RestActionsController">
<children>
<Label layoutX="15.0" layoutY="14.0" text="Идентификатор" />
<TextField fx:id="id" disable="true" editable="false" layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="181.0" />
<Label layoutX="33.0" layoutY="49.0" text="Имя клиента" />
<Label layoutX="27.0" layoutY="83.0" text="Email клиента" />
<TextField fx:id="name" layoutX="111.0" layoutY="45.0" prefHeight="25.0" prefWidth="181.0" />
<TextField fx:id="email" layoutX="111.0" layoutY="79.0" prefHeight="25.0" prefWidth="181.0" />
<Button fx:id="cancel" layoutX="14.0" layoutY="118.0" mnemonicParsing="false" onAction="#cancelAction" text="Отмена" />
<Button fx:id="createBtn" layoutX="240.0" layoutY="118.0" mnemonicParsing="false" onAction="#createAction" text="Добавить" />
</children>
</AnchorPane>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="deleteBtn" onDragDetected="#deleteAction" prefHeight="68.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.RestActionsController">
<children>
<Label layoutX="15.0" layoutY="14.0" text="Идентификатор" />
<TextField fx:id="id" layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="181.0" />
<Button fx:id="cancel" layoutX="15.0" layoutY="40.0" mnemonicParsing="false" onAction="#cancelAction" text="Отмена" />
<Button fx:id="doAction" layoutX="232.0" layoutY="40.0" mnemonicParsing="false" text="Удалить" />
</children>
</AnchorPane>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="147.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.RestActionsController">
<children>
<Label layoutX="15.0" layoutY="14.0" text="Идентификатор" />
<TextField fx:id="id" layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="181.0" />
<Label layoutX="33.0" layoutY="49.0" text="Имя клиента" />
<Label layoutX="27.0" layoutY="83.0" text="Email клиента" />
<TextField fx:id="name" layoutX="111.0" layoutY="45.0" prefHeight="25.0" prefWidth="181.0" />
<TextField fx:id="email" layoutX="111.0" layoutY="79.0" prefHeight="25.0" prefWidth="181.0" />
<Button fx:id="cancel" layoutX="14.0" layoutY="118.0" mnemonicParsing="false" onAction="#cancelAction" text="Отмена" />
<Button fx:id="readBtn" layoutX="240.0" layoutY="118.0" mnemonicParsing="false" onAction="#readAction" text="Найти" />
</children>
</AnchorPane>

View File

@ -8,12 +8,12 @@
<AnchorPane prefHeight="147.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.RestActionsController">
<children>
<Label layoutX="15.0" layoutY="14.0" text="Идентификатор" />
<TextField layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="181.0" />
<TextField fx:id="id" layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="181.0" />
<Label layoutX="33.0" layoutY="49.0" text="Имя клиента" />
<Label layoutX="27.0" layoutY="83.0" text="Email клиента" />
<TextField layoutX="111.0" layoutY="45.0" prefHeight="25.0" prefWidth="181.0" />
<TextField layoutX="111.0" layoutY="79.0" prefHeight="25.0" prefWidth="181.0" />
<TextField fx:id="name" layoutX="111.0" layoutY="45.0" prefHeight="25.0" prefWidth="181.0" />
<TextField fx:id="email" layoutX="111.0" layoutY="79.0" prefHeight="25.0" prefWidth="181.0" />
<Button fx:id="cancel" layoutX="14.0" layoutY="118.0" mnemonicParsing="false" onAction="#cancelAction" text="Отмена" />
<Button fx:id="doAction" layoutX="240.0" layoutY="118.0" mnemonicParsing="false" text="Button" />
<Button fx:id="doAction" layoutX="240.0" layoutY="118.0" mnemonicParsing="false" onAction="#updateAction" text="Изменить" />
</children>
</AnchorPane>