/*
 * Copyright 1998-2010 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ru.org.linux.spring.boxlets;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.org.linux.site.LorDataSource;
import ru.org.linux.site.Message;
import ru.org.linux.site.MessageNotFoundException;
import ru.org.linux.site.Poll;
import ru.org.linux.spring.commons.CacheProvider;
import ru.org.linux.spring.dao.PollDao;
import ru.org.linux.spring.dao.VoteDTO;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Controller
public class PollBoxletImpl extends SpringBoxlet  {
  private CacheProvider cacheProvider;
  private PollDao pollDao;

  public PollDao getPollDao() {
    return pollDao;
  }

  @Autowired
  public void setPollDao(PollDao pollDao) {
    this.pollDao = pollDao;
  }

  @Autowired
  public void setCacheProvider(CacheProvider cacheProvider) {
    this.cacheProvider = cacheProvider;
  }

  @Override
  @RequestMapping("/poll.boxlet")
  protected ModelAndView getData(HttpServletRequest request) throws Exception {
    final Poll poll = getFromCache(cacheProvider, getCacheKey() + "poll", new GetCommand<Poll>() {
      @Override
      public Poll get() throws Exception {
        return pollDao.getCurrentPoll();
      }
    });

    List<VoteDTO> votes = getFromCache(cacheProvider, getCacheKey() + "votes"+poll.getId(), new GetCommand<List<VoteDTO>>() {
      @Override
      public List<VoteDTO> get() {
        return pollDao.getVoteDTO(poll.getId());
      }
    });

    Message msg = getFromCache(cacheProvider, getCacheKey() + "topic"+poll.getId(), new GetCommand<Message>() {
      @Override
      public Message get() throws SQLException, MessageNotFoundException {
        Connection db = null;

        try {
          db = LorDataSource.getConnection();

          return Message.getMessage(db, poll.getTopicId());
        } finally {
          if (db!=null) {
            db.close();
          }
        }
      }
    });

    Integer count = getFromCache(cacheProvider, getCacheKey() + "count"+poll.getId(), new GetCommand<Integer>() {
      @Override
      public Integer get() {
        return pollDao.getVotersCount(poll.getId());
      }
    });

    ModelAndView result = new ModelAndView("boxlets/poll");
    result.addObject("poll", poll);
    result.addObject("votes", votes);
    result.addObject("count", count);
    result.addObject("message", msg);
    return result;
  }

  @Override
  public int getExpiryTime() {
    return super.getExpiryTime() * 2;
  }
}

@@ -890,48 +890,8 @@
 ew;%0A
-import ru.org.linux.site.LorDataSource;%0A
 impo
@@ -920,16 +920,16 @@
 essage;%0A
+
 import r
@@ -1048,24 +1048,67 @@
 heProvider;%0A
+import ru.org.linux.spring.dao.MessageDao;%0A
 import ru.or
@@ -1226,36 +1226,8 @@
 st;%0A
-import java.sql.Connection;%0A
 impo
@@ -1410,64 +1410,40 @@
 ao;%0A
-%0A
   p
-ublic PollDao getPollDao() %7B%0A    return poll
+rivate MessageDao message
 Dao;
-%0A  %7D
 %0A%0A  
@@ -1646,24 +1646,129 @@
 vider;%0A  %7D%0A%0A
+  @Autowired%0A  public void setMessageDao(MessageDao messageDao) %7B%0A    this.messageDao = messageDao;%0A  %7D%0A%0A
   @Override%0A
@@ -2547,102 +2547,8 @@
 n %7B%0A
-        Connection db = null;%0A%0A        try %7B%0A          db = LorDataSource.getConnection();%0A%0A  
     
@@ -2562,31 +2562,27 @@
 urn 
-M
+m
 essage
+Dao
 .get
-Message(db, 
+ById(
 poll
@@ -2601,100 +2601,8 @@
 ));%0A
-        %7D finally %7B%0A          if (db!=null) %7B%0A            db.close();%0A          %7D%0A        %7D%0A
     

remove LorDataSource