/**
   * Retrieves a certain number of blog posts with information about the last <b>BlogPost</b>-s that
   * are made under an <b>BlogSite</b> that is received as parameter.
   */
  @Override
  public Model getModel() throws ModelException {
    BlogPostListModel model = new BlogPostListModel();
    if (WebEngine.getActiveContext() != null) {
      WebContext ctx = WebEngine.getActiveContext();
      CoreSession session = ctx.getCoreSession();
      DocumentModel documentModel = ctx.getTargetObject().getAdapter(DocumentModel.class);

      SimpleDateFormat simpleMonthFormat =
          new SimpleDateFormat("dd MMMM yyyy", WebEngine.getActiveContext().getLocale());
      try {
        DocumentModel blogSite = SiteUtils.getFirstWebSiteParent(session, documentModel);
        DocumentModelList blogPosts =
            SiteQueriesCollection.queryLastModifiedPages(
                session,
                blogSite.getPathAsString(),
                BLOG_POST_DOC_TYPE,
                BLOG_POST_DOC_TYPE.equals(documentModel.getType()) ? noForBlogPost : noForBlogSite);

        for (DocumentModel blogPost : blogPosts) {
          String title = SiteUtils.getString(blogPost, "dc:title");
          String path = SiteUtils.getPagePath(blogSite, blogPost);

          String description = SiteUtils.getString(blogPost, "dc:description");

          String content =
              SiteUtils.getFistNWordsFromString(
                  SiteUtils.getString(blogPost, WEBPAGE_CONTENT), Integer.MAX_VALUE);
          String author = SiteUtils.getString(blogPost, "dc:creator");

          GregorianCalendar creationDate = SiteUtils.getGregorianCalendar(blogPost, "dc:created");

          String day = getWeekDay(creationDate.get(Calendar.DAY_OF_WEEK));
          BlogSiteArchiveDayModel dayModel = getYearModel(model, day);
          if (dayModel == null) {
            dayModel =
                new BlogSiteArchiveDayModel(
                    day, simpleMonthFormat.format(creationDate.getTime()), 0);
            model.addItem(dayModel);
          }
          dayModel.increaseCount();

          BlogPostModel blogPostModel =
              new BlogPostModel(title, path, description, content, author);
          dayModel.addItem(blogPostModel);
        }
      } catch (Exception e) {
        throw new ModelException(e);
      }
    }
    return model;
  }
 /**
  * Utility method used to return the day of the week as a string representation.
  *
  * @param day day of the week as integer
  * @return day of the week as string
  */
 private static String getWeekDay(int day) {
   if (weekDays == null) {
     DateFormatSymbols dfs = new DateFormatSymbols(WebEngine.getActiveContext().getLocale());
     weekDays = dfs.getWeekdays();
   }
   return weekDays[day];
 }
Exemplo n.º 3
0
 public static List<String> getTemplates() {
   LabsThemeManager themeService = getThemeService();
   List<String> entriesTemplate = new ArrayList<String>();
   if (themeService != null) {
     entriesTemplate =
         themeService.getTemplateList(
             WebEngine.getActiveContext().getModule().getRoot().getAbsolutePath());
   }
   if (entriesTemplate.isEmpty()) {
     LOG.error("The themes should not be empty !");
     LOG.error("Verify the package " + LabsSiteWebAppUtils.DIRECTORY_THEME);
   }
   return entriesTemplate;
 }
Exemplo n.º 4
0
 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     throws IOException, ServletException {
   initIfNeeded();
   if (request instanceof HttpServletRequest) {
     HttpServletRequest req = (HttpServletRequest) request;
     HttpServletResponse resp = (HttpServletResponse) response;
     PathDescriptor pd = engine.getRequestConfiguration().getMatchingConfiguration(req);
     Config config = new Config(req, pd);
     AbstractWebContext ctx = initRequest(config, req, resp);
     if (config.txStarted) {
       resp = new BufferingHttpServletResponse(resp);
     }
     boolean completedAbruptly = true;
     try {
       preRequest(req, resp);
       chain.doFilter(request, resp);
       postRequest(req, resp);
       completedAbruptly = false;
     } finally {
       if (completedAbruptly) {
         TransactionHelper.setTransactionRollbackOnly();
       }
       try {
         cleanup(config, ctx, req, resp);
       } catch (TransactionRuntimeException e) {
         // commit failed, report this to the client before stopping buffering
         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
         throw e;
       } finally {
         if (config.txStarted) {
           ((BufferingHttpServletResponse) resp).stopBuffering();
         }
       }
     }
   } else {
     chain.doFilter(request, response);
   }
 }
Exemplo n.º 5
0
 private static CoreSession getCoreSession() {
   return WebEngine.getActiveContext().getCoreSession();
 }