Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ua.aits.oblenergo_site.model; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.text.ParseException; import java.util.LinkedList; import java.util.List; import org.apache.commons.lang.StringEscapeUtils; import ua.aits.oblenergo_site.functions.DB; /** * * @author kiwi */ public class ArticleModel { public Integer article_id; public String article_title_ua; public String article_title_ru; public String article_title_hu; public String article_text_ua; public String article_text_ru; public String article_text_hu; public Integer article_category; public String article_author; public String article_editor; public String article_add_date; public String article_edit_date; public Integer article_is_delete; public Integer article_is_publish; public String article_lang; public Integer getArticle_id() { return article_id; } public void setArticle_id(Integer article_id) { this.article_id = article_id; } public String getArticle_title_ua() { return article_title_ua; } public void setArticle_title_ua(String article_title_ua) { this.article_title_ua = article_title_ua; } public String getArticle_title_ru() { return article_title_ru; } public void setArticle_title_ru(String article_title_ru) { this.article_title_ru = article_title_ru; } public String getArticle_title_hu() { return article_title_hu; } public void setArticle_title_hu(String article_title_hu) { this.article_title_hu = article_title_hu; } public String getArticle_text_ua() { return article_text_ua; } public void setArticle_text_ua(String article_text_ua) { this.article_text_ua = article_text_ua; } public String getArticle_text_ru() { return article_text_ru; } public void setArticle_text_ru(String article_text_ru) { this.article_text_ru = article_text_ru; } public String getArticle_text_hu() { return article_text_hu; } public void setArticle_text_hu(String article_text_hu) { this.article_text_hu = article_text_hu; } public Integer getArticle_category() { return article_category; } public void setArticle_category(Integer article_category) { this.article_category = article_category; } public String getArticle_author() { return article_author; } public void setArticle_author(String article_author) { this.article_author = article_author; } public String getArticle_editor() { return article_editor; } public void setArticle_editor(String article_editor) { this.article_editor = article_editor; } public String getArticle_add_date() { return article_add_date; } public void setArticle_add_date(String article_add_date) { this.article_add_date = article_add_date; } public String getArticle_edit_date() { return article_edit_date; } public void setArticle_edit_date(String article_edit_date) { this.article_edit_date = article_edit_date; } public Integer getArticle_is_delete() { return article_is_delete; } public void setArticle_is_delete(Integer article_is_delete) { this.article_is_delete = article_is_delete; } public Integer getArticle_is_publish() { return article_is_publish; } public void setArticle_is_publish(Integer article_is_publish) { this.article_is_publish = article_is_publish; } public String getArticle_lang() { return article_lang; } public void setArticle_lang(String article_lang) { this.article_lang = article_lang; } public List<ArticleModel> fillModel(ResultSet result) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, ParseException { List<ArticleModel> tempList = new LinkedList<>(); while (result.next()) { ArticleModel temp = new ArticleModel(); temp.setArticle_id(result.getInt("article_id")); temp.setArticle_title_ua(result.getString("article_title_ua").replace("\"", """)); temp.setArticle_title_ru(result.getString("article_title_ru").replace("\"", """)); temp.setArticle_title_hu(result.getString("article_title_hu").replace("\"", """)); temp.setArticle_text_ua(result.getString("article_text_ua")); temp.setArticle_text_ru(result.getString("article_text_ru")); temp.setArticle_text_hu(result.getString("article_text_hu")); temp.setArticle_category(result.getInt("article_category")); temp.setArticle_author(result.getString("article_author")); temp.setArticle_editor(result.getString("article_editor")); temp.setArticle_add_date(result.getString("article_add_date")); temp.setArticle_edit_date(result.getString("article_edit_date")); temp.setArticle_is_delete(result.getInt("article_is_delete")); temp.setArticle_is_publish(result.getInt("article_is_publish")); String lang = ""; if (!"".equals(temp.article_text_ua) && temp.article_text_ua != null) { lang += "UA,"; } if (!"".equals(temp.article_text_ru) && temp.article_text_ru != null) { lang += "RU,"; } if (!"".equals(temp.article_text_hu) && temp.article_text_hu != null) { lang += "HU,"; } if (lang.length() > 0) { temp.setArticle_lang(lang.substring(0, lang.length() - 1)); } tempList.add(temp); } return tempList; } public List<ArticleModel> getAllArticlesInCategory(String catID) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, ParseException { ResultSet result = DB.getResultSet("SELECT * FROM obl_articles WHERE obl_articles.article_category = " + catID + " AND obl_articles.article_is_delete = 0 AND obl_articles.article_is_publish = 1 ORDER BY obl_articles_id;"); List<ArticleModel> articleList = this.fillModel(result); DB.closeCon(); return articleList; } public List<ArticleModel> getAllArticlesInCategoryForAdmin(String catID) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, ParseException { ResultSet result = DB.getResultSet("SELECT * FROM obl_articles WHERE obl_articles.article_category = " + catID + " AND obl_articles.article_is_delete = 0 ORDER BY obl_articles_id;"); List<ArticleModel> articleList = this.fillModel(result); DB.closeCon(); return articleList; } public ArticleModel getOneArticle(String id) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, ParseException { ResultSet result = DB.getResultSet("SELECT * FROM obl_articles WHERE obl_articles.article_id = " + id + " AND obl_articles.article_is_delete = 0;"); ArticleModel article = this.fillModel(result).get(0); DB.closeCon(); return article; } public void insertArticle(String titleUA, String titleRU, String titleHU, String textUA, String textRU, String textHU, String category, String author, String date) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { DB.runQuery("INSERT INTO `obl_articles`(" + "`article_title_ua`, `article_title_ru`, `article_title_hu`, " + "`article_text_ua`, `article_text_ru`, `article_text_hu`, " + "`article_category`, `article_author`, `article_editor`, `article_add_date`, `article_edit_date`, `article_publish`, `article_is_delete`) " + "VALUES ('" + StringEscapeUtils.escapeSql(titleUA) + "','" + StringEscapeUtils.escapeSql(titleRU) + "','" + StringEscapeUtils.escapeSql(titleHU) + "'," + "'" + StringEscapeUtils.escapeSql(textUA) + "','" + StringEscapeUtils.escapeSql(textRU) + "','" + StringEscapeUtils.escapeSql(textHU) + "'," + "" + category + ",'" + author + "','" + author + "','" + date + "','" + date + "',0,0);"); DB.closeCon(); } public void updateArticle(String id, String titleUA, String titleRU, String titleHU, String textUA, String textRU, String textHU, String category, String author, String date, String editor, String edit_date) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { DB.runQuery("UPDATE `obl_articles` SET " + "`article_title_ua`='" + StringEscapeUtils.escapeSql(titleUA) + "'," + "`article_title_ru`='" + StringEscapeUtils.escapeSql(titleRU) + "'," + "`article_title_hu`='" + StringEscapeUtils.escapeSql(titleHU) + "'," + "`article_text_ua`='" + StringEscapeUtils.escapeSql(textUA) + "'," + "`article_text_ru`='" + StringEscapeUtils.escapeSql(textRU) + "'," + "`article_text_hu`='" + StringEscapeUtils.escapeSql(textHU) + "'," + "`article_category`=" + category + "," + "`article_author`='" + author + "'," + "`article_editor`='" + editor + "'," + "`article_add_date`='" + date + "'," + "`article_edit_date`='" + edit_date + "'," + "`article_publish`=0," + " WHERE article_id = " + id + ";"); DB.closeCon(); } public String deleteArticle(String id) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { ResultSet result = DB.getResultSet( "SELECT obl_articles.article_id, obl_articles.article_category FROM obl_articles WHERE obl_articles.article_id = " + id + ";"); result.first(); DB.runQuery("UPDATE `obl_articles` SET `obl_articles.article_is_delete`= 1 WHERE obl_articles.article_id = " + id + ";"); String category = result.getString("article_category"); DB.closeCon(); return category; } public void publishArticle(String article_id) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { DB.runQuery( "UPDATE `obl_articles` SET `obl_articles.article_is_publish` = 1 WHERE obl_articles.article_id = " + article_id + ";"); DB.closeCon(); } public List<ArticleModel> getAllArticlesForSearch(String str) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, ParseException { ResultSet result = DB.getResultSet("SELECT * from obl_articles where (" + "obl_articles.article_title_ua LIKE '%" + str + "%' OR " + "obl_articles.article_title_ru LIKE '%" + str + "%' OR " + "obl_articles.article_title_hu LIKE '%" + str + "%' OR " + "obl_articles.article_text_ua LIKE '%" + str + "%' OR " + "obl_articles.article_text_ru LIKE '%" + str + "%' OR " + "obl_articles.article_text_hu LIKE '%" + str + "%' OR " + "obl_articles.article_author LIKE '%" + str + "%' OR " + "obl_articles.article_editor LIKE '%" + str + "%'" + ") AND obl_articles.article_is_delete = 0 AND obl_articles.article_is_publish = 1 ORDER BY obl_articles.article_id desc;"); List<ArticleModel> articleList = this.fillModel(result); DB.closeCon(); return articleList; } }