package com.sap.wcm.examples.using-api.services.scheduling;


import java.util.Properties;

import com.sapportals.wcm.WcmException;
import com.sapportals.wcm.repository.ResourceFactory;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.ResourceContext;
import com.sapportals.wcm.repository.IResourceContext;
import com.sapportals.wcm.service.scheduler.ISchedulerTask;
import com.sap.tc.logging.Location;


/**
* A scheduler task example.
*
* Copyright (c) SAP AG 2003
*/ public class SchedulerTaskExample implements ISchedulerTask { // ---------------- // Static Variables --------------------------------------------------------- // ---------------- private static Location TRACE = Location.getLocation(SchedulerTaskExample.class);
private void checkRID(RID rid) throws WcmException { // Get the context for anonymous user.
IResourceContext context = new ResourceContext(null);

// Try to retrieve the resource for the given RID.
IResource resource = ResourceFactory.getInstance().getResource(rid, context);
if( resource != null ) {
if( resource.isCollection() ) {
TRACE.warningT("SchedulerTaskExample: resource '" + rid.toString() + "' is a collection");
} else {
TRACE.warningT("SchedulerTaskExample: resource '" + rid.toString() + "' found!");
}
} else {
TRACE.warningT("SchedulerTaskExample: resource '" + rid.toString() + "' NOT found");
}
}


// ------------------------------------------------------------------------
/**
* The run() method is called by the scheduler, when the
* task is scheduled.
* @param id a String with the task's id from the SchedulerTask configurable.
* @param properties the Properties as given for this id in the
* SchedulerTask configurable.
*/
public void run(String id, Properties properties) {
try {
  String ridString = properties.getProperty("rid");

      if( ridString == null ) {
TRACE.warnT("SchedulerTaskExample: no 'rid' property specified");
return;
}

checkRID(new RID(ridString));
}
catch( Exception e ) {
TRACE.errorT("SchedulerTaskExample: caught exception " + e.getMessage());
}
}

}