01: package simpleorm.examples;
02:
03: import simpleorm.core.*;
04:
05: /** This test class defines the Employee table */
06: public class Employee extends SRecordInstance implements
07: java.io.Serializable {
08:
09: public static final SRecordMeta meta = new SRecordMeta(
10: Employee.class, "XX_EMPLOYEE");
11: // ie. SRecord objects describe SRecordInstances
12:
13: public static final SFieldString EMPEE_ID = new SFieldString(meta,
14: "EMPEE_ID", 20, SCon.SFD_PRIMARY_KEY);
15:
16: public static final SFieldString NAME = new SFieldString(meta,
17: "NAME", 40, SCon.SFD_DESCRIPTIVE, SCon.SFD_MANDATORY);
18:
19: public static final SFieldString PHONE_NR = new SFieldString(meta,
20: "PHONE_NR", 20);
21:
22: public static final SFieldDouble SALARY = new SFieldDouble(meta,
23: "SALARY");
24:
25: public static final SFieldInteger NR_DEPENDENTS = new SFieldInteger(
26: meta, "NR_DEPENDENTS");
27:
28: static final SFieldReference DEPARTMENT = new SFieldReference(meta,
29: Department.meta, null);
30: //SCon.SEXTRA_FKEY_DDL.pvalue(" Junk To test"));
31:
32: static final SFieldReference MANAGER = // Recursive Reference
33: new SFieldReference(meta, Employee.meta, "MANAGER_");
34:
35: static final SFieldString RESUME = // Curriculam Vitae
36: new SFieldString(meta, "RESUME", 200, SCon.SFD_UNQUERIED);
37: // Not normally retrieved.
38:
39: static final SFieldString DEPT_NAME = new SFieldString(
40: meta,
41: "DEPT_NAME",
42: 100,
43: SCon.SFD_UNQUERIED,
44: SCon.SCOLUMN_QUERY
45: .pvalue("(SELECT NAME FROM XX_DEPARTMENT D WHERE D.DEPT_ID = XX_EMPLOYEE.DEPT_ID)"));
46:
47: public SRecordMeta getMeta() {
48: return meta;
49: }; // specializes abstract method
50:
51: public static Employee findOrCreate(String empeeId) { // Convenience
52: return (Employee) meta.findOrCreate(empeeId);
53: }
54:
55: /** Called when the Salary is setDouble()d. */
56: public void validateField(SFieldMeta field, Object newValue) {
57: if (field == SALARY) {
58: double sal = newValue == null ? 0 : ((Number) newValue)
59: .doubleValue();
60: if (sal < 0)
61: throw new SValidationException("Salary {0} < 0.",
62: SJSharp.newDouble(sal));
63: }
64: }
65:
66: /** Called when the record is flushed, and so both Departent and
67: Salary will have been set. */
68: public void validateRecord() {
69: if (isValid(SALARY)) {
70: double sal = getDouble(SALARY);
71: Department dept = (Department) getReference(DEPARTMENT);
72: if (dept != null) {
73: double max = dept.getDouble(Department.MAX_SALARY);
74: if (sal > max)
75: throw new SValidationException(
76: "Salary {0} is greater than the Departments Maximum {1}.",
77: new Object[] { SJSharp.newDouble(sal),
78: SJSharp.newDouble(max) }, this ); // Always add the record instance for validate record.
79: }
80: }
81: }
82: }
|