Java tutorial
/*********************************************************************************** * $URL: https://source.sakaiproject.org/contrib/etudes/sakai-jforum/tags/2.9.11/jforum-tool/src/java/org/etudes/jforum/dao/generic/GenericGradeDAO.java $ * $Id: GenericGradeDAO.java 83559 2013-04-30 19:03:29Z murthy@etudes.org $ *********************************************************************************** * * Copyright (c) 2008, 2009, 2010, 2011 Etudes, Inc. * * Portions completed before September 1, 2008 Copyright (c) 2004, 2005, 2006, 2007 Foothill College, ETUDES Project * * 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 org.etudes.jforum.dao.generic; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etudes.jforum.JForum; import org.etudes.jforum.dao.GradeDAO; import org.etudes.jforum.entities.Forum; import org.etudes.jforum.entities.Grade; import org.etudes.jforum.util.preferences.SystemGlobals; public class GenericGradeDAO extends AutoKeys implements GradeDAO { private static Log logger = LogFactory.getLog(GenericGradeDAO.class); /** * {@inheritDoc} */ public int addNew(Grade grade) throws Exception { PreparedStatement p = this.getStatementForAutoKeys("GradeModel.addNew"); p.setString(1, grade.getContext()); p.setInt(2, grade.getType()); p.setInt(3, grade.getForumId()); p.setInt(4, grade.getTopicId()); p.setInt(5, grade.getCategoryId()); p.setFloat(6, grade.getPoints()); p.setInt(7, grade.isAddToGradeBook() ? 1 : 0); if (grade.isMinimumPostsRequired()) { p.setInt(8, grade.getMinimumPosts()); p.setInt(9, 1); } else { p.setInt(9, 0); p.setInt(8, 0); } this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("GradeModel.lastGeneratedGradeId")); int gradeId = this.executeAutoKeysQuery(p); p.close(); return gradeId; } /** * {@inheritDoc} */ public Grade selectByForumId(int forumId) throws Exception { return selectByForumTopicCategoryId(forumId, 0, 0); } /** * {@inheritDoc} */ public Grade selectByCategoryId(int categoryId) throws Exception { return selectByForumTopicCategoryId(0, 0, categoryId); } /** * {@inheritDoc} */ public Grade selectByForumTopicId(int forumId, int topicId) throws Exception { return selectByForumTopicCategoryId(forumId, topicId, 0); } /** * {@inheritDoc} */ public Grade selectByForumTopicCategoryId(int forumId, int topicId, int categoryId) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.selectByForumTopicCategoryId")); p.setInt(1, forumId); p.setInt(2, topicId); p.setInt(3, categoryId); ResultSet rs = p.executeQuery(); Grade grade = null; if (rs.next()) { grade = this.fillGrade(rs); } rs.close(); p.close(); return grade; } /** * @param rs - result set * @return Grade - grade */ protected Grade fillGrade(ResultSet rs) throws Exception { Grade grade = new Grade(); grade.setId(rs.getInt("grade_id")); grade.setContext(rs.getString("context")); grade.setType(rs.getInt("grade_type")); grade.setForumId(rs.getInt("forum_id")); grade.setTopicId(rs.getInt("topic_id")); grade.setCategoryId(rs.getInt("categories_id")); grade.setPoints(rs.getFloat("points")); if (rs.getInt("add_to_gradebook") == 1) grade.setAddToGradeBook(true); else grade.setAddToGradeBook(false); if (rs.getInt("min_posts_required") == 1) { grade.setMinimumPostsRequired(true); grade.setMinimumPosts(rs.getInt("min_posts")); } else { grade.setMinimumPostsRequired(false); grade.setMinimumPosts(rs.getInt("min_posts")); } return grade; } /** * {@inheritDoc} */ public void updateForumGrade(Grade grade) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.updateForumGrade")); p.setFloat(1, grade.getPoints()); p.setInt(2, grade.getType()); p.setInt(3, grade.isAddToGradeBook() ? 1 : 0); if (grade.isMinimumPostsRequired()) { p.setInt(4, 1); p.setInt(5, grade.getMinimumPosts()); } else { p.setInt(4, 0); p.setInt(5, 0); } p.setInt(6, grade.getForumId()); p.executeUpdate(); p.close(); } /** * {@inheritDoc} */ public void updateTopicGrade(Grade grade) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.updateTopicGrade")); p.setFloat(1, grade.getPoints()); p.setInt(2, grade.getType()); if (grade.isMinimumPostsRequired()) { p.setInt(3, 1); p.setInt(4, grade.getMinimumPosts()); } else { p.setInt(3, 0); p.setInt(4, 0); } p.setInt(5, grade.getForumId()); p.setInt(6, grade.getTopicId()); p.executeUpdate(); p.close(); } /** * {@inheritDoc} */ public void updateCategoriesGrade(Grade grade) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.updateCategoryGrade")); p.setFloat(1, grade.getPoints()); if (grade.isMinimumPostsRequired()) { p.setInt(2, 1); p.setInt(3, grade.getMinimumPosts()); } else { p.setInt(2, 0); p.setInt(3, 0); } p.setInt(4, grade.getCategoryId()); p.executeUpdate(); p.close(); } /** * {@inheritDoc} */ public void delete(int gradeId) throws Exception { //delete grade PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.deleteById")); p.setInt(1, gradeId); p.executeUpdate(); p.close(); } /** * {@inheritDoc} */ public void delete(int forumId, int topicId) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.deleteByForumIdTopicId")); p.setInt(1, forumId); p.setInt(2, topicId); p.executeUpdate(); p.close(); } /** * {@inheritDoc} */ public List<Grade> selectForumTopicGradesByForumId(int forumId) throws Exception { List<Grade> grades = new ArrayList<Grade>(); PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.selectForumTopicGradesByForumId")); p.setInt(1, forumId); p.setInt(2, Forum.GRADE_BY_TOPIC); ResultSet rs = p.executeQuery(); while (rs.next()) { grades.add(this.fillGrade(rs)); } rs.close(); p.close(); return grades; } /** * {@inheritDoc} */ public List<Grade> selectForumTopicGradesByCategoryId(int categoryId) throws Exception { List<Grade> grades = new ArrayList<Grade>(); PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.selectForumTopicGradesByCategoryId")); p.setInt(1, categoryId); p.setInt(2, Forum.GRADE_BY_FORUM); p.setInt(3, Forum.GRADE_BY_TOPIC); ResultSet rs = p.executeQuery(); while (rs.next()) { grades.add(this.fillGrade(rs)); } rs.close(); p.close(); return grades; } /** * {@inheritDoc} */ public Grade selectById(int gradeId) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.selectById")); p.setInt(1, gradeId); ResultSet rs = p.executeQuery(); Grade grade = null; if (rs.next()) { grade = this.fillGrade(rs); } rs.close(); p.close(); return grade; } /** * {@inheritDoc} */ public void updateAddToGradeBookStatus(int gradeId, boolean addToGradeBook) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.updateAddToGradeBookStatus")); p.setInt(1, addToGradeBook ? 1 : 0); p.setInt(2, gradeId); p.executeUpdate(); p.close(); } /** * {@inheritDoc} */ public boolean isCategoryForumsGradable(int categoryId) throws Exception { boolean result = false; PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("GradeModel.selectGradableForumsByCategoryId")); p.setInt(1, categoryId); ResultSet rs = p.executeQuery(); if (rs.next()) { result = true; } rs.close(); p.close(); return result; } }