Implementation of university member, education member, and student.

A Design Pattern for Type Extension With Facets and Decorators
by Chung-Yeung Pang
Listing 4. Implementation of university member, education member, and student.


package Accidental_Multi_Inheritance;

import Type_Extension.*;
import Persistent_Type_Decoration.*;
import java.sql.*;
import java.math.BigInteger;
import java.util.*;

public class UniversityMember extends ExtensibleObject implements PersistentObject {
    String id = null;
    String lastName;
    String firstName;
    BigInteger roles = BigInteger.valueOf(0);
    PersistentDecorator pd = null;
    int state = new_state;

    public UniversityMember(String _id) {
      state = new_state;
      id = _id;
      setName("University Member");
    }

    public void fetchData() throws Exception {
      try {
        if (pd == null) {
          if (decorator != null)
            pd = (PersistentDecorator) decorator.get("PersistentDecorator");
        }

        if (pd != null) {
          Statement statement = pd.getConnection().createStatement();
          ResultSet rSet = statement.executeQuery("select * from UNI_MEMBER where ID = '"+id+"'");
          if (rSet != null) {
            rSet.next();
            lastName = rSet.getString("LASTNAME");
            firstName = rSet.getString("FIRSTNAME");
            roles = BigInteger.valueOf(rSet.getLong("ROLES"));
          }
          state = saved_state;
          rSet.close();
          statement.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
    }

    public void state(int _state) {
      state = _state;
    }

    public int state() {
      return state;
    }

    public void save() throws Exception {
      try {
        if (pd == null) {
          if (decorator != null)
            pd= (PersistentDecorator)
              decorator.get("PersistentDecorator");
        }
        if (pd != null) {
          Statement statement = pd.getConnection().createStatement();
          switch (state) {
            case new_state:
              statement.executeUpdate(
 "insert into UNI_MEMBER (ID, FIRSTNAME, LASTNAME, ROLES) values ('"
 +id+"','"+firstName+"','"+lastName+"',"+roles.longValue()+")");
              statement.close();
              state = saved_state;
              break;
            case edited_state:
              statement.executeUpdate(
 "update UNI_MEMBER set FIRSTNAME = '"+firstName+"', LASTNAME = '"
 +lastName+"', ROLES = "+roles.longValue()+" where ID = '"+id+"'");
              statement.close();
              state = saved_state;
              break;
            default:
              // nothing to save
              break;
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
    }

    public void remove() throws Exception {
      try {
        if (pd == null) {
          if (decorator != null)
            pd = (PersistentDecorator)
              decorator.get("PersistentDecorator");
        }
        if (pd != null) {
          Statement statement = pd.getConnection().createStatement();
          statement.executeUpdate(
 "delete from UNI_MEMBER where ID = '"+id+"'");
          firstName = null;
          lastName = null;
          roles = BigInteger.valueOf(0);
          statement.close();
          state = new_state;
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
    }

    public String getID() {
      return id;
    }

    public void setID(String _id) {
      id = _id;
    }

    public String getFirstName() {
      return firstName;
    }

    public void setFirstName(String _firstName) {
      firstName = _firstName;
    }

    public String getLastName() {
      return lastName;
    }

    public void setLastName(String _lastName) {
      lastName = _lastName;
    }

    BigInteger getRoles() {
      return roles;
    }

    void setRoles(BigInteger _roles) {
      roles = _roles;
    }
}

public class EducationMember extends TypeDecorator implements PersistentObject {
    String faculty = null;
    int state = new_state;
    String myID = null;
    Vector newCourses;
    Vector existingCourses;
    Vector deletedCourses;
    int role;
    PersistentDecorator pd = null;

  public EducationMember(int _role, UniversityMember u) {
    role = _role;
    newCourses = new Vector();
    existingCourses = new Vector();
    deletedCourses = new Vector();
  }

    public void fetchData() throws Exception {
      try {
        if (myID == null)
          myID = ((UniversityMember) me).getID();
        if (pd == null)
          pd = (PersistentDecorator)
            me.decorator.get("PersistentDecorator");

        if (pd != null) {
          Statement statement = pd.getConnection().createStatement();
          ResultSet rSet = statement.executeQuery(
           "select FACULTY from FACULTY_LINK where ID = '"+myID+"'");
          if (rSet != null) {
            rSet.next();
            faculty = rSet.getString(1);
          }
          rSet.close();
          rSet = statement.executeQuery(
  "select COURSE from COURSE_LINK where ID = '"+myID+"' and ROLE = "
  +role);
          if (rSet != null) {
            while (rSet.next()) {
              String course = rSet.getString(1);
              existingCourses.addElement(course);
            }
          }
          state = saved_state;
          rSet.close();
          statement.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
    }

    ....

    public void save() throws Exception {
      try {
        if (myID == null)
          myID = ((UniversityMember) me).getID();
        if (pd == null)
          pd = (PersistentDecorator)
            me.decorator.get("PersistentDecorator");
        if ((pd != null) && (faculty != null)) {
          Statement statement = pd.getConnection().createStatement();
          switch (state) {
            case new_state:
              statement.executeUpdate(
  "insert into FACULTY_LINK (FACULTY, ID, ROLE) values ('"
  +faculty+"','"+myID+"',"+role+")");
              for (Enumeration e = newCourses.elements() ;
                  e.hasMoreElements() ;) {
                statement.executeUpdate(
  "insert into COURSE_LINK (COURSE, ID, ROLE) values ('"
  +(String) e.nextElement()+"','"+myID+"',"+role+")");
              }
              statement.close();
              state = saved_state;
              break;
            case edited_state:
              statement.executeUpdate(
  "update FACULTY_LINK set FACULTY = '"
  +faculty+"', ROLE = "+role+" where ID = '"+myID+"'");
              for (Enumeration e = existingCourses.elements() ;
                  e.hasMoreElements() ;) {
                statement.executeUpdate(
  "update COURSE_LINK set COURSE = '" +(String) e.nextElement()+
  "', ROLE = "+role+" where ID = '"+myID+"'");
              }
              for (Enumeration e = newCourses.elements() ;
                  e.hasMoreElements() ;) {
                String str = (String) e.nextElement();
                statement.executeUpdate(
  "insert into COURSE_LINK (COURSE, ID, ROLE) values ('" +str+
  "','"+myID+"',"+role+")");
                existingCourses.addElement(str);
              }
              for (Enumeration e = deletedCourses.elements() ;
                  e.hasMoreElements() ;) {
                statement.executeUpdate(
  "delete from COURSE_LINK where ID = '"+myID+"'");
              }
              existingCourses.removeAllElements();
              deletedCourses.removeAllElements();
              statement.close();
              state = saved_state;
              break;
            default:
              // nothing to save
              break;
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
   }

    public void remove() throws Exception {
      try {
        if (myID == null)
          myID = ((UniversityMember) me).getID();
        if (pd == null)
          pd = (PersistentDecorator)
            me.decorator.get("Persistent Decorator");
        if (pd != null) {
          Statement statement = pd.getConnection().createStatement();
          statement.executeUpdate(
            "delete from FACULTY_LINK where ID = '"+myID+"'");
          statement.executeUpdate(
            "delete from COURSE_LINK where ID = '"+myID+"'");
          existingCourses.removeAllElements();
          deletedCourses.removeAllElements();
          statement.close();
          state = new_state;
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
    }

    public String getFaculty() {
      return faculty;
    }

    public void changeFaculty(String _faculty) {
      faculty = _faculty;
      state = edited_state;
    }

    public String[] getCourses() {
      int count = 0;
      count = existingCourses.size() + newCourses.size();
      String[] courses = new String[count];
      int i = 0;
      for (Enumeration e = existingCourses.elements() ;
          e.hasMoreElements() ;)
        courses[i++] = (String) e.nextElement();
      for (Enumeration e = newCourses.elements() ;
          e.hasMoreElements() ;)
        courses[i++] = (String) e.nextElement();
      return courses;
    }

    public void addCourse(String course) {
      newCourses.addElement(course);
      state = edited_state;
    }

    public void removeCourse(String course) {
      if (existingCourses.removeElement(course))
        deletedCourses.addElement(course);
      else
        newCourses.removeElement(course);
      state = edited_state;
    }
}

public class Student extends EducationMember {

    public Student(UniversityMember u) {
      super(RoleFacet.student_role, u);
      setName("Student");
      decorate(u);
    }
}

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.