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

# src/cz/neubauer/tomas/hazardanalysis/model/components/Component.java

Zobrazit dokumentaci tohoto souboru.

    00001
    00004 package cz.neubauer.tomas.hazardanalysis.model.components;
    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
    00012 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerGroup;
    00013 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerType;
    00014 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
    00015
    00021 public class Component {
    00027         private static int last_insert_id = -1;
    00028
    00032         private int id;
    00033
    00037         private String name;
    00038
    00042         private String location;
    00043
    00048         private ArrayList<DangerGroup> dangerGroups;
    00049
    00060         public Component(int id, String name, String location) {
    00061                 this.id = id;
    00062                 this.name = name;
    00063                 this.location = location;
    00064                 dangerGroups = new ArrayList<DangerGroup>(10);
    00065         }
    00066
    00072         public String getName() {
    00073                 return name;
    00074         }
    00075
    00082         public void setName(String name) {
    00083                 this.name = name;
    00084         }
    00085
    00092         public void addDangerGroup(DangerGroup danger) {
    00093                 dangerGroups.add(danger);
    00094         }
    00095
    00104         public DangerGroup getDangerGroup(int index) {
    00105                 return dangerGroups.get(index);
    00106         }
    00107
    00113         public int getDangerGroupCount() {
    00114                 return dangerGroups.size();
    00115         }
    00116
    00122         public ArrayList<DangerType> getDangerTypes() {
    00123                 ArrayList<DangerType> types = new ArrayList<DangerType>(10);
    00124                 int dangerGroupCount = dangerGroups.size();
    00125                 for (int i = 0; i < dangerGroupCount; i++) {
    00126                         DangerGroup currentDangerGroup = dangerGroups.get(i);
    00127                         int dangerTypeCount = currentDangerGroup.getDangerTypeCount();
    00128                         for (int j = 0; j < dangerTypeCount; j++) {
    00129                                 DangerType currentDangerType = currentDangerGroup
    00130                                                 .getDangerType(j);
    00131                                 types.add(currentDangerType);
    00132                         }
    00133                 }
    00134                 return types;
    00135         }
    00136
    00143         public String getDangersAsString() {
    00144                 String retVal = "";
    00145                 int dangerGroupCount = dangerGroups.size();
    00146                 int count = 0;
    00147                 for (int i = 0; i < dangerGroupCount; i++) {
    00148                         DangerGroup currentDangerGroup = dangerGroups.get(i);
    00149                         int dangerTypeCount = currentDangerGroup.getDangerTypeCount();
    00150                         for (int j = 0; j < dangerTypeCount; j++) {
    00151                                 DangerType currentDangerType = currentDangerGroup
    00152                                                 .getDangerType(j);
    00153                                 if (count > 0) {
    00154                                         retVal += ", ";
    00155                                 }
    00156                                 retVal += currentDangerType.getDescription();
    00157                                 count++;
    00158                         }
    00159                 }
    00160                 return retVal;
    00161         }
    00162
    00168         public int getId() {
    00169                 return id;
    00170         }
    00171
    00178         public void setId(int id) {
    00179                 this.id = id;
    00180         }
    00181
    00187         public ArrayList<DangerGroup> getDangerGroups() {
    00188                 return dangerGroups;
    00189         }
    00190
    00197         public void setDangerGroups(ArrayList<DangerGroup> dangerGroups) {
    00198                 this.dangerGroups = dangerGroups;
    00199         }
    00200
    00209         public void saveToDB(Connection connection) {
    00210                 if (connection != null) {
    00211                         try {
    00212                                 Statement stmt = connection.createStatement();
    00213                                 if (last_insert_id < 1) {
    00214                                         stmt.executeQuery("SELECT max(id) FROM component;");
    00215                                         ResultSet rs = stmt.getResultSet();
    00216                                         if (rs.next()) {
    00217                                                 last_insert_id = rs.getInt(1);
    00218                                         }
    00219                                 }
    00220                                 stmt
    00221                                                 .executeQuery("SELECT count(id) as count FROM component WHERE id = '"
    00222                                                                 + id + "';");
    00223                                 ResultSet rs1 = stmt.getResultSet();
    00224                                 if (rs1.next() && rs1.getInt(1) == 0) {
    00225                                         stmt.executeUpdate("INSERT INTO component VALUES ("
    00226                                                         + (last_insert_id > 0 ? "'" + (++last_insert_id)
    00227                                                                         + "'" : "LAST_INSERT_ID()") + ", '" + name
    00228                                                         + "', '" + location + "');",
    00229                                                         Statement.RETURN_GENERATED_KEYS);
    00230                                 } else {
    00231
    00232                                         stmt.executeUpdate("UPDATE component SET name='" + name
    00233                                                         + "', location='" + location + "' WHERE id='" + id
    00234                                                         + "';");
    00235                                 }
    00236                                 stmt.close();
    00237                         } catch (SQLException e) {
    00238                                 e.printStackTrace();
    00239                         }
    00240                 }
    00241         }
    00242
    00250         public void deleteFromDB(Connection connection) {
    00251                 if (connection != null) {
    00252                         try {
    00253                                 Statement stmt = connection.createStatement();
    00254
    00255                                 stmt.executeUpdate("DELETE FROM component WHERE id = '" + id
    00256                                                 + "';");
    00257
    00258                                 stmt
    00259                                                 .executeUpdate("DELETE FROM component_danger_variant WHERE component_id = '"
    00260                                                                 + id + "';");
    00261                                 stmt.close();
    00262                                 finalize();
    00263                         } catch (SQLException e) {
    00264                                 e.printStackTrace();
    00265                         } catch (Throwable e) {
    00266                                 e.printStackTrace();
    00267                         }
    00268                 }
    00269         }
    00270
    00280         public boolean hasDangerVariant(DangerVariant dangerVariant) {
    00281
    00282                 int dangerGroupCount = dangerGroups.size();
    00283                 DangerGroup currentDangerGroup;
    00284                 for (int i = 0; i < dangerGroupCount; i++) {
    00285                         currentDangerGroup = dangerGroups.get(i);
    00286                         DangerType dangerType = dangerVariant.getDangerType();
    00287                         if (dangerType.getDangerGroup().getId() == currentDangerGroup
    00288                                         .getId()) {
    00289                                 ArrayList<DangerType> dangerTypes = currentDangerGroup
    00290                                                 .getTypes();
    00291                                 int dangerTypeCount = dangerTypes.size();
    00292                                 DangerType currentDangerType;
    00293                                 for (int j = 0; j < dangerTypeCount; j++) {
    00294                                         currentDangerType = dangerTypes.get(j);
    00295                                         if (dangerVariant.getDangerType().getId() == currentDangerType
    00296                                                         .getId()) {
    00297                                                 ArrayList<DangerVariant> dangerVariants = currentDangerType
    00298                                                                 .getVariants();
    00299                                                 int dangerVariantCount = dangerVariants.size();
    00300                                                 DangerVariant currentDangerVariant;
    00301                                                 for (int k = 0; k < dangerVariantCount; k++) {
    00302                                                         currentDangerVariant = dangerVariants.get(k);
    00303                                                         if (currentDangerVariant.getId() == dangerVariant
    00304                                                                         .getId()) {
    00305                                                                 return true;
    00306                                                         }
    00307                                                 }
    00308                                         }
    00309                                 }
    00310                         }
    00311                 }
    00312                 return false;
    00313         }
    00314
    00320         public ArrayList<DangerVariant> getDangerVariants() {
    00321                 ArrayList<DangerVariant> variants = new ArrayList<DangerVariant>(100);
    00322                 int dangerGroupCount = dangerGroups.size();
    00323                 DangerGroup dg;
    00324                 DangerType dt;
    00325                 for (int i = 0; i < dangerGroupCount; i++) {
    00326                         dg = dangerGroups.get(i);
    00327                         int dtc = dg.getDangerTypeCount();
    00328
    00329                         for (int j = 0; j < dtc; j++) {
    00330                                 dt = dg.getDangerType(j);
    00331                                 int dvc = dt.getDangerVariantCount();
    00332                                 for (int k = 0; k < dvc; k++) {
    00333                                         variants.add(dt.getDangerVariant(k));
    00334                                 }
    00335                         }
    00336                 }
    00337
    00338                 return variants;
    00339         }
    00340
    00347         public ArrayList<DangerVariant> getCriticalDangerVariants() {
    00348                 ArrayList<DangerVariant> variants = new ArrayList<DangerVariant>(100);
    00349                 int dangerGroupCount = dangerGroups.size();
    00350                 DangerGroup dg;
    00351                 DangerType dt;
    00352                 DangerVariant dv;
    00353                 for (int i = 0; i < dangerGroupCount; i++) {
    00354                         dg = dangerGroups.get(i);
    00355                         int dtc = dg.getDangerTypeCount();
    00356
    00357                         for (int j = 0; j < dtc; j++) {
    00358                                 dt = dg.getDangerType(i);
    00359                                 int dvc = dt.getDangerVariantCount();
    00360                                 for (int k = 0; k < dvc; k++) {
    00361                                         dv = dt.getDangerVariant(k);
    00362                                         if (dv.isCritical()) {
    00363                                                 variants.add(dv);
    00364                                         }
    00365                                 }
    00366                         }
    00367                 }
    00368
    00369                 return variants;
    00370         }
    00371
    00372         /*
    00373          * (non-Javadoc)
    00374          *
    00375          * @see java.lang.Object#toString()
    00376          */
    00377         @Override
    00378         public String toString() {
    00379                 return name;
    00380         }
    00381
    00387         public String getLocation() {
    00388                 return location;
    00389         }
    00390
    00397         public void setLocation(String location) {
    00398                 this.location = location;
    00399         }
    00400 }