package org.se.lab.service.impl;

import org.apache.log4j.Logger;
import org.se.lab.data.Community;
import org.se.lab.data.Post;
import org.se.lab.data.User;
import org.se.lab.service.ActivityStreamService;
import org.se.lab.service.ServiceException;
import org.se.lab.service.dao.PostDAO;

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

@Stateless
public class ActivityStreamServiceImpl implements ActivityStreamService {
    private final Logger LOG = Logger.getLogger(ActivityStreamServiceImpl.class);

    @Inject
    private PostDAO dao;

    /* (non-Javadoc)
	 * @see org.se.lab.service.ActivityStreamService#insert(org.se.lab.data.Post)
	 */
    @Override
	public void insert(Post article) {
        insert(article, null);
    }

    /* (non-Javadoc)
	 * @see org.se.lab.service.ActivityStreamService#insert(org.se.lab.data.Post, org.se.lab.data.Community)
	 */
    @Override
	public void insert(Post post, Community community) {
        LOG.debug("insert " + post);

        try {
            if (community == null) {
                dao.insert(post);
            } else {
                dao.insert(post, community);
            }
        } catch (Exception e) {
            LOG.error("Can't insert post " + post, e);
            throw new ServiceException("Can't insert post " + post);
        }
    }

    /* (non-Javadoc)
	 * @see org.se.lab.service.ActivityStreamService#delete(org.se.lab.data.Post)
	 */
    @Override
	public void delete(Post post,User user) {
        //ToDo checken wegen Like DAO -> stellt ihn auf wenn der Post likes hat. Wegen der beziehnung. Like gehört auch gelöscht
        LOG.debug("delete " + post);
        Post postToDelete;

        // delete childposts
        for (Post childPost : post.getChildPosts()) {
            postToDelete = dao.findById(childPost.getId());
            deleteExecuter(postToDelete);
        }

        // delete mainpost
        postToDelete = dao.findById(post.getId());
        deleteExecuter(postToDelete);
    }

    public void deleteExecuter(Post post){
        try {
            dao.delete(post);
        } catch (Exception e) {
            LOG.error("Can't delete post " + post, e);
            throw new ServiceException("Can't delete post " + post);
        }
    }

    /* (non-Javadoc)
	 * @see org.se.lab.service.ActivityStreamService#update(org.se.lab.data.Post)
	 */
    @Override
	public void update(Post post) {
        LOG.debug("update " + post);
        try {
            dao.update(post);
        } catch (Exception e) {
            LOG.error("Can't update post " + post, e);
            throw new ServiceException("Can't update post " + post);
        }
    }

    /* (non-Javadoc)
	 * @see org.se.lab.service.ActivityStreamService#getPostsForUser(org.se.lab.data.User)
	 */
    @Override
	public List<Post> getPostsForUser(User user) {
        LOG.debug("getting posts relevant for " + user);
        return dao.getPostsForUser(user);
    }

    /* (non-Javadoc)
	 * @see org.se.lab.service.ActivityStreamService#getPostsForCommunity(org.se.lab.data.Community)
	 */
    @Override
	public List<Post> getPostsForCommunity(Community community) {
        LOG.debug("getting posts relevant for " + community);
        return dao.getPostsForCommunity(community);
    }
}

@@ -93,16 +93,52 @@
 munity;%0A
+import org.se.lab.data.Enumeration;%0A
 import o
@@ -158,16 +158,16 @@
 a.Post;%0A
-
 import o
@@ -276,24 +276,70 @@
 eException;%0A
+import org.se.lab.service.dao.EnumerationDAO;%0A
 import org.s
@@ -652,16 +652,72 @@
 O dao;%0A%0A
+    @Inject%0A    private EnumerationDAO enumerationDAO;%0A%0A
     /* (
@@ -1643,137 +1643,8 @@
 ) %7B%0A
-        //ToDo checken wegen Like DAO -%3E stellt ihn auf wenn der Post likes hat. Wegen der beziehnung. Like geh%C3%B6rt auch gel%C3%B6scht%0A
     
@@ -2066,16 +2066,16 @@
  post)%7B%0A
-
         
@@ -2072,32 +2072,227 @@
 %7B%0A        try %7B%0A
+            // delete likes from post%0A            List%3CEnumeration%3E likes = post.getLikes();%0A            for(Enumeration like: likes)%7B%0A                enumerationDAO.delete(like);%0A            %7D%0A%0A
             dao.

delete post with likes is now successful