package ext.ngc.pdmlink.change2.server.processors;

import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

import wt.eff.*;
import wt.change2.*;
import wt.doc.WTDocument;
import wt.fc.ObjectReference;
import wt.fc.QueryResult;
import wt.fc.collections.WTArrayList;

import wt.iba.definition.litedefinition.*;
import wt.iba.definition.service.StandardIBADefinitionService;
import wt.iba.value.DefaultAttributeContainer;
import wt.iba.value.IBAHolder;
import wt.iba.value.litevalue.*;
import wt.iba.value.litevalue.AbstractValueView;
import wt.iba.value.service.IBAValueHelper;
import wt.iba.value.service.IBAValueService;

import wt.inf.container.*;
import wt.org.WTPrincipalReference;
import wt.part.WTPart;
import wt.util.WTException;
import wt.workflow.engine.*;

public class NGCChangeData
{
	public NGCChangeData() { }

	// checking for Related PR's through CR's

	public static String getCNrelPR (WTChangeOrder2 chgOrd)
	{
		String relPR2 = "&nbsp;";
		try {
			QueryResult wt2cr = ChangeHelper2.service.getChangeRequest(chgOrd);	// related CR
			if (wt2cr.size()>0) {
				while(wt2cr.hasMoreElements()) {
					WTChangeRequest2 cr=(WTChangeRequest2)wt2cr.nextElement();
					QueryResult wt2ci = ChangeHelper2.service.getChangeIssues(cr);	// related PR
					if (wt2ci.size()>0) {
						while(wt2ci.hasMoreElements()) {
							WTChangeIssue pr=(WTChangeIssue)wt2ci.nextElement();
							relPR2 = relPR2 + "<br>" + pr.getNumber();
						}
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return relPR2;
	}

	// checking for Related PR's through CR's

	public static String getCRrelPR (WTChangeRequest2 chgReq)
	{
		String relPR2 = "&nbsp;";
		try {
			QueryResult wt2ci = ChangeHelper2.service.getChangeIssues(chgReq);
			if (wt2ci.size()>0) {
				while(wt2ci.hasMoreElements()) {
					WTChangeIssue pr=(WTChangeIssue)wt2ci.nextElement();
					relPR2 = relPR2 + "<br>" + pr.getNumber();
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return relPR2;
	}

	// query of CN workflow process to gather voting assignee's & dates

	public static ArrayList getCNdata (WTChangeOrder2 chgOrd)
	{
		ArrayList rtnStgArr = new ArrayList();

		try{
			QueryResult qrcm = WfEngineHelper.service.getAssociatedProcesses(chgOrd, null, ((WTContained)chgOrd).getContainerReference());

			while(qrcm.hasMoreElements()) {
				WfProcess wfprocess = (WfProcess)qrcm.nextElement();
				wt.fc.collections.WTArrayList voting = (wt.fc.collections.WTArrayList) wt.workflow.engine.WfEngineHelper.service.getVotingEvents((wt.workflow.engine.WfProcess)wfprocess, null, null, null);
				Object anItem = null;

				wt.workflow.engine.WfVotingEventAudit aVote = null;
				if (voting != null && !voting.isEmpty())
				{
					java.util.Iterator voteIterator = voting.iterator();
					while (voteIterator.hasNext())
					{
						wt.fc.ObjectReference anItemRef = (wt.fc.ObjectReference)voteIterator.next();
						anItem = anItemRef.getObject();
						if (anItem instanceof wt.workflow.engine.WfVotingEventAudit)
						{
							aVote = (wt.workflow.engine.WfVotingEventAudit)anItem;

							String tmpDtStg="";
							tmpDtStg = aVote.getTimestamp().toString();
							SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yy", Locale.getDefault());
							String dtStg = "";
							dtStg = sdf.format(aVote.getTimestamp());

							String tempVoteStg = "";
							tempVoteStg = aVote.getAssigneeRef().getFullName()+" "+dtStg;
							rtnStgArr.add(tempVoteStg);
						}
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return rtnStgArr;
	}

	// query of CN workflow process to gather voting assignee's & dates

	public static ArrayList getCRdata (WTChangeRequest2 chgReq)
	{
		ArrayList rtnStgArr = new ArrayList();

		try{
			QueryResult qrcm = WfEngineHelper.service.getAssociatedProcesses(chgReq, null, ((WTContained)chgReq).getContainerReference());

			while(qrcm.hasMoreElements()) {
				WfProcess wfprocess = (WfProcess)qrcm.nextElement();
				wt.fc.collections.WTArrayList voting = (wt.fc.collections.WTArrayList) wt.workflow.engine.WfEngineHelper.service.getVotingEvents((wt.workflow.engine.WfProcess)wfprocess, null, null, null);
				Object anItem = null;

				wt.workflow.engine.WfVotingEventAudit aVote = null;
				if (voting != null && !voting.isEmpty())
				{
					java.util.Iterator voteIterator = voting.iterator();
					while (voteIterator.hasNext())
					{
						wt.fc.ObjectReference anItemRef = (wt.fc.ObjectReference)voteIterator.next();
						anItem = anItemRef.getObject();
						if (anItem instanceof wt.workflow.engine.WfVotingEventAudit)
						{
							aVote = (wt.workflow.engine.WfVotingEventAudit)anItem;

							String tmpDtStg="";
							tmpDtStg = aVote.getTimestamp().toString();
							SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yy", Locale.getDefault());
							String dtStg = "";
							dtStg = sdf.format(aVote.getTimestamp());

							String tempVoteStg = "";
							tempVoteStg = aVote.getAssigneeRef().getFullName()+" "+dtStg;
							rtnStgArr.add(tempVoteStg);
						}
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return rtnStgArr;
	}

	// query of CA workflow process to gather voting assignee's & dates

	public static ArrayList getCAdata (WTChangeOrder2 chgOrd)
	{
		ArrayList rtnStgArr = new ArrayList();

		try{
			QueryResult qrChgAct = ChangeHelper2.service.getChangeActivities(chgOrd);
			while(qrChgAct.hasMoreElements())
			{
				if (qrChgAct.size()>0) {
					while(qrChgAct.hasMoreElements()) {
						WTChangeActivity2 ca=(WTChangeActivity2)qrChgAct.nextElement();

						QueryResult qrcm = WfEngineHelper.service.getAssociatedProcesses(ca, null, ((WTContained)ca).getContainerReference());
						while(qrcm.hasMoreElements()) {
							WfProcess wfprocess = (WfProcess)qrcm.nextElement();
							WTContainer container = WTContainerHelper.getContainer(wfprocess);

							wt.fc.collections.WTArrayList voting = (wt.fc.collections.WTArrayList) wt.workflow.engine.WfEngineHelper.service.getVotingEvents((wt.workflow.engine.WfProcess)wfprocess, null, null, null);
							Object anItem = null;

							wt.workflow.engine.WfVotingEventAudit aVote = null;
							if (voting != null && !voting.isEmpty())
							{
								java.util.Iterator voteIterator = voting.iterator();
								while (voteIterator.hasNext())
								{
									wt.fc.ObjectReference anItemRef = (wt.fc.ObjectReference)voteIterator.next();
									anItem = anItemRef.getObject();
									if (anItem instanceof wt.workflow.engine.WfVotingEventAudit)
									{
										aVote = (wt.workflow.engine.WfVotingEventAudit)anItem;

										String tmpDtStg="";
										tmpDtStg = aVote.getTimestamp().toString();
										SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yy", Locale.getDefault());
										String dtStg = "";
										dtStg = sdf.format(aVote.getTimestamp());

										String tempVoteStg = "";
										tempVoteStg = aVote.getAssigneeRef().getFullName()+" "+dtStg;

										rtnStgArr.add(tempVoteStg);
									}
								}
							}
						}
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return rtnStgArr;
	}

	// NGC CN IBA's

	public static String getIBAval( String attrStg, WTChangeOrder2 chgOrd)
	{
		String attrVal="&nbsp;";

		try{
			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)chgOrd, null, null, null);
			StandardIBADefinitionService defService=new StandardIBADefinitionService();
			DefaultAttributeContainer attributeContainer=(DefaultAttributeContainer)ibaHolder.getAttributeContainer();
			AttributeDefDefaultView attributeDefinition=defService.getAttributeDefDefaultViewByPath(attrStg);

			System.out.println("\n++++\nattrStg: "+attrStg);
			System.out.println("attributeContainer.getAttributeValues().length: "+attributeContainer.getAttributeValues().length);

			if(attributeContainer.getAttributeValues().length>0){
				AbstractValueView attValue= attributeContainer.getAttributeValues(attributeDefinition)[0];

				String s = new String();
				if(!(attValue instanceof ReferenceValueDefaultView))
				{
					if(attValue instanceof StringValueDefaultView)
						s = ((StringValueDefaultView)attValue).getLocalizedDisplayString();
					else
					if(attValue instanceof FloatValueDefaultView)
						s = ((FloatValueDefaultView)attValue).getLocalizedDisplayString();
					else
					if(attValue instanceof IntegerValueDefaultView)
						s = ((IntegerValueDefaultView)attValue).getLocalizedDisplayString();
					else
					if(attValue instanceof BooleanValueDefaultView)
						s = ((BooleanValueDefaultView)attValue).getLocalizedDisplayString();
					else
					if(attValue instanceof TimestampValueDefaultView)
						s = timestampToString(((TimestampValueDefaultView)attValue).getValue());
				}

				System.out.println("s: "+s);

				System.out.println("\n\nattValue.getDefinition().getDisplayName(): "+attValue.getDefinition().getDisplayName());
				System.out.println("attValue.getLocalizedDisplayString(): "+attValue.getLocalizedDisplayString());
				attrVal=attValue.getLocalizedDisplayString();
			}
		}
		catch(WTException ew){ ew.printStackTrace(); }
		catch(RemoteException er){ er.printStackTrace(); }
		catch(ArrayIndexOutOfBoundsException ea){ ea.printStackTrace(); }

		if (attrVal == null)
			attrVal="&nbsp;";

		return attrVal;
	}

	private String timestampToString(Timestamp timestamp)
	{
		WTContext wtcontext = WTContext.getContext();
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", wtcontext.getLocale());
		simpledateformat.setTimeZone(wtcontext.getTimeZone());
		Date date = new Date(timestamp.getTime());
		String s = simpledateformat.format(date);
		return s;
	}

	// NGC CR IBA's

	public static String getIBAval( String attrStg, WTChangeRequest2 chgReq)
	{
		String attrVal="&nbsp;";

		try{
			IBAHolder ibaHolder=IBAValueHelper.service.refreshAttributeContainer((IBAHolder)chgReq, null, null, null);
			StandardIBADefinitionService defService=new StandardIBADefinitionService();
			DefaultAttributeContainer attributeContainer=(DefaultAttributeContainer)ibaHolder.getAttributeContainer();
			AttributeDefDefaultView attributeDefinition=defService.getAttributeDefDefaultViewByPath(attrStg);

			if(attributeContainer.getAttributeValues().length>0){
				AbstractValueView attValue= attributeContainer.getAttributeValues(attributeDefinition)[0];
				attrVal=attValue.getLocalizedDisplayString();
			}
		}
		catch(WTException ew){ ew.printStackTrace(); }
		catch(RemoteException er){ er.printStackTrace(); }
		catch(ArrayIndexOutOfBoundsException ea){ ea.printStackTrace(); }

		if (attrVal == null)
			attrVal="&nbsp;";

		return attrVal;
	}

	// CN Name - getChangeablesAfter = resulting items

	public static ArrayList getChgNm4CN(QueryResult qrChg1)
	{
		ArrayList chgNM = new ArrayList();
		String tmpStg="&nbsp;";
		Changeable2 changeable2;

		try{
			if (qrChg1.size() > 0) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg="&nbsp;";
					changeable2 = (Changeable2)qrChg1.nextElement();

					if (changeable2 instanceof wt.doc.WTDocument) {
						tmpStg = ((wt.doc.WTDocument) changeable2).getName();
						if (tmpStg == null) { tmpStg="&nbsp;"; }
						chgNM.add(tmpStg);
					}
					else if (changeable2 instanceof wt.part.WTPart) {
						tmpStg = ((wt.part.WTPart) changeable2).getName();
						if (tmpStg == null) { tmpStg="&nbsp;"; }
						chgNM.add(tmpStg);
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgNM;
	}

	// CN Number - getChangeablesAfter = resulting items

	public static ArrayList getChgNo4CN(QueryResult qrChg1)
	{
		ArrayList chgNO = new ArrayList();
		String tmpStg="&nbsp;";
		Changeable2 changeable2;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg="&nbsp;";
					changeable2 = (Changeable2)qrChg1.nextElement();

					if (changeable2 instanceof wt.doc.WTDocument) {
						tmpStg = ((wt.doc.WTDocument) changeable2).getNumber();
						if (tmpStg == null) { tmpStg="&nbsp;"; }
						chgNO.add(tmpStg);
					}
					else if (changeable2 instanceof wt.part.WTPart) {
						tmpStg = ((wt.part.WTPart) changeable2).getNumber();
						if (tmpStg == null) { tmpStg="&nbsp;"; }
						chgNO.add(tmpStg);
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgNO;
	}

	// CN Disposition - getChangeablesBefore = affected items

	public static ArrayList getChgDis(QueryResult qrChg1, ArrayList chgNO )
	{
		ArrayList chgDis = new ArrayList();
		String tmpStg,tmpStg2;
		Changeable2 changeable2;
		AffectedActivityData affData;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg ="&nbsp;";

					Object obj1 = null;
					obj1 = qrChg1.nextElement();

					IncludedInIfc includedinifc = (IncludedInIfc)obj1;
					QueryResult qrAft = ChangeHelper2.service.getChangeablesAfter(includedinifc.getChangeActivityIfc(), false);

					if ( qrAft.size() > 0 ) {
						QueryResult qrBef = ChangeHelper2.service.getChangeablesBefore(includedinifc.getChangeActivityIfc(), false);

						while(qrBef.hasMoreElements())
						{
							tmpStg ="&nbsp;";
							tmpStg2="&nbsp;";

							Object obj2 = qrBef.nextElement();

							affData = (AffectedActivityData)obj2;
							changeable2 = affData.getChangeable2();

							if(changeable2 instanceof wt.part.WTPart) {
								tmpStg2 = ((wt.part.WTPart) changeable2).getNumber();
								tmpStg = affData.getInventoryDisposition().toString();

								if (tmpStg == null || tmpStg.equals("") ) { tmpStg="&nbsp;"; }
								chgDis.add(tmpStg+"|"+tmpStg2);
							}
							else if (changeable2 instanceof wt.doc.WTDocument) { chgDis.add("&nbsp;"); }
						}
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgDis;
	}

	// CN Disposition - getChangeablesBefore = affected items

	public static ArrayList getChgDisOld(QueryResult qrChg1)
	{
		ArrayList chgDis = new ArrayList();
		String tmpStg ="&nbsp;";
		Changeable2 changeable2;
		AffectedActivityData affData;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg ="&nbsp;";

					IncludedInIfc includedinifc = (IncludedInIfc)qrChg1.nextElement();
					QueryResult qrAft = ChangeHelper2.service.getChangeablesAfter(includedinifc.getChangeActivityIfc(), false);

					if ( qrAft.size() > 0 ) {
						QueryResult qrBef = ChangeHelper2.service.getChangeablesBefore(includedinifc.getChangeActivityIfc(), false);

						while(qrBef.hasMoreElements())
						{
							tmpStg="&nbsp;";
							affData = (AffectedActivityData)qrBef.nextElement();
							changeable2 = affData.getChangeable2();

							if(changeable2 instanceof wt.part.WTPart) {
								tmpStg = affData.getInventoryDisposition().toString();
								if (tmpStg == null || tmpStg.equals("") ) { tmpStg="&nbsp;"; }
								chgDis.add(tmpStg);
							}
							else if (changeable2 instanceof wt.doc.WTDocument) { chgDis.add("&nbsp;"); }
						}
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgDis;
	}

	// CN Effectivity - getChangeablesAfter

	public static ArrayList getChgEff(QueryResult qrChg1)
	{
		ArrayList chgEff = new ArrayList();
		String tmpStg="&nbsp;";
		Changeable2 changeable2;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					changeable2 = (Changeable2)qrChg1.nextElement();
					if (changeable2 instanceof wt.part.WTPart) {
						tmpStg="&nbsp;";
						wt.part.WTPart wtpart2 = (wt.part.WTPart)EffGroupAssistant.populateEffVector((wt.part.WTPart) changeable2);
						if(wtpart2.getEffVector() == null) { chgEff.add("&nbsp;"); }
						else {
							Enumeration enumeration = wtpart2.getEffVector().elements();

							if(enumeration == null) { chgEff.add("&nbsp;"); }
							else {
								tmpStg ="";
								do
								{
									if(!enumeration.hasMoreElements()) { break; }
									EffGroup effgroup = (EffGroup)enumeration.nextElement();
									tmpStg += ((wt.part.WTPartMaster)effgroup.getEffContext()).getNumber()
									+" + "+effgroup.getRange()
									+" + "+effgroup.getDisplayType()
									+",<br>";
								} while(true);
								if (tmpStg == null) { tmpStg="&nbsp;"; }
								if (tmpStg.endsWith(",<br>")) { tmpStg = tmpStg.substring(0, tmpStg.length() - 5); }
								chgEff.add(tmpStg);
							}
						}
					}
					else if (changeable2 instanceof wt.doc.WTDocument) { chgEff.add("&nbsp;"); }
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgEff;
	}

	// CR Name - getChangeables

	public static ArrayList getChgNm4CR(QueryResult qrChg1)
	{
		ArrayList chgNM = new ArrayList();
		String tmpStg="&nbsp;";
		Changeable2 changeable2;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg="&nbsp;";
					changeable2 = (Changeable2)qrChg1.nextElement();

					if (changeable2 instanceof wt.doc.WTDocument) {
						tmpStg = ((wt.doc.WTDocument) changeable2).getName();
						chgNM.add(tmpStg);
					}
					else if (changeable2 instanceof wt.part.WTPart) {
						tmpStg = ((wt.part.WTPart) changeable2).getName();
						chgNM.add(tmpStg);
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgNM;
	}

	// CR Number - getChangeables

	public static ArrayList getChgNo4CR(QueryResult qrChg1)
	{
		ArrayList chgNO = new ArrayList();
		String tmpStg="&nbsp;";
		Changeable2 changeable2;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg="&nbsp;";
					changeable2 = (Changeable2)qrChg1.nextElement();

					if (changeable2 instanceof wt.doc.WTDocument) {
						tmpStg = ((wt.doc.WTDocument) changeable2).getNumber();
						chgNO.add(tmpStg);
					}
					else if (changeable2 instanceof wt.part.WTPart) {
						tmpStg = ((wt.part.WTPart) changeable2).getNumber();
						chgNO.add(tmpStg);
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgNO;
	}

	// CN Data (Name, Number, Effectivity)

	public static ArrayList getData (QueryResult qrChg1)
	{
		ArrayList chgData2 = new ArrayList();
		String tmpStg,tmpStg1,tmpStg2,tmpStg3,tmpStg4;
		Changeable2 changeable2;

		try{
			if ( qrChg1.size() > 0 ) {
				while(qrChg1.hasMoreElements())
				{
					tmpStg="&nbsp;";tmpStg1="&nbsp;";tmpStg2="&nbsp;";tmpStg3="&nbsp;";tmpStg4="&nbsp;";

					changeable2 = (Changeable2)qrChg1.nextElement();

					if (changeable2 instanceof wt.doc.WTDocument) {
						if ( ((wt.doc.WTDocument) changeable2).getNumber() != null ) { tmpStg1 = ((wt.doc.WTDocument) changeable2).getNumber(); }
						if ( tmpStg1 == "") { tmpStg1="&nbsp;"; }

						if ( ((wt.doc.WTDocument) changeable2).getName() != null )   { tmpStg2 = ((wt.doc.WTDocument) changeable2).getName(); }
						if ( tmpStg2 == "") { tmpStg2="&nbsp;"; }
						tmpStg3="&nbsp;";

						tmpStg = tmpStg1+"|"+tmpStg2+"|"+tmpStg3;
						chgData2.add(tmpStg);
					}
					else if (changeable2 instanceof wt.part.WTPart) {

						if ( ((wt.part.WTPart) changeable2).getNumber() != null ) { tmpStg1 = ((wt.part.WTPart) changeable2).getNumber(); }
						if ( tmpStg1 == "") { tmpStg1="&nbsp;"; }

						if ( ((wt.part.WTPart) changeable2).getName() != null )   { tmpStg2 = ((wt.part.WTPart) changeable2).getName(); }
						if ( tmpStg2 == "") { tmpStg2="&nbsp;"; }

						wt.part.WTPart wtpart2 = (wt.part.WTPart)EffGroupAssistant.populateEffVector((wt.part.WTPart) changeable2);
						if(wtpart2.getEffVector() == null) { tmpStg3="&nbsp;";; }
						else {
							Enumeration enumeration = wtpart2.getEffVector().elements();

							if(enumeration == null) { tmpStg3="&nbsp;"; }
							else {
								tmpStg3 ="";
								do
								{
									if(!enumeration.hasMoreElements()) { break; }
									EffGroup effgroup = (EffGroup)enumeration.nextElement();
									tmpStg3 += ((wt.part.WTPartMaster)effgroup.getEffContext()).getNumber()
									+" + "+effgroup.getRange()
									+" + "+effgroup.getDisplayType()
									+",<br>";
								} while(true);

								if (tmpStg3 == null) { tmpStg3="&nbsp;"; }
								if (tmpStg3.endsWith(",<br>")) { tmpStg3 = tmpStg3.substring(0, tmpStg3.length() - 5); }
							}
						}

						AffectedActivityData affData2;

						tmpStg = tmpStg1+"|"+tmpStg2+"|"+tmpStg3;
						chgData2.add(tmpStg);
					}
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }

		return chgData2;
	}
}
