/*
 * Copyright (c) 2014 Lucas Holt
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

package com.justjournal.services;

import com.justjournal.db.*;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * Provide Statistics Services for Just Journal
 *
 * @author Lucas Holt
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    private static final Logger log = Logger.getLogger(StatisticsServiceImpl.class);

    @Override
    @Nullable
    public UserStatistics getUserStatistics(String username) throws ServiceException {
        try {
            UserStatistics userStatistics = new UserStatisticsImpl();

            // check if user is valid
            if (UserDao.view(username) == null) return null;

            userStatistics.setUsername(username);
            userStatistics.setEntryCount(EntryDAO.entryCount(username));
            userStatistics.setCommentCount(CommentDao.commentCount(username));

            return userStatistics;
        } catch (Exception e) {
            log.error(e);
            throw new ServiceException(e);
        }
    }

    @Override
    @NotNull
    public Statistics getStatistics() throws ServiceException {
        try {
            Statistics statistics = new StatisticsImpl();

            statistics.setComments(SQLHelper.count("comments"));
            statistics.setEntries(SQLHelper.count("entry"));
            statistics.setUsers(SQLHelper.count("user"));
            statistics.setTags(SQLHelper.count("tags"));
            statistics.setStyles(SQLHelper.count("style"));
            statistics.setPublicEntries(SQLHelper.scalarInt("SELECT count(*) FROM entry WHERE security='2';"));
            statistics.setPrivateEntries(SQLHelper.scalarInt("SELECT count(*) FROM entry WHERE security='0';"));
            statistics.setFriendsEntries(SQLHelper.scalarInt("SELECT count(*) FROM entry WHERE security='1';"));

            return statistics;
        } catch (Exception e) {
            log.error(e);
            throw new ServiceException(e);
        }
    }
}

@@ -1522,16 +1522,79 @@
 Logger;%0A
+import org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -1632,16 +1632,16 @@
 ervice;%0A
-
 %0A/**%0A * 
@@ -1871,24 +1871,182 @@
 pl.class);%0A%0A
+    private CommentDao commentDao = null;%0A%0A    @Autowired%0A    public void setCommentDao(CommentDao commentDao) %7B%0A        this.commentDao = commentDao;%0A    %7D%0A%0A
     @Overrid
@@ -2434,10 +2434,10 @@
 tryD
-AO
+ao
 .ent
@@ -2499,17 +2499,17 @@
 ntCount(
-C
+c
 ommentDa
@@ -2512,23 +2512,16 @@
 ntDao.co
-mmentCo
 unt(user

use autowired dao