package isip.java.bullyse.bullydb; import java.io.*; import java.util.*; import java.text.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; // This file handles the output of News items public class News extends HttpServlet { static BullyDB sessionbase = new BullyDB(); // System parameters are stored in the database, retrieve the values // through the BullyDB object. static String SystemURL = sessionbase.getURL(); static String ServletURL = sessionbase.getServletURL(); static String SystemEmail = sessionbase.getEmail(); static String SystemPath = sessionbase.getPath(); static String SystemName = sessionbase.getName(); static String SystemShortName = sessionbase.getShortName(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Initialize variables String task = request.getParameter("Task"); HttpSession session = request.getSession(); String SessionId = session.getId(); // Retrieve AccountId String AccountId = sessionbase.getAccountId(SessionId); if (AccountId == null) { response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html"); } else { if (task == null) { // Display the News page displayNews(request, response, SessionId); } else if (task.equals("showHeadlines")) { displayHeadlines(request, response, SessionId); } else if (task.equals("showArchives")) { displayArchives(request, response, SessionId); } else if (task.equals("showArticle")) { displayArticle(request, response, SessionId); } else if (task.equals("addNews")) { addNews(request, response, SessionId); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Initialize variables String task = request.getParameter("task"); HttpSession session = request.getSession(); String SessionId = session.getId(); // Retrieve AccountId String AccountId = sessionbase.getAccountId(SessionId); if (AccountId == null) { response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html"); } else { if (task == null) { // Display the News page displayNews(request, response, SessionId); } else if (task.equals("addNews")) { // Add the news String Title = request.getParameter("Title"); String Type = request.getParameter("Type"); String Story = request.getParameter("Story"); newNews(response, Title, Type, Story, SessionId); } } } public void displayNews(HttpServletRequest request, HttpServletResponse response, String SessionId) throws IOException, ServletException { String query = (String)null; ResultSet result = null; // Initialize the output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Let's get the news sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("\"BSE
"); // Get the Front Article String frontTitle = (String)null; String frontAuthor = (String)null; int frontId = 0; java.util.Date frontDate = new java.util.Date(); java.util.Date frontTime = new java.util.Date(); Locale locale = new Locale("en","US"); // Set the format for time and date DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale); DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale); String frontStory = (String)null; query = "select Title, Author, Date, Story, Id from News where Type = 3"; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { frontTitle = result.getString("Title"); frontAuthor = result.getString("Author"); frontDate = result.getDate("Date"); frontTime = result.getTime("Date"); frontStory = result.getString("Story"); frontId = result.getInt("Id"); } } catch (Exception e) { e.printStackTrace(); } // Get Author's Full Name query = "select Fname, Lname from Account where Username = '" + frontAuthor + "'"; result = sessionbase.doQuery(query, SessionId); String frontFullAuthor = ""; try { while (result.next()) { frontFullAuthor += result.getString("Fname"); frontFullAuthor += " "; frontFullAuthor += result.getString("Lname"); } } catch (Exception e) { e.printStackTrace(); } out.println(""); out.println("Article Submitted: " + dateFormat.format(frontDate) + " at " + timeFormat.format(frontTime) + "
"); out.println("" + frontTitle + "
"); out.println(frontStory + "
"); out.println(""); out.println("Article submitted by: " + frontFullAuthor + ""); // Get Breaking News Links query = "select Title, Date, Id from News where Type = 2"; result = sessionbase.doQuery(query, SessionId); String breakingTitle = (String)null; java.util.Date breakingDate = new java.util.Date(); java.util.Date breakingTime = new java.util.Date(); String breakingId = (String)null; // Make sure there are any Breaking News articles int bId = 0; query = "select Id from News where Type = 2"; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { bId = result.getInt("Id"); } } catch (Exception e) { e.printStackTrace(); } if (bId > 0) { out.println("
"); out.println("Breaking News
"); try { while (result.next()) { breakingTitle = result.getString("Title"); breakingDate = result.getDate("Date"); breakingId = result.getString("Id"); out.println("" + dateFormat.format(breakingDate) + " " + breakingTitle + "
"); } } catch (Exception e) { e.printStackTrace(); } } out.println("
MoreView all headlines...

