Cách hiệu quả để lấy danh sách EntityModel (Java, DXA 2.0)
Oct 28 2020
Chúng tôi có một trang truy vấn các thực thể với các tiêu chí nhất định.
Cách chúng tôi triển khai nó là lấy danh sách các ID thực thể, lặp lại các ID để truy xuất mô hình thực thể cho mỗi ID và thêm nó vào danh sách.
Mã giả:
Query query = new Query(...);
// Get list of ids from query
List<String> ids = Arrays.asList(query.executeQuery());
final WebComponentMetaFactory cmf = new WebComponentMetaFactoryImpl(brokerQuery.getPublicationId());
// Get component meta
List<ComponentMeta> componentMetas = ids.stream().map(cmf::getMeta)
.collect(Collectors.toList());
// Get entity model per component and add it to the list
componentMetas.forEach(meta -> {
EntityModel entityModel = contentProvider.getEntityModel(meta.getId() + templateId, webRequestContext.getLocalization())
list.add(entityModel)
});
Điều này hoạt động, tuy nhiên theo quan điểm hiệu suất là rất chậm vì nó cần phải gọi contentProvider.getEntityModel(...)cho mọi ID.
Có một giải pháp tốt hơn cho điều này? Có lẽ một API từ DXA tạo danh sách EntityModel?
Trả lời
Velmurugan Nov 03 2020 at 06:06
Sử dụng API CIL bên dưới này để Tìm nạp nhiều nội dung DCP bằng một ví dụ lệnh gọi được cung cấp bên dưới dựa trên API tương tự .NET cũng có sẵn trong java.
string[] componentUris = query.ExecuteQuery();
var components =
componentUris
.Select(componentUri => { TcmUri uri = new TcmUri(componentUri); return (ComponentPresentation)new ComponentPresentationFactory(uri.PublicationId).FindAllComponentPresentations(componentUri)[0]; })
.Where(cp => cp != null)
.Select(cp => cp.Content)
.ToList();
Tôi hy vọng nó sẽ giúp.