DAO Medge Code Examples
Back to the Main Page

Multiple Key

package au.id.medge.daotester.testsuite;

import au.id.medge.dao.annotations.daoproperty;
import au.id.medge.dao.annotations.daotable;
import au.id.medge.dao.query.OrderBy;
import au.id.medge.dao.query.PropertyFilter;
import au.id.medge.dao.utilities.DAOException;
import au.id.medge.tutorial.Student;
import au.id.medge.tutorial.StudentSubject;
import au.id.medge.tutorial.Subject;
import java.sql.SQLException;
import java.util.List;

public class MultiKeyTests extends AbstractTestSuite {

    private String newStudentNumber;

    public MultiKeyTests() {
        super("Testing of multiple column joins");
    }

    private String incStudentNumber(String oldStudentNumber) {
        String numberPartString = oldStudentNumber.substring(0, 8);
        int numberPart = Integer.parseInt(numberPartString);
        char c = oldStudentNumber.charAt(8);
        switch (c) {
            case 'A' -> {
                c = 'B';
                numberPart++;
            }
            case 'B' -> {
                c = 'C';
                numberPart++;
            }
            case 'C' -> {
                c = 'D';
                numberPart++;
            }
            case 'D' -> {
                c = 'A';
                numberPart++;
            }
            default ->
                numberPart++;
        }
        StringBuilder buf = new StringBuilder();
        buf.append(numberPart);
        while (buf.length() < 8) {
            buf.insert(0, "0");
        }
        buf.append(c);
        return buf.toString();
    }

    private Student createStudent() {
        try {
            daoproperty property = getDAO().getProperty(Student.class, Student.STUDENTNUMBER);
            OrderBy order = new OrderBy(property, OrderBy.SORT_DIRECTION.SORT_DESCENDING);
            List students = getDAO().select(Student.class, order);
            if (students.isEmpty()) {
                newStudentNumber = "00000000A";
            } else {
                String oldStudentNumber = students.get(0).getStudentNumber();
                newStudentNumber = incStudentNumber(oldStudentNumber);
            }
            Student newStudent = new Student();
            newStudent.setStudentNumber(newStudentNumber);
            newStudent.setStudentFamilyName("Testing");
            newStudent.setStudentGivensNames("Testing 1 2 3");
            return newStudent;
        } catch (DAOException | SQLException ex) {
            reportFailure("Cannot read students", ex);
            return null;
        }
    }

    private void loadStudent() {
        Student newStudent = createStudent();
        if (newStudent != null) {
            try {
                getDAO().insert(newStudent);
            } catch (DAOException | SQLException ex) {
                reportFailure("Cannot insert new student", ex);
            }
        }
    }

    private Student readStudent() {
        try {
            Student s = new Student();
            s.setStudentNumber(newStudentNumber);
            s = getDAO().selectByKey(s);
            return s;
        } catch (DAOException | SQLException ex) {
            reportFailure("Cannot read new student", ex);
            return null;
        }
    }

    private void readRelated(Student s) {
        if (s != null) {
            List studentSubjects = s.getStudentSubject();
            List subjects = s.getSubject();
            reportSuccess("Student Subject Count = " + studentSubjects.size());
            reportSuccess("Subject Count = " + subjects.size());
        }
    }

    private void readRelatedDirect(Student s) {
        try {
            if (s != null) {
                String studentNumber = s.getStudentNumber();
                daotable table = getDAO().getTable(Subject.class);
                daotable studentTable = getDAO().getTable(Student.class);
                daoproperty property = getDAO().getProperty(studentTable, Student.STUDENTNUMBER);
                PropertyFilter filter = new PropertyFilter(studentTable, property, studentNumber);
                List subjects = getDAO().select(table, filter);
                reportSuccess("Subject count = " + subjects.size());
            }
        } catch (DAOException | SQLException ex) {
            reportFailure("Cannot get subjects direct", ex);
        }
    }

    @Override
    public void runTestSuite() {
        loadStudent();
        Student s = readStudent();
        readRelated(s);
        readRelatedDirect(s);
    }

}
Back to the Main Page