Ти ако мислиш че БГ програмистите са по долу от другите много се лъжеш пич, да не си 5-ти клас?
За това ли SAP имат лаборатория тук, че и търсят още персонал, но това е предполагам защото БГ програмистите са заядливи "копелета" които знаят всичко и го израждат в swap файлове, CString-ове и бира.
Айде сега поздрав за всички C++ програмисти и т.н.:
package edu.ecl.delc.modules.actions.portlets;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.modules.actions.portlets.*;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;
import org.apache.torque.*;
import java.sql.*;
import java.util.*;
import edu.ecl.delc.dataobjects.*;
public class QuestionsAction extends VelocityPortletAction {
private final String INPUT_COMBO_LECTURES = "lecture_combo";
private final String INPUT_COMBO_THEMES = "theme_combo";
private final String INPUT_QUESTION = "input_question_1";
private final String INPUT_QUESTION_POINTS = "input_question_points_1";
private final String INPUT_QUESTION_ID = "input_question_id_1";
private final String INPUT_THEME_ID = "input_theme_id_1";
private final String INPUT_ANSWER = "input_answer_1";
private final String INPUT_ANSWER_ID = "input_answer_id_1";
private final String INPUT_ANSWER_IS_CORRECT = "input_answer_is_correct_1";
protected void buildNormalContext(VelocityPortlet portlet, Context context, RunData rundata) throws java.lang.Exception {
String doaddquestion = rundata.getRequest().getParameter("addquestion");
String doeditquestion = rundata.getRequest().getParameter("editquestion");
String doeditanswer = rundata.getRequest().getParameter("editanswer");
String doaddanswer = rundata.getRequest().getParameter("addanswer");
String dodeleteanswer = rundata.getRequest().getParameter("deleteanswer");
String dodeletequestion = rundata.getRequest().getParameter("deletequestion");
if(doaddquestion!=null){
doAddQuestion(rundata,context);
}
else if(doeditquestion!=null){
doEditQuestion(rundata,context);
}
else if(doeditanswer!=null){
doEditAnswer(rundata,context);
}
else if(doaddanswer!=null){
doAddAnswer(rundata,context);
}
else if(dodeleteanswer!=null){
doDeleteAnswer(rundata,context);
}
else{
doView(rundata, context);
}
}
private void doView(RunData rundata,Context context){
ArrayList lectureCourses = new ArrayList();
ArrayList lectureThemes = new ArrayList();
ArrayList testQuestions = new ArrayList();
Connection conn = null;
PreparedStatement stmt;
ResultSet rs;
Integer lc_id = (Integer)rundata.getSession().getAttribute("questions_lc_id");
Integer old_lc_id = (Integer)rundata.getSession().getAttribute("questions_old_lc_id");
if(lc_id==null){
lc_id = new Integer(0);
}
Integer lt_id = (Integer)rundata.getSession().getAttribute("questions_lt_id");
if (lt_id == null) {
lt_id = new Integer(0);
}
if (old_lc_id == null) {
old_lc_id = lc_id;
}
else if (old_lc_id.intValue() != lc_id.intValue()){
old_lc_id = lc_id;
lt_id = new Integer(0);
}
rundata.getSession().setAttribute("questions_old_lc_id",old_lc_id);
try{
conn = Torque.getConnection();
stmt = conn.prepareStatement("SELECT lc.* FROM lecture_courses lc JOIN TURBINE_USER tu ON lc.lecturer_id = tu.USER_ID " +
"WHERE tu.LOGIN_NAME = ?");
stmt.setString(1,rundata.getUser().getUserName());
rs = stmt.executeQuery();
while(rs.next()){
LectureCoursesNew lc = new LectureCoursesNew();
lc.setLecture_course_id(rs.getInt(1));
lc.setTitle(rs.getString(2));
lc.setDescription(rs.getString(3));
lc.setLang(rs.getString(4));
lc.setCountry(rs.getString(5));
lc.setLecturer_id(rs.getInt(6));
lectureCourses.add(lc);
}
stmt = conn.prepareStatement("SELECT lt.* FROM lecture_courses lc "+
"JOIN TURBINE_USER tu "+
"ON lc.lecturer_id = tu.USER_ID "+
"JOIN lecture_themes lt "+
"ON lc.lecture_course_id = lt.lecture_course_id "+
"WHERE tu.LOGIN_NAME = ? "+
"AND lt.lecture_course_id = ?"
);
stmt.setString(1,rundata.getUser().getUserName());
stmt.setInt(2,lc_id.intValue());
rs = stmt.executeQuery();
while(rs.next()){
LectureTheme lt = new LectureTheme();
lt.setLecture_theme_id(rs.getInt(1));
lt.setTitle(rs.getString(2));
lt.setDescription(rs.getString(3));
lt.setLecture_course_id(rs.getInt(4));
lectureThemes.add(lt);
}
stmt = conn.prepareStatement("SELECT tq.* FROM lecture_courses lc "+
"JOIN TURBINE_USER tu "+
"ON lc.lecturer_id = tu.USER_ID "+
"JOIN lecture_themes lt "+
"ON lc.lecture_course_id = lt.lecture_course_id "+
"JOIN test_questions tq "+
"ON tq.lecture_theme_id = lt.id "+
"WHERE tu.LOGIN_NAME = ? "+
"AND tq.lecture_theme_id = ?"
);
stmt.setString(1,rundata.getUser().getUserName());
stmt.setInt(2,lt_id.intValue());
rs = stmt.executeQuery();
while(rs.next()){
Question tq = new Question();
tq.setId(rs.getInt(1));
tq.setLecture_theme_id(rs.getInt(2));
tq.setType_id(rs.getInt(3));
tq.setQuestion(rs.getString(4));
tq.setPoints(rs.getInt(5));
testQuestions.add(tq);
}
context.put("lecture_courses",lectureCourses);
context.put("lecture_themes",lectureThemes);
context.put("test_questions",testQuestions);
context.put("lc_id",lc_id);
context.put("lt_id",lt_id);
context.put("theme_id",lt_id);
context.put("action","view");
context.put("input_combo_lectures",INPUT_COMBO_LECTURES);
context.put("input_combo_themes",INPUT_COMBO_THEMES);
}
catch(Exception e){
System.err.println(e.getMessage());
}
finally{
try{
Torque.closeConnection(conn);
}
catch (Exception e){
System.err.println(e.getMessage());
}
}
}
public void doSelect(RunData rundata,Context context) throws Exception {
rundata.getSession().setAttribute("questions_lc_id",new Integer(rundata.getParameters().getString(INPUT_COMBO_LECTURES)));
rundata.getSession().setAttribute("questions_lt_id",new Integer(rundata.getParameters().getString(INPUT_COMBO_THEMES)));
}
private void doAddQuestion(RunData rundata,Context context){
Integer lt_id = new Integer(rundata.getRequest().getParameter("theme_id"));
context.put("action","addQuestion");
context.put("input_question", INPUT_QUESTION);
context.put("input_question_points", INPUT_QUESTION_POINTS);
context.put("input_theme_id", INPUT_THEME_ID);
context.put("theme_id",lt_id);
}
private void doAddAnswer(RunData rundata,Context context){
Integer qid = new Integer(rundata.getRequest().getParameter("question_id"));
context.put("action", "addAnswer");
context.put("input_answer", INPUT_ANSWER);
context.put("input_answer_is_correct", INPUT_ANSWER_IS_CORRECT);
context.put("input_question_id", INPUT_QUESTION_ID);
context.put("question_id", qid);
}
private void doEditQuestion(RunData rundata,Context context){
ArrayList questionAnswers = new ArrayList();
Connection conn = null;
PreparedStatement stmt;
ResultSet rs;
int qid = Integer.parseInt(rundata.getRequest().getParameter("id"));
try
{
conn = Torque.getConnection();
stmt = conn.prepareStatement("SELECT tq.* FROM test_questions tq WHERE tq.id=?");
stmt.setInt(1,qid);
rs = stmt.executeQuery();
rs.next();
Question question = new Question();
question.setId(rs.getInt(1));
question.setQuestion(rs.getString(4));
question.setPoints(rs.getInt(5));
context.put("question",question);
stmt = conn.prepareStatement("SELECT qa.* FROM question_answers qa WHERE qa.question_id=?");
stmt.setInt(1,qid);
rs = stmt.executeQuery();
while(rs.next()){
Answer answer = new Answer();
answer.setId(rs.getInt(1));
answer.setQuestion_id(rs.getInt(2));
answer.setAnswer(rs.getString(3));
answer.setIs_correct(rs.getInt(4));
questionAnswers.add(answer);
}
context.put("question_answers",questionAnswers);
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
}
finally{
try
{
Torque.closeConnection(conn);
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
}
}
context.put("action","editQuestion");
context.put("input_question", INPUT_QUESTION);
context.put("input_question_points", INPUT_QUESTION_POINTS);
context.put("input_question_id", INPUT_QUESTION_ID);
context.put("theme_id",(Integer)rundata.getSession().getAttribute("questions_lt_id"));
}
public void doUpdatequestion(RunData rundata,Context context) throws Exception{
Connection conn = null;
PreparedStatement stmt;
try
{
conn = Torque.getConnection();
stmt = conn.prepareStatement("UPDATE test_questions SET question = ?,points = ? WHERE id=?");
stmt.setString(1,rundata.getParameters().getString(INPUT_QUESTION));
stmt.setInt(2,Integer.parseInt(rundata.getParameters().getString(INPUT_QUESTION_POINTS)));
stmt.setInt(3,Integer.parseInt(rundata.getParameters().getString(INPUT_QUESTION_ID)));
stmt.execute();
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.getMessage());
}
finally{
try
{
Torque.closeConnection(conn);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
}
private void doEditAnswer(RunData rundata,Context context){
Connection conn = null;
PreparedStatement stmt;
ResultSet rs;
Answer answer = new Answer();
try
{
conn = Torque.getConnection();
stmt = conn.prepareStatement("SELECT * FROM question_answers WHERE id=?");
stmt.setInt(1,Integer.parseInt(rundata.getRequest().getParameter("answer_id")));
rs = stmt.executeQuery();
rs.next();
answer.setId(rs.getInt(1));
answer.setQuestion_id(rs.getInt(2));
answer.setAnswer(rs.getString(3));
answer.setIs_correct(rs.getInt(4));
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.getMessage());
}
finally{
try
{
Torque.closeConnection(conn);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
context.put("answer",answer);
context.put("action","editAnswer");
context.put("input_answer",INPUT_ANSWER);
context.put("input_answer_id",INPUT_ANSWER_ID);
context.put("input_answer_is_correct",INPUT_ANSWER_IS_CORRECT);
}
public void doUpdateanswer(RunData rundata,Context context) throws Exception{
Connection conn = null;
PreparedStatement stmt;
try
{
conn = Torque.getConnection();
stmt = conn.prepareStatement("UPDATE question_answers SET answer = ?,is_correct = ? WHERE id=?");
stmt.setString(1,rundata.getParameters().getString(INPUT_ANSWER));
stmt.setInt(2,Integer.parseInt(rundata.getParameters().getString(INPUT_ANSWER_IS_CORRECT)));
stmt.setInt(3,Integer.parseInt(rundata.getParameters().getString(INPUT_ANSWER_ID)));
stmt.execute();
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.getMessage());
}
finally{
try
{
Torque.closeConnection(conn);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
}
public void doSavequestion(RunData rundata,Context context) throws Exception{
Connection conn = null;
PreparedStatement stmt;
try {
conn = Torque.getConnection();
stmt = conn.prepareStatement("INSERT INTO test_questions VALUES(?,?,?,?,?,NULL)");
stmt.setString(1, rundata.getParameters().getString(INPUT_THEME_ID));
stmt.setInt(2, 1);
stmt.setString(3, rundata.getParameters().getString(INPUT_QUESTION));
stmt.setInt(4,Integer.parseInt(rundata.getParameters().getString(INPUT_QUESTION_POINTS)));
stmt.setInt(5, 0);
stmt.execute();
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
finally {
try {
Torque.closeConnection(conn);
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
}
public void doSaveanswer(RunData rundata, Context context) throws Exception {
Connection conn = null;
PreparedStatement stmt;
try {
conn = Torque.getConnection();
stmt = conn.prepareStatement("INSERT INTO question_answers VALUES(?,?,?)");
stmt.setInt(1, Integer.parseInt(rundata.getParameters().getString(INPUT_QUESTION_ID)));
stmt.setString(2, rundata.getParameters().getString(INPUT_ANSWER));
stmt.setInt(3,Integer.parseInt(rundata.getParameters().getString(INPUT_ANSWER_IS_CORRECT)));
stmt.execute();
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
finally {
try {
Torque.closeConnection(conn);
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
}
public void doDeleteAnswer(RunData rundata, Context context) throws Exception {
Connection conn = null;
PreparedStatement stmt;
try {
conn = Torque.getConnection();
stmt = conn.prepareStatement("DELETE FROM question_answers WHERE id=?");
stmt.setInt(1, Integer.parseInt(rundata.getRequest().getParameter("answer_id")));
stmt.execute();
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
finally {
try {
Torque.closeConnection(conn);
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
doEditQuestion(rundata,context);
}
}