if (!isConnected()) {
throw new IllegalStateException(
"The BPEL Engine has not configured.");
}
Workflow workflow = new Workflow();
switch (workflowType) {
case TEMPLATE:
workflow.setGPELTemplateID(id);
break;
case INSTANCE:
workflow.setGPELInstanceID(id);
break;
}
// This is either a workflow template or a workflow instance.
GcTemplate workflowTemplate;
GcInstance workflowInstance = null;
try {
switch (workflowType) {
case TEMPLATE:
workflowTemplate = this.client.retrieveTemplate(id);
break;
case INSTANCE:
// TODO change this when we start modifying the instance.
workflowInstance = this.client.retrieveInstance(id);
GcWebResource templateResource = workflowInstance
.getLinkWithRel(GpelConstants.REL_TEMPLATE);
URI templateID = templateResource.getId();
workflow.setGPELTemplateID(templateID);
workflowTemplate = this.client.retrieveTemplate(templateID);
break;
default:
// This doesn't happen
throw new XBayaRuntimeException();
}
} catch (GcResourceNotFoundException e) {
// The workflow was not found in the engine.
throw new WorkflowEngineException(
ErrorMessages.GPEL_WORKFLOW_NOT_FOUND_ERROR, e);
} catch (RuntimeException e) {
throw new WorkflowEngineException(ErrorMessages.GPEL_ERROR, e);
}
GcProcessResource processResource = null;
if (workflowInstance != null) {
processResource = (GcProcessResource) workflowInstance
.getLinkWithRel(GpelConstants.REL_PROCESS);
}
if (processResource == null) {
processResource = (GcProcessResource) workflowTemplate
.getLinkWithRel(GpelConstants.REL_PROCESS);
}
GcXmlWebResource graphResource = null;
if (workflowInstance != null) {
graphResource = (GcXmlWebResource) workflowInstance
.getLinkWithRel(GPELLinksFilter.REL_XWF);
}
if (graphResource == null) {
graphResource = (GcXmlWebResource) workflowTemplate
.getLinkWithRel(GPELLinksFilter.REL_XWF);
}
GcBinaryWebResource imageResource = null;
if (workflowInstance != null) {
imageResource = (GcBinaryWebResource) workflowInstance
.getLinkWithRel(GPELLinksFilter.REL_IMAGE);
}
if (imageResource == null) {
imageResource = (GcBinaryWebResource) workflowTemplate
.getLinkWithRel(GPELLinksFilter.REL_IMAGE);
}
// Look for wsdl resource.
GcWsdlResource workflowWSDLresource = null;
Map<String, GcWsdlResource> wsdlResourceMap = new HashMap<String, GcWsdlResource>();
List<GcWebResource> links = workflowTemplate.getLinks();
for (GcWebResource link : links) {
String rel = link.getRel();
logger.finest("rel: " + rel);
if (GpelConstants.REL_WSDL.equals(rel)) {
GcWsdlResource wsdlResouece = (GcWsdlResource) link;
String wsdlTitle = wsdlResouece.getTitle();
logger.finest("wsdlTitle: " + wsdlTitle);
// Use use title to do matching
wsdlResourceMap.put(wsdlTitle, wsdlResouece);
}
}
// BPEL process
GpelProcess process = new GpelProcess(processResource.getXmlContent());
workflow.setGpelProcess(process);
// WSDL for the process
workflowWSDLresource = wsdlResourceMap.remove(PROCESS_WSDL_TYTLE);
WsdlDefinitions workflowWSDL = new WsdlDefinitions(workflowWSDLresource
.getXmlContent());
workflow.setWorkflowWSDL(workflowWSDL);
// WSDLs for services.
for (String wsdlID : wsdlResourceMap.keySet()) {
GcWsdlResource wsdlResource = wsdlResourceMap.get(wsdlID);
WsdlDefinitions definition = new WsdlDefinitions(wsdlResource
.getXmlContent());
workflow.addWSDL(wsdlID, definition);
}
// Graph
WSGraph graph = WSGraphFactory.createGraph(graphResource
.getXmlContent());
workflow.setGraph(graph);
workflow.bindComponents();
// Set the name of the workflow.
// This has to be after parsing the graph so that workflow instance name
// overwrites the name in the graph.
String name = null;
if (workflowInstance != null) {
name = workflowInstance.getTitle();
logger.finest("name from the instance: " + name);
}
if (name == null) {
name = workflowTemplate.getTitle();
logger.finest("name from the template: " + name);
}
workflow.setName(name);
// Image
if (imageResource != null) {
byte[] imageBytes = imageResource.getBinaryContent();
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(
imageBytes));
workflow.setImage(image);
} catch (IOException e) {
// Not crucial
logger.caught(e);
}
}