package com.hermanmiller.pdmlink.services;

import java.io.Serializable;
import java.lang.String;
import wt.services.StandardManager;
import wt.util.WTException;
import wt.lifecycle.LifeCycleHistory;
import wt.lifecycle.LifeCycleService;
import wt.lifecycle.ObjectHistory;
import wt.introspection.*;
import wt.epm.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.text.MessageFormat;
import wt.events.KeyedEvent;
import wt.events.KeyedEventBranch;
import wt.events.KeyedEventListener;
import wt.fc.PersistenceHelper;
import wt.fc.Persistable;
import wt.fc.ObjectReference;
import wt.fc.PersistenceServerHelper;
import wt.fc.PersistenceManagerEvent;
import wt.folder.FolderHelper;
import wt.project.Project;
import wt.services.ManagerService;
import wt.services.ManagerServiceFactory;
import wt.services.ServiceEventListenerAdapter;
import wt.services.ManagerException;
import wt.util.WTProperties;
import wt.util.WTContext;
import wt.util.WTPropertyVetoException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
import java.util.Properties;
import java.sql.*;
import oracle.jdbc.driver.*;
import wt.util.WTProperties;
import java.io.*;
import wt.lifecycle.LifeCycleServiceEvent;

public class StandardEPMReleaseService extends StandardManager implements EPMReleaseService, Serializable {

	String buildDate = "12/13/2007 @ 11:09:00";

	private static final String CLASSNAME = StandardEPMReleaseService.class.getName();

	private KeyedEventListener listener;

	public String getConceptualClassname() {
		return CLASSNAME;
	}

	public static StandardEPMReleaseService newStandardEPMReleaseService() throws WTException {
		StandardEPMReleaseService instance = new 
		StandardEPMReleaseService();

		instance.initialize();

		return instance;
	}

	class EPMReleaseEventListener extends ServiceEventListenerAdapter {

		public EPMReleaseEventListener(String manager_name) {
			super(manager_name);
		}

		public void notifyVetoableEvent(Object event) throws WTException {
			if (!(event instanceof KeyedEvent)) {
				return;
			}

			KeyedEvent keyedEvent = (KeyedEvent) event;
			Object target = keyedEvent.getEventTarget();

			if (keyedEvent.getEventType().equals(LifeCycleServiceEvent.STATE_CHANGE)) {
				processStateChangeEvent((Persistable) target);
			}
		}
	}

	protected void processStateChangeEvent(Persistable target) throws WTException {
		try {
			if (target instanceof EPMDocument) {
				EPMDocument tempEPM = (EPMDocument) target;
			
				if (tempEPM.getLifeCycleState().toString().equalsIgnoreCase("RELEASED")){

					//we have an EPMDocument that it's LifeCycle state was set to released
					//so lets get the part number and rev letter 

					String partNumber = tempEPM.getNumber().toLowerCase();
					String revLetter = tempEPM.getIterationDisplayIdentifier().toString();

					if (revLetter.indexOf('.') > 0) {
						String [] tempStr = revLetter.split("\\.");
						revLetter = tempStr[0];
				}

				//Now Insert your code to do something with this information

			}

		} catch (Exception e){
			e.printStackTrace();
		}
	}

	protected void performStartupProcess() throws ManagerException {
		System.out.println("Registering Service 
		com.hermanmiller.pdmlink.services.StandardEPMReleaseService BuildDate = " + buildDate);
		listener = new EPMReleaseEventListener(this.getConceptualClassname());

		getManagerService().addEventListener(listener, LifeCycleServiceEvent.generateEventKey(LifeCycleServiceEvent.STATE_CHANGE));
	}
}

