package org.arachb.arachadmin;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;



public class PElementBean implements BeanBase {
	
	final static int DBID = 1;
	final static int DBTYPE = 2;
	final static int DBPARTICIPANT = 3;
	
	// these have their own results types
	final static int DBTERM = 1;
	final static int DBINDIVIDUAL = 1;
	
	final static int DBPARENTID = 1;
	final static int DBPARENTPROPERTY = 2;
	
	final static int DBCHILDID = 1;
	final static int DBCHILDPROPERTY = 2;
	
	private int id;
	private int eletype;
	private int participant;
	final private Map<Integer,Plink> parentLinks = new HashMap<Integer,Plink>();
	final private Map<Integer,Plink> childLinks = new HashMap<Integer,Plink>();
	
	private IndividualBean individual = null;
	private TermBean term;
	
	
	@Override
	public int getId() {
		return id;
	}

	
	public int getType(){
		return eletype;
	}
	
	
	public int getParticipant(){
		return participant;
	}
	
	
	public TermBean getTerm(){
		return term;
	}

	
	public IndividualBean getIndividual(){
		return individual;
	}
	
	
	@Override
	public void fill(AbstractResults record) throws SQLException {
		id = record.getInt(DBID);
		eletype = record.getInt(DBTYPE);
		participant = record.getInt(DBPARTICIPANT);
	}

	public void fillTerm(AbstractResults record, AbstractConnection c) throws SQLException{
		int termId = record.getInt(DBTERM);
		term = c.getTerm(termId);		
	}

	
	public void fillIndividual(AbstractResults record, AbstractConnection c) throws SQLException{
		int individualId = record.getInt(DBINDIVIDUAL);
		individual = c.getIndividual(individualId);
	}

	
	public void fillParents(AbstractResults parentResults) throws Exception{		
		while (parentResults.next()){
			Plink pl = new Plink();
			pl.element_id = parentResults.getInt(DBPARENTID);
			pl.property_id = parentResults.getInt(DBPARENTPROPERTY);
			parentLinks.put(pl.element_id,pl);
		}
	}


	public void fillChildren(AbstractResults childrenResults) throws Exception{
		while (childrenResults.next()){
			Plink pl = new Plink();
			pl.element_id = childrenResults.getInt(DBCHILDID);
			pl.property_id = childrenResults.getInt(DBCHILDPROPERTY);
			childLinks.put(pl.element_id, pl);			
		}
	}
	

	public void resolveParents(ParticipantBean pb, AbstractConnection c) throws Exception{
		resolveDependents(pb, parentLinks, c);
	}
	
	public void resolveChildren (ParticipantBean pb, AbstractConnection c) throws Exception{
		resolveDependents(pb, childLinks,c );
	}
	
	private void resolveDependents(ParticipantBean participantBean, Map<Integer, Plink> links, AbstractConnection c) throws Exception{
		for (Plink pl : links.values()){
			pl.element = participantBean.getElementBean(pl.element_id);
			pl.property = c.getProperty(pl.property_id);
		}
	}



	public Set<Integer> getParents(){
		return parentLinks.keySet();
	}
	
	public Integer getSingletonParent(){
		if (parentLinks.size() != 1){
			throw new RuntimeException("PElementBean " + toString() + " has number of parents other than 1");
		}
		return parentLinks.keySet().iterator().next();
	}
	
	public PElementBean getParentElement(Integer index){
		return parentLinks.get(index).getElement();
	}


	public PropertyBean getParentProperty(Integer index){
		return parentLinks.get(index).getProperty();
	}

	public Set<Integer> getChildren(){
		return childLinks.keySet();
	}
	
	/**
	 * Convenience method for checking and returning the link to the pelement's child
	 * @return id of plink linking this pelement and its child
	 * @throws RuntimeException - if PElement has more than one child
	 */
	public Integer getSingletonChild(){
		if (childLinks.size() != 1){
			throw new RuntimeException("PElementBean " + toString() + " has number of children other than 1");
		}
		return childLinks.keySet().iterator().next();
	}

	
	public PElementBean getChildElement(Integer index){
		return childLinks.get(index).getElement();
	}


	public PropertyBean getChildProperty(Integer index){
		return childLinks.get(index).getProperty();
	}

	static private class Plink{
		private int property_id;
		private int element_id;
		private PropertyBean property;
		private PElementBean element;		
		
		public PropertyBean getProperty() {
			return property;
		}
		
		public PElementBean getElement() {
			return element;
		}
	}
	
	
	
}

@@ -126,16 +126,62 @@
 l.Set;%0A%0A
+import org.arachb.owlbuilder.lib.Individual;%0A%0A
 %0A%0Apublic
@@ -865,16 +865,23 @@
 ean term
+ = null
 ;%0A%09%0A%09%0A%09@
@@ -1696,24 +1696,685 @@
 ualId);%0A%09%7D%0A%0A
+%09%0A%09/**%0A%09 * Current only tests for a term - no need for owlgenerating Term class at present,%0A%09 * so just test and return true if no term (so resolve Individual gets called)%0A%09 * @param c connection that might be used to load beans for a term (if needed)%0A%09 * @return true if no term present%0A%09 */%0A%09public boolean resolveTerm(AbstractConnection c)%7B%0A%09%09return term == null;%0A%09%7D%0A%09%0A%09public boolean resolveIndividual(AbstractConnection c)%7B%0A%09%09if (individual != null)%7B%0A%09%09%09generatingIndividual = new Individual(individual);%0A%09%09%09return false;%0A%09%09%7D%0A%09%09throw new RuntimeException(%0A%09%09%09%09String.format(%22Participant Element %25s seems to have neither a term or an individual%22,this));%0A%09%7D%0A
 %09%0A%09public vo

update to wrap a bean, normalize constructor