public class ComponentLink extends AbstractLink
ComponentLink
class provides functionality to validate
and resolve links to ComponentPresentations on Pages. After a link has been resolved,
the PageLink class is used to link to the targetted page.
The goal of the ComponentLink is to create a link to another (published) instance of a component, having a different template ID than the one supplied as a parameter.
From all candidates, the one with the highest priority is selected. If more than one is left, the first one on the same page as the page parameter is selected: if there is no one on the same with the same page, then the first one having the same path as supplied page is selected.
If still no link is found, the link which is nearest the current page is returned. Otherwise, the latest published Component Presentation with the highest priority is selected. If no other published instance of the supplied component is found, then the link can't resolve.
Constructor and Description |
---|
ComponentLink(int publicationId)
Creates a new ComponentLink instance.
|
ComponentLink(String publicationURI)
Creates a new ComponentLink instance.
|
ComponentLink(String namespace,
int publicationId,
boolean relativeLink)
Creates a new ComponentLink instance.
|
Modifier and Type | Method and Description |
---|---|
protected int |
buildScore(String basePageURL,
String foundLinkURL)
Assigns a score to a Link URL (second parameter) based on its proximity to the Page URL (first parameter).
|
protected com.tridion.storage.LinkInfo |
findBestMatch(List<com.tridion.storage.LinkInfo> filteredLinkInfo,
int sourcePageId,
String sourcePageURL)
This method implements steps 3 - 6 of the business logic for linking.
|
Link |
getLink(int targetComponentId)
Gets a URL to the first page that is found containing the requested
targetComponentId.
|
Link |
getLink(int sourcePageId,
int targetComponentId,
int excludeTemplateId,
String linkTagAttributes,
String linkText,
boolean showTextOnFail,
boolean addAnchor)
Tries to create a link to a suitable component presentation on a page
and returns this as a
Link object. |
Link |
getLink(String targetComponentURI)
Gets a URL to the first page that is found containing the requested
targetComponentId.
|
Link |
getLink(String sourcePageURI,
String targetComponentURI,
String excludeTemplateURI,
String linkTagAttributes,
String linkText,
boolean showTextOnFail,
boolean showAnchor)
Tries to create a link to a suitable component presentation on a page
and returnes this as a
Link object. |
String |
getLinkAsString(int sourcePageId,
int targetComponentId,
int excludeTemplateId,
String linkTagAttributes,
String linkText,
boolean showTextOnFail)
Calls the public method getLinkAsString with an additional
parameter for the anchor with the value false.
|
String |
getLinkAsString(int sourcePageId,
int targetComponentId,
int excludeTemplateId,
String linkTagAttributes,
String linkText,
boolean showTextOnFail,
boolean showAnchor)
Creates a new Link instance and returnes this as a
String . |
String |
getLinkAsString(String pageURI,
String componentURI,
String templateURI,
String linkTagAttributes,
String linkText,
boolean showTextOnFail)
Tries to create a link to a suitable component presentation on a page
and returnes this as a
Link object. |
String |
getLinkAsString(String sourcePageURI,
String targetComponentURI,
String excludeTemplateURI,
String linkTagAttributes,
String linkText,
boolean showTextOnFail,
boolean showAnchor)
Creates a new Link instance and returnes this as a
String . |
String |
getLinkAsString(String sourcePageURI,
String targetComponentURI,
String excludeTemplateURI,
String linkTagAttributes,
String linkText,
boolean showTextOnFail,
boolean showAnchor,
LinkStringStyle style)
Creates a new Link instance and returnes this as a
String . |
protected List<com.tridion.storage.LinkInfo> |
getLinkInfo(int publicationId,
int componentId)
Loads the ComponentLinkInfo for a component in a publication.
|
getBaseNamespaceId, getErrorList, getFullyQualifiedURL, getLinking, getNamespace, getNamespaceService, getPublicationId, getURL, isRelativeLink, isWrappedCacheDAO, setPublicationId
public ComponentLink(int publicationId)
publicationId
- The ID of the publication that is targeted.public ComponentLink(String namespace, int publicationId, boolean relativeLink)
namespace
- The namespace of the publication that is targetedpublicationId
- The ID of the publication that is targeted.relativeLink
- If true then the realtive URL will be returned from a Link
object
despite on linking configuration.public ComponentLink(String publicationURI)
publicationURI
- The TCM:URI of the publication that is targeted.public String getLinkAsString(int sourcePageId, int targetComponentId, int excludeTemplateId, String linkTagAttributes, String linkText, boolean showTextOnFail)
sourcePageId
- The ID of the page where this link is inserted on.targetComponentId
- The ID of the component to link to.excludeTemplateId
- The ID of the component template used in this
component presentation. Component presentations having the component template
specified here are not used as a target for this link. This is why you
usually specify the template id of the source component presentation here.linkTagAttributes
- Attributes to include in the link.linkText
- The text that builds up the link.showTextOnFail
- Indicates if the linkText needs to be returned .
in case no link can be build: an empty string is returned if false.String
the link (<a href="..">... </a>),
linktext or empty string.public String getLinkAsString(int sourcePageId, int targetComponentId, int excludeTemplateId, String linkTagAttributes, String linkText, boolean showTextOnFail, boolean showAnchor)
String
.sourcePageId
- The ID of the page where this link is inserted on.targetComponentId
- The ID of the component to link to.excludeTemplateId
- The ID of the component template used in this
component presentation. Component presentations having the component template
specified here are not used as a target for this link. This is why you
usually specify the template id of the source component presentation here.linkTagAttributes
- Attributes to include in the link.linkText
- The text that builds up the link.showTextOnFail
- Indicates if the linkText needs to be returned .
in case no link can be build: an empty string is returned if false.showAnchor
- Indicates if an showAnchor should be used to point to the position of
the targetted component presentation on a page.String
the link (<a href="..">... </a>),
linktext or empty string.public String getLinkAsString(String sourcePageURI, String targetComponentURI, String excludeTemplateURI, String linkTagAttributes, String linkText, boolean showTextOnFail, boolean showAnchor, LinkStringStyle style)
String
.sourcePageURI
- The TCM:URI of the page where this link is inserted on.targetComponentURI
- The TCM:URI of the component to link to.excludeTemplateURI
- The TCM:URI of the component template used in this
component presentation. Component presentations having the component template
specified here are not used as a target for this link. This is why you
usually specify the template TCM:URI of the source component presentation here.linkTagAttributes
- Attributes to include in the link.linkText
- The text that builds up the link.showTextOnFail
- Indicates if the linkText needs to be returned .
in case no link can be build: an empty string is returned if false.showAnchor
- Indicates if an showAnchor should be used to point to the position of
the targeted component presentation on a page.style
- link styleString
the link (<a href="..">... </a>),
linktext or empty string.public String getLinkAsString(String sourcePageURI, String targetComponentURI, String excludeTemplateURI, String linkTagAttributes, String linkText, boolean showTextOnFail, boolean showAnchor)
String
.sourcePageURI
- The TCM:URI of the page where this link is inserted on.targetComponentURI
- The TCM:URI of the component to link to.excludeTemplateURI
- The TCM:URI of the component template used in this
component presentation. Component presentations having the component template
specified here are not used as a target for this link. This is why you
usually specify the template TCM:URI of the source component presentation here.linkTagAttributes
- Attributes to include in the link.linkText
- The text that builds up the link.showTextOnFail
- Indicates if the linkText needs to be returned .
in case no link can be build: an empty string is returned if false.showAnchor
- Indicates if an showAnchor should be used to point to the position of
the targeted component presentation on a page.String
the link (<a href="..">... </a>),
linktext or empty string.public String getLinkAsString(String pageURI, String componentURI, String templateURI, String linkTagAttributes, String linkText, boolean showTextOnFail)
Link
object.pageURI
- The TCM:URI of the page where this link is inserted on.componentURI
- The TCM:URI of the component to link to.templateURI
- The TCM:URI of the component template used in this
component presentation.linkTagAttributes
- Attributes to include in the link.linkText
- The text that builds up the link.showTextOnFail
- Indicates if the linkText needs to be returned .
in case no link can be build: an empty string is returned if false.String
the link (<a href="..">... </a>),
linktext or empty string.public Link getLink(String sourcePageURI, String targetComponentURI, String excludeTemplateURI, String linkTagAttributes, String linkText, boolean showTextOnFail, boolean showAnchor)
Link
object.sourcePageURI
- The TCM:URI of the page where this link is inserted on.targetComponentURI
- The TCM:URI of the component to link to.excludeTemplateURI
- The TCM:URI of the component template used in this
component presentation. Component presentations having the component template
specified here are not used as a target for this link. This is why you
usually specify the template id of the source component presentation here.linkTagAttributes
- Attributes to include in the link.linkText
- The text that builds up the link.showTextOnFail
- Indicates if the linkText needs to be returned .
in case no link can be build: an empty string is returned if false.showAnchor
- Indicates if an showAnchor should be used to point to the position of
the targetted component presentation on a page.Link
object that resolves if the link was resolved.public Link getLink(int targetComponentId)
targetComponentId
- Component ID for the component that needs to found.Link
object that resolves if the link was resolved.public Link getLink(String targetComponentURI)
targetComponentURI
- Component URI for the component that needs to found.Link
object that resolves if the link was resolved.public Link getLink(int sourcePageId, int targetComponentId, int excludeTemplateId, String linkTagAttributes, String linkText, boolean showTextOnFail, boolean addAnchor)
Link
object. Component Presentations using the Component
with the ID passed as the componentID parameter and the Component Template with the ID
passed in the componentTemplateID parameter will NOT be linked to.>a href
element.sourcePageId
- The ID of the page you want to create a link from.targetComponentId
- The ID of the component you want to link to.excludeTemplateId
- The ID of the component template used in this
Component Presentation. Component presentations having the component template
specified here are not used as a target for this link. This is why you
usually specify the template id of the source component presentation here.linkTagAttributes
- Attributes to add to the >a href
element.linkText
- The text that will be displayed in the link (the text between the >
a
tags).showTextOnFail
- Sets whether or not the link text will be displayed if no suitable link can
be resolved.addAnchor
- Sets whether or not the addAnchor to the component will be added to the link.Link
object that resolves if the link was resolved.protected com.tridion.storage.LinkInfo findBestMatch(List<com.tridion.storage.LinkInfo> filteredLinkInfo, int sourcePageId, String sourcePageURL)
filteredLinkInfo
- A collection containing all possible links, with the current template filtered outsourcePageId
- The ID of the page the link originates fromsourcePageURL
- The URL of the page the link originates fromprotected int buildScore(String basePageURL, String foundLinkURL)
ComponentLink.SUBDIR_SCORE
points for each directory below the current directory, plus
ComponentLink.HIGHER_DIR_SCORE
points for each directory we have to go up, to find a valid link
./root/ a / d / e / f / g / b / h / i / j / / k / l / c / m / n / o / / p / q / / r / s / t /If a component is on a page in directory d, and also on another page in directory d, the score would be 0.
basePageURL
- The URL of the page to link fromfoundLinkURL
- The URL of the page you wish to compareprotected List<com.tridion.storage.LinkInfo> getLinkInfo(int publicationId, int componentId)
publicationId
- The ID of the publication.componentId
- The ID of the component.Copyright (c) 2014-2021 All Rights Reserved by the RWS Group for and on behalf of its affiliates and subsidiaries