Package org.nb.blog

Source Code of org.nb.blog.blogView

/**
* Title:        blogView
* Description:  User connects to this object to retrieve threads and entries<p>
* Company:      Nathan Binford<p>
* @author Nathan Binford
* @version 1.2
*/
package org.nb.blog;

import java.sql.*;
import org.nb.Preferences;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class blogView
{
  Preferences pref;     /** Application settings */
  blogUser user;        /** Authenticated user */

    public blogView(blogUser user, String prefPath) throws blogException
    {
        this.user = user;       //set active user
        try
        {
            pref = new Preferences(prefPath);
        }
        catch (ParserConfigurationException e) { throw new blogException("Cannot load preferences file. Message: " + e.getMessage()); }
        catch (SAXException e) { throw new blogException("Cannot load preferences file. Message: " + e.getMessage()); }
        catch (IOException e) { throw new blogException("Cannot load preferences file. Message: " + e.getMessage()); }
    }

    /** Return an array of all threads in journal which have a parent with the threadID matching parent (for top level threads - w/o parents - set parent = 0) */
    public blogThread[] getAllThreads(int parent) throws blogException
    {
        blogThread threads[] = new blogThread[0];
        blogThread tmp[];
        Connection conn;
        Statement stmt;
        ResultSet rs;
        int i;

        try
        {
            Class.forName(pref.getPreference("DB", "ClassString"));
            conn = DriverManager.getConnection(pref.getPreference("DB", "ConnectionString"));
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM threads");

            //load threads into array
            while (rs.next())
            {
                if (rs.getInt("parent") == parent)
                {
                    if (user.checkRights(rs.getInt("threadID")).indexOf("read") != -1)
                    {
                        tmp = new blogThread[threads.length + 1];
                        for (i = 0; i < threads.length; i++)
                            tmp[i] = threads[i];
                        tmp[threads.length] = new blogThread(rs.getInt("threadID"), rs.getInt("parent"), rs.getString("title"), rs.getString("description"), rs.getString("owner"));
                        threads = new blogThread[tmp.length];
                        for (i = 0; i < tmp.length; i++)
                            threads[i] = tmp[i];
                    }
                }
            }
            rs.close();
            stmt.close();
            conn.close();

            //return array
            if (threads.length != 0)
                return threads;
            else
                return null;
        }
        catch (ClassNotFoundException e) { throw new blogException("Invalid Preference File Setting: JDBC Class. Message: " + e.getMessage()); }
        catch (SQLException e) { throw new blogException("Database error. Message: " + e.getMessage()); }
    }

    //return specific thread
    public blogThread getThread(int id) throws blogException
    {
        blogThread thread;
        Connection conn;
        Statement stmt;
        ResultSet rs;

        if (user.checkRights(id).indexOf("read") != -1)
        {
            try
            {
                Class.forName(pref.getPreference("DB", "ClassString"));
                conn = DriverManager.getConnection(pref.getPreference("DB", "ConnectionString"));
                stmt = conn.createStatement();
                rs = stmt.executeQuery("SELECT * FROM threads WHERE threadID = " + id);

                //load thread if found
                if (rs.next())
                    thread = new blogThread(rs.getInt("threadID"), rs.getInt("parent"), rs.getString("title"), rs.getString("description"), rs.getString("owner"));
                else
                    thread = null;

                rs.close();
                stmt.close();
                conn.close();

                return thread;
            }
            catch (ClassNotFoundException e) { throw new blogException("Invalid Preference File Setting: JDBC Class. Message: " + e.getMessage()); }
            catch (SQLException e) { throw new blogException("Database error. Message: " + e.getMessage()); }
}
    else
            throw new blogException("Authentication Error. User does not have sufficent access rights.");
    }

    //return all entries of the specified thread
    public blogEntry[] getAllEntries(int threadID) throws blogException
    {
        blogEntry entries[] = new blogEntry[0];
        blogEntry tmp[];
        Connection conn;
        Statement stmt;
        ResultSet rs;
        int i;

        if (user.checkRights(threadID).indexOf("read") != -1)
        {
            try
            {
                Class.forName(pref.getPreference("DB", "ClassString"));
                conn = DriverManager.getConnection(pref.getPreference("DB", "ConnectionString"));
                stmt = conn.createStatement();
                rs = stmt.executeQuery("SELECT * FROM entries WHERE threadID = " + threadID);

                //load entries into array
                while (rs.next())
                {
                    tmp = new blogEntry[entries.length + 1];
                    for (i = 0; i < entries.length; i++)
                        tmp[i] = entries[i];
                    tmp[entries.length] = new blogEntry(rs.getInt("threadID"), rs.getInt("entryID"), rs.getString("name"), rs.getString("author"), rs.getString("date"), rs.getString("content"));
                    entries = new blogEntry[tmp.length];
                    for (i = 0; i < tmp.length; i++)
                        entries[i] = tmp[i];
                }
                rs.close();
                stmt.close();
                conn.close();

                //return array
                if (entries.length != 0)
                    return entries;
                else
                    return null;
            }
            catch (ClassNotFoundException e) { throw new blogException("Invalid Preference File Setting: JDBC Class. Message: " + e.getMessage()); }
            catch (SQLException e) { throw new blogException("Database error. Message: " + e.getMessage()); }
        }
        else
            throw new blogException("Authentication Error. User does not have sufficent access rights.");
    }

    public blogEntry getEntry(int entryID) throws blogException
    {
        blogEntry entry;
        Connection conn;
        Statement stmt;
        ResultSet rs;

        try
        {
            Class.forName(pref.getPreference("DB", "ClassString"));
            conn = DriverManager.getConnection(pref.getPreference("DB", "ConnectionString"));
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM entries WHERE entryID = " + entryID);

            //load entry
            if (rs.next())
            {
                if (user.checkRights(rs.getInt("threadID")).indexOf("read") != -1)
                    entry = new blogEntry(rs.getInt("threadID"), rs.getInt("entryID"), rs.getString("name"), rs.getString("author"), rs.getString("date"), rs.getString("content"));
                else
                    throw new blogException("Authentication Error. User does not have sufficent access rights.");
            }
            else
                entry = null;

            rs.close();
            stmt.close();
            conn.close();

            return entry;    //return entry
        }
        catch (ClassNotFoundException e) { throw new blogException("Invalid Preference File Setting: JDBC Class. Message: " + e.getMessage()); }
        catch (SQLException e) { throw new blogException("Database error. Message: " + e.getMessage()); }
    }
}
TOP

Related Classes of org.nb.blog.blogView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.