if (r.getRole().equals(RelationshipData.SUPERIOR)) { substitutions.put(":role:", sqlQuote("Subordinate")); } else { substitutions.put(":role:", sqlQuote(r.getRole()));
long startTime = r.getStartTime(); long endTime = r.getEndTime(); substitutions.put(":role:", sqlQuote(r.getRole())); substitutions.put(":supporting:", getComponentAlibId(data)); substitutions.put(":supported:", getAgentAlibId(r.getSupported())); substitutions.put(":start_date:", "?"); substitutions.put(":end_date:", "?"); String query = dbp.getQuery(CHECK_RELATIONSHIP, substitutions); PreparedStatement pstmt = dbConnection.prepareStatement(query); pstmt.setTimestamp(1, new Timestamp(r.getStartTime())); ResultSet rs = pstmt.executeQuery(); if (!rs.next()) { query = dbp.getQuery(INSERT_RELATIONSHIP, substitutions); PreparedStatement pstmt2 = dbConnection.prepareStatement(query); pstmt2.setTimestamp(1, new Timestamp(startTime)); if (endTime > 0L) { pstmt2.setTimestamp(2, new Timestamp(endTime)); } else { pstmt2.setNull(2, Types.TIMESTAMP); } pstmt2.executeUpdate(); pstmt2.close();
    private void populateAgent(ComponentData data, boolean isAdded) throws SQLException {        AgentAssetData assetData = data.getAgentAssetData();        if (assetData == null) return;        substitutions.put(":component_lib_id:", getComponentLibId(data));        substitutions.put(":agent_org_prototype:", sqlQuote(assetData.getAssetClass()));        if (isAdded) {            // finish populating a new agent            executeUpdate(stmt, dbp.getQuery(INSERT_AGENT_ORG, substitutions));            PropGroupData[] pgs = assetData.getPropGroups();            for (int i = 0; i < pgs.length; i++) {                PropGroupData pg = pgs[i];                String pgName = pg.getName();                PGPropData[] props = pg.getProperties();                for (int j = 0; j < props.length; j++) {                    PGPropData prop = props[i];                    PropertyInfo propInfo = getPropertyInfo(pgName, prop.getName());                    substitutions.put(":component_alib_id:", getComponentAlibId(data));                    substitutions.put(":pg_attribute_lib_id:", propInfo.getAttributeLibId());                    substitutions.put(":start_date:", sqlQuote("2000-01-01 00:00:00"));                    substitutions.put(":end_date:", sqlQuote(null));                    if (propInfo.isCollection()) {                        if (!prop.isListType())                            throw new RuntimeException("Property is not a collection: "                                                       + propInfo.toString());                        String[] values = ((PGPropMultiVal) prop.getValue()).getValuesStringArray();                        for (int k = 0; k < values.length; k++) {                            substitutions.put(":attribute_value:", sqlQuote(values[k]));                            substitutions.put(":attribute_order:", String.valueOf(k + 1));                            executeUpdate(stmt, dbp.getQuery(INSERT_ATTRIBUTE, substitutions));                        }                    } else {                        if (prop.isListType())                            throw new RuntimeException("Property is not a single value: "                                                       + propInfo.toString());                        substitutions.put(":attribute_value:", sqlQuote(prop.getValue().toString()));                        substitutions.put(":attribute_order:", "1");                        executeUpdate(stmt, dbp.getQuery(INSERT_ATTRIBUTE, substitutions));                    }                }            }        }        RelationshipData[] relationships = assetData.getRelationshipData();        for (int i = 0; i < relationships.length; i++) {            RelationshipData r = relationships[i];            if (r.getRole().equals(RelationshipData.SUPERIOR)) {                substitutions.put(":role:", sqlQuote("Subordinate"));            } else {                substitutions.put(":role:", sqlQuote(r.getRole()));            }            substitutions.put(":supporting:", getComponentAlibId(data));            substitutions.put(":supported:", getAgentAlibId(r.getCluster()));            substitutions.put(":start_date:", sqlQuote(r.getStartTime()));            substitutions.put(":end_date:", sqlQuote(r.getStopTime()));            ResultSet rs = executeQuery(stmt, dbp.getQuery(CHECK_RELATIONSHIP, substitutions));            if (!rs.next())                executeUpdate(stmt, dbp.getQuery(INSERT_RELATIONSHIP, substitutions));        }    }