Create a Standard Service Class
Save the standard service class to:
<Windchill>/src/com/acme/listen/StandardListenService.java
The example below has comments that indicate "standard" pieces of code that should not be changed as they are necessary to interact with the standard service manager and other services. There are other comments that indicate pieces of code that should be changed, for example, to reflect your choice of package and class names and to listen to the particular events you are interested in. Here is the source code:
/* Change to your package name */
package com.acme. listen;

/* javac -classpath z:/Windchill/codebase -d z:/Windchill/codebase *.java */
/* xconfmanager —t codebase/wt.properties -s

wt.services.service.4160=com.acme.listen.Listenservice/com.acme.listen.StandardList
enservice -p */

/* Change to import you Service Interface */
import com.acme.listen.ListenService;
import java.lang.String;

/* Standard imports for services */
import wt.services.ManagerException;
import wt.services.ServiceEventListenerAdapter;
import wt.services.StandardManager;
import wt.session.SessionContext;
import wt.session.SessionHelper;
import wt.session.SessionServerHelper;
import wt.util.DebugFlag;
import wt.util.DebugProperties;
import wt.util.DebugType;
import wt.util.DebugWriter;
import wt.util.WIException;
import wt.util.WTMessage;


/* Changes required here:
* Imports necessary for listening to particular events, in this case
* pre delete events */
import wt.fc.PersistenceManagerEvent;
import wt.fc.collections.WTCollection;

/**
* Listens for pre delete event. When an event is
* "heard", prints messages to the MethodServer log and console.
* <p>
* Use the <code>newStandardlListenService</code> static factory method (s),
* not the <code>StandardListenService</code> constructor, to construct
* instances of this class. Instances must be constructed using the static
* factory(s), in order to ensure proper initialization of the instance.
* <p>
**/

/* Change to your class name */
public final class StandardListenService extends StandardManager
implements ListenService {

/* Standard class identification string */

private static final String CLASSNAME =
StandardListenService.class.getName () ;

/* Standard debug variables for services */
private static final boolean DEBUG = DebugProperties.isDebugOn (CLASSNAME) ;
private static final DebugWriter LOG =
(DEBUG ? DebugProperties.getWriter (CLASSNAME) : null);

/* Standard getter for class name */

public String getConceptualClassname() {
return CLASSNAME;
}

/*
* Standard default factory for the class.
*
*/

public static StandardListenService newStandardlistenService()
throws WTException {
StandardListenService instance = new StandardListenService();
instance. initialize ();
return instance;
}

/*
* Initializations of event listeners
*/

/* Override inherited method */
protected void performStartupProcess() throws ManagerException {

/* Standard debug output */
if (DEBUG && DebugProperties.isTrace (this))
LOG.enter(CLASSNAME, "performStartupProcess”) ;

/* Standard way to become admin */
SessionContext prev = SessionContext.newContext ();
try{
SessionHelper.manager.setidministrator ();
}

catch (WTException wte) {
System.err.println("StandardListenService: failed to set Administrator (ok
if installation)");
return;
}
finally {
SessionContext.setContext (prev) ;
}

/* Change: Add event listeners here */

getManagerService().addEventListener(
new ServiceEventListenerAdapter(this.getClass().getName()) {
@Override
public void notifyVetoableMultiObjectEvent(Object event) throws WTException {
PersistenceManagerEvent pmEvent =(PersistenceManagerEvent)event;
WTCollection targets = (WTCollection)pmEvent.getEventTarget();

WTCollection mdEntityList = targets.subCollection(MDEntityDemo.class, true);
if(mdEntityList.size() > 0)
{
String errorMessage = "People or Place Objects can not be deleted";
throw new WTException(errorMessage);
}

}
}, PersistenceManagerEvent.generateEventKey(
PersistenceManagerEvent.class.getName(), PersistenceManagerEvent.PRE_DELETE));

/* Standard debug output */
if (DEBUG && DebugProperties.isTrace (this))
LOG.exit(CLASSNAME, "performStartupProcess”) ;
}
}
Was this helpful?