
import com.ptc.cipjava.*;
import com.ptc.cipjava.jxthrowable;

import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcSession.Session;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcGlobal.pfcGlobal;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcSolid.Solid;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcAssembly.*;
import com.ptc.pfc.pfcAssembly.Assembly;
import com.ptc.pfc.pfcComponentFeat.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcBase.*;
import com.ptc.pfc.pfcDimension.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcGeometry.*;
import com.ptc.pfc.pfcLayer.*;
import com.ptc.pfc.pfcExceptions.*;

import java.io.*;
import java.util.*;
import java.text.*;

public class Make_BOM_list3 {

	static Session curSession;
	static final String MSGFILE = "bom.txt";

	public static void start() {

		try {
			curSession = pfcGlobal.GetProESession();
			UICommand inputCommand = curSession.UICreateCommand("Make BOM List", new BOM_list_Listener3(curSession));
			curSession.UIAddButton(inputCommand,
									"Applications",
									"Applications.psh_util_pproc",
									"Make_BOM_List",
									"Create_BOM_List",
									MSGFILE);
		}
		catch(jxthrowable x) {
			System.out.println("Exception:"+x);
		}
	}

	public static void stop() {}
}

class BOM_list_Listener3 extends DefaultUICommandActionListener {

	static Session session;
	static Model model;
	static Assembly assy;
	static Models models;

	public BOM_list_Listener3(Session sess) {
		session = sess;
	}

	public void OnCommand () {

		try {

			String CREATION_PARAM = "JLINK_TRACKER_CREATION_PARAM";
			String ACCESS_PARAM = "JLINK_TRACKER_ACCESS_PARAM";

	 		SimpleDateFormat df = new SimpleDateFormat ("MMddyy", Locale.getDefault());
			Date tm = new Date();
			String DateStg = df.format(tm);

			session=pfcGlobal.GetProESession();
			model=session.GetCurrentModel();
			String full_name = model.GetFullName();

			models = session.ListModels();

			int mc = models.getarraysize();

			Model model_i;

			String name, typeString, path, creationString, accessString;
			String fname, ftypeString, fstatusString;
			ModelType type;
			ModelDescriptor desc;
			Integer versionStamp, fid;
			DateFormat defaultDateFormat;
			Parameter creation, access;
			ParamValue creationValue, accessValue;
			FeatureType ftype;
			FeatureStatus fstatus;

			String inf_name = full_name + "_list." + DateStg + ".inf";

			try {
				PrintStream vf = new PrintStream(new FileOutputStream(inf_name));

		    	for (int i = 0; i< mc; i++) {

					model_i = models.get(mc);

					name = model_i.GetFullName();
					vf.println( "Model Name: " + name );

					type = model_i.GetType();
					typeString = Labeler2.pfcModelType[type.getValue()];
					vf.println( "Model Type: " + typeString );

					versionStamp = new Integer(model_i.GetVersionStamp());
					vf.println( "Version Stamp: " + versionStamp );

					defaultDateFormat = DateFormat.getDateTimeInstance();
					creation = model_i.GetParam(CREATION_PARAM);

					if (creation == null) {
						creationValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
						creation = model_i.CreateParam(CREATION_PARAM, creationValue);
					}

					creationValue = creation.GetValue();
					creationString = creationValue.GetStringValue();
					vf.println( "Creation Date: " + creationString );

					access = model_i.GetParam(ACCESS_PARAM);

					if (access == null) {
						accessValue = pfcModelItem.CreateStringParamValue("NEW");
						access = model_i.CreateParam(ACCESS_PARAM, accessValue);
					}

					accessValue = access.GetValue();
					accessString = accessValue.GetStringValue();
					vf.println( "Access Date: " + accessString );

					accessValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
					access.SetValue(accessValue);
      			}

				vf.flush();
				vf.close();
			}
			catch(java.io.IOException IOEx) {
				System.out.println("Cannot create " + inf_name + " file.");
			}
		}
		catch (jxthrowable x) {
			System.out.println ("Caught exception: "+x);
			x.printStackTrace ();
		}
	}
}
