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

# src/cz/neubauer/tomas/hazardanalysis/model/components/ComponentCatalog.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
    00022 public class ComponentCatalog {
    00023
    00027         private ArrayList<Component> components;
    00028
    00032         public ComponentCatalog() {
    00033                 components = new ArrayList<Component>(10);
    00034         }
    00035
    00042         public void addComponent(Component component) {
    00043                 components.add(component);
    00044         }
    00045
    00054         public void setComponent(int index, Component component) {
    00055                 components.set(index, component);
    00056         }
    00057
    00065         public Component getComponent(int index) {
    00066                 return components.get(index);
    00067         }
    00068
    00077         public Component getComponentByName(String name) {
    00078                 int count = getCount();
    00079
    00080                 Component c;
    00081                 for (int i = 0; i < count; i++) {
    00082                         c = getComponent(i);
    00083                         if (c.getName().compareToIgnoreCase(name) == 0) {
    00084                                 return c;
    00085                         }
    00086                 }
    00087                 return null;
    00088         }
    00089
    00095         public int getCount() {
    00096                 return components.size();
    00097         }
    00098
    00105         public void loadFromDB(Connection connection) {
    00106                 if (connection != null) {
    00107                         try {
    00108                                 Statement stmt = connection.createStatement();
    00109                                 Statement stmt2 = connection.createStatement();
    00110                                 ResultSet rs = stmt.executeQuery("SELECT * FROM component;");
    00111
    00112                                 components.clear();
    00113                                 String componentName, componentLocation;
    00114                                 int componentId;
    00115                                 ResultSet rs2;
    00116                                 while (rs.next()) {
    00117                                         componentId = rs.getInt("id");
    00118                                         componentName = rs.getString("name");
    00119                                         componentLocation = rs.getString("location");
    00120
    00121                                         rs2 = stmt2
    00122                                                         .executeQuery("select"
    00123                                                                         + " dg.id as dg_id,"
    00124                                                                         + " dg.name as dg_name,"
    00125                                                                         + " dt.id as dt_id,"
    00126                                                                         + " dt.code as dt_code,"
    00127                                                                         + " dt.description as dt_description,"
    00128                                                                         + " dt.column_cast2_a1_1995 as dt_column_cast2_a1_1995,"
    00129                                                                         + " dt.column_cast1_r1991 as dt_column_cast1_R1991,"
    00130                                                                         + " dt.column_cast2_r1991 as dt_column_cast2_R1991,"
    00131                                                                         + " dt.column_csn_en_iso_12100_1_2003 as dt_column_csn_en_iso_12100_1_2003,"
    00132                                                                         + " dt.column_csn_en_iso_12100_2_2003 as dt_column_csn_en_iso_12100_2_2003,"
    00133                                                                         + " dt.column_normy_typu_b as dt_column_normy_typu_b,"
    00134                                                                         + " dt.column_clanky_csn_en_12415_2001 as dt_column_clanky_csn_en_12415_2001,"
    00135                                                                         + " dt.situation_description as dt_situation_description,"
    00136                                                                         + " dv.id as dv_id,"
    00137                                                                         + " dv.danger_type_id as dv_danger_type_id,"
    00138                                                                         + " dv.description as dv_description"
    00139                                                                         + " from ((component_danger_variant cdv join danger_variant dv on ( cdv.danger_variant_id = dv.id)) join danger_type dt on (dv.danger_type_id = dt.id)) join danger_group dg on (dt.danger_group_id = dg.id) where cdv.component_id = '"
    00140                                                                         + componentId + "';");
    00141
    00142                                         Component newComponent = new Component(componentId,
    00143                                                         componentName, componentLocation);
    00144                                         int oldDGID = 0, currentDGID;
    00145                                         int oldDTID = 0, currentDTID;
    00146                                         int oldDVID = 0, currentDVID;
    00147                                         DangerGroup dg = null;
    00148                                         DangerType dt = null;
    00149                                         DangerVariant dv = null;
    00150                                         while (rs2.next()) {
    00151                                                 currentDGID = rs2.getInt("dg_id");
    00152                                                 if (currentDGID != oldDGID) {
    00153                                                         dg = new DangerGroup(currentDGID, rs2
    00154                                                                         .getString("dg_name"));
    00155                                                         newComponent.addDangerGroup(dg);
    00156                                                         oldDGID = currentDGID;
    00157                                                 }
    00158
    00159                                                 currentDTID = rs2.getInt("dt_id");
    00160                                                 if (currentDTID != oldDTID) {
    00161                                                         dt = new DangerType(
    00162                                                                         dg,
    00163                                                                         currentDTID,
    00164                                                                         rs2.getString("dt_code"),
    00165                                                                         rs2.getString("dt_description"),
    00166                                                                         rs2.getString("dt_column_cast1_r1991"),
    00167                                                                         rs2.getString("dt_column_cast2_r1991"),
    00168                                                                         rs2
    00169                                                                                         .getString("dt_column_csn_en_iso_12100_1_2003"),
    00170                                                                         rs2
    00171                                                                                         .getString("dt_column_csn_en_iso_12100_2_2003"),
    00172                                                                         rs2
    00173                                                                                         .getString("dt_column_csn_en_iso_12100_2_2003"),
    00174                                                                         rs2.getString("dt_column_normy_typu_b"),
    00175                                                                         rs2
    00176                                                                                         .getString("dt_column_clanky_csn_en_12415_2001"),
    00177                                                                         rs2.getString("dt_situation_description"));
    00178                                                         dg.addDangerType(dt);
    00179                                                         oldDTID = currentDTID;
    00180                                                 }
    00181
    00182                                                 currentDVID = rs2.getInt("dv_id");
    00183                                                 if (currentDVID != oldDVID) {
    00184                                                         dv = new DangerVariant(dt, rs2.getInt("dv_id"), rs2
    00185                                                                         .getString("dv_description"));
    00186                                                         dt.addDangerVariant(dv);
    00187                                                         oldDVID = currentDVID;
    00188                                                 }
    00189                                         }
    00190                                         addComponent(newComponent);
    00191                                 }
    00192                                 stmt2.close();
    00193                                 stmt.close();
    00194                         } catch (SQLException e) {
    00195                                 e.printStackTrace();
    00196                         }
    00197                 }
    00198
    00199         }
    00200
    00204         public void clear() {
    00205                 components.clear();
    00206         }
    00207
    00213         public ArrayList<Component> getComponents() {
    00214                 return components;
    00215         }
    00216
    00223         public ArrayList<DangerVariant> getCriticalVariants() {
    00224                 ArrayList<DangerVariant> criticalVariants = new ArrayList<DangerVariant>(
    00225                                 10);
    00226
    00227                 int componentCount = getCount();
    00228                 Component c;
    00229                 for (int i = 0; i < componentCount; i++) {
    00230                         c = getComponent(i);
    00231
    00232                         int groupCount = c.getDangerGroupCount();
    00233                         DangerGroup dg;
    00234                         for (int j = 0; j < groupCount; j++) {
    00235                                 dg = c.getDangerGroup(j);
    00236
    00237                                 int typeCount = dg.getDangerTypeCount();
    00238                                 DangerType dt;
    00239                                 for (int k = 0; k < typeCount; k++) {
    00240                                         dt = dg.getDangerType(k);
    00241
    00242                                         int variantCount = dt.getDangerVariantCount();
    00243                                         DangerVariant dv;
    00244                                         for (int l = 0; l < variantCount; l++) {
    00245                                                 dv = dt.getDangerVariant(l);
    00246
    00247                                                 if (dv.isCritical()) {
    00248                                                         criticalVariants.add(dv);
    00249                                                 }
    00250                                         }
    00251                                 }
    00252                         }
    00253                 }
    00254
    00255                 return criticalVariants;
    00256         }
    00257 }