Sample Code
The following code is extracted from the AssignmentsBreadCrumbDelegate which generates the bread crumbs for an task (WorkItem) object.
public class AssignmentBreadCrumbDelegate implements BreadCrumbDelegate {
@Override
public boolean canHandle(Persistable contextObject, WTContainer container,
String url)
throws WTException {
if (contextObject instanceof WorkItem || contextObject instanceof
ActionItem) {
return true;
}
return false;
}
@Override
public AbstractBreadCrumbGenerator getGenerator(Persistable contextObject,
WTContainer container, String url) throws WTException {
return new AssignmentCrumbGenerator(p, container);
}
public class AssignmentCrumbGenerator extends AbstractBreadCrumbGenerator {
/**
* IDs for Assignment list components.
*/
protected static final String PRODUCT_ASSIGNMENTS_LIST_ID =
"netmarkets.product.assignments.list";
protected static final String LIBRARY_ASSIGNMENTS_LIST_ID =
"netmarkets.library.assignments.list";
protected static final String QUALITY_ASSIGNMENTS_LIST_ID =
"com.ptc.qms.listAssignments";
protected static final String PROJECT_ASSIGNMENTS_LIST_ID =
"project.assignments";
protected WTContainer container;
/**
* Initialize as a generator and set instance variable
* container so that the Assignments crumb can be made.
* First tries to set container to c. Next
* tries to set container to contextObject's container (if
* contextObject is WTContained). Next tries to set
* container to contextObject's source object's container.
* Lastly tries to set container to contextObject's
* primary business object container.
*
* @param contextObject
* @param c
* @throws WTException
*/
public AssignmentCrumbGenerator(Persistable contextObject,
WTContainer c)
throws WTException {
init();
// If no container is passed in, try to get it from contextObject.
if (c == null && contextObject!= null) {
WTContained containedItem = null;
if (contextObject instanceof WTContained) {
// This will either be an ActionItem (which is contained)
// or could possibly be a future WorkItem, if it ever
// implements WTContained.
containedItem = (WTContained) contextObject;
} else if (contextObject instanceof WorkItem) {
// Use the WorkItem's source if it is contained.
ObjectReference sourceRef = ((WorkItem)
contextObject).getSource();
if (sourceRef != null) {
Persistable source = sourceRef.getObject();
if (source instanceof WTContained) {
containedItem = (WTContained) source;
}
}
// If there is still no containedItem, try use the
// WorkItem's primary business object.
if (containedItem == null) {
PersistentReference primaryObjectRef = ((WorkItem)
contextObject)
.getPrimaryBusinessObject();
if (primaryObjectRef != null) {
Persistable primaryObject = primaryObjectRef
.getObject();
if (primaryObject instanceof WTContained) {
containedItem = (WTContained) primaryObject;
}
}
}
}
if (containedItem != null) {
c = containedItem.getContainer();
}
}
container = c;
}
@Override
public JSONArray getCrumbs() {
try {
addContainerCrumbs(container);
} catch (Exception e) {
log.error("Couldn't create the crumbs for container "
+ container, e);
addCrumb(getSecuredInfoCrumb());
}
try {
addCrumb(createAssignmentsCrumb());
} catch (Exception e) {
log.error("Couldn't create the Assignments Table crumb.", e);
addCrumb(getSecuredInfoCrumb());
}
return crumbs;
}
/**
* Makes the Assignments List crumb for a given assignment object.
*
* @return JSONObject
* @throws WTException
* @throws JSONException
*/
protected JSONObject createAssignmentsCrumb() throws JSONException,
WTException {
if (container == null) {
log.warn("The Assignments list crumb will not be made because
container is null.");
return null;
}
String url = getAssignmentsListUrl(container);
String localizedText = navigationBundle
.getString(navigationRB.ASSIGNMENTS);
return createCrumb(url, localizedText);
}
/**
* Given a container, will return a URL for the assignment list
table.
* Example:
* app/#ptc1/comp/netmarkets.product.assignments.list?
oid=OR%3Awt
* .pdmlink.PDMLinkProduct%3A12345&u8=1
*
* @param container
* @return String
*/
protected String getAssignmentsListUrl(WTContainer container) {
String url = "";
if (container instanceof PDMLinkProduct) {
url = MVCUrlHelper.getComponentURL(PRODUCT_ASSIGNMENTS_
LIST_ID);
} else if (container instanceof Project2) {
// Could be project or program. Same URL in either case.
url = MVCUrlHelper.getComponentURL(PROJECT_ASSIGNMENTS_
LIST_ID);
} else if (container instanceof WTLibrary) {
WTLibrary libObj = (WTLibrary) container;
if (libObj.isQMSTypeContainer()) {
// If Quality container then navigate to Quality
Assignment
// page
url = MVCUrlHelper
.getComponentURL(QUALITY_ASSIGNMENTS_LIST_ID);
} else {
// If Library container then navigate to Library
Assignment
// page
url = MVCUrlHelper
.getComponentURL(LIBRARY_ASSIGNMENTS_LIST_ID);
}
} else {
// This is the case where container is null or of unsupported
// container type.
return null;
}
url = url.replaceFirst("/", MVCUrlHelper.APPPREFIX);
String containerOid = "OR:"
+ container.getPersistInfo().getObjectIdentifier()
.toString();
try {
containerOid = URLEncoder.encode(containerOid, "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("Could not encode the container oid.", e);
}
url += "?oid=" + containerOid + "&u8=1";
return url;
}
}
}
Parent topic