package org.arachb.arachadmin;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.arachb.owlbuilder.lib.Publication;

public class ClaimBean implements UpdateableBean{  //do all claims need IRI's?
	
	final static int DBID = 1;
	final static int DBPUBLICATION = 2;
	final static int DBPUBLICATIONBEHAVIOR = 3;
	final static int DBBEHAVIORTERM = 4;
	final static int DBEVIDENCE = 5;
	final static int DBGENERATEDID = 6;
	final static int DBPUBDOI = 7;
	final static int DBPUBGENERATEDID = 8;
	final static int DBBEHAVIORSOURCEID = 9;
	final static int DBBEHAVIORGENERATEDID = 10;
	final static int DBEVIDENCESOURCEID = 11;
	final static int DBEVIDENCEGENERATEDID = 12;
	
	final static String BADPUBLICATIONIRI =
			"Publication without IRI referenced as publication cited in claim: claim id = %s; publication id = %s";
	final static String BADBEHAVIORIRI =
			"Term without IRI referenced as behavior cited in claim: claim id = %s; behavior id = %s";
	final static String BADEVIDENCEIRI =
			"Term without IRI referenced as evidence type supporting claim; claim id = %s; evidence id = %s";

	
	private final static Logger log = Logger.getLogger(ClaimBean.class);

	private int id;
	private int publication;
	private int behavior;
	private String publicationBehavior;
	private int evidence;
	private String generated_id = null;  //for validity checking
	private String behaviorIRI;
	private String publicationIRI;
	private String evidenceIRI;
	
	static final ClaimBean dummy = new ClaimBean(); 
	
	
	//maybe make this a constructor
	@Override
	public void fill(AbstractResults record) throws SQLException{
		id = record.getInt(DBID);
		publication = record.getInt(DBPUBLICATION);
		publicationBehavior = record.getString(DBPUBLICATIONBEHAVIOR);
		behavior= record.getInt(DBBEHAVIORTERM);
		evidence = record.getInt(DBEVIDENCE);
		generated_id = record.getString(DBGENERATEDID);
		if (publication != 0){
			updatePublicationIRI(record);
		}
		if (behavior != 0){
			updateBehaviorIRI(record);
		}
		if (evidence != 0){
			updateEvidenceIRI(record);
		}
	}
	
	private void updatePublicationIRI(AbstractResults record) throws SQLException{
		if (record.getString(DBPUBDOI) != null){
			this.setPublicationIri(IRIManager.cleanupDoi(record.getString(DBPUBDOI)));
		}
		else if (record.getString(DBPUBGENERATEDID) != null){
			this.setPublicationIri(record.getString(DBPUBGENERATEDID));
		}
		else{
			throwBadState(BADPUBLICATIONIRI);
		}
	}

	private void updateBehaviorIRI(AbstractResults record) throws SQLException{
		if (record.getString(DBBEHAVIORSOURCEID) != null){
			this.setBehaviorIri(record.getString(DBBEHAVIORSOURCEID));
		}
		else if (record.getString(DBBEHAVIORGENERATEDID) != null){
			this.setBehaviorIri(record.getString(DBBEHAVIORGENERATEDID));
		}
		else{
			throwBadState(BADBEHAVIORIRI);
		}
	}

	private void updateEvidenceIRI(AbstractResults record) throws SQLException{
		if (record.getString(DBEVIDENCESOURCEID) != null){
			this.setEvidenceIri(record.getString(DBEVIDENCESOURCEID));
		}
		else if (record.getString(DBEVIDENCEGENERATEDID) != null){
			this.setEvidenceIri(record.getString(DBEVIDENCEGENERATEDID));
		}
		else{
			throwBadState(BADEVIDENCEIRI);
		}
	}
	
	private void throwBadState(String template){
		final String msg = String.format(template, id, evidence);
		throw new IllegalStateException(msg);
	}
	


	@Override
	public int getId(){
		return id;
	}
	
	public int getPublication(){
		return publication;
	}
	
	public String getPublicationBehavior(){
		return publicationBehavior;
	}
	
	public int getBehavior(){
		return behavior;
	}
			
	
	public int getEvidence(){
		return evidence;
	}
	

	public void setPublicationIri(String s){
		publicationIRI = s;
	}

	public void setBehaviorIri(String s){
		behaviorIRI = s;
	}

	public void setEvidenceIri(String s){
		evidenceIRI = s;
	}

	
	public void setGeneratedId(String id){
		generated_id = id;
	}
	
	public String getGeneratedId(){
		return generated_id;
	}

	
	public String getBehaviorIri(){
		return behaviorIRI;
	}
	
	
	final static String NOCLAIMGENID = "Claim has no generated id; db id = %s";

	@Override
	public String getIRIString(){
		final String genId = getGeneratedId();
		if (genId == null){
			final String msg = String.format(NOCLAIMGENID, getId());
			throw new IllegalStateException(msg);
		}
		return genId;
	}

	@Override
	public String checkIRIString(IRIManager manager) throws SQLException{
		if (getGeneratedId() == null){
			manager.generateIRI(this);
		}
		return getGeneratedId();
	}

	@Override
	public void updateDB(AbstractConnection c) throws SQLException{
		c.updateClaim(this);
	}
	
	
}

@@ -91,54 +91,8 @@
 ger;
-%0Aimport org.arachb.owlbuilder.lib.Publication;
 %0A%0Apu
@@ -3662,32 +3662,100 @@
 ionIRI = s;%0A%09%7D%0A%0A
+%09%0A%09public String getPublicationIri()%7B%0A%09%09return publicationIRI;%0A%09%7D%0A%09%0A
 %09public void set
@@ -3854,32 +3854,90 @@
 nceIRI = s;%0A%09%7D%0A%0A
+%09public String getEvidenceIri()%7B%0A%09%09return evidenceIRI;%0A%09%7D%0A
 %09%0A%09public void s

remove dependency on publication class, handle publication and evidence iri strings like a simple bean