Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.pc.companyAnnotatedJPA » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software 
   12    * distributed under the License is distributed on an "AS IS" BASIS, 
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   14    * See the License for the specific language governing permissions and 
   15    * limitations under the License.
   16    */
   17    
   18   package org.apache.jdo.tck.pc.companyAnnotatedJPA;
   19   
   20   import javax.persistence;
   21   
   22   import java.io.Serializable;
   23   import java.io.ObjectInputStream;
   24   import java.io.IOException;
   25   
   26   import java.util.Collections;
   27   import java.util.Comparator;
   28   import java.util.HashSet;
   29   import java.util.Set;
   30   import org.apache.jdo.tck.pc.company.ICompany;
   31   
   32   import org.apache.jdo.tck.pc.company.IDepartment;
   33   import org.apache.jdo.tck.pc.company.IEmployee;
   34   import org.apache.jdo.tck.util.DeepEquality;
   35   import org.apache.jdo.tck.util.EqualityHelper;
   36   
   37   /**
   38    * This class represents a department within a company.
   39    */
   40   @Entity
   41   @Table(name="departments")
   42   @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
   43   @DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING,
   44           name="DISCRIMINATOR")
   45   public class JPAAppDepartment
   46       implements IDepartment, Serializable, Comparable, Comparator, DeepEquality {
   47   
   48       public static final int RECOMMENDED_NO_OF_EMPS = 2;
   49   
   50       @Id
   51       @Column(name="ID")
   52       private long deptid;
   53       @Column(name="NAME")
   54       private String  name;
   55       @Column(name="COMPANYID")
   56       private JPAAppCompany company;
   57       @Column(name="EMP_OF_THE_MONTH")
   58       private JPAAppEmployee employeeOfTheMonth;
   59       @OneToMany(mappedBy="department",
   60           targetEntity=org.apache.jdo.tck.pc.companyAnnotatedJPA.JPAAppEmployee.class)
   61       private Set employees = new HashSet();
   62       @OneToMany(mappedBy="fundingDept",
   63           targetEntity=org.apache.jdo.tck.pc.companyAnnotatedJPA.JPAAppEmployee.class)
   64       private Set fundedEmps = new HashSet();
   65   
   66       /** This is the JDO-required no-args constructor. The TCK relies on
   67        * this constructor for testing PersistenceManager.newInstance(PCClass).
   68        */
   69       public JPAAppDepartment() {}
   70   
   71       /**
   72        * Construct a <code>Department</code> instance.
   73        * @param deptid The department id.
   74        * @param name The name of the department.
   75        */
   76       public JPAAppDepartment(long deptid, String name) {
   77           this.deptid = deptid;
   78           this.name = name;
   79       }
   80   
   81       /**
   82        * Construct a <code>Department</code> instance.
   83        * @param deptid The department id.
   84        * @param name The name of the department.
   85        * @param company The company that the department is associated with. 
   86        */
   87       public JPAAppDepartment(long deptid, String name, ICompany company) {
   88           this.deptid = deptid;
   89           this.name = name;
   90           this.company = (JPAAppCompany)company;
   91       }
   92   
   93       /**
   94        * Construct a <code>Department</code> instance.
   95        * @param deptid The department id.
   96        * @param name The name of the department.
   97        * @param company The company that the department is associated with.
   98        * @param employeeOfTheMonth The employee of the month the
   99        * department is associated with.
  100        */
  101       public JPAAppDepartment(long deptid, String name, ICompany company, 
  102                         IEmployee employeeOfTheMonth) {
  103           this.deptid = deptid;
  104           this.name = name;
  105           this.company = (JPAAppCompany)company;
  106           this.employeeOfTheMonth = (JPAAppEmployee)employeeOfTheMonth;
  107       }
  108   
  109       /**
  110        * Set the id associated with this object.
  111        * @param id the id.
  112        */
  113       public void setDeptid(long id) {
  114           if (this.deptid != 0)
  115               throw new IllegalStateException("Id is already set.");
  116           this.deptid = id;
  117       }
  118   
  119       /**
  120        * Get the department id.
  121        * @return The department id.
  122        */
  123       public long getDeptid() {
  124           return deptid;
  125       }
  126   
  127       /**
  128        * Get the name of the department.
  129        * @return The name of the department.
  130        */
  131       public String getName() {
  132           return name;
  133       }
  134   
  135       /**
  136        * Set the name of the department.
  137        * @param name The name to set for the department.
  138        */
  139       public void setName(String name) {
  140           this.name = name;
  141       }
  142   
  143       /**
  144        * Get the company associated with the department.
  145        * @return The company.
  146        */
  147       public ICompany getCompany() {
  148           return company;
  149       }
  150   
  151       /**
  152        * Set the company for the department.
  153        * @param company The company to associate with the department.
  154        */
  155       public void setCompany(ICompany company) {
  156           this.company = (JPAAppCompany)company;
  157       }
  158   
  159       /**
  160        * Get the employee of the month associated with the department.
  161        * @return The employee of the month.
  162        */
  163       public IEmployee getEmployeeOfTheMonth() {
  164           return employeeOfTheMonth;
  165       }
  166   
  167       /**
  168        * Set the employee of the month for the department.
  169        * @param employeeOfTheMonth The employee of the month to
  170        * associate with the department. 
  171        */
  172       public void setEmployeeOfTheMonth(IEmployee employeeOfTheMonth) {
  173           this.employeeOfTheMonth = (JPAAppEmployee)employeeOfTheMonth;
  174       }
  175   
  176       /**
  177        * Get the employees in the department as an unmodifiable set.
  178        * @return The set of employees in the department, as an unmodifiable
  179        * set. 
  180        */
  181       public Set getEmployees() {
  182           return Collections.unmodifiableSet(employees);
  183       }
  184   
  185       /**
  186        * Add an employee to the department.
  187        * @param emp The employee to add to the department.
  188        */
  189       public void addEmployee(JPAAppEmployee emp) {
  190           employees.add(emp);
  191       }
  192   
  193       /**
  194        * Remove an employee from the department.
  195        * @param emp The employee to remove from the department.
  196        */
  197       public void removeEmployee(JPAAppEmployee emp) {
  198           employees.remove(emp);
  199       }
  200   
  201       /**
  202        * Set the employees to be in this department.
  203        * @param employees The set of employees for this department.
  204        */
  205       public void setEmployees(Set employees) {
  206           // workaround: create a new HashSet, because fostore does not
  207           // support LinkedHashSet
  208           this.employees = (employees != null) ? new HashSet(employees) : null;
  209       }
  210   
  211       /**
  212        * Get the funded employees in the department as an unmodifiable set.
  213        * @return The set of funded employees in the department, as an
  214        * unmodifiable set. 
  215        */
  216       public Set getFundedEmps() {
  217           return Collections.unmodifiableSet(fundedEmps);
  218       }
  219   
  220       /**
  221        * Add an employee to the collection of funded employees of this
  222        * department. 
  223        * @param emp The employee to add to the department.
  224        */
  225       public void addFundedEmp(JPAAppEmployee emp) {
  226           fundedEmps.add(emp);
  227       }
  228   
  229       /**
  230        * Remove an employee from collection of funded employees of this
  231        * department. 
  232        * @param emp The employee to remove from the department.
  233        */
  234       public void removeFundedEmp(JPAAppEmployee emp) {
  235           fundedEmps.remove(emp);
  236       }
  237   
  238       /**
  239        * Set the funded employees to be in this department.
  240        * @param employees The set of funded employees for this department. 
  241        */
  242       public void setFundedEmps(Set employees) {
  243           // workaround: create a new HashSet, because fostore does not
  244           // support LinkedHashSet
  245           this.fundedEmps = (fundedEmps != null) ? new HashSet(employees) : null;
  246       }
  247   
  248       /** Serialization support: initialize transient fields. */
  249       private void readObject(ObjectInputStream in)
  250           throws IOException, ClassNotFoundException {
  251           in.defaultReadObject();
  252           employees = new HashSet();
  253           fundedEmps = new HashSet();
  254       }
  255   
  256       /**
  257        * 
  258        * Returns <code>true</code> if all the fields of this instance are
  259        * deep equal to the coresponding fields of the other JPAAppDepartment.
  260        * 
  261        * 
  262        * @param other the object with which to compare.
  263        * @param helper EqualityHelper to keep track of instances that have
  264        * already been processed.
  265        * @return <code>true</code> if all the fields are deep equal;
  266        * <code>false</code> otherwise.
  267        * @throws ClassCastException if the specified instances' type prevents
  268        * it from being compared to this instance.
  269        */
  270       public boolean deepCompareFields(Object other, 
  271                                        EqualityHelper helper) {
  272           JPAAppDepartment otherDept = (JPAAppDepartment)other;
  273           String where = "JPADepartment<" + deptid + ">";
  274           return 
  275               helper.equals(deptid, otherDept.getDeptid(), where + ".deptid") & 
  276               helper.equals(name, otherDept.getName(), where + ".name") &
  277               helper.deepEquals(company, otherDept.getCompany(), where + ".company") &
  278               helper.deepEquals(employeeOfTheMonth, otherDept.getEmployeeOfTheMonth(), where + ".employeeOfTheMonth") &
  279               helper.deepEquals(employees, otherDept.getEmployees(), where + ".employees") &
  280               helper.deepEquals(fundedEmps, otherDept.getFundedEmps(), where + ".fundedEmps");
  281       }
  282       
  283       /**
  284        * Returns a String representation of a <code>JPAAppDepartment</code> object.
  285        * 
  286        * 
  287        * @return a String representation of a <code>JPAAppDepartment</code> object.
  288        */
  289       public String toString() {
  290           return "JPADepartment(" + getFieldRepr()+ ")";
  291       }
  292   
  293       /**
  294        * Returns a String representation of the non-relationship fields.
  295        * @return a String representation of the non-relationship fields.
  296        */
  297       protected String getFieldRepr() {
  298           StringBuffer rc = new StringBuffer();
  299           rc.append(deptid);
  300           rc.append(", name ").append(name);
  301           return rc.toString();
  302       }
  303   
  304       /** 
  305        * Compares this object with the specified object for order. Returns a
  306        * negative integer, zero, or a positive integer as this object is less
  307        * than, equal to, or greater than the specified object. 
  308        * @param o The Object to be compared. 
  309        * @return a negative integer, zero, or a positive integer as this 
  310        * object is less than, equal to, or greater than the specified object. 
  311        * @throws ClassCastException - if the specified object's type prevents
  312        * it from being compared to this Object. 
  313        */
  314       public int compareTo(Object o) {
  315           return compareTo((JPAAppDepartment)o);
  316       }
  317   
  318       /** 
  319        * Compare two instances. This is a method in Comparator.
  320        */
  321       public int compare(Object o1, Object o2) {
  322           return compare((JPAAppDepartment)o1, (JPAAppDepartment)o2);
  323       }
  324   
  325       /** 
  326        * Compares this object with the specified Department object for
  327        * order. Returns a negative integer, zero, or a positive integer as
  328        * this object is less than, equal to, or greater than the specified
  329        * object.  
  330        * @param other The Department object to be compared. 
  331        * @return a negative integer, zero, or a positive integer as this
  332        * object is less than, equal to, or greater than the specified
  333        * Department object. 
  334        */
  335       public int compareTo(JPAAppDepartment other) {
  336           return compare(this, other);
  337       }
  338   
  339       /**
  340        * Compares its two IDepartment arguments for order. Returns a negative
  341        * integer, zero, or a positive integer as the first argument is less
  342        * than, equal to, or greater than the second. 
  343        * @param o1 the first IDepartment object to be compared. 
  344        * @param o2 the second IDepartment object to be compared. 
  345        * @return a negative integer, zero, or a positive integer as the first
  346        * object is less than, equal to, or greater than the second object. 
  347        */
  348       public static int compare(JPAAppDepartment o1, JPAAppDepartment o2) {
  349           return EqualityHelper.compare(o1.getDeptid(), o2.getDeptid());
  350       }
  351       
  352       /** 
  353        * Indicates whether some other object is "equal to" this one.
  354        * @param obj the object with which to compare.
  355        * @return <code>true</code> if this object is the same as the obj
  356        * argument; <code>false</code> otherwise. 
  357        */
  358       public boolean equals(Object obj) {
  359           if (obj instanceof JPAAppDepartment) {
  360               return compareTo((JPAAppDepartment)obj) == 0;
  361           }
  362           return false;
  363       }
  364           
  365       /**
  366        * Returns a hash code value for the object. 
  367        * @return a hash code value for this object.
  368        */
  369       public int hashCode() {
  370           return (int)deptid;
  371       }
  372   
  373       /**
  374        * The application identity class associated with the
  375        * <code>Department</code> class. 
  376        */
  377       public static class Oid implements Serializable, Comparable {
  378   
  379           /**
  380            * This field represents the application identifier field 
  381            * for the <code>Department</code> class. 
  382            * It must match in name and type with the field in the
  383            * <code>Department</code> class. 
  384            */
  385           public long deptid;
  386   
  387           /**
  388            * The required public, no-arg constructor.
  389            */
  390           public Oid() { }
  391   
  392           /**
  393            * A constructor to initialize the identifier field.
  394            * @param deptid the deptid of the Department.
  395            */
  396           public Oid(long deptid) {
  397               this.deptid = deptid;
  398           }
  399           
  400           public Oid(String s) { deptid = Long.parseLong(justTheId(s)); }
  401   
  402           public String toString() { return this.getClass().getName() + ": "  + deptid;}
  403   
  404   
  405           /** */
  406           public boolean equals(java.lang.Object obj) {
  407               if( obj==null || !this.getClass().equals(obj.getClass()) )
  408                   return( false );
  409               Oid o = (Oid) obj;
  410               if( this.deptid != o.deptid ) return( false );
  411               return( true );
  412           }
  413   
  414           /** */
  415           public int hashCode() {
  416               return( (int) deptid );
  417           }
  418           
  419           protected static String justTheId(String str) {
  420               return str.substring(str.indexOf(':') + 1);
  421           }
  422   
  423           /** */
  424           public int compareTo(Object obj) {
  425               // may throw ClassCastException which the user must handle
  426               Oid other = (Oid) obj;
  427               if( deptid < other.deptid ) return -1;
  428               if( deptid > other.deptid ) return 1;
  429               return 0;
  430           }
  431   
  432       }
  433   
  434   }
  435   

Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.pc.companyAnnotatedJPA » [javadoc | source]