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

# src/cz/neubauer/tomas/hazardanalysis/model/dangers/DangerVariant.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
    00016 public class DangerVariant implements Comparable<DangerVariant> {
    00022         private static int last_insert_id = -1;
    00023
    00027         private int id;
    00028
    00032         private String description;
    00033
    00037         private boolean critical;
    00038
    00042         private boolean selected;
    00043
    00047         private DangerType dangerType;
    00048
    00059         public DangerVariant(DangerType owner, int id, String description) {
    00060                 this.id = id;
    00061                 dangerType = owner;
    00062                 this.description = description;
    00063                 critical = false;
    00064                 selected = false;
    00065         }
    00066
    00072         public String getDescription() {
    00073                 return description;
    00074         }
    00075
    00082         public void setDescription(String description) {
    00083                 this.description = description;
    00084         }
    00085
    00091         public DangerType getDangerType() {
    00092                 return dangerType;
    00093         }
    00094
    00101         public void setDangerType(DangerType dangerType) {
    00102                 this.dangerType = dangerType;
    00103         }
    00104
    00110         public boolean isCritical() {
    00111                 return critical;
    00112         }
    00113
    00120         public void setCritical(boolean critical) {
    00121                 this.critical = critical;
    00122         }
    00123
    00129         public int getId() {
    00130                 return id;
    00131         }
    00132
    00139         public void setId(int id) {
    00140                 this.id = id;
    00141         }
    00142
    00151         public void saveToDB(Connection con) {
    00152                 if (con != null) {
    00153                         try {
    00154                                 Statement stmt = con.createStatement();
    00155                                 if (last_insert_id < 1) {
    00156                                         stmt.executeQuery("SELECT max(id) FROM danger_variant;");
    00157                                         ResultSet rs = stmt.getResultSet();
    00158                                         if (rs.next()) {
    00159                                                 last_insert_id = rs.getInt(1);
    00160                                         }
    00161                                 }
    00162                                 stmt
    00163                                                 .executeQuery("SELECT count(id) as count FROM danger_variant WHERE id = '"
    00164                                                                 + id + "';");
    00165                                 ResultSet rs1 = stmt.getResultSet();
    00166                                 if (rs1.next() && rs1.getInt(1) == 0) {
    00167                                         stmt.executeUpdate(
    00168                                                         "INSERT INTO danger_variant VALUES ("
    00169                                                                         + (last_insert_id > 0 ? "'"
    00170                                                                                         + (++last_insert_id) + "'"
    00171                                                                                         : "LAST_INSERT_ID()") + ", '"
    00172                                                                         + dangerType.getId() + "', '" + description
    00173                                                                         + "');", Statement.RETURN_GENERATED_KEYS);
    00174                                         ResultSet rs = stmt.getGeneratedKeys();
    00175                                         if (rs.next()) {
    00176                                                 id = rs.getInt(1);
    00177                                                 last_insert_id = id;
    00178                                         }
    00179                                 } else {
    00180                                         stmt
    00181                                                         .executeUpdate("UPDATE danger_variant SET danger_type_id='"
    00182                                                                         + dangerType.getId()
    00183                                                                         + "', description = '"
    00184                                                                         + description + "' WHERE id='" + id + "';");
    00185                                 }
    00186                                 stmt.close();
    00187                         } catch (SQLException e) {
    00188                                 e.printStackTrace();
    00189                         }
    00190                 }
    00191         }
    00192
    00200         public void deleteFromDB(Connection con) {
    00201                 if (con != null) {
    00202                         Statement stmt;
    00203                         try {
    00204                                 stmt = con.createStatement();
    00205                                 // smazání přiřazení varianty komponentám
    00206
    00207                                 stmt
    00208                                                 .executeUpdate("DELETE FROM component_danger_variant WHERE danger_variant_id = '"
    00209                                                                 + id + "';");
    00210
    00211                                 stmt.executeUpdate("DELETE FROM danger_variant WHERE id = '"
    00212                                                 + id + "';");
    00213                                 stmt.close();
    00214                                 finalize();
    00215                         } catch (SQLException e) {
    00216                                 e.printStackTrace();
    00217                         } catch (Throwable e) {
    00218                                 e.printStackTrace();
    00219                         }
    00220                 }
    00221         }
    00222
    00223         /*
    00224          * (non-Javadoc)
    00225          *
    00226          * @see java.lang.Object#toString()
    00227          */
    00228         @Override
    00229         public String toString() {
    00230                 return description;
    00231         }
    00232
    00233         /*
    00234          * (non-Javadoc)
    00235          *
    00236          * @see java.lang.Object#equals(java.lang.Object)
    00237          */
    00238         @Override
    00239         public boolean equals(Object obj) {
    00240                 if (obj instanceof DangerVariant) {
    00241                         return ((DangerVariant) obj).getId() == id;
    00242                 }
    00243                 return false;
    00244         }
    00245
    00253         public boolean isSelected() {
    00254                 return selected;
    00255         }
    00256
    00264         public void setSelected(boolean selected) {
    00265                 this.selected = selected;
    00266         }
    00267
    00268         /*
    00269          * (non-Javadoc)
    00270          *
    00271          * @see java.lang.Comparable#compareTo(java.lang.Object)
    00272          */
    00273         @Override
    00274         public int compareTo(DangerVariant o) {
    00275                 if (id < o.id) {
    00276                         return -1;
    00277                 } else if (equals(o)) {
    00278                         return 0;
    00279                 } else {
    00280                         return 1;
    00281                 }
    00282         }
    00283 }