/*
 * GoodrichBOMTemplateProcessor.java
 *
 * Created on November 17, 2004, 9:25 AM
 */

package ext.reports;
import wt.part.BOMHierarchyTemplateProcessor;
import wt.part.BOMTemplateProcessor;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Properties;
import wt.fc.WTObject;
import wt.util.*;
import wt.part.*;
import wt.change2.*;
import wt.fc.QueryResult;
import java.text.SimpleDateFormat;
import java.util.*;
import com.infoengine.au.NamingService;
import com.infoengine.object.factory.*;
import com.infoengine.SAK.Task;
import com.infoengine.util.IEException;
import com.infoengine.object.factory.Group;
import com.infoengine.SAK.Task;
import java.util.Enumeration;
import wt.vc.*;
import wt.fc.*;
import wt.util.*;

/**
 *
 * @author  antonio.villanueva
 */
public class GoodrichBOMTemplateProcessor extends BOMHierarchyTemplateProcessor {

    /** Creates a new instance of GoodrichBOMTemplateProcessor */
    public GoodrichBOMTemplateProcessor() throws WTException {
        //Line below calls out where the BOM HTML template is located
        BOMTemplateProcessor.setTemplateName("ext/reports/GoodrichBOMReport");
    }

    public void buildBOM(Properties properties, Locale locale, OutputStream outputStream)
    throws WTException, WTPropertyVetoException
    {
        PrintWriter printWriter = getPrintWriter(outputStream, locale);
        wt.visitor.Navigator navigator = new GoodrichSingleLevelWalker();
        //setConfigSpec();
        GoodrichPartConfigSpecNodeExpander expander=new GoodrichPartConfigSpecNodeExpander((WTPartConfigSpec)getConfigSpec());
        System.out.println("Config Spec: " + getConfigSpec().toString());
        navigator.setNodeExpander(expander);
        BOMHierarchyVisitor bomhierarchyvisitor = (BOMHierarchyVisitor)getVisitor(GoodrichHierarchyVisitor.class,locale,printWriter);
        navigator.setVisitor(bomhierarchyvisitor);
        bomhierarchyvisitor.preNavigateWrite();
        navigator.traverse((Persistable)getContextObj());
        bomhierarchyvisitor.postNavigateWrite();
        printWriter.flush();
    }

    //display title information for BOM
    public void displayBOMTitle(Properties properties, Locale locale, OutputStream outputStream)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z",locale);
        sdf.setTimeZone(WTContext.getContext().getTimeZone());
        if (getContextObj() instanceof WTPart)
        {
            WTPart part=(WTPart)getContextObj();
            wt.identity.DisplayIdentity displayIdentity = ((WTObject)getContextObj()).getDisplayIdentity();
            Object aobj[] = { displayIdentity.getLocalizedMessage(locale) };
            PrintWriter printWriter = getPrintWriter(outputStream, locale);
            printWriter.println("<TR><TD COLSPAN=3>");
            // Use a resource bundle call for the BOM title below
            // 95 calls out Single Level Bill of Materials for {0}
            printWriter.println(WTMessage.getLocalizedMessage("com.ptc.windchill.pdmlink.part.server.processors.processorsResource","95",aobj, locale));
            printWriter.println("</TD></TR>");
            printWriter.println("<TR><TD align=\"left\" COLSPAN=2>");
            printWriter.println("<B>Description:</B> " + part.getName());
            printWriter.println("</TD>");
            printWriter.println("<TD align=\"left\">");

            printWriter.println("<B>Last Updated:</B> " + sdf.format(part.getModifyTimestamp()));
            printWriter.println("</TD></TR>");
            printWriter.println("<TR><TD align=\"left\" COLSPAN=2>");
            printWriter.print("<B>View:</B> ");
            if (part.getViewName()!=null)
                printWriter.println(part.getViewName());
            else
                printWriter.println("NONE");
            printWriter.println("</TD>");
            printWriter.println("<TD align=\"left\">");
            printWriter.print("<B>Revision:</B> ");
            try {
                printWriter.println(VersionControlHelper.getVersionIdentifier((Versioned)part).getValue());
            }
            catch (WTException e) {}
            printWriter.println("</TD></TR>");
            printWriter.println("<TR><TD align=\"left\">");
            //Get Associated ECNs (WTChangeOrder2 objects)
            printWriter.print("<B>ECN Number:</B> ");
            try {
                QueryResult result=ChangeHelper2.service.getUniqueImplementedChangeOrders((Changeable2)part);
                if (result.size()>0) {
                    WTChangeOrder2 ecn=(WTChangeOrder2)result.nextElement();
                    printWriter.println(ecn.getNumber());
                }
            }
            catch (WTException e) {}
            printWriter.println("<TD align=\"left\">");
            printWriter.print("<B>Cage Code:</B> ");
            String cagecode="&nbsp;";
            try {
                Task task = new Task ("/customization/reports/Query-Part-CAGECODE.xml");
                task.setParam("rev",VersionControlHelper.getVersionIdentifier((Versioned)part).getValue());
                task.setParam("number",part.getNumber());
                task.invoke();
                Group group=task.getGroup("cagecode");
                if (group.getElementCount()>0) {
                    Element element=group.getElementAt(0); //should be only one hit
                    Enumeration attributes=element.getAtts();
                    int i=0;
                    while (attributes.hasMoreElements()) {
                        Att att=(Att)attributes.nextElement();;
                        if (att.getName().equals("IBA|CAGE_CODE")){
                            if (!(att.getValue().toString().equals(""))&&(att.getValue().toString()!=null))
                                cagecode=att.getValue().toString();
                        }
                    }
                }
            }
            catch (Exception e) {
                System.out.println("ERROR");
                e.printStackTrace();
            }
            printWriter.print(cagecode);
            printWriter.println("</TD>");
            printWriter.println("<TD align=\"left\">");
            printWriter.println("<B>Created On:</B> " + sdf.format(part.getCreateTimestamp()));
            printWriter.println("</TD></TR>");
            printWriter.flush();

        }
    }
}
