package com.ptc.tracker;
/*****************************************************************************\

FILE:    Tracker.java
PURPOSE: Tracks model information and displays this information in a dialog upon creation.

09-Apr-99    I-01-34     JCN   $$1   Created.

\*****************************************************************************/
import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcExceptions.*;

import java.util.*;
import java.text.DateFormat;



public class Tracker {

  TrackerDialog dialog;
  Vector [] modelData;
  

  static Session session;
  static Models models;
  
  // Constants
  public static final String CREATION_PARAM = "JLINK_TRACKER_CREATION_PARAM";
  public static final String ACCESS_PARAM = "JLINK_TRACKER_ACCESS_PARAM";

  public Tracker (Session inSession, Models inModels) throws jxthrowable
  {
    session = inSession;
    models = inModels;
    int modelCount = models.getarraysize();
    if (modelCount < 1)
      {
	printMsg("No  models in session");
	printMsg("Exiting");
	System.exit(-1);
      }

    modelData = new Vector [modelCount];
    
    for (int i = 0; i< modelCount; i++)
      {
      getModelInfo(i);
      }
    printMsg("Showing dialog");
    dialog = new TrackerDialog (modelData, modelCount);
  }


  /* Info collected and displayed:
   * Model FullName (String)
   * Model Type (String)
   * Directory path (String)
   * Model Version stamp (Integer)
   * Creation date (String)
   * Last access date (String)
   * Last feature created (String)
   * Last feature type (String)
   * Last feature id (Integer)
   * Last feature display status (String) */
     

  public void getModelInfo(int index) throws jxthrowable
  {
    Vector data_i;
    Model model_i;

    /* Model data */
    String name;
    ModelType type;
    String typeString;
    ModelDescriptor desc;
    String path;
    Integer versionStamp;
    DateFormat defaultDateFormat;
    Parameter creation;
    ParamValue creationValue;
    String creationString;
    Parameter access;
    ParamValue accessValue;
    String accessString;
    Feature last;
    String fname;
    FeatureType ftype;
    String ftypeString;
    Integer fid;
    FeatureStatus fstatus;
    String fstatusString;

    model_i = models.get(index);
    data_i = new Vector (10);

    /* Get model full name */
    name = model_i.GetFullName();
    printMsg("Getting information from model: "+name);
    data_i.addElement(name);

    /* Get model type */
    type = model_i.GetType();
    typeString = Labeler.pfcModelType[type.getValue()];
    data_i.addElement(typeString);

    /* Get directory path */
    desc = model_i.GetDescr();
    path = desc.GetPath();
    data_i.addElement(path);

    /* Get version stamp */
    versionStamp = new Integer(model_i.GetVersionStamp());
    data_i.addElement(versionStamp);

    /* Get creation date from a user parameter */
    defaultDateFormat = DateFormat.getDateTimeInstance();

   
    creation = model_i.GetParam(CREATION_PARAM);
    
    if (creation == null)
      {
	// Parameter does not yet exist
	creationValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
	creation = model_i.CreateParam(CREATION_PARAM, creationValue);
      }
    creationValue = creation.GetValue();
    creationString = creationValue.GetStringValue();
    data_i.addElement(creationString);
    
    /* Get access date from a user parameter */
    
    access = model_i.GetParam(ACCESS_PARAM);
    
    if (access == null)
      {
	// Parameter does not yet exist
	accessValue = pfcModelItem.CreateStringParamValue("NEW");
	access = model_i.CreateParam(ACCESS_PARAM, accessValue);
      }
    accessValue = access.GetValue();
    accessString = accessValue.GetStringValue();
    data_i.addElement(accessString);

    //Set the value to the current date
    accessValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
    access.SetValue(accessValue);
    
    if (typeString.equals("MDL_PART") || typeString.equals("MDL_ASSEMBLY"))
      {
	
	/* Get last feature */
	last = getLastFeature(model_i);
	if (last == null) 
	  {
	    modelData[index] = data_i;
	    return;
	  }
	fname = last.GetName();
	if (fname == null) fname = "no_name";
	data_i.addElement(fname);
    
	/* Get feature type */
	ftype = last.GetFeatType();
	ftypeString = Labeler.pfcFeatureType[ftype.getValue()];
	data_i.addElement(ftypeString);
	
	/* Get feature id */
	fid = new Integer(last.GetId());
	data_i.addElement(fid);
	
	/* Get feature status */
	fstatus = last.GetStatus();
	fstatusString = Labeler.pfcFeatureStatus[fstatus.getValue()];
	data_i.addElement(fstatusString);
      }

    modelData[index] = data_i;
    
    return;
  }
  
  private Feature getLastFeature (Model model) throws jxthrowable
  {
    int latest_id;
    int next_id;
    int featCount;
    Features feats;
    Feature latest;
    Feature next;

    feats = ((Solid)model).ListFeaturesByType(Boolean.FALSE, null);
    featCount = feats.getarraysize();

    if (featCount < 1)
      {
	printMsg("No features in solid model");
	return (null);
      }
	

    latest = feats.get(0);
    latest_id = latest.GetId();
    for (int i =1; i<featCount; i++)
      {
	next = feats.get(i);
	next_id = next.GetId();
	if (next_id > latest_id)
	  {
	    latest = next;
	    latest_id = next_id;
	  }
      }

    return (latest);
  }

  private void printMsg (String Msg)
  {
    System.out.println(Msg);
  }

}




