public URI uriForBean()

in taverna-scufl2-api/src/main/java/org/apache/taverna/scufl2/api/common/URITools.java [141:248]


	public URI uriForBean(WorkflowBean bean) {
		if (bean == null)
			throw new NullPointerException("Bean can't be null");

		if (bean instanceof Root) {
			Root root = (Root) bean;
			if (root.getGlobalBaseURI() == null) {
				if (!(root instanceof WorkflowBundle))
					throw new IllegalArgumentException(
							"sameBaseAs is null for bean " + bean);
				((WorkflowBundle) root).newRevision();
			}
			return root.getGlobalBaseURI();
		}
		if (bean instanceof Child) {
			@SuppressWarnings("rawtypes")
			Child child = (Child) bean;
			WorkflowBean parent = child.getParent();
			if (parent == null)
				throw new IllegalStateException("Bean does not have a parent: "
						+ child);
			URI parentUri = uriForBean(parent);

			if (!parentUri.getPath().endsWith("/"))
				parentUri = parentUri.resolve(parentUri.getPath() + "/");
			String relation;
			if (child instanceof InputPort)
				relation = "in/";
			else if (child instanceof OutputPort)
				relation = "out/";
			else if (child instanceof IterationStrategyStack)
				relation = "iterationstrategy/";
			else
				// TODO: Get relation by container annotations
				relation = child.getClass().getSimpleName() + "/"; // Stupid fallback

			URI relationURI = parentUri.resolve(relation.toLowerCase());
			if (parent instanceof List) {
				@SuppressWarnings("rawtypes")
				int index = ((List) parent).indexOf(child);
				return parentUri.resolve(index + "/");
			}
			if (bean instanceof Named) {
				Named named = (Named) bean;
				String name = validFilename(named.getName());
				if (!(bean instanceof Port || bean instanceof Annotation))
					name = name + "/";
				return relationURI.resolve(name);
			} else if (bean instanceof DataLink) {
				DataLink dataLink = (DataLink) bean;
				Workflow wf = dataLink.getParent();
				URI wfUri = uriForBean(wf);
				URI receivesFrom = relativePath(wfUri,
						uriForBean(dataLink.getReceivesFrom()));
				URI sendsTo = relativePath(wfUri,
						uriForBean(dataLink.getSendsTo()));
				String dataLinkUri = MessageFormat.format(
						"{0}?{1}={2}&{3}={4}", DATALINK, FROM, receivesFrom,
						TO, sendsTo);
				if (dataLink.getMergePosition() != null)
					dataLinkUri += MessageFormat.format("&{0}={1}",
							MERGE_POSITION, dataLink.getMergePosition());
				return wfUri.resolve(dataLinkUri);
			} else if (bean instanceof BlockingControlLink) {
				BlockingControlLink runAfterCondition = (BlockingControlLink) bean;
				Workflow wf = runAfterCondition.getParent();
				URI wfUri = uriForBean(wf);

				URI start = relativePath(wfUri,
						uriForBean(runAfterCondition.getBlock()));
				URI after = relativePath(wfUri,
						uriForBean(runAfterCondition.getUntilFinished()));
				String conditionUri = MessageFormat.format(
						"{0}?{1}={2}&{3}={4}", "control", "block", start,
						"untilFinished", after);
				return wfUri.resolve(conditionUri);
			} else if (bean instanceof IterationStrategyStack) {
				return relationURI;
			} else if (bean instanceof IterationStrategyNode) {
				IterationStrategyNode iterationStrategyNode = (IterationStrategyNode) bean;
				parent = iterationStrategyNode.getParent();
				@SuppressWarnings("unchecked")
				List<IterationStrategyNode> parentList = (List<IterationStrategyNode>) parent;
				int index = parentList.indexOf(iterationStrategyNode);
				return parentUri.resolve(index + "/");
			} else if (bean instanceof ProcessorPortBinding) {
				// Named after the processor port, extract in/blah part.
				ProcessorPortBinding<?, ?> processorPortBinding = (ProcessorPortBinding<?, ?>) bean;
				ProcessorPort procPort = processorPortBinding
						.getBoundProcessorPort();
				if (procPort == null)
					throw new IllegalStateException(
							"ProcessorPortBinding has no bound processor port: "
									+ bean);
				URI procPortUri = relativeUriForBean(procPort,
						processorPortBinding.getParent().getBoundProcessor());
				return parentUri.resolve(procPortUri);
			} else
				throw new IllegalStateException(
						"Can't create URIs for child of unrecogized type " + bean.getClass());
		}
		if (bean instanceof Revision) {
			Revision revision = (Revision) bean;
			return revision.getIdentifier();
		}
		throw new IllegalArgumentException("Unsupported type "
				+ bean.getClass() + " for bean " + bean);
	}