diff --git a/com.connor.jd.plm/.classpath b/com.connor.jd.plm/.classpath index de347d7..5862cd5 100644 --- a/com.connor.jd.plm/.classpath +++ b/com.connor.jd.plm/.classpath @@ -1,555 +1,557 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/com.connor.jd.plm/META-INF/MANIFEST.MF b/com.connor.jd.plm/META-INF/MANIFEST.MF index c3b4388..cad01e9 100644 --- a/com.connor.jd.plm/META-INF/MANIFEST.MF +++ b/com.connor.jd.plm/META-INF/MANIFEST.MF @@ -52,3 +52,5 @@ Bundle-ClassPath: ., lib/poi-scratchpad-4.1.1.jar, lib/xmlbeans-3.1.0.jar, src/com/connor/jd/plm/db/ + lib/ojdbc6.jar, + lib/commons-lang3-3.9.jar diff --git a/com.connor.jd.plm/build.properties b/com.connor.jd.plm/build.properties index 6847fdf..50cbbff 100644 --- a/com.connor.jd.plm/build.properties +++ b/com.connor.jd.plm/build.properties @@ -22,4 +22,7 @@ bin.includes = plugin.xml,\ lib/poi-ooxml-schemas-4.1.1.jar,\ lib/poi-scratchpad-4.1.1.jar,\ lib/xmlbeans-3.1.0.jar,\ - src/com/connor/jd/plm/db/ + src/com/connor/jd/plm/db/,\ + lib/ojdbc6.jar,\ + lib/commons-lang3-3.9.jar + diff --git a/com.connor.jd.plm/src/com/connor/jd/operations/CreateTableOperation.java b/com.connor.jd.plm/src/com/connor/jd/operations/CreateTableOperation.java new file mode 100644 index 0000000..678206b --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/operations/CreateTableOperation.java @@ -0,0 +1,69 @@ +package com.connor.jd.operations; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import com.connor.jd.plm.utils.DBConnectionUtil; + +public class CreateTableOperation { + private static Connection con = null; + private static Statement ps = null; + private static ResultSet rs = null; + + public static void create() { + String createTable1 = "CREATE TABLE JD2_Component (test_type VARCHAR(255),test_project VARCHAR(255),test_time VARCHAR(255),test_num NUMBER,test_cost FLOAT, test_id NUMBER primary key)"; + String createSeq1 = "CREATE sequence component_id_seq minvalue 1 nomaxvalue INCREMENT BY 1 START WITH 1 nocache"; + String createTRG1 = "CREATE or REPLACE TRIGGER component_id_ins_trg before INSERT ON JD2_COMPONENT for each ROW BEGIN SELECT component_id_seq.Nextval into : new.TEST_ID from dual; END;"; + + String createTable2 = "CREATE TABLE JD2_Machine (test_type VARCHAR(255),test_project VARCHAR(255),test_cost FLOAT, test_id NUMBER primary key)"; + String createSeq2 = "CREATE sequence machine_id_seq minvalue 1 nomaxvalue INCREMENT BY 1 START WITH 1 nocache"; + String createTRG2 = "CREATE or REPLACE TRIGGER machine_id_ins_trg before INSERT ON JD2_MACHINE for each ROW BEGIN SELECT machine_id_seq.Nextval into : new.TEST_ID from dual; END;"; + String[] sqls = { createTable1, createSeq1, createTRG1, createTable2, createSeq2, createTRG2 }; + try { + con = DBConnectionUtil.dbConn("infodba", "infodba"); + String sqlString = "SELECT count(*) from user_tables WHERE table_name='JD2_COMPONENT'"; + if (con == null) { + System.out.print("连接失败"); + } else { + ps = con.createStatement(); + rs = ps.executeQuery(sqlString); + while (rs.next()) { + System.out.println(rs.getInt(1)); + if (rs.getInt(1) == 0) { + for (int i = 0; i < sqls.length; i++) { + ps = con.createStatement(); + ps.execute(sqls[i]); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (ps != null) { + try { + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + } +} diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/beans/ComponentBean.java b/com.connor.jd.plm/src/com/connor/jd/plm/beans/ComponentBean.java new file mode 100644 index 0000000..31c5cd8 --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/plm/beans/ComponentBean.java @@ -0,0 +1,101 @@ +package com.connor.jd.plm.beans; + +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.value.ObservableValue; + +public class ComponentBean { + + private int compId; + private SimpleStringProperty type; + private SimpleStringProperty project; + private SimpleStringProperty time; + private SimpleStringProperty number; + private SimpleStringProperty cost; + private int addId;// 只用于添加所用 + private int stage;// 状态 0为从数据库中取出的,1为新增的,2为从数据库取出但被修改了的 + + public int getAddId() { + return addId; + } + + public void setAddId(int addId) { + this.addId = addId; + } + + public int getStage() { + return stage; + } + + public void setStage(int stage) { + this.stage = stage; + } + + public ObservableValue typeProperty() { + return type; + } + + public ObservableValue projectProperty() { + return project; + } + + public ObservableValue timeProperty() { + return time; + } + + public ObservableValue numberProperty() { + return number; + } + + public ObservableValue costProperty() { + return cost; + } + + public int getCompId() { + return compId; + } + + public void setCompId(int compId) { + this.compId = compId; + } + + public String getType() { + return type.get(); + } + + public void setType(String type) { + this.type = new SimpleStringProperty(type); + } + + public String getProject() { + return project.get(); + } + + public void setProject(String project) { + this.project = new SimpleStringProperty(project); + } + + public String getTime() { + return time.get(); + } + + public void setTime(String time) { + this.time = new SimpleStringProperty(time); + } + + public String getNumber() { + return number.get(); + } + + public void setNumber(String number) { + this.number = new SimpleStringProperty(number); + } + + public String getCost() { + return cost.get(); + } + + public void setCost(String cost) { + this.cost = new SimpleStringProperty(cost); + } + +} diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/beans/MachineBean.java b/com.connor.jd.plm/src/com/connor/jd/plm/beans/MachineBean.java new file mode 100644 index 0000000..51ebd0f --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/plm/beans/MachineBean.java @@ -0,0 +1,75 @@ +package com.connor.jd.plm.beans; + +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.value.ObservableValue; + +public class MachineBean { + + private int macId; + private SimpleStringProperty type; + private SimpleStringProperty project; + private SimpleStringProperty cost; + private int addId;// 只用于添加所用 + private int stage;// 状态 0为从数据库中取出的,1为新增的,2为从数据库取出但被修改了的 + + public int getMacId() { + return macId; + } + + public void setMacId(int macId) { + this.macId = macId; + } + + public int getAddId() { + return addId; + } + + public void setAddId(int addId) { + this.addId = addId; + } + + public int getStage() { + return stage; + } + + public void setStage(int stage) { + this.stage = stage; + } + + public ObservableValue typeProperty() { + return type; + } + + public ObservableValue projectProperty() { + return project; + } + + public ObservableValue costProperty() { + return cost; + } + + public String getType() { + return type.get(); + } + + public void setType(String type) { + this.type = new SimpleStringProperty(type); + } + + public String getProject() { + return project.get(); + } + + public void setProject(String project) { + this.project = new SimpleStringProperty(project); + } + + public String getCost() { + return cost.get(); + } + + public void setCost(String cost) { + this.cost = new SimpleStringProperty(cost); + ; + } +} diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java b/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java index a054c5c..a334a53 100644 --- a/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java +++ b/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java @@ -1,39 +1,714 @@ package com.connor.jd.plm.dialogs; -import javax.swing.JPanel; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang3.math.NumberUtils; + +import com.connor.jd.operations.CreateTableOperation; +import com.connor.jd.plm.beans.ComponentBean; +import com.connor.jd.plm.beans.MachineBean; +import com.connor.jd.plm.utils.DBConnectionUtil; import com.teamcenter.rac.aif.AbstractAIFApplication; import com.teamcenter.rac.kernel.TCException; import javafx.application.Application; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; +import javafx.scene.control.Button; +import javafx.scene.control.SelectionMode; +import javafx.scene.control.Tab; +import javafx.scene.control.TabPane; +import javafx.scene.control.TableCell; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableColumn.CellEditEvent; +import javafx.scene.control.TableView; +import javafx.scene.control.TextField; +import javafx.scene.control.cell.PropertyValueFactory; +import javafx.scene.control.cell.TextFieldTableCell; +import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; public class CSXWHDialog extends Application { private AbstractAIFApplication app; + private static Connection con = null; + private static PreparedStatement ps = null; + private static ResultSet rs = null; + private ObservableList componentData = FXCollections.observableArrayList(); + private ObservableList machineData = FXCollections.observableArrayList(); + private ObservableList findList = FXCollections.observableArrayList(); + private ObservableList findMacList = FXCollections.observableArrayList(); public CSXWHDialog() throws TCException { } - private JPanel Panel; - private Object[] obj = new Object[] { "序号", "产品类型", "试验项目", "试验时间", "样品数量", "试验费用" };// - private Object[] obj2 = new Object[] { "序号", "类别", "项目", "项目费用" };// - - public void initUI() { - - } + private TableView tableView0; + private TableView tableView1; + private int index = 10000; + private int macindex = 10000; @Override public void start(Stage arg0) throws Exception { + CreateTableOperation.create(); + try { + con = DBConnectionUtil.dbConn("infodba", "infodba"); + String sqlString = "select * from JD2_Component"; + if (con == null) { + Alert alert = new Alert(AlertType.ERROR, "连接失败"); + alert.show(); + } else { + ps = con.prepareStatement(sqlString); + rs = ps.executeQuery(); + while (rs.next()) { + ComponentBean comBean = new ComponentBean(); + // System.out.println("第一列内容:" + rs.getString(1)); + comBean.setType(rs.getString(1)); + comBean.setProject(rs.getString(2)); + comBean.setTime(rs.getString(3)); + comBean.setNumber(String.valueOf(rs.getInt(4))); + comBean.setCost(String.valueOf(rs.getDouble(5))); + comBean.setCompId(rs.getInt(6)); + comBean.setAddId(0); + comBean.setStage(0);// 刚从数据库取出 + componentData.add(comBean); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (rs != null) { + rs.close(); + } + if (ps != null) { + ps.close(); + } + if (con != null) { + con.close(); + } + } + for (int i = 0; i < componentData.size(); i++) { + findList.add(componentData.get(i)); + } Stage primaryStage = new Stage(); Pane root = new Pane(); - Scene scene = new Scene(root, 382, 178); + TabPane tabPane = new TabPane(); + Tab tab0 = new Tab("部件测试项维护"); + Tab tab1 = new Tab("整机测试项维护"); + // 第一个页面 + AnchorPane ap0 = new AnchorPane(); + tableView0 = new TableView(); + tableView0.setEditable(true); + tableView0.setLayoutY(27.0); + tableView0.setPrefSize(486.0, 270.0); + TableColumn num0 = new TableColumn("序号"); + num0.setPrefWidth(75); + + TableColumn type0 = new TableColumn("部品类型"); + type0.setPrefWidth(90); + type0.setCellValueFactory(new PropertyValueFactory<>("type")); + type0.setCellFactory(TextFieldTableCell.forTableColumn()); + type0.setOnEditCommit((CellEditEvent t) -> { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setType(t.getNewValue()); + // 如果数据本身是从数据库中取出的,那么将数据状态改为2->从数据库取出但被修改了的 + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + }); + TableColumn project0 = new TableColumn("试验项目"); + project0.setPrefWidth(89); + project0.setCellValueFactory(new PropertyValueFactory<>("project")); + project0.setCellFactory(TextFieldTableCell.forTableColumn()); + project0.setOnEditCommit((CellEditEvent t) -> { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setProject(t.getNewValue()); + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + }); + TableColumn time = new TableColumn("试验时间"); + time.setPrefWidth(92); + time.setCellValueFactory(new PropertyValueFactory<>("time")); + time.setCellFactory(TextFieldTableCell.forTableColumn()); + time.setOnEditCommit((CellEditEvent t) -> { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setTime(t.getNewValue()); + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + }); + TableColumn number = new TableColumn("样品数量"); + number.setPrefWidth(68); + number.setCellValueFactory(new PropertyValueFactory<>("number")); + number.setCellFactory(TextFieldTableCell.forTableColumn()); + number.setOnEditCommit((CellEditEvent t) -> { + if (NumberUtils.isNumber(t.getNewValue())) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setNumber(t.getNewValue()); + // 如果数据本身是从数据库中取出的,那么将数据状态改为2->从数据库取出但被修改了的 + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + } else { + tableView0.refresh(); + Alert alert = new Alert(AlertType.ERROR, "请输入数字"); + alert.show(); + } + + }); + TableColumn cost = new TableColumn("试验费用"); + cost.setPrefWidth(71); + cost.setCellValueFactory(new PropertyValueFactory<>("cost")); + cost.setCellFactory(TextFieldTableCell.forTableColumn()); + cost.setOnEditCommit((CellEditEvent t) -> { + t.getNewValue(); + if (NumberUtils.isNumber(t.getNewValue())) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setCost(t.getNewValue()); + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + } else { + tableView0.refresh(); + Alert alert = new Alert(AlertType.ERROR, "请输入数字"); + alert.show(); + } + + }); + tableView0.getColumns().addAll(num0, type0, project0, time, number, cost); + num0.setCellFactory((col) -> { + TableCell cell = new TableCell() { + @Override + public void updateItem(String item, boolean empty) { + super.updateItem(item, empty); + this.setText(null); + this.setGraphic(null); + if (!empty) { + int rowIndex = this.getIndex() + 1; + this.setText(String.valueOf(rowIndex)); + } + } + }; + return cell; + }); + tableView0.setItems(findList); + tableView0.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); + TextField search0 = new TextField(); + search0.setPrefSize(416.0, 23.0); + search0.setLayoutY(3.0); + Button searchBtn0 = new Button("查询"); + searchBtn0.setLayoutX(431.0); + searchBtn0.setLayoutY(3.0); + searchBtn0.setPrefSize(55.0, 23.0); + searchBtn0.setOnAction((arg) -> { + findList.clear(); + findList = search(search0.getText(), componentData); + tableView0.setItems(findList); + }); + Button addBtn0 = new Button("添加"); + addBtn0.setLayoutX(501.0); + addBtn0.setLayoutY(63.0); + addBtn0.setPrefSize(48.0, 23.0); + addBtn0.setOnAction((arg) -> { + + ComponentBean comBean = new ComponentBean(); + comBean.setType(""); + comBean.setProject(""); + comBean.setTime(""); + comBean.setNumber(String.valueOf("")); + comBean.setCost(String.valueOf("")); + comBean.setCompId(0); + comBean.setAddId(index++);// 只用于判断是不是新增的 + comBean.setStage(1);// 新增的 + findList.add(comBean); + componentData.add(comBean); + // tableView0.refresh(); + }); + Button removeBtn0 = new Button("删除"); + removeBtn0.setLayoutX(501.0); + removeBtn0.setLayoutY(127.0); + removeBtn0.setPrefSize(48.0, 23.0); + removeBtn0.setOnAction((arg) -> { + ObservableList delList = tableView0.getSelectionModel().getSelectedItems(); + List delAddList = new ArrayList(); + List delCompList = new ArrayList(); + for (int del = 0; del < delList.size(); del++) { + delAddList.add(delList.get(del).getAddId()); + delCompList.add(delList.get(del).getCompId()); + System.out.println("选中的id" + delList.get(del).getAddId()); + } + for (int i = 0; i < delAddList.size(); i++) { + System.out.println("deList的长度" + delList.size()); + System.out.println("选中的" + delAddList.get(i)); + for (int j = 0; j < findList.size(); j++) { + // System.out.println("addId:" + componentData.get(j).getAddId() + "--delId:" + + // delAddList.get(i)); + if (findList.get(j).getCompId() == 0) { + if (delAddList.get(i) == findList.get(j).getAddId()) { + // System.out.println("只移除了新增还未进数据库的"); + findList.remove(j); + } + Iterator compIt = componentData.iterator(); + while (compIt.hasNext()) { + int addid = compIt.next().getAddId(); + if (delAddList.get(i) == addid) { + compIt.remove(); + // System.out.println("初始结果集移除了一条"); + } + } + } else { + if (findList.get(j).getCompId() != 0) { + if (delCompList.get(i) == findList.get(j).getCompId()) { + String sql = "delete from JD2_Component where test_id=?"; + update(sql, findList.get(j).getCompId()); + // System.out.println("移除了原本数据库就有的"); + findList.remove(j); + } + Iterator compIt = componentData.iterator(); + while (compIt.hasNext()) { + int compid = compIt.next().getCompId(); + if (delCompList.get(i) == compid) { + compIt.remove(); + // System.out.println("初始结果集移除了一条"); + } + } + } + } + } + } + // tableView0.refresh(); + }); + Button saveBtn0 = new Button("保存"); + saveBtn0.setLayoutX(161.0); + saveBtn0.setLayoutY(319.0); + saveBtn0.setPrefSize(59.0, 23.0); + saveBtn0.setOnAction((arg) -> { + Boolean flag = true; + for (ComponentBean com : findList) { + if (com.getType().equals("") && com.getProject().equals("") && com.getTime().equals("") + && com.getNumber().equals("") && com.getCost().equals("")) { + flag = false; + } + } + if (flag) { + for (ComponentBean com : findList) { + System.out.println("状态" + com.getStage()); + if (com.getStage() == 1) { + insert(com); + com.setStage(0); + } + if (com.getStage() == 2) { + update(com); + com.setStage(0); + } + } + tableView0.refresh(); + } else { + Alert alert = new Alert(AlertType.ERROR, "存在某行的内容全为空,请检查"); + alert.show(); + } + + }); + Button exitBtn0 = new Button("取消"); + exitBtn0.setLayoutX(362.0); + exitBtn0.setLayoutY(319.0); + exitBtn0.setPrefSize(59.0, 23.0); + exitBtn0.setOnAction((arg) -> { +// for (int i = 0; i < findList.size(); i++) { +// System.out.println("状态:" + findList.get(i).getStage()); +// } + primaryStage.close(); + }); + ap0.getChildren().addAll(tableView0, search0, searchBtn0, addBtn0, removeBtn0, saveBtn0, exitBtn0); + tab0.setContent(ap0); + tab0.setClosable(false); + // 第二个页面 + try { + con = DBConnectionUtil.dbConn("infodba", "infodba"); + String sqlString = "select * from JD2_Machine"; + if (con == null) { + System.out.print("连接失败"); + // System.exit(0); + } else { + ps = con.prepareStatement(sqlString); + rs = ps.executeQuery(); + while (rs.next()) { + MachineBean macBean = new MachineBean(); + macBean.setType(rs.getString(1)); + macBean.setProject(rs.getString(2)); + macBean.setCost(String.valueOf(rs.getDouble(3))); + macBean.setMacId(rs.getInt(4)); + macBean.setAddId(0); + macBean.setStage(0);// 刚从数据库取出 + machineData.add(macBean); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (rs != null) { + rs.close(); + } + if (ps != null) { + ps.close(); + } + if (con != null) { + con.close(); + } + } + for (int i = 0; i < machineData.size(); i++) { + findMacList.add(machineData.get(i)); + } + AnchorPane ap1 = new AnchorPane(); + tableView1 = new TableView(); + tableView1.setEditable(true); + tableView1.setLayoutY(27.0); + tableView1.setPrefSize(486.0, 270.0); + TableColumn num1 = new TableColumn("序号"); + num1.setPrefWidth(136.0); + TableColumn type1 = new TableColumn("类别"); + type1.setPrefWidth(110.0); + type1.setCellValueFactory(new PropertyValueFactory<>("type")); + type1.setCellFactory(TextFieldTableCell.forTableColumn()); + type1.setOnEditCommit((CellEditEvent t) -> { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setType(t.getNewValue()); + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + }); + TableColumn project1 = new TableColumn("项目"); + project1.setPrefWidth(121.0); + project1.setCellValueFactory(new PropertyValueFactory<>("project")); + project1.setCellFactory(TextFieldTableCell.forTableColumn()); + project1.setOnEditCommit((CellEditEvent t) -> { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setProject(t.getNewValue()); + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + }); + TableColumn cost1 = new TableColumn("项目费用"); + cost1.setPrefWidth(118.0); + cost1.setCellValueFactory(new PropertyValueFactory<>("cost")); + cost1.setCellFactory(TextFieldTableCell.forTableColumn()); + cost1.setOnEditCommit((CellEditEvent t) -> { + if (NumberUtils.isNumber(t.getNewValue())) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setCost(t.getNewValue()); + if (t.getTableView().getItems().get(t.getTablePosition().getRow()).getStage() == 0) { + t.getTableView().getItems().get(t.getTablePosition().getRow()).setStage(2); + } + } else { + tableView0.refresh(); + Alert alert = new Alert(AlertType.ERROR, "请输入数字"); + alert.show(); + } + }); + tableView1.getColumns().addAll(num1, type1, project1, cost1); + num1.setCellFactory((col) -> { + TableCell cell = new TableCell() { + @Override + public void updateItem(String item, boolean empty) { + super.updateItem(item, empty); + this.setText(null); + this.setGraphic(null); + if (!empty) { + int rowIndex = this.getIndex() + 1; + this.setText(String.valueOf(rowIndex)); + } + } + }; + return cell; + }); + tableView1.setItems(findMacList); + tableView1.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); + TextField search1 = new TextField(); + search1.setPrefSize(416.0, 23.0); + search1.setLayoutY(3.0); + Button searchBtn1 = new Button("查询"); + searchBtn1.setLayoutX(431.0); + searchBtn1.setLayoutY(3.0); + searchBtn1.setPrefSize(55.0, 23.0); + searchBtn1.setOnAction((arg) -> { + findMacList.clear(); + findMacList = searchMac(search1.getText(), machineData); + tableView1.setItems(findMacList); + }); + Button addBtn1 = new Button("添加"); + addBtn1.setLayoutX(501.0); + addBtn1.setLayoutY(63.0); + addBtn1.setPrefSize(48.0, 23.0); + addBtn1.setOnAction((arg) -> { + + MachineBean macBean = new MachineBean(); + macBean.setType(""); + macBean.setProject(""); + macBean.setCost(String.valueOf("")); + macBean.setMacId(0); + macBean.setAddId(macindex++);// 只用于判断是不是新增的 + macBean.setStage(1);// 新增的 + findMacList.add(macBean); + machineData.add(macBean); + }); + Button removeBtn1 = new Button("删除"); + removeBtn1.setLayoutX(501.0); + removeBtn1.setLayoutY(127.0); + removeBtn1.setPrefSize(48.0, 23.0); + removeBtn1.setOnAction((arg) -> { + ObservableList delList = tableView1.getSelectionModel().getSelectedItems(); + List delAddList = new ArrayList(); + List delMacList = new ArrayList(); + for (int del = 0; del < delList.size(); del++) { + delAddList.add(delList.get(del).getAddId()); + delMacList.add(delList.get(del).getMacId()); + } + for (int i = 0; i < delAddList.size(); i++) { + System.out.println("deList的长度" + delList.size()); + System.out.println("选中的" + delAddList.get(i)); + for (int j = 0; j < findList.size(); j++) { + if (findMacList.get(j).getMacId() == 0) { + if (delAddList.get(i) == findMacList.get(j).getAddId()) { + findMacList.remove(j); + } + Iterator compIt = machineData.iterator(); + while (compIt.hasNext()) { + int addid = compIt.next().getAddId(); + if (delAddList.get(i) == addid) { + compIt.remove(); + } + } + } else { + if (findMacList.get(j).getMacId() != 0) { + if (delMacList.get(i) == findMacList.get(j).getMacId()) { + String sql = "delete from JD2_Machine where test_id=?"; + update(sql, findMacList.get(j).getMacId()); + findMacList.remove(j); + } + Iterator compIt = machineData.iterator(); + while (compIt.hasNext()) { + int compid = compIt.next().getMacId(); + if (delMacList.get(i) == compid) { + compIt.remove(); + } + } + } + } + } + } + }); + + Button saveBtn1 = new Button("保存"); + saveBtn1.setLayoutX(161.0); + saveBtn1.setLayoutY(319.0); + saveBtn1.setPrefSize(59.0, 23.0); + saveBtn1.setOnAction((arg) -> { + Boolean flag = true; + for (MachineBean com : findMacList) { + if (com.getType().equals("") && com.getProject().equals("") && com.getCost().equals("")) { + flag = false; + } + } + if (flag) { + for (MachineBean mac : findMacList) { + if (mac.getStage() == 1) { + insertMac(mac); + mac.setStage(0); + } + if (mac.getStage() == 2) { + updateMac(mac); + mac.setStage(0); + } + } + tableView1.refresh(); + } else { + Alert alert = new Alert(AlertType.ERROR, "存在某行的内容全为空,请检查"); + alert.show(); + } + + }); + Button exitBtn1 = new Button("取消"); + exitBtn1.setOnAction((arg) -> { + primaryStage.close(); + }); + exitBtn1.setLayoutX(362.0); + exitBtn1.setLayoutY(319.0); + exitBtn1.setPrefSize(59.0, 23.0); + ap1.getChildren().addAll(tableView1, search1, searchBtn1, addBtn1, removeBtn1, saveBtn1, exitBtn1); + tab1.setContent(ap1); + tab1.setClosable(false); + + tabPane.getTabs().addAll(tab0, tab1); + root.getChildren().add(tabPane); + Scene scene = new Scene(root, 600, 400); primaryStage.setScene(scene); primaryStage.setTitle("测试项维护"); primaryStage.show(); } + + public ObservableList search(String name, ObservableList list) { + ObservableList results = FXCollections.observableArrayList(); + Pattern pattern = Pattern.compile(name); + for (int i = 0; i < list.size(); i++) { + Matcher matcher = pattern.matcher(((ComponentBean) list.get(i)).getType()); + if (matcher.find()) { + results.add(list.get(i)); + } + } + return results; + } + + public ObservableList searchMac(String name, ObservableList list) { + ObservableList results = FXCollections.observableArrayList(); + Pattern pattern = Pattern.compile(name); + for (int i = 0; i < list.size(); i++) { + Matcher matcher = pattern.matcher(((MachineBean) list.get(i)).getType()); + if (matcher.find()) { + results.add(list.get(i)); + } + } + return results; + } + + private void insert(ComponentBean com) { + String sql = "insert into JD2_Component(test_type,test_project,test_time,test_num,test_cost) values(?,?,?,?,?)"; + con = DBConnectionUtil.dbConn("infodba", "infodba"); + try { + System.out.println("添加"); + ps = con.prepareStatement(sql); + ps.setString(1, com.getType()); + ps.setString(2, com.getProject()); + ps.setString(3, com.getTime()); + if (com.getNumber().equals("")) { + com.setNumber("0"); + } + ps.setInt(4, Integer.valueOf(com.getNumber())); + if (com.getCost().equals("")) { + com.setCost("0"); + } + ps.setDouble(5, Double.valueOf(com.getCost())); + int rs = ps.executeUpdate(); + if (rs == 1) + System.out.println("添加成功"); + else + System.out.println("添加失败"); + + } catch (Exception e) { + System.out.println("添加失败"); + } finally { + } + + } + + private void insertMac(MachineBean mac) { + String sql = "insert into JD2_Machine(test_type,test_project,test_cost) values(?,?,?)"; + con = DBConnectionUtil.dbConn("infodba", "infodba"); + try { + ps = con.prepareStatement(sql); + ps.setString(1, mac.getType()); + ps.setString(2, mac.getProject()); + if (mac.getCost().equals("")) { + mac.setCost("0"); + } + ps.setDouble(3, Double.valueOf(mac.getCost())); + int rs = ps.executeUpdate(); + System.out.println(rs); + if (rs == 1) + System.out.println("添加成功"); + else + System.out.println("添加失败"); + + } catch (Exception e) { + System.out.println("添加失败"); + } finally { + } + + } + + private void update(ComponentBean com) { + String sql = "update JD2_Component set test_type=?,test_project=?,test_time=?,test_num=?,test_cost=? where test_id=?"; + con = DBConnectionUtil.dbConn("infodba", "infodba"); + try { + ps = con.prepareStatement(sql); + ps.setString(1, com.getType()); + ps.setString(2, com.getProject()); + ps.setString(3, com.getTime()); + if (com.getNumber().equals("")) { + com.setNumber("0"); + } + ps.setInt(4, Integer.valueOf(com.getNumber())); + if (com.getCost().equals("")) { + com.setCost("0"); + } + ps.setDouble(5, Double.valueOf(com.getCost())); + ps.setInt(6, com.getCompId()); + int rs = ps.executeUpdate(); + if (rs == 1) + System.out.println("更新成功"); + else + System.out.println("更新失败"); + + } catch (Exception e) { + } finally { + } + } + + private void update(String sql, int id) { + con = DBConnectionUtil.dbConn("infodba", "infodba"); + try { + ps = con.prepareStatement(sql); + ps.setInt(1, id); + int rs = ps.executeUpdate(); + if (rs > 0) + System.out.println("成功"); + else + System.out.println("失败"); + } catch (Exception e) { + } finally { + if (ps != null) { + try { + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + } + + private void updateMac(MachineBean mac) { + String sql = "update JD2_Machine set test_type=?,test_project=?,test_cost=? where test_id=?"; + con = DBConnectionUtil.dbConn("infodba", "infodba"); + try { + ps = con.prepareStatement(sql); + ps.setString(1, mac.getType()); + ps.setString(2, mac.getProject()); + if (mac.getCost().equals("")) { + mac.setCost("0"); + } + ps.setDouble(3, Double.valueOf(mac.getCost())); + ps.setInt(4, mac.getMacId()); + int rs = ps.executeUpdate(); + if (rs == 1) + System.out.println("更新成功"); + else + System.out.println("更新失败"); + + } catch (Exception e) { + } finally { + } + } } \ No newline at end of file diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/handlers/CSXWHHandler.java b/com.connor.jd.plm/src/com/connor/jd/plm/handlers/CSXWHHandler.java index 70994ff..5a050f2 100644 --- a/com.connor.jd.plm/src/com/connor/jd/plm/handlers/CSXWHHandler.java +++ b/com.connor.jd.plm/src/com/connor/jd/plm/handlers/CSXWHHandler.java @@ -5,9 +5,12 @@ import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import com.connor.jd.plm.dialogs.CSXWHDialog; +import com.connor.jd.plm.utils.JDMethodUtil; import com.teamcenter.rac.aif.AbstractAIFUIApplication; import com.teamcenter.rac.aifrcp.AIFUtility; +import com.teamcenter.rac.kernel.TCException; import com.teamcenter.rac.kernel.TCSession; +import com.teamcenter.rac.util.MessageBox; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; @@ -19,21 +22,36 @@ public class CSXWHHandler extends AbstractHandler { public Object execute(ExecutionEvent arg0) throws ExecutionException { AbstractAIFUIApplication app = AIFUtility.getCurrentApplication(); TCSession session = (TCSession) app.getSession(); - new Thread(() -> { - new JFXPanel(); - Platform.setImplicitExit(false); - Platform.runLater(new Runnable() { - @Override - public void run() { - try { - CSXWHDialog csxwh = new CSXWHDialog(); - csxwh.start(new Stage()); - } catch (Exception e) { - e.printStackTrace(); - } + String[] userIdList = JDMethodUtil.getPrefStrArray("JD2_CSXWH", session); + Boolean tag = true; + for (int i = 0; i < userIdList.length; i++) { + try { + if (session.getUser().getUserId().equals(userIdList[i])) { + tag = false; + new Thread(() -> { + new JFXPanel(); + Platform.setImplicitExit(false); + Platform.runLater(new Runnable() { + @Override + public void run() { + try { + CSXWHDialog csxwh = new CSXWHDialog(); + csxwh.start(new Stage()); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + }).start(); } - }); - }).start(); + } catch (TCException e) { + e.printStackTrace(); + } + } + if (tag) { + MessageBox.post("没有开启权限,请联系管理员", "提示", MessageBox.INFORMATION); + } + return null; } diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/utils/DBConnectionUtil.java b/com.connor.jd.plm/src/com/connor/jd/plm/utils/DBConnectionUtil.java new file mode 100644 index 0000000..bcef275 --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/plm/utils/DBConnectionUtil.java @@ -0,0 +1,31 @@ +package com.connor.jd.plm.utils; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DBConnectionUtil { + + public static Connection dbConn(String name, String pass) { + Connection c = null; + try { + Class.forName("oracle.jdbc.driver.OracleDriver"); + // 要是导入驱动没有成功的话都是会出现classnotfoundException.自己看看是不是哪里错了,例如classpath这些设置 + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + try { + c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:TC12", name, pass); + // 连接数据的方法有四种, 这个属于最简单的,一般用网页程序 TC是你的数据库实例名称,在下载的文件test.sql中可以执行语句查看 + // "jdbc:oracle:thin:@计算机名称:监听端口:系统实例名", username, password, + // 计算机名称,要是自己不知道可以在计算机属性查知. + // 监听端口一般默认是1521, 要是改变了就看自己的监听文件listener.ora + // 系统实例名一般是默认orcl, 要是不是的话就用 select name from v$database; + // username,password,就是登陆数据库的用户名和密码. + + } catch (SQLException e) { + e.printStackTrace(); + } + return c; + } +}