executeUpdate(dbp.getQuery("insertAttribute", substitutions));
String insertQuery = dbp.getQuery("insertAttribute", substitutions); if (log.isDebugEnabled()) log.debug("InsertPGAttr query: " + insertQuery); executeUpdate(insertQuery);
  private void populateAgent(ComponentData data, boolean isAdded) throws SQLException {          if(log.isDebugEnabled()) {      log.debug("populating Agent: " + data.getName());    }    AgentAssetData assetData = data.getAgentAssetData();    if (assetData == null) return;    substitutions.put(":agent_org_class:", sqlQuote(assetData.getAssetClass()));    substitutions.put(":agent_lib_name:", sqlQuote(data.getName()));    substitutions.put(":component_name:", sqlQuote(data.getName()));    // Only put in the PGs if the Agent is new    if (isAdded) {      // finish populating a new agent      Statement stmt = getStatement();      ResultSet rs = executeQuery(stmt, dbp.getQuery("checkAgentOrg", substitutions));      if (!rs.next()) {	if (log.isDebugEnabled()) {	  log.debug("pAgent: insertingAgentOrg ag: " + data.getName() + ", orgtype: " + assetData.getAssetClass());	}	executeUpdate(dbp.getQuery("insertAgentOrg", substitutions));      }      rs.close();      stmt.close();      stmt = getStatement();      rs = executeQuery(stmt, dbp.getQuery("checkAsbAgent", substitutions));      if (!rs.next()) {	if (log.isDebugEnabled()) {	  log.debug("pAgent " + data.getName() + " inserting asb_agent under assembly " + substitutions.get(":assembly_id:"));	}	executeUpdate(dbp.getQuery("insertAsbAgent", substitutions));      }      rs.close();      stmt.close();      // Now the Property Groups      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 each property in the property group	for (int j = 0; j < props.length; j++) {	  PGPropData prop = props[j];	  PropertyInfo propInfo = getPropertyInfo(pgName, prop.getName());          if(propInfo == null) {            // This is a new PG, and must be stored in the db.            setPropertyInfo(pgName, prop.getName(), prop.getType(), prop.getSubType());            propInfo = getPropertyInfo(pgName, prop.getName());            if(log.isErrorEnabled() && propInfo == null) {              log.error("Error: Cannot obtain property info for: " +                         pgName + ", " + prop.getName());            }          }// 	  if (log.isDebugEnabled()) {// 	    log.debug("Pag for " + data.getName() + " at PropGroupData " + i + " with name " + pgName + " has " + props.length + " props, of which #" + j + " is " + propInfo.toString());// 	  }	  substitutions.put(":component_alib_id:", sqlQuote(getComponentAlibId(data)));	  substitutions.put(":pg_attribute_lib_id:", sqlQuote(propInfo.getAttributeLibId()));	  // FIXME: Are we trying to support time-phase PGs?	  // Are these defaults?	  substitutions.put(":start_date:", sqlQuote("2000-01-01 00:00:00"));	  substitutions.put(":end_date:", sqlQuote(null));	  // If there are multiple values for this property	  if (propInfo.isCollection()) {	    if (!prop.isListType())	      throw new RuntimeException("Property is not a collection: "					 + propInfo.toString());	    // For each value of this multiple valued property	    String[] values = ((PGPropMultiVal) prop.getValue()).getValuesStringArray();// 	    if (log.isDebugEnabled()) {// 	      log.debug("pag for agent " + data.getName() + " and alibid " + propInfo.getAttributeLibId() + " has " + values.length + " values");// 	      for (int k = 0; k < values.length; k++) {// 		log.debug("Value " + k + " = " + values[k]);// 	      }// 	    }	    for (int k = 0; k < values.length; k++) {	      substitutions.put(":attribute_value:", sqlQuote(values[k]));	      substitutions.put(":attribute_order:", String.valueOf(k));	      // if val not already there for this runtime configuration	      stmt = getStatement();	      ResultSet rschck = executeQuery(stmt, dbp.getQuery("checkAttribute", substitutions));	      if (! rschck.next()) {		// Query the values for this PG slot that have order of 0		// If there is more than one,		// and one of the values is this one,		// then dont insert it again - CMT asbs do this		Statement stmt2 = getStatement();		ResultSet rschck2 = executeQuery(stmt2, dbp.getQuery("queryAttributeValueZeroOrder", substitutions));		int count = 0; // Number of values at order 0		boolean hasval = false; // this value already in DB		while (rschck2.next()) {		  count++;		  if (values[k].equals(rschck2.getString(1)))		    hasval = true; // the value we were inserting is there		}		rschck2.close();		stmt2.close();		// If the only value is already there,		// or if there are multiple values		// all at order 0, including this one, dont add it again		if ((count > 1 && hasval) || (count == 1 && values.length == 1 && hasval)) {		  // Dont bother inserting again// 		  if (log.isDebugEnabled()) {// 		    log.debug("PopAgent not inserting PG multival " + values[k] + " for pagent " + data.getName() + " under attlibid " + propInfo.getAttributeLibId() + ", cause already have " + count + " values all with order 0, including this one.");// 		  }		} else {		  		  if (log.isDebugEnabled()) {		    log.debug("pagent " + data.getName() + " inserting pgval under pgattid " + propInfo.getAttributeLibId() + ", val " + values[k] + " into assembly " + substitutions.get(":assembly_id:"));		  }		  executeUpdate(dbp.getQuery("insertAttribute", substitutions));		} // end of block to really insert multival again	      } // end of loop where this multival not there yet	      rschck.close();	      stmt.close();	    }	  } 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:", "0");	    // if val not already there for this runtime	    // if val not already there for this runtime	    stmt = getStatement();	    ResultSet rschck = executeQuery(stmt, dbp.getQuery("checkAttribute", substitutions));	    if (! rschck.next()) {	      if (log.isDebugEnabled()) {		log.debug("pagent " + data.getName() + " inserting pgval under pgattid " + propInfo.getAttributeLibId() + ", val " + prop.getValue().toString() + " into assembly " + substitutions.get(":assembly_id:"));	      }	      executeUpdate(dbp.getQuery("insertAttribute", substitutions));	    }	    rschck.close();	    stmt.close();	  }	} // loop over properties in pg      } // loop over PGs    }    // Add relationships even to Agents that    // aren't new here    RelationshipData[] relationships = assetData.getRelationshipData();    //        dbp.setDebug(true);    for (int i = 0; i < relationships.length; i++) {      RelationshipData r = relationships[i];      long startTime = r.getStartTime();      long endTime = r.getEndTime();      // Role is empty if its really Subordinate, in which      // case interesting stuff is in the Type      // FIXME: Or maybe it should be the literal?      substitutions.put(":role:", sqlQuote((r.getRole() == null || r.getRole().equals("")) ? r.getType() : r.getRole()));      substitutions.put(":supporting:", sqlQuote(getComponentAlibId(data)));      substitutions.put(":supported:", sqlQuote(getAgentAlibId(r.getSupported())));      // These appear to be magic with the preparedStatement below....      substitutions.put(":start_date:", "?");      substitutions.put(":end_date:", "?");            String query = dbp.getQuery("checkRelationship", substitutions);      PreparedStatement pstmt = dbConnection.prepareStatement(query);      pstmt.setTimestamp(1, new Timestamp(startTime));      ResultSet rs = executeQuery(pstmt, query);      if (!rs.next()) {	if (log.isDebugEnabled()) {	  log.debug("pagent " + data.getName() + " inserting rel " + r.getRole() + " with " + getAgentAlibId(r.getSupported()) + " into assembly " + substitutions.get(":assembly_id:"));	}	query = dbp.getQuery("insertRelationship", 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);	}	try {	  executeUpdate(pstmt2, query);	} catch (SQLException e) {	  if(log.isErrorEnabled()) {	    log.error("SQLException query: " + query, e);	  }	  throw e;	} finally {	  pstmt2.close();	}      }      rs.close();      pstmt.close();    }  }