- Hlavní stránka
- Balíky
- Třídy
- Soubory

# src/cz/neubauer/tomas/hazardanalysis/model/dangers/DangerGroup.java

Zobrazit dokumentaci tohoto souboru.

    00001
    00004 package cz.neubauer.tomas.hazardanalysis.model.dangers;
    00005
    00006 import java.sql.Connection;
    00007 import java.sql.ResultSet;
    00008 import java.sql.SQLException;
    00009 import java.sql.Statement;
    00010 import java.util.ArrayList;
    00011
    00017 public class DangerGroup {
    00023         private static int last_insert_id = -1;
    00024
    00028         private int id;
    00029
    00033         private String name;
    00034
    00038         private ArrayList<DangerType> types;
    00039
    00048         public DangerGroup(int id, String name) {
    00049                 this.id = id;
    00050                 this.name = name;
    00051                 types = new ArrayList<DangerType>(10);
    00052         }
    00053
    00059         public String getName() {
    00060                 return name;
    00061         }
    00062
    00069         public void setName(String name) {
    00070                 this.name = name;
    00071         }
    00072
    00079         public void addDangerType(DangerType dangerType) {
    00080                 types.add(dangerType);
    00081         }
    00082
    00090         public DangerType getDangerType(int index) {
    00091                 return types.get(index);
    00092         }
    00093
    00099         public int getDangerTypeCount() {
    00100                 return types.size();
    00101         }
    00102
    00111         public void saveToDB(Connection connection) {
    00112                 if (connection != null) {
    00113                         try {
    00114                                 Statement stmt = connection.createStatement();
    00115                                 if (last_insert_id < 1) {
    00116                                         stmt.executeQuery("SELECT max(id) FROM danger_group;");
    00117                                         ResultSet rs = stmt.getResultSet();
    00118                                         if (rs.next()) {
    00119                                                 last_insert_id = rs.getInt(1);
    00120                                         }
    00121                                 }
    00122                                 stmt
    00123                                                 .executeQuery("SELECT count(id) as count FROM danger_group WHERE id = '"
    00124                                                                 + id + "';");
    00125                                 ResultSet rs1 = stmt.getResultSet();
    00126                                 if (rs1.next() && rs1.getInt(1) == 0) {
    00127                                         stmt.executeUpdate("INSERT INTO danger_group VALUES ("
    00128                                                         + (last_insert_id > 0 ? "'" + (++last_insert_id)
    00129                                                                         + "'" : "LAST_INSERT_ID()") + ", '" + name
    00130                                                         + "');", Statement.RETURN_GENERATED_KEYS);
    00131                                         ResultSet rs = stmt.getGeneratedKeys();
    00132                                         if (rs.next()) {
    00133                                                 id = rs.getInt(1);
    00134                                                 last_insert_id = id;
    00135                                         }
    00136                                 } else {
    00137                                         stmt.executeUpdate("UPDATE danger_group SET name='" + name
    00138                                                         + "' WHERE id='" + id + "';");
    00139                                 }
    00140                                 stmt.close();
    00141                         } catch (SQLException e) {
    00142                                 e.printStackTrace();
    00143                         }
    00144                 }
    00145         }
    00146
    00154         public void deleteFromDB(Connection connection) {
    00155                 if (connection != null) {
    00156                         try {
    00157                                 // smazání všech typů, které patří do skupiny
    00158                                 for (int i = 0; i < types.size(); i++) {
    00159                                         types.get(i).deleteFromDB(connection);
    00160                                 }
    00161
    00162                                 // vlastní smazání skupiny
    00163                                 Statement stmt = connection.createStatement();
    00164                                 stmt.executeUpdate("DELETE FROM danger_group WHERE id = '" + id
    00165                                                 + "';");
    00166
    00167                         } catch (SQLException e) {
    00168                                 e.printStackTrace();
    00169                         }
    00170
    00171                 }
    00172         }
    00173
    00179         public int getId() {
    00180                 return id;
    00181         }
    00182
    00189         public void setId(int id) {
    00190                 this.id = id;
    00191         }
    00192
    00198         public ArrayList<DangerType> getTypes() {
    00199                 return types;
    00200         }
    00201
    00208         public void setTypes(ArrayList<DangerType> types) {
    00209                 this.types = types;
    00210         }
    00211
    00212         /*
    00213          * (non-Javadoc)
    00214          *
    00215          * @see java.lang.Object#toString()
    00216          */
    00217         @Override
    00218         public String toString() {
    00219                 return name;
    00220         }
    00221
    00222         /*
    00223          * (non-Javadoc)
    00224          *
    00225          * @see java.lang.Object#equals(java.lang.Object)
    00226          */
    00227         @Override
    00228         public boolean equals(Object obj) {
    00229                 if (obj instanceof DangerGroup) {
    00230                         return ((DangerGroup) obj).getId() == id;
    00231                 }
    00232                 return false;
    00233         }
    00234 }