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()); }
  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      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();      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();      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[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());            }          }	  substitutions.put(":component_alib_id:", sqlQuote(getComponentAlibId(data)));	  substitutions.put(":pg_attribute_lib_id:", sqlQuote(propInfo.getAttributeLibId()));	  // Are these defaults?	  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++) {	      /////////////////	      // FIXME: CMT assemblies have all multi-valed PGAttr use attribute_order	      // of 0, while we give them a real order.	      // As a result, we end up adding extra, for example, Roles	      substitutions.put(":attribute_value:", sqlQuote(values[k]));	      substitutions.put(":attribute_order:", String.valueOf(k));	      // if val not already there for this runtime	      ResultSet rschck = executeQuery(stmt, dbp.getQuery("checkAttribute", substitutions));	      if (! rschck.next()) {		// FIXME: Add a query: Get # of entries for this PG/slot		// with attribute_order of 0. If its > 1, then,		// maybe doesnt a checkAttribute variant that doesn't		// check order. IF it matches, don't do an insert		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));	      }	      rschck.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	      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();	  }	} // 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();	}      }      pstmt.close();    }  }