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.Set;
   29   import java.util.HashSet;
   30   import java.math.BigDecimal;
   31   
   32   import org.apache.jdo.tck.pc.company.IProject;
   33   import org.apache.jdo.tck.util.DeepEquality;
   34   import org.apache.jdo.tck.util.EqualityHelper;
   35   
   36   /**
   37    * This class represents a project, a budgeted task with one or more
   38    * employees working on it.
   39    */
   40   
   41   @Entity
   42   @Table(name="projects")
   43   @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
   44   @DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING,
   45           name="DISCRIMINATOR")
   46       public class JPAAppProject 
   47       implements IProject, Serializable, Comparable, Comparator, DeepEquality  {
   48   
   49       @Id
   50       @Column(name="PROJID")
   51       private long projid;
   52       @Column(name="NAME")
   53       private String     name;
   54       @Column(name="BUDGET", length=11, scale=2)
   55       private BigDecimal budget;
   56       @ManyToMany(targetEntity=org.apache.jdo.tck.pc.companyAnnotatedJPA.JPAAppEmployee.class)
   57       @JoinTable(name="project_reviewer", joinColumns=@JoinColumn(name="PROJID"),
   58               inverseJoinColumns=@JoinColumn(name="REVIEWER"))
   59       private Set reviewers = new HashSet();
   60       @ManyToMany(targetEntity=org.apache.jdo.tck.pc.companyAnnotatedJPA.JPAAppEmployee.class)
   61       @JoinTable(name="project_member", joinColumns=@JoinColumn(name="PROJID"),
   62               inverseJoinColumns=@JoinColumn(name="MEMBER"))
   63       private Set members = new HashSet();
   64       
   65       /** This is the JDO-required no-args constructor. The TCK relies on
   66        * this constructor for testing PersistenceManager.newInstance(PCClass).
   67        */
   68       public JPAAppProject() {}
   69   
   70       /**
   71        * Initialize a project.
   72        * @param projid The project identifier.
   73        * @param name The name of the project.
   74        * @param budget The budget for the project.
   75        */
   76       public JPAAppProject(long projid, String name, BigDecimal budget) {
   77           this.projid = projid;
   78           this.name = name;
   79           this.budget = budget;
   80       }
   81   
   82       /**
   83        * Set the id associated with this object.
   84        * @param id the id.
   85        */
   86       public void setProjid(long id) {
   87           if (this.projid != 0)
   88               throw new IllegalStateException("Id is already set.");
   89           this.projid = id;
   90       }
   91   
   92       /**
   93        * Get the project ID.
   94        * @return The project ID.
   95        */
   96       public long getProjid() {
   97           return projid;
   98       }
   99   
  100       /**
  101        * Get the name of the project.
  102        * @return The name of the project.
  103        */
  104       public String getName() {
  105           return name;
  106       }
  107   
  108       /**
  109        * Set the name of the project.
  110        * @param name The name of the project.
  111        */
  112       public void setName(String name) {
  113           this.name = name;
  114       }
  115   
  116       /**
  117        * Get the project's budget.
  118        * @return The project's budget.
  119        */
  120       public BigDecimal getBudget() {
  121           return budget;
  122       }
  123   
  124       /**
  125        * Set the project's budget.
  126        * @param budget The project's budget.
  127        */
  128       public void setBudget(BigDecimal budget) {
  129           this.budget = budget;
  130       }
  131   
  132       /**
  133        * Get the reviewers associated with this project.
  134        */
  135       public Set getReviewers() {
  136           return Collections.unmodifiableSet(reviewers);
  137       }
  138   
  139       /**
  140        * Add a reviewer to the project.
  141        * @param emp The employee to add as a reviewer.
  142        */
  143       public void addReviewer(JPAAppEmployee emp) {
  144           reviewers.add(emp);
  145       }
  146   
  147       /**
  148        * Remove a reviewer from the project.
  149        * @param emp The employee to remove as a reviewer of this project.
  150        */
  151       public void removeReviewer(JPAAppEmployee emp) {
  152           reviewers.remove(emp);
  153       }
  154   
  155       /**
  156        * Set the reviewers associated with this project.
  157        * @param reviewers The set of reviewers to associate with this project.
  158        */
  159       public void setReviewers(Set reviewers) {
  160           // workaround: create a new HashSet, because fostore does not
  161           // support LinkedHashSet
  162           this.reviewers = (reviewers != null) ? new HashSet(reviewers) : null;
  163       }
  164   
  165       /**
  166        * Get the project members.
  167        * 
  168        * 
  169        * @return The members of the project is returned as an unmodifiable
  170        * set of <code>JPAAppEmployee</code>s.
  171        */
  172       public Set getMembers() {
  173           return Collections.unmodifiableSet(members);
  174       }
  175   
  176       /**
  177        * Add a new member to the project.
  178        * @param emp The employee to add to the project.
  179        */
  180       public void addMember(JPAAppEmployee emp) {
  181           members.add(emp);
  182       }
  183   
  184       /**
  185        * Remove a member from the project.
  186        * @param emp The employee to remove from the project.
  187        */
  188       public void removeMember(JPAAppEmployee emp) {
  189           members.remove(emp);
  190       }
  191   
  192       /**
  193        * Set the members of the project.
  194        * @param employees The set of employees to be the members of this
  195        * project. 
  196        */
  197       public void setMembers(Set employees) {
  198           // workaround: create a new HashSet, because fostore does not
  199           // support LinkedHashSet
  200           this.members = (employees != null) ? new HashSet(employees) : null;
  201       }
  202   
  203       /** Serialization support: initialize transient fields. */
  204       private void readObject(ObjectInputStream in)
  205           throws IOException, ClassNotFoundException {
  206           in.defaultReadObject();
  207           reviewers = new HashSet();
  208           members = new HashSet();
  209       }
  210   
  211       /**
  212        * Returns a String representation of a <code>JPAAppProject</code> object.
  213        * 
  214        * 
  215        * @return a String representation of a <code>JPAAppProject</code> object.
  216        */
  217       public String toString() {
  218           return "JPAProject(" + getFieldRepr() + ")";
  219       }
  220       
  221       /**
  222        * Returns a String representation of the non-relationship fields.
  223        * @return a String representation of the non-relationship fields.
  224        */
  225       protected String getFieldRepr() {
  226           StringBuffer rc = new StringBuffer();
  227           rc.append(projid);
  228           rc.append(", name ").append(name);
  229           rc.append(", budget ").append(budget);
  230           return rc.toString();
  231       }
  232   
  233       /** 
  234        * Returns <code>true</code> if all the fields of this instance are
  235        * deep equal to the coresponding fields of the specified Person.
  236        * @param other the object with which to compare.
  237        * @param helper EqualityHelper to keep track of instances that have
  238        * already been processed. 
  239        * @return <code>true</code> if all the fields are deep equal;
  240        * <code>false</code> otherwise.  
  241        * @throws ClassCastException if the specified instances' type prevents
  242        * it from being compared to this instance. 
  243        */
  244       public boolean deepCompareFields(Object other, 
  245                                        EqualityHelper helper) {
  246           JPAAppProject otherProject = (JPAAppProject)other;
  247           String where = "JPAProject<" + projid + ">";
  248           return 
  249               helper.equals(projid, otherProject.getProjid(), where + ".projid") &
  250               helper.equals(name, otherProject.getName(), where + ".name") &
  251               helper.equals(budget, otherProject.getBudget(), where + ".budget") &
  252               helper.deepEquals(reviewers, otherProject.getReviewers(), where + ".reviewers") &
  253               helper.deepEquals(members, otherProject.getMembers(), where + ".members");
  254       }
  255       
  256       /** 
  257        * Compares this object with the specified object for order. Returns a
  258        * negative integer, zero, or a positive integer as this object is less
  259        * than, equal to, or greater than the specified object. 
  260        * @param o The Object to be compared. 
  261        * @return a negative integer, zero, or a positive integer as this 
  262        * object is less than, equal to, or greater than the specified object. 
  263        * @throws ClassCastException - if the specified object's type prevents
  264        * it from being compared to this Object. 
  265        */
  266       public int compareTo(Object o) {
  267           return compareTo((JPAAppProject)o);
  268       }
  269   
  270       /** 
  271        * Compare two instances. This is a method in Comparator.
  272        */
  273       public int compare(Object o1, Object o2) {
  274           return compare((JPAAppProject)o1, (JPAAppProject)o2);
  275       }
  276   
  277       /**
  278        * 
  279        * Compares this object with the specified JPAAppProject object for
  280        * order. Returns a negative integer, zero, or a positive integer as
  281        * this object is less than, equal to, or greater than the specified
  282        * object.  
  283        * 
  284        * 
  285        * @param other The JPAAppProject object to be compared.
  286        * @return a negative integer, zero, or a positive integer as this
  287        * object is less than, equal to, or greater than the specified JPAAppProject object.
  288        */
  289       public int compareTo(JPAAppProject other) {
  290           return compare(this, other);
  291       }
  292   
  293       /**
  294        * Compares its two IProject arguments for order. Returns a negative
  295        * integer, zero, or a positive integer as the first argument is less
  296        * than, equal to, or greater than the second. 
  297        * @param o1 the first IProject object to be compared. 
  298        * @param o2 the second IProject object to be compared. 
  299        * @return a negative integer, zero, or a positive integer as the first
  300        * object is less than, equal to, or greater than the second object. 
  301        */
  302       public static int compare(JPAAppProject o1, JPAAppProject o2) {
  303           return EqualityHelper.compare(o1.getProjid(), o2.getProjid());
  304       }
  305   
  306       /** 
  307        * Indicates whether some other object is "equal to" this one.
  308        * @param obj the object with which to compare.
  309        * @return <code>true</code> if this object is the same as the obj
  310        * argument; <code>false</code> otherwise. 
  311        */
  312       public boolean equals(Object obj) {
  313           if (obj instanceof JPAAppProject) {
  314               return compareTo((JPAAppProject)obj) == 0;
  315           }
  316           return false;
  317       }
  318           
  319       /**
  320        * Returns a hash code value for the object. 
  321        * @return a hash code value for this object.
  322        */
  323       public int hashCode() {
  324           return (int)projid;
  325       }
  326   
  327       /**
  328        * This class is used to represent the application identity
  329        * for the <code>JPAAppProject</code> class.
  330        */
  331       public static class Oid implements Serializable, Comparable {
  332   
  333           /**
  334            * This field represents the identifier for the
  335            * <code>JPAAppProject</code> class. It must match a field in the
  336            * <code>JPAAppProject</code> class in both name and type.
  337            */
  338           public long projid;
  339   
  340           /**
  341            * The required public no-arg constructor.
  342            */
  343           public Oid() { }
  344   
  345           /**
  346            * Initialize the application identifier with a project ID.
  347            * @param projid The id of the project.
  348            */
  349           public Oid(long projid) {
  350               this.projid = projid;
  351           }
  352           
  353           public Oid(String s) { projid = Long.parseLong(justTheId(s)); }
  354   
  355           public String toString() { return this.getClass().getName() + ": "  + projid;}
  356   
  357           /** */
  358           public boolean equals(java.lang.Object obj) {
  359               if( obj==null || !this.getClass().equals(obj.getClass()) )
  360                   return( false );
  361               Oid o = (Oid) obj;
  362               if( this.projid != o.projid ) return( false );
  363               return( true );
  364           }
  365   
  366           /** */
  367           public int hashCode() {
  368               return( (int) projid );
  369           }
  370           
  371           protected static String justTheId(String str) {
  372               return str.substring(str.indexOf(':') + 1);
  373           }
  374   
  375           /** */
  376           public int compareTo(Object obj) {
  377               // may throw ClassCastException which the user must handle
  378               Oid other = (Oid) obj;
  379               if( projid < other.projid ) return -1;
  380               if( projid > other.projid ) return 1;
  381               return 0;
  382           }
  383   
  384       }
  385   
  386   }
  387   

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