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

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