"); out.println("
"); sessionbase.printBot(out, SessionId); out.close(); } public void displayHeadlines(HttpServletRequest request, HttpServletResponse response, String SessionId) throws IOException, ServletException { String query = (String)null; ResultSet result = null; // Initialize the output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Let's get the news sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("\"Admin\"

"); java.util.Date frontDate = new java.util.Date(); java.util.Date frontTime = new java.util.Date(); Locale locale = new Locale("en","US"); // Set the format for time and date DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale); DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale); java.util.Date curDate = new java.util.Date(); int Day = curDate.getDate(); int Month = (curDate.getMonth()) + 1; int Year = (curDate.getYear()) + 1900; Day = Day - 14; if (Day < 1) { Month = Month - 1; if (Month == 1) { Day = 31 + Day; } else if (Month == 2) { Day = 28 + Day; } else if (Month == 3) { Day = 31 + Day; } else if (Month == 4) { Day = 30 + Day; } else if (Month == 5) { Day = 31 + Day; } else if (Month == 6) { Day = 30 + Day; } else if (Month == 7) { Day = 31 + Day; } else if (Month == 8) { Day = 31 + Day; } else if (Month == 9) { Day = 30 + Day; } else if (Month == 10) { Day = 31 + Day; } else if (Month == 11) { Day = 30 + Day; } else if (Month == 12) { Day = 31 + Day; } if (Month < 1) { Month = 12; Year = Year - 1; } } // Get all Headlines String headTitle = (String)null; String headId = (String)null; java.util.Date headDate = new java.util.Date(); java.util.Date headDateO = new java.util.Date(); query = "select Title, Date, Id from News where Date >= " + Year + "-" + Month + "-" + Day + " order by Date desc"; result = sessionbase.doQuery(query, SessionId); int i = 0; out.println("These articles have been submitted within the last two weeks.
"); out.println("To view older articles, visit the archives."); out.println("

News Articles

"); try { while (result.next()) { i++; headDateO = headDate; headTitle = result.getString("Title"); headDate = result.getDate("Date"); headId = result.getString("Id"); if (!headDate.equals(headDateO)) { if (i > 1) { out.println("
"); } out.println("" + shortFormat.format(headDate) + "
"); out.println("° " + headTitle + "
"); } else { out.println("° " + headTitle + "
"); } } } catch (Exception e) { e.printStackTrace(); } if (i == 0) { out.println("No New Articles
"); } out.println("
MoreView old news...

"); out.println("
"); sessionbase.printBot(out, SessionId); out.close(); } public void displayArchives(HttpServletRequest request, HttpServletResponse response, String SessionId) throws IOException, ServletException { String query = (String)null; ResultSet result = null; // Initialize the output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Let's get the news sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("\"Admin\"

"); java.util.Date frontDate = new java.util.Date(); java.util.Date frontTime = new java.util.Date(); Locale locale = new Locale("en","US"); // Set the format for time and date DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale); DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale); java.util.Date curDate = new java.util.Date(); int Day = curDate.getDate(); int Month = (curDate.getMonth()) + 1; int Year = (curDate.getYear()) + 1900; Day = Day - 14; if (Day < 1) { Month = Month - 1; if (Month == 1) { Day = 31 + Day; } else if (Month == 2) { Day = 28 + Day; } else if (Month == 3) { Day = 31 + Day; } else if (Month == 4) { Day = 30 + Day; } else if (Month == 5) { Day = 31 + Day; } else if (Month == 6) { Day = 30 + Day; } else if (Month == 7) { Day = 31 + Day; } else if (Month == 8) { Day = 31 + Day; } else if (Month == 9) { Day = 30 + Day; } else if (Month == 10) { Day = 31 + Day; } else if (Month == 11) { Day = 30 + Day; } else if (Month == 12) { Day = 31 + Day; } if (Month < 1) { Month = 12; Year = Year - 1; } } // Get all Headlines String headTitle = (String)null; String headId = (String)null; java.util.Date headDate = new java.util.Date(); java.util.Date headDateO = new java.util.Date(); query = "select Title, Date, Id from News where Date < " + Year + "-" + Month + "-" + Day + " order by Date desc"; result = sessionbase.doQuery(query, SessionId); int i = 0; out.println("These articles are all over two weeks old. To view newer "); out.println("articles, visit the headlines."); out.println("

News Articles

"); try { while (result.next()) { i++; headDateO = headDate; headTitle = result.getString("Title"); headDate = result.getDate("Date"); headId = result.getString("Id"); if (!headDate.equals(headDateO)) { if (i > 1) { out.println("
"); } out.println("" + shortFormat.format(headDate) + "
"); out.println("° " + headTitle + "
"); } else { out.println("° " + headTitle + "
"); } } } catch (Exception e) { e.printStackTrace(); } if (i == 0) { out.println("No Old Articles
"); } out.println("
MoreView all headlines...

"); out.println("
"); sessionbase.printBot(out, SessionId); out.close(); } public void displayArticle(HttpServletRequest request, HttpServletResponse response, String SessionId) throws IOException, ServletException { String Id = request.getParameter("Id"); String query = (String)null; ResultSet result = null; // Initialize the output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Get the Article String Title = (String)null; String Author = (String)null; java.util.Date Date = new java.util.Date(); java.util.Date Time = new java.util.Date(); Locale locale = new Locale("en","US"); // Set the format for time and date DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale); DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale); String Story = (String)null; query = "select Title, Author, Date, Story from News where Id = " + Id; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { Title = result.getString("Title"); Author = result.getString("Author"); Date = result.getDate("Date"); Time = result.getTime("Date"); Story = result.getString("Story"); } } catch (Exception e) { e.printStackTrace(); } // Get Author's Full Name query = "select Fname, Lname from Account where Username = '" + Author + "'"; result = sessionbase.doQuery(query, SessionId); String FullAuthor = ""; try { while (result.next()) { FullAuthor += result.getString("Fname"); FullAuthor += " "; FullAuthor += result.getString("Lname"); } } catch (Exception e) { e.printStackTrace(); } sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("\"BSE
"); out.println("

"); out.println("Article Submitted: " + dateFormat.format(Date) + " at " + timeFormat.format(Time) + "

"); out.println("" + Title + "
"); out.println(Story + "
"); out.println("

"); out.println("Article submitted by: " + FullAuthor + "
"); out.println("

"); // Get Breaking News Links // Make sure there are Breaking News articles int bId = 0; query = "select COUNT(Id) from News where Type = 2"; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { bId = result.getInt("COUNT(Id)"); } } catch (Exception e) { e.printStackTrace(); } if (bId > 0) { query = "select Title, Date, Id from News where Type = 2"; result = sessionbase.doQuery(query, SessionId); String breakingTitle = (String)null; java.util.Date breakingDate = new java.util.Date(); java.util.Date breakingTime = new java.util.Date(); String breakingId = (String)null; out.println("

"); out.println("Breaking News
"); try { while (result.next()) { breakingTitle = result.getString("Title"); breakingDate = result.getDate("Date"); breakingId = result.getString("Id"); out.println("" + dateFormat.format(breakingDate) + " " + breakingTitle + "
"); } } catch (Exception e) { e.printStackTrace(); } } out.println("
MoreView all headlines...

"); out.println("
"); sessionbase.printBot(out, SessionId); out.close(); } public void addNews(HttpServletRequest request, HttpServletResponse response, String SessionId) throws IOException, ServletException { ResultSet result = null; String query = (String)null; // Initialize the output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Double check to make sure this is really a Company user String Type = (String)null; query = "select Type from Account where AccountId = " + sessionbase.getAccountId(SessionId); result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { Type = result.getString("Type"); } } catch (Exception e) { e.printStackTrace(); } if (!Type.equals("C")) { sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println("

"); out.println("\"Admin\"

"); out.println("Error

"); out.println("Only Company users and Administrators are able to add news.
"); out.println("
"); sessionbase.printBot(out, SessionId); } else { sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println("

"); out.println("\"Admin\"

"); out.println("Add News"); out.println("
"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
Title:
(60 character limit)
Article Type:
General News
Breaking News
Story: (HTML Tags Accepted)
"); out.println("
"); sessionbase.printBot(out, SessionId); } out.close(); } public void newNews (HttpServletResponse response, String Title, String Type, String Story, String SessionId) throws IOException, ServletException { String query = (String)null; ResultSet result = null; int Id = 0; int newsId = 0; Timestamp newsTime = null; Timestamp newsTimeCmp = null; int count = 0; String CmpId = (String)null; String Author = (String)null; // Make sure this is a Company user and get Username Author = sessionbase.getUsername(SessionId); query = "select Type from Account where Username = '" + Author + "'"; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { CmpId = result.getString("Type"); } } catch (Exception e) { e.printStackTrace(); } if (!CmpId.equals("C")) { // Set up output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println("

"); out.println("\"Admin\"

"); out.println("Error

"); out.println("Only Company users and Administrators are able to add news.
"); out.println("
"); sessionbase.printBot(out, SessionId); out.close(); } else { // Lock News table sessionbase.doUpdate("lock tables News write", SessionId); // Get next NewsId query = "select max(Id) from News"; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { newsId = result.getInt("max(Id)"); } } catch (Exception e) { e.printStackTrace(); } newsId++; if (Type.equals("2")) { // If item is Breaking News, check the total number of Breaking // News items currently in queue. Shouldn't be more than three, // if this would cause more, change the oldest to General News query = "select Id, Stamp from News where Type = 2"; result = sessionbase.doQuery(query, SessionId); try { while (result.next()) { count++; newsTime = result.getTimestamp("Stamp"); if (count == 1) { newsTimeCmp = newsTime; Id = result.getInt("Id"); } if (newsTime.before(newsTimeCmp)) { Id = result.getInt("Id"); } } } catch (Exception e) { e.printStackTrace(); } if (count >= 3) { // Move oldest article to General query = "update News set Type = 1 where Id = " + Id; sessionbase.doUpdate(query, SessionId); } } // Modify any ' in the string char Checkit; String StorChek = ""; for (int i = 0 ; i < Story.length() ; i++) { Checkit = Story.charAt(i); if (Checkit == '\''){ StorChek += "\\'"; } else { StorChek += Checkit; } } Story = StorChek; String TitlChek = ""; for (int i = 0 ; i < Title.length() ; i++) { Checkit = Title.charAt(i); if (Checkit == '\''){ TitlChek += "\'"; } else { TitlChek += Checkit; } } Title = TitlChek; // Add news item query = "insert into News (Title, Author, Date, Type, Story, Id, Stamp) values ('" + Title + "', '" + Author + "', NOW(), " + Type + ", '" + Story + "', " + newsId + ", NOW())"; sessionbase.doUpdate(query, SessionId); // Unlock tables sessionbase.doUpdate("unlock tables", SessionId); // Initialize the output stream response.setContentType("text/html"); PrintWriter out = response.getWriter(); sessionbase.printTop(out, 6); sessionbase.QuoteFlash(out); // Fill in the blanks from the template out.println(""); out.println("

"); out.println("\"Admin\"

"); out.println("Success

"); out.println("News Article successfully added.
"); out.println("
"); sessionbase.printBot(out, SessionId); out.close(); } } static public void reload() { SystemURL = sessionbase.getURL(); ServletURL = sessionbase.getServletURL(); SystemEmail = sessionbase.getEmail(); SystemPath = sessionbase.getPath(); SystemName = sessionbase.getName(); SystemShortName = sessionbase.getShortName(); } }