001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.survey.data;
017
018import java.sql.Connection;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.ibatis.session.SqlSession;
025
026import org.ametys.core.datasource.AbstractMyBatisDAO;
027import org.ametys.core.datasource.ConnectionHelper;
028import org.ametys.core.user.UserIdentity;
029
030/**
031 * Survey answer DAO.
032 */
033public class SurveyAnswerDao extends AbstractMyBatisDAO
034{
035    /** The Avalon role name. */
036    public static final String ROLE = SurveyAnswerDao.class.getName();
037    
038    /**
039     * Get a session from its ID.
040     * @param sessionId the session ID.
041     * @return the session.
042     */
043    public SurveySession getSession(int sessionId)
044    {
045        Map<String, Object> params = new HashMap<>();
046        params.put("id", sessionId);
047        
048        try (SqlSession session = getSession())
049        {
050            return (SurveySession) session.selectOne("SurveyAnswer.getSession", params);
051        }
052    }
053    
054    /**
055     * Get a user session from the survey ID and the user identity.
056     * @param surveyId the survey ID.
057     * @param user the user.
058     * @return the session.
059     */
060    public SurveySession getSession(String surveyId, UserIdentity user)
061    {
062        Map<String, Object> params = new HashMap<>();
063        params.put("surveyId", surveyId);
064        params.put("login", user.getLogin());
065        params.put("population", user.getPopulationId());
066        
067        try (SqlSession sqlSession = getSession())
068        {
069            List<SurveySession> sessions = sqlSession.selectList("SurveyAnswer.getUserSession", params);
070            SurveySession session = sessions != null && !sessions.isEmpty() ? sessions.get(0) : null;
071            return session;
072        }
073    }
074    
075    /**
076     * Get a session from its ID.
077     * @param sessionId the session ID.
078     * @return the session.
079     */
080    public SurveySession getSessionWithAnswers(int sessionId)
081    {
082        Map<String, Object> params = new HashMap<>();
083        params.put("id", sessionId);
084        
085        try (SqlSession session = getSession())
086        {
087            return (SurveySession) session.selectOne("SurveyAnswer.getSessionWithAnswers", params);
088        }
089    }
090    
091    /**
092     * Get the count of sessions for a given survey.
093     * @param surveyId the survey ID.
094     * @return the session count for this survey.
095     */
096    public int getSessionCount(String surveyId)
097    {
098        Map<String, Object> params = new HashMap<>();
099        params.put("surveyId", surveyId);
100        
101        try (SqlSession session = getSession())
102        {
103            return (Integer) session.selectOne("SurveyAnswer.getSessionCount", params);
104        }
105    }
106    
107    /**
108     * Get the session count list.
109     * @return the session count list.
110     */
111    public List<Map<String, Object>> getAllSessionCount()
112    {
113        try (SqlSession session = getSession())
114        {
115            return session.selectList("SurveyAnswer.getAllSessionCount");
116        }
117    }
118    
119    /**
120     * Get the exhaustive list of sessions.
121     * @return the sessions.
122     */
123    public List<SurveySession> getSessions()
124    {
125        try (SqlSession session = getSession())
126        {
127            return session.selectList("SurveyAnswer.getSessions", Collections.EMPTY_MAP);
128        }
129    }
130    
131    /**
132     * Get all the sessions of a given survey.
133     * @param surveyId the survey ID.
134     * @return the sessions.
135     */
136    public List<SurveySession> getSessions(String surveyId)
137    {
138        Map<String, Object> params = new HashMap<>();
139        params.put("surveyId", surveyId);
140        
141        try (SqlSession session = getSession())
142        {
143            return session.selectList("SurveyAnswer.getSessions", params);
144        }
145    }
146    
147    /**
148     * Get all the sessions of a given survey, with their answers.
149     * @param surveyId the survey ID.
150     * @return the sessions with their answers.
151     */
152    public List<SurveySession> getSessionsWithAnswers(String surveyId)
153    {
154        Map<String, Object> params = new HashMap<>();
155        params.put("surveyId", surveyId);
156        
157        try (SqlSession session = getSession())
158        {
159            return session.selectList("SurveyAnswer.getSessionsWithAnswers", params);
160        }
161    }
162    
163    /**
164     * Get the answers of a session.
165     * @param sessionId the session ID.
166     * @return the session answers.
167     */
168    public List<SurveyAnswer> getAnswers(int sessionId)
169    {
170        Map<String, Object> params = new HashMap<>();
171        params.put("sessionId", sessionId);
172        
173        try (SqlSession session = getSession())
174        {
175            return session.selectList("SurveyAnswer.getAnswers", params);
176        }
177    }
178    
179    /**
180     * Insert a session with its answers.
181     * @param session the session.
182     */
183    public void addSession(SurveySession session)
184    {
185        try (SqlSession sqlSession = getSession())
186        {
187            String stmtId = "SurveyAnswer.addSession" + getStatementSuffix(sqlSession.getConnection());
188            
189            // Insert the session object and get the generated ID.
190            sqlSession.insert(stmtId, session);
191            
192            int sessionId = session.getId();
193            
194            Map<String, Object> params = new HashMap<>();
195            params.put("sessionId", sessionId);
196            
197            // Insert each answer.
198            for (SurveyAnswer answer : session.getAnswers())
199            {
200                params.put("questionId", answer.getQuestionId());
201                params.put("answer", answer.getValue());
202                
203                sqlSession.insert("SurveyAnswer.addAnswer", params);
204            }
205            
206            // Commit the transaction.
207            sqlSession.commit();
208        }
209    }
210    
211    /**
212     * Delete a session with its answers.
213     * @param sessionId the session ID.
214     */
215    public void deleteSession(String sessionId)
216    {
217        try (SqlSession session = getSession())
218        {
219            // Delete the answers.
220            session.delete("SurveyAnswer.deleteAnswers", sessionId);
221            
222            // Delete the session itself.
223            session.delete("SurveyAnswer.deleteSession", sessionId);
224            
225            // Commit the transaction.
226            session.commit();
227        }
228    }
229    
230    /**
231     * Delete sessions related to a given survey.
232     * @param surveyId the survey ID.
233     */
234    public void deleteSessions(String surveyId)
235    {
236        try (SqlSession session = getSession())
237        {
238            // Delete the answers.
239            session.delete("SurveyAnswer.deleteSurveyAnswers", surveyId);
240            
241            // Delete the sessions.
242            session.delete("SurveyAnswer.deleteSurveySessions", surveyId);
243            
244            // Commit the transaction.
245            session.commit();
246        }
247    }
248    
249    /**
250     * Get the statement name suffix, depending of the DBMS family.
251     * @param connection the connection.
252     * @return the statement suffix.
253     */
254    protected String getStatementSuffix(Connection connection)
255    {
256        String dbType = ConnectionHelper.getDatabaseType(connection);
257        switch (dbType)
258        {
259            case ConnectionHelper.DATABASE_MYSQL:
260                return "-mysql";
261            case ConnectionHelper.DATABASE_POSTGRES:
262                return "-postgresql";
263            case ConnectionHelper.DATABASE_ORACLE:
264                return "-oracle";
265            case ConnectionHelper.DATABASE_DERBY:
266                return "-derby";
267            default:
268                return "";
269        }
270    }
271}