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   
   19   package org.apache.jdo.tck.pc.companyAnnotatedJPA;
   20   
   21   import javax.persistence;
   22   
   23   import java.io.Serializable;
   24   
   25   import java.text.SimpleDateFormat;
   26   import java.util.Comparator;
   27   import java.util.Date;
   28   import java.util.HashMap;
   29   import java.util.Iterator;
   30   import java.util.Map;
   31   import java.util.Set;
   32   import org.apache.jdo.tck.pc.company.IAddress;
   33   
   34   import org.apache.jdo.tck.pc.company.IPerson;
   35   import org.apache.jdo.tck.util.DeepEquality;
   36   import org.apache.jdo.tck.util.EqualityHelper;
   37   
   38   /**
   39    * This class represents a person.
   40    */
   41   @Entity
   42   @Table(name="persons")
   43   @IdClass(org.apache.jdo.tck.pc.companyAnnotatedJPA.JPAAppPerson.Oid.class)
   44   @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
   45   @DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING,
   46           name="DISCRIMINATOR")
   47   public class JPAAppPerson 
   48       implements IPerson, Serializable, Comparable, Comparator, DeepEquality  {
   49   
   50       @Id
   51       @Column(name="PERSONID")
   52       private long    personid;
   53       @Column(name="FIRSTNAME")
   54       private String  firstname;
   55       @Column(name="LASTNAME")
   56       private String  lastname;
   57       @Basic(optional=true, fetch=FetchType.LAZY)
   58       @Column(name="MIDDLENAME")
   59       private String  middlename;
   60       @Column(name="BIRTHDATE")
   61       @Temporal(TemporalType.TIMESTAMP)
   62       private Date    birthdate;
   63       @Embedded
   64           @AttributeOverrides({
   65               @AttributeOverride(name="street",
   66                   column=@Column(name="STREET")),
   67               @AttributeOverride(name="city",
   68                   column=@Column(name="CITY")),
   69               @AttributeOverride(name="state",
   70                      column=@Column(name="STATE")),
   71               @AttributeOverride(name="zipcode",
   72                      column=@Column(name="ZIPCODE")),
   73               @AttributeOverride(name="country",
   74                      column=@Column(name="COUNTRY"))
   75           })
   76       private JPAAppAddress address;
   77   
   78       @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
   79       @MapKey(name="type")
   80       private Map<String, JPAAppPhoneNumber> phoneNumbers
   81               = new HashMap<String, JPAAppPhoneNumber>();    
   82       protected static SimpleDateFormat formatter =
   83           new SimpleDateFormat("d/MMM/yyyy");
   84   
   85       /** This is the JDO-required no-args constructor. */
   86       protected JPAAppPerson() {}
   87   
   88       /**
   89        * Construct a <code>JPAAppPerson</code> instance.
   90        * 
   91        * 
   92        * @param personid The person identifier.
   93        * @param firstname The person's first name.
   94        * @param lastname The person's last name.
   95        * @param middlename The person's middle name.
   96        * @param birthdate The person's birthdate.
   97        */
   98       public JPAAppPerson(long personid, String firstname, String lastname, 
   99                     String middlename, Date birthdate) {
  100           this.personid = personid;
  101           this.firstname = firstname;
  102           this.lastname = lastname;
  103           this.middlename = middlename;
  104           this.birthdate = birthdate;
  105       }
  106   
  107       /**
  108        * Construct a <code>JPAAppPerson</code> instance.
  109        * 
  110        * 
  111        * @param personid The person identifier.
  112        * @param firstname The person's first name.
  113        * @param lastname The person's last name.
  114        * @param middlename The person's middle name.
  115        * @param birthdate The person's birthdate.
  116        * @param address The person's address.
  117        */
  118       public JPAAppPerson(long personid, String firstname, String lastname, 
  119                     String middlename, Date birthdate, IAddress address) {
  120           this(personid, firstname, lastname, middlename, birthdate);
  121           this.address = (JPAAppAddress)address;
  122       }
  123   
  124       /**
  125        * Set the id associated with this object.
  126        * @param id the id.
  127        */
  128       public void setPersonid(long id) {
  129           if (this.personid != 0)
  130               throw new IllegalStateException("Id is already set.");
  131           this.personid = id;
  132       }
  133   
  134       /**
  135        * Get the person's id.
  136        * @return The personid.
  137        */
  138       public long getPersonid() {
  139           return personid;
  140       }
  141   
  142       /**
  143        * Get the person's last name.
  144        * @return The last name.
  145        */
  146       public String getLastname() {
  147           return lastname;
  148       }
  149   
  150       /**
  151        * Set the person's last name.
  152        * @param lastname The last name.
  153        */
  154       public void setLastname(String lastname) {
  155           this.lastname = lastname;
  156       }
  157   
  158       /**
  159        * Get the person's first name.
  160        * @return The first name.
  161        */
  162       public String getFirstname() {
  163           return firstname;
  164       }
  165   
  166       /**
  167        * Set the person's first name.
  168        * @param firstname The first name.
  169        */
  170       public void setFirstname(String firstname) {
  171           this.firstname = firstname;
  172       }
  173   
  174       /**
  175        * Get the person's middle name.
  176        * @return The middle name.
  177        */
  178       public String getMiddlename() {
  179           return middlename;
  180       }
  181   
  182       /**
  183        * Set the person's middle name.
  184        * @param middlename The middle name.
  185        */
  186       public void setMiddlename(String middlename) {
  187           this.middlename = middlename;
  188       }
  189   
  190       /**
  191        * Get the address.
  192        * @return The address.
  193        */
  194       public IAddress getAddress() {
  195           return address;
  196       }
  197   
  198       /**
  199        * Set the address.
  200        * @param address The address.
  201        */
  202       public void setAddress(IAddress address) {
  203           this.address = (JPAAppAddress)address;
  204       }
  205   
  206       /**
  207        * Get the person's birthdate.
  208        * @return The person's birthdate.
  209        */
  210       public Date getBirthdate() {
  211           return birthdate;
  212       }
  213   
  214       /**
  215        * Set the person's birthdate.
  216        * @param birthdate The person's birthdate.
  217        */
  218       public void setBirthdate(Date birthdate) {
  219           this. birthdate = birthdate;
  220       }
  221   
  222       /**
  223        * Get the map of phone numbers as an unmodifiable map.
  224        * @return A Map<String, String> of phone numbers.
  225        */
  226       public Map getPhoneNumbers() {
  227           return (convertPhone2String(phoneNumbers));
  228       }
  229   
  230       /**
  231        * Get the phone number for the specified phone number type. 
  232        * @param type The phone number type ("home", "work", "mobile", etc.).
  233        * @return The phone number associated with specified type, or
  234        * <code>null</code> if there was no phone number for the type. 
  235        */
  236       public String getPhoneNumber(String type) {
  237           JPAAppPhoneNumber pnum = phoneNumbers.get(type);
  238           return pnum.getPhoneNumber();
  239       }
  240       
  241       /**
  242        * Associates the specified phone number with the specified type in the
  243        * map of phone numbers of this person. 
  244        * @param type The phone number type ("home", "work", "mobile", etc.).
  245        * @param phoneNumber The phone number 
  246        * @return The previous phone number associated with specified type, or
  247        * <code>null</code> if there was no phone number for the type. 
  248        */
  249       public String putPhoneNumber(String type, String phoneNumber) {
  250           JPAAppPhoneNumber pnum = phoneNumbers.get(type);
  251           String pnumAsString = null;
  252           if (pnum != null) {
  253               pnumAsString = pnum.getPhoneNumber(); // old val
  254           }
  255           pnum = phoneNumbers.put(type,
  256                   new JPAAppPhoneNumber(this, type, phoneNumber));
  257           return pnumAsString;
  258       }
  259   
  260       /**
  261        * Remove a phoneNumber from the map of phone numbers.
  262        * @param type The phone number type ("home", "work", "mobile", etc.).
  263        * @return The previous phone number associated with specified type, or
  264        * <code>null</code> if there was no phone number for the type. 
  265        */
  266       public String removePhoneNumber(String type) {
  267           JPAAppPhoneNumber pnum = phoneNumbers.get(type);
  268           if (pnum == null)
  269               return null;
  270           String pnumAsString = pnum.getPhoneNumber(); // old val
  271           pnum = phoneNumbers.remove(type);
  272           return pnumAsString;
  273       }
  274   
  275       /**
  276        * Set the phoneNumber map to be in this person.
  277        * @param phoneNumbers A Map<String, String> of phoneNumbers for this person.
  278        */
  279       public void setPhoneNumbers(Map phoneNumbers) {
  280           this.phoneNumbers = (phoneNumbers != null) ? 
  281                   convertString2Phone(phoneNumbers) : null;
  282       }
  283       
  284       /**
  285        * Converts HashMap<String, String> to HashMap<String, JPAAppPhoneNmber>
  286        */
  287       protected HashMap convertString2Phone(Map pnums) {
  288           HashMap retval = new HashMap();
  289           for (Object objEntry: pnums.entrySet()) {
  290               Map.Entry entry = (Map.Entry)objEntry;
  291               String key = (String)entry.getKey();
  292               String value = (String)entry.getValue();
  293               JPAAppPhoneNumber newValue = 
  294                       new JPAAppPhoneNumber(this, key, value);
  295   //            System.out.println("Key = " + key + "  Value = " + value);
  296               retval.put(key, newValue);
  297           }
  298           return retval;
  299       }
  300       
  301       /**
  302        * Converts HashMap<String, JPAAppPhoneNmber> to HashMap<String, String>
  303        */
  304       protected HashMap convertPhone2String(Map pnums) {
  305           HashMap retval = new HashMap();
  306           for (Object objEntry: pnums.entrySet()) {
  307               Map.Entry entry = (Map.Entry)objEntry;
  308               String key = (String)entry.getKey();
  309               JPAAppPhoneNumber value = (JPAAppPhoneNumber)entry.getValue();
  310               String newValue = 
  311                       value.getPhoneNumber();
  312               retval.put(key, newValue);
  313           }
  314           return retval;
  315       }
  316   
  317       /**
  318        * Returns a String representation of a <code>JPAAppPerson</code> object.
  319        * 
  320        * @return a string representation of a <code>JPAAppPerson</code> object.
  321        */
  322       public String toString() {
  323           return "JPAPerson(" + getFieldRepr() + ")";
  324       }
  325       
  326       /**
  327        * Returns a String representation of the non-relationship fields.
  328        * @return a String representation of the non-relationship fields.
  329        */
  330       protected String getFieldRepr() {
  331           StringBuffer rc = new StringBuffer();
  332           rc.append(personid);
  333           rc.append(", ").append(lastname);
  334           rc.append(", ").append(firstname);
  335           rc.append(", born ").append(formatter.format(birthdate));
  336           rc.append(", phone ").append(convertPhone2String(phoneNumbers));
  337           return rc.toString();
  338       }
  339   
  340       /**
  341        * 
  342        * Returns <code>true</code> if all the fields of this instance are
  343        * deep equal to the coresponding fields of the specified JPAAppPerson.
  344        * 
  345        * 
  346        * @param other the object with which to compare.
  347        * @param helper EqualityHelper to keep track of instances that have
  348        * already been processed.
  349        * @return <code>true</code> if all the fields are deep equal;
  350        * <code>false</code> otherwise.
  351        * @throws ClassCastException if the specified instances' type prevents
  352        * it from being compared to this instance.
  353        */
  354       public boolean deepCompareFields(Object other, 
  355                                        EqualityHelper helper) {
  356           JPAAppPerson otherPerson = (JPAAppPerson)other;
  357           String where = "JPAPerson<" + personid + ">";
  358           return 
  359               helper.equals(personid, otherPerson.getPersonid(), where + ".personid") &
  360               helper.equals(firstname, otherPerson.getFirstname(), where + ".firstname") &
  361               helper.equals(lastname, otherPerson.getLastname(), where + ".lastname") &
  362               helper.equals(middlename, otherPerson.getMiddlename(), where + ".middlename") &
  363               helper.equals(birthdate, otherPerson.getBirthdate(), where + ".birthdate") &
  364               helper.deepEquals(address, otherPerson.getAddress(), where + ".address") &
  365               helper.deepEquals(convertPhone2String(phoneNumbers), otherPerson.getPhoneNumbers(), where + ".phoneNumbers");
  366       }
  367   
  368       /** 
  369        * Compares this object with the specified object for order. Returns a
  370        * negative integer, zero, or a positive integer as this object is less
  371        * than, equal to, or greater than the specified object. 
  372        * @param o The Object to be compared. 
  373        * @return a negative integer, zero, or a positive integer as this 
  374        * object is less than, equal to, or greater than the specified object. 
  375        * @throws ClassCastException - if the specified object's type prevents
  376        * it from being compared to this Object. 
  377        */
  378       public int compareTo(Object o) {
  379           return compareTo((JPAAppPerson)o);
  380       }
  381   
  382       /** 
  383        * Compare two instances. This is a method in Comparator.
  384        */
  385       public int compare(Object o1, Object o2) {
  386           return compare((JPAAppPerson)o1, (JPAAppPerson)o2);
  387       }
  388   
  389       /**
  390        * 
  391        * Compares this object with the specified JPAAppPerson object for
  392        * order. Returns a negative integer, zero, or a positive integer as
  393        * this object is less than, equal to, or greater than the specified
  394        * object.  
  395        * 
  396        * 
  397        * @param other The JPAAppPerson object to be compared.
  398        * @return a negative integer, zero, or a positive integer as this
  399        * object is less than, equal to, or greater than the specified JPAAppPerson
  400        * object.
  401        */
  402       public int compareTo(JPAAppPerson other) {
  403           return compare(this, other);
  404       }
  405   
  406       /**
  407        * Compares its two IPerson arguments for order. Returns a negative
  408        * integer, zero, or a positive integer as the first argument is less
  409        * than, equal to, or greater than the second. 
  410        * @param o1 the first IPerson object to be compared. 
  411        * @param o2 the second IPerson object to be compared. 
  412        * @return a negative integer, zero, or a positive integer as the first
  413        * object is less than, equal to, or greater than the second object. 
  414        */
  415       public static int compare(JPAAppPerson o1, JPAAppPerson o2) {
  416           return EqualityHelper.compare(o1.getPersonid(), o2.getPersonid());
  417       }
  418       
  419       /** 
  420        * Indicates whether some other object is "equal to" this one.
  421        * @param obj the object with which to compare.
  422        * @return <code>true</code> if this object is the same as the obj
  423        * argument; <code>false</code> otherwise. 
  424        */
  425       public boolean equals(Object obj) {
  426           if (obj instanceof JPAAppPerson) {
  427               return compareTo((JPAAppPerson)obj) == 0;
  428           }
  429           return false;
  430       }
  431           
  432       /**
  433        * Returns a hash code value for the object. 
  434        * @return a hash code value for this object.
  435        */
  436       public int hashCode() {
  437           return (int)personid;
  438       }
  439       /**
  440        * This class is used to represent the application identifier
  441        * for the <code>Person</code> class.
  442        */
  443       public static class Oid implements Serializable, Comparable {
  444   
  445           /**
  446            * This field represents the identifier for the <code>Person</code>
  447            * class. It must match a field in the <code>Person</code> class in
  448            * both name and type. 
  449            */
  450           public long personid;
  451   
  452           /**
  453            * The required public no-arg constructor.
  454            */
  455           public Oid() { }
  456   
  457           /**
  458            * Initialize the identifier.
  459            * @param personid The person identifier.
  460            */
  461           public Oid(long personid) {
  462               this.personid = personid;
  463           }
  464           
  465           public Oid(String s) { personid = Long.parseLong(justTheId(s)); }
  466   
  467           public String toString() { return this.getClass().getName() + ": "  + personid;}
  468   
  469           /** */
  470           public boolean equals(java.lang.Object obj) {
  471               if( obj==null ||
  472                   !this.getClass().equals(obj.getClass()) ) return( false );
  473               Oid o = (Oid) obj;
  474               if( this.personid != o.personid ) return( false );
  475               return( true );
  476           }
  477   
  478           /** */
  479           public int hashCode() {
  480               return( (int) personid );
  481           }
  482           
  483           protected static String justTheId(String str) {
  484               return str.substring(str.indexOf(':') + 1);
  485           }
  486   
  487           /** */
  488           public int compareTo(Object obj) {
  489               // may throw ClassCastException which the user must handle
  490               Oid other = (Oid) obj;
  491               if( personid < other.personid ) return -1;
  492               if( personid > other.personid ) return 1;
  493               return 0;
  494           }
  495   
  496       }
  497   
  498   }

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