Liferay DynamicQuery

My use case is create a custom portlet to show the latest web content and filter by journal article type. It will require to join 2 tables with journalarticle and journalcontentsearch for filter the content. I will show my partial code to join 2 table by uysing liferay dynamic query.

  1. public List getArticleList(ThemeDisplay themeDisplay, ConfigEntity config)
  2. {
  3.  
  4. List list = new ArrayList();
  5.  
  6. try {
  7.  
  8.      DynamicQuery dquery = DynamicQueryFactoryUtil.forClass(
  9.                          JournalContentSearch.class, "journalcontentsearch")
  10.      .setProjection(ProjectionFactoryUtil.property("articleId"));
  11.  
  12.      DynamicQuery query = DynamicQueryFactoryUtil.forClass(
  13.                          JournalArticle.class, "journalarticle")
  14.  
  15.     .add(PropertyFactoryUtil.forName("journalarticle.articleId").in(dquery))
  16.     .add(PropertyFactoryUtil.forName("journalarticle.companyId")
  17.                               .eq(themeDisplay.getCompanyId()))
  18.     .add(PropertyFactoryUtil.forName("journalarticle.groupId")
  19.                               .eq(themeDisplay.getScopeGroupId()))
  20.     .add(PropertyFactoryUtil.forName("journalarticle.type")
  21.                               .eq(config.getType()))
  22.     .add(PropertyFactoryUtil.forName("journalarticle.approved")
  23.                               .eq(new Boolean(true)))
  24.     .addOrder(OrderFactoryUtil.desc("createDate"));
  25.  
  26.   if(articles.size() > 0) {
  27.         JournalArticle article = null;
  28.         ArticleContent content= null;
  29.                                
  30.         for(Object obj : articles) {                           
  31.        
  32.               article = (JournalArticle)obj;           
  33.                                        
  34.               content= parseArticle(article, "en_US");
  35.        
  36.               if(content!= null) {
  37.                       list.add(contentItem);
  38.               }
  39.         }
  40.  }
  41.                        
  42. }catch (Exception e) {
  43.         if(log.isErrorEnabled()){                              
  44.                 log.error(e.getMessage());     
  45.         }       
  46. }
  47.  
  48. return list;
  49.  
  50.  

ArticleContent

  1. public class ArticleContent implements Comparable {
  2.         private String _title;
  3.         private String _url;
  4.         private Date _publishDate;
  5.         private String _imageUrl;
  6.         private String _ingress;
  7.         private String _displayDate;
  8.         private int _articleId;
  9.         private String _author;
  10.  
  11.  
  12.         …getter
  13.         …setter      
  14.  

Parse method

  1.  
  2. public ArticleContent parseArticle(JournalArticle journalArticle, String locale) throws PortalException, SystemException {
  3.  
  4. ArticleContent item = null;
  5. String content= journalArticle.getContentByLocale(locale);
  6. try
  7.    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");          
  8.    Document document = SAXReaderUtil.read(new StringReader(content));
  9.  
  10.    item.setDisplayDate(sdf.format(journalArticle.getDisplayDate()));
  11.    item.setPublishDate(journalArticle.getDisplayDate());
  12.    item.setArticleId(new Long(journalArticle.getId()).intValue());
  13.    String title = articleStructureParserService.parseField(document, "title");
  14.  
  15. }
  16. catch (DocumentException e)  {
  17.      log.error("Error parsing the structure. ",e);
  18. }                        
  19. catch (InvalidArticleTypeException e)
  20. {
  21.      log.error("Error parsing the structure. ",e);
  22. }                        
  23. return item;           
  24. }
  25.  
You can leave a response, or trackback from your own site.

Leave a Reply

Security Code: