package org.se.lab.service;

import org.apache.log4j.Logger;
import org.se.lab.db.dao.PostDAO;
import org.se.lab.db.data.Community;
import org.se.lab.db.data.Post;
import org.se.lab.db.data.User;
import org.se.lab.utils.ArgumentChecker;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.Date;

@Stateless
public class PostServiceImpl implements PostService {
    private final static Logger LOG = Logger.getLogger(PostServiceImpl.class);

    @Inject
    private PostDAO postDAO;

    @Override
    public Post createRootPost(User user, String text, Date created) {
        return createChildPost(null, null, user, text, created);
    }

    public Post createChildPost(Post parentpost, Community community, User user, String text, Date created) {
        ArgumentChecker.assertNotNull(user, "user");
        ArgumentChecker.assertNotNullAndEmpty(text, "postMessage");
        ArgumentChecker.assertNotNull(created, "createdDate");
        assertBothOrNoneNull(parentpost,community);

        try {
            return postDAO.createPost(parentpost, community, user, text, created);
        } catch (IllegalArgumentException e) {
            String msg = "Can't create post(ill. Argument)";
            LOG.error(msg, e);
            throw new ServiceException(msg);
        } catch (Exception e) {
            String msg = "Can't create post";
            LOG.error(msg, e);
            throw new ServiceException(msg);
        }
    }

    private void assertBothOrNoneNull(Post parentpost, Community community) {
        if(parentpost == null ^ community != null){
            throw new IllegalArgumentException("ParentPost and Community have to be both null or none");
        }
    }

    @Override
    public Post updatePost(Post post) {
        ArgumentChecker.assertNotNull(post, "post");
        try {
            return postDAO.update(post);
        } catch (IllegalArgumentException e) {
            String msg = "Can't update post(ill. Argument)";
            LOG.error(msg, e);
            throw new ServiceException(msg);
        } catch (Exception e) {
            String msg = "Can't update post";
            LOG.error(msg, e);
            throw new ServiceException(msg);
        }
    }
}
@@ -961,51 +961,8 @@
     
-assertBothOrNoneNull(parentpost,community);
 %0A%0A  
@@ -1417,256 +1417,8 @@
 %0A%0A  
-  private void assertBothOrNoneNull(Post parentpost, Community community) %7B%0A        if(parentpost == null %5E community != null)%7B%0A            throw new IllegalArgumentException(%22ParentPost and Community have to be both null or none%22);%0A        %7D%0A    %7D
 %0A%0A  

Remove wrong validation from PostServiceImpl