All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogient.cameleon.sdk.community.blog.util.BlogModelToVoConverter Maven / Gradle / Ivy

The newest version!
package com.alogient.cameleon.sdk.community.blog.util;

import com.alogient.cameleon.sdk.common.dao.model.Culture;
import com.alogient.cameleon.sdk.community.blog.dao.model.Blog;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogArticle;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogArticleComment;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogArticleCulture;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogCulture;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogTag;
import com.alogient.cameleon.sdk.community.blog.vo.ArticleVo;
import com.alogient.cameleon.sdk.community.blog.vo.BlogVo;
import com.alogient.cameleon.sdk.community.blog.vo.CommentVo;
import com.alogient.cameleon.sdk.community.blog.vo.TagVo;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public final class BlogModelToVoConverter {
    
    /**
     * An utility class should not hava a visible ctor.
     * This private one is used to hide the default public ctor.
     */
    private BlogModelToVoConverter() {
    }


    /**
     * Used internally to convert a Blog model into a blog value object
     *
     * @param srcBlog  The source data from the DAO
     * @param numberOfArticles  the number of articles
     * @param culture  The culture to convert into  @return The converted value object
     * @return the blog vo
     */
    public static BlogVo convertToBlogVo(Blog srcBlog, int numberOfArticles, Culture culture) {

        BlogVo blogVo = new BlogVo();

        blogVo.setId(srcBlog.getId());
        blogVo.setOwnerId(srcBlog.getOwnerId());
        blogVo.setNumberOfArticles(numberOfArticles);
        blogVo.setOwnerFirstName(srcBlog.getOwnerFirstName());
        blogVo.setOwnerLastName(srcBlog.getOwnerLastName());
        blogVo.setCreationDate(srcBlog.getCreationDate());

        Set blogCultures = srcBlog.getBlogCultures();

        for (BlogCulture blogCulture : blogCultures) {
            if (blogCulture.getCulture().getCultureCode().equals(culture.getCultureCode())) {
                blogVo.setTitle(blogCulture.getTitle());
                blogVo.setText(blogCulture.getText());
                blogVo.setLastModified(blogCulture.getLastModified());
            }
        }

        return blogVo;
    }

    /**
     * Used internally to convert a list of BlogTag to a list of TagVo.
     *
     * @param srcTags the source data from the DAO
     * @return the list of tagVo
     */
    public static List convertToTagVo(Collection srcTags) {

        if ((srcTags == null) || (srcTags.size() == 0)) {
            return Collections.emptyList();
        }

        List tags = new LinkedList();

        for (BlogTag srcTag : srcTags) {
            TagVo vo = new TagVo();
            vo.setId(srcTag.getId());
            vo.setName(srcTag.getName());

            tags.add(vo);
        }


        return tags;
    }

    /**
     * Used internally to convert an article model into a list of article Vo
     * @param blogArticleCulture article associated to the blog from the DAO
     * @return an articleVo
     */
    public static ArticleVo convertToArticleVo(BlogArticleCulture blogArticleCulture) {

        ArticleVo articleVo = new ArticleVo();
        articleVo.setId(blogArticleCulture.getId());
        articleVo.setTitle(blogArticleCulture.getTitle());
        articleVo.setText(blogArticleCulture.getText());
        articleVo.setLastModified(blogArticleCulture.getLastModified());
        articleVo.setAuthorId(blogArticleCulture.getAuthorId());
        articleVo.setAuthorFirstName(blogArticleCulture.getAuthorFirstName());
        articleVo.setAuthorLastName(blogArticleCulture.getAuthorLastName());

        articleVo.setComments(convertToCommentVo(blogArticleCulture.getId(), blogArticleCulture.getBlogArticleCommentSet()));
        articleVo.setTags(convertToTagVo(blogArticleCulture.getBlogTags()));

        return articleVo;
    }

    /**
     * Used internally to convert a list of article model into a list of article Vo 
     *
     * @param articles articles associated to the blog from the DAO
     * @param srcCulture the culture to convert into
     * @return a list of articleVo
     */
    public static List convertToArticleVo(Collection articles, Culture srcCulture) {
        List articleVos = new LinkedList();

        for (BlogArticle article : articles) {
            Set cultures = article.getBlogArticleCulture();

            for (BlogArticleCulture culture : cultures) {
                if (srcCulture.getCultureCode().equals(culture.getCulture().getCultureCode())) {
                    articleVos.add(convertToArticleVo(culture));
                }
            }
        }

        return articleVos;
    }

    /**
     * Used internally to convert a list of comment model into a list of comment Vo
     *
     * @param articleId id of the article
     * @param blogArticleCommentSet list of comments associated to the article  @return a list of comment Vo
     * @return a list of comments
     */
    private static List convertToCommentVo(Integer articleId, Set blogArticleCommentSet) {

        List comments = new LinkedList();

        for (BlogArticleComment blogArticleComment : blogArticleCommentSet) {
            CommentVo commentVo = new CommentVo();

            commentVo.setArticleId(articleId);
            commentVo.setId(blogArticleComment.getId());
            commentVo.setText(blogArticleComment.getText());
            commentVo.setCreationDate(blogArticleComment.getCreationDate());
            commentVo.setLastModified(blogArticleComment.getLastModified());
            commentVo.setAuthorId(blogArticleComment.getAuthorId());
            commentVo.setAuthorFirstName(blogArticleComment.getAuthorFirstName());
            commentVo.setAuthorLastName(blogArticleComment.getAuthorLastName());

            comments.add(commentVo);
        }

        return comments;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy