We're trying to load some dynamic content (Component) to one of our pages, for most of the cases fetching the metadata based on tcmId
and using WebComponentMetaFactory.getMeta(tcmid)
would suffice, but now we need to also retrieve the whole component, to fetch some information that's not included as part of the metadata.
Attempt 1, using WebComponentPresentationFactory
and the getComponentPresentation
, getComponentPresentationWithHighestPriority
, getComponentPresentationWithLowestPriority
will always return null
(it cannot find the ComponentPresentation for publication: {publicationID} Component: {itemID} and template: {itemID})
Attempt 2, using DD4T's ComponentPresentation is also returning always a null
value (it cannot find the ComponentPresentation for publication: {publicationID} Component: {itemID} and template: {itemID})
Attempt 3, using WebTaxonomiFactory
will execute the call to the backend, but it is searching to all components of that type, but it will return null
anyhow.
What's the proper way to retrieve a component based on the tcm id's obtained after executing a broker query?
EDIT: Got the code from one of our Java developers
So this is the current implementation which works, this is to retrieve the component's metadata
protected List<ComponentMetadata> executeQuery(CorpSimpleBrokerQuery simpleBrokerQuery) throws SerializationException {
BrokerQuery query = new QueryImpl(this.buildCriteria(simpleBrokerQuery));
String[] ids = null;
try {
ids=query.executeQuery();
} catch (StorageException var10) {
LOG.warn("Exception while execution of broker query", var10);
return Collections.emptyList();
}
int idListLength = (ids == null) ? 0 : ids.length;
WebComponentMetaFactory cmf = new WebComponentMetaFactoryImpl(simpleBrokerQuery.getPublicationId());
List<ComponentMetadata> results = new ArrayList();
for(int i = 0; i < idListLength; i++) {
ComponentMeta componentMeta = cmf.getMeta(ids[i]);
//Fetching and parsing components appropiately
}
Attempt 1 - Using WebComponentPresentationFactory
protected List<ComponentMetadata> executeQuery(CorpSimpleBrokerQuery simpleBrokerQuery) throws SerializationException {
BrokerQuery query = new QueryImpl(this.buildCriteria(simpleBrokerQuery));
String[] ids = null;
try {
ids=query.executeQuery();
} catch (StorageException var10) {
LOG.warn("Exception while execution of broker query", var10);
return Collections.emptyList();
}
int idListLength = (ids == null) ? 0 : ids.length;
WebComponentPresentationFactory wcpf = new WebComponentPresentationFactoryImpl(simpleBrokerQuery.getPublicationId());
for(int i = 0; i < idListLength; i++) {
//Presentation will be null
ComponentPresentation presentation = wcpf.getComponentPresentation(ids[i], "tcm:0-0-0");
Object giveMeSomething = presentation.getContent();
}
}
Attempt 2 - Using DD4T ComponentFactory
@Autowired
ComponentPresentationFactory dd4tComponentPresentationFactory;
protected List<ComponentMetadata> executeQuery(CorpSimpleBrokerQuery simpleBrokerQuery) throws SerializationException {
BrokerQuery query = new QueryImpl(this.buildCriteria(simpleBrokerQuery));
String[] ids = null;
try {
ids=query.executeQuery();
} catch (StorageException var10) {
LOG.warn("Exception while execution of broker query", var10);
return Collections.emptyList();
}
int idListLength = (ids == null) ? 0 : ids.length;
WebComponentPresentationFactory wcpf = new WebComponentPresentationFactoryImpl(simpleBrokerQuery.getPublicationId());
for(int i = 0; i < idListLength; i++) {
//Presentation will be null
org.dd4t.contentmodel.ComponentPresentation presentation = dd4tComponentPresentationFactory.getComponentPresentation(ids[i], "tcm:0-0-0");
Object giveMeSomething = presentation.getComponent();
}
}