def queryMpMaterial(conn, annotation, claimid):

	qry = """	
	select mann.type, mf.material_field_type, mf.value_as_string, mf.value_as_number, s.exact, s.prefix, s.suffix, mann.mp_data_index, mann.ev_supports
	from mp_material_annotation mann,oa_material_body mbody, material_field mf, oa_target t, oa_selector s
	where mann.mp_claim_id = %s
	and mann.has_body = mbody.id
	and mf.material_body_id = mbody.id
	and mann.has_target = t.id
	and t.has_selector = s.id
	""" % (claimid)

	results = []

	cur = conn.cursor()
	cur.execute(qry)

	for row in cur.fetchall():

		mType = row[0]  # material type
		mfType = row[1] # material field 

		exact = row[4]; value = str(row[2] or row[3]) # value as string or number
		index = row[7] # data & material index
		evRelationship = row[8] # EV supports or refutes

		if annotation.getSpecificDataMaterial(index) == None:
			dmRow = DataMaterialRow() # create new row of data & material

			if evRelationship:				
				dmRow.setEvRelationship("supports")
			else:
				dmRow.setEvRelationship("refutes")

			if mType in ["object_dose","subject_dose"]: # dose
				doseItem = MaterialDoseItem(mType)
				doseItem.setAttribute(mfType, value)
				doseItem.setSelector("", exact, "")
				dmRow.setMaterialDoseItem(doseItem)

			elif mType == "participants":
				partItem = MaterialParticipants(value)
				partItem.setSelector("", exact, "")
				dmRow.setParticipants(partItem)

			annotation.setSpecificDataMaterial(dmRow, index)

		else: # current row of material & material exists 
			dmRow = annotation.getSpecificDataMaterial(index)

			if dmRow.getEvRelationship() == None and evRelationship is True:
				dmRow.setEvRelationship("supports")
			elif dmRow.getEvRelationship() == None and evRelationship is False:
				dmRow.setEvRelationship("refutes")

			if mType in ["object_dose","subject_dose"]:
				if dmRow.getMaterialDoseInRow(mType): # current MaterialItem exists
					doseItem = dmRow.getMaterialDoseInRow(mType)
				else:
					doseItem = MaterialDoseItem(mType) 

				doseItem.setAttribute(mfType, value)
				doseItem.setSelector("", exact, "")				
				dmRow.setMaterialDoseItem(doseItem)

			elif mType == "participants":
				if dmRow.getParticipantsInRow(): # participants exists
					partItem = dmRow.getParticipantsInRow()
					partItem.setValue(value)
				else:
					partItem = MaterialParticipants(value)
					dmRow.setParticipants(partItem)
				partItem.setSelector("", exact, "")

	return annotation
def queryMpData(conn, annotation, claimid):

	qry = """	
	select dann.type, df.data_field_type, df.value_as_string, df.value_as_number, s.exact, s.prefix, s.suffix, dann.mp_data_index, dann.ev_supports
	from mp_data_annotation dann,oa_data_body dbody, data_field df, oa_target t, oa_selector s
	where dann.mp_claim_id = %s
	and dann.has_body = dbody.id
	and df.data_body_id = dbody.id
	and dann.has_target = t.id
	and t.has_selector = s.id
	""" % (claimid)

	cur = conn.cursor()
	cur.execute(qry)
		
	for row in cur.fetchall():

		dType = row[0]  # data type
		dfType = row[1] # data field 
		exact = row[4]; value = str(row[2] or row[3]) # value as string or number
		index = row[7] # data index
		evRelationship = row[8] # EV supports or refutes
		dmRow = None

		if annotation.getSpecificDataMaterial(index) == None:
			dmRow = DataMaterialRow() # create new row of data & material
			dataItem = DataItem(dType)
			dataItem.setAttribute(dfType, value) # add value
			dataItem.setSelector("", exact, "")

			dmRow.setDataItem(dataItem)

			if evRelationship is True:				
				dmRow.setEvRelationship("supports")
			elif evRelationship is False:
				dmRow.setEvRelationship("refutes")

			annotation.setSpecificDataMaterial(dmRow, index)

		else: # current row of data & material exists 
			dmRow = annotation.getSpecificDataMaterial(index)

			if dmRow.getDataItemInRow(dType) != None: # current DataItem exists
				dataItem = dmRow.getDataItemInRow(dType)

			else: # current DataItem not exists
				dataItem = DataItem(dType) 
				dmRow.setDataItem(dataItem)

			dataItem.setAttribute(dfType, value)
			dataItem.setSelector("", exact, "")

			if dmRow.getEvRelationship() == None and evRelationship:
				dmRow.setEvRelationship("supports")
			elif dmRow.getEvRelationship() == None and not evRelationship:
				dmRow.setEvRelationship("refutes")
	return annotation