Implementation of role facet.

A Design Pattern for Type Extension With Facets and Decorators
by Chung-Yeung Pang
Listing 3. Implementation of role facet.


package Accidental_Multi_Inheritance;

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

public class RoleFacet extends TypeFacet {
    static final int student_role = 0;
    static final int instructor_role = 1;
    static final int staff_role = 2;

    protected static int totalRoleTypes = 3;
    protected BigInteger roles;

    public RoleFacet(ExtensibleObject e) {
        super(e);
        roles = ((UniversityMember) e).getRoles();
    }
 
    public TypeDecorator getRole(String roleName) throws Exception {
      try {
        Object obj = me.decorator.get(roleName);
        if (obj != null)
          return (TypeDecorator) obj;

        int index = mapRole(roleName);
        if ((index > -1) && roles.testBit(index)) {
          TypeDecorator typeDecorator = create(index);
          return typeDecorator;
        }
        return null;
      }
      catch (Exception e) {
        throw e;
      }
    }

    public String[] listRoleNames() {
      int i = 0;
      int j;
      for (j = 0; j < totalRoleTypes; j++)
        if (roles.testBit(j))
          i++;
      if (i == 0)
        return null;
      String[] names = new String[i];
      for (j = 0; j < i; j++)
        if (roles.testBit(j))
          names[j] = mapRole(j);
      return names;
    }

    public boolean hasRole(String roleName) {
      int index = mapRole(roleName);
      if ((index > -1) && (roles.testBit(index)))
        return true;
      return false;
    }

    public TypeDecorator addRole(String roleName) throws Exception {
      int index = mapRole(roleName);
      try {
        if (index > -1) {
          long l = roles.longValue();
          TypeDecorator typeDecorator = create(index);
          boolean b = roles.testBit(index);
          roles = roles.setBit(index);
          b = roles.testBit(index);
          l = roles.longValue();
          return typeDecorator;
        }
        return null;
      }
      catch (Exception e) {
        throw e;
      }
    }

    public void removeRole(String roleName) throws Exception {
      try {
        TypeDecorator typeDecorator = getRole(roleName);
        if (typeDecorator != null) {
          me.decorator.remove(roleName);
          int index = mapRole(roleName);
          roles = roles.clearBit(index);
          ((PersistentObject) typeDecorator).remove();
        }
      }
      catch (Exception e) {
        throw e;
      }
    }

    public void save() throws Exception {
      PersistentDecorator pD =
        (PersistentDecorator)me.decorator.get("PersistentDecorator");
      try {
        if (pD != null) {
          pD.getConnection().setAutoCommit(false);
          for (int index = 0; index < totalRoleTypes; index++) {
            TypeDecorator t = getRole(mapRole(index));
            if (t != null)
              ((PersistentObject) t).save();
          }
          ((UniversityMember) me).setRoles(roles);
          ((PersistentObject) me).save();
          pD.getConnection().commit();
          pD.getConnection().setAutoCommit(true);
        }
      }
      catch (Exception e) {
        pD.getConnection().rollback();
        pD.getConnection().setAutoCommit(true);
        throw e;
      }
    }

    public void remove() throws Exception {
      PersistentDecorator pD =
        (PersistentDecorator)me.decorator.get("PersistentDecorator");
      try {
        if (pD != null) {
          pD.getConnection().setAutoCommit(false);
          for (int index = 0; index < totalRoleTypes; index++) {
            TypeDecorator t = getRole(mapRole(index));
            if (t != null)
              ((PersistentObject) t).remove();
          }
          ((PersistentObject) me).remove();
          pD.getConnection().commit();
          pD.getConnection().setAutoCommit(true);
        }
      }
      catch (Exception e) {
        pD.getConnection().rollback();
        pD.getConnection().setAutoCommit(true);
        throw e;
      }
    }

    public void fetchAllRoles() throws Exception {
      try {
        for (int index = 0; index < totalRoleTypes; index++) {
          if (roles.testBit(index)) {
            getRole(mapRole(index));
          }
        }
      }
      catch (Exception e) {
        throw e;
      }
    }

    protected int mapRole(String roleName) {
      if (roleName.equals("Student"))
        return student_role;
      if (roleName.equals("Instructor"))
        return instructor_role;
      if (roleName.equals("Staff"))
        return staff_role;
      return -1;
    }

    protected String mapRole(int index) {
      switch (index) {
        case (student_role):
          return "Student";
        case (instructor_role):
          return "Instructor";
        case (staff_role):
          return "Staff";
        default:
          return null;
      }
    }

    protected TypeDecorator create (int index) throws Exception {
      try {
        switch (index) {
          case (student_role):
            Student st = new Student((UniversityMember) me);
            st.fetchData();
            return st;
          case (instructor_role):
            Instructor ins = new Instructor ((UniversityMember) me);
            ins.fetchData();
            return ins;
          case (staff_role):
            return null;
          default:
            throw new Exception("No such role!");
        }
      }
      catch (Exception e) {
        throw e;
      }
    }
}