/*
 *  The MIT License (MIT)
 *
 *  View the full license at:
 *  https://github.com/hendrixjoseph/FamilyTree/blob/master/LICENSE.md
 *
 *  Copyright (c) 2015 Joseph Hendrix
 *
 *  Hosted on GitHub at https://github.com/hendrixjoseph/FamilyTree
 *
 */

package edu.wright.hendrix11.familyTree.entity.place;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 * @author Joe Hendrix
 */
@Entity
@DiscriminatorValue(value = "4")
@NamedQueries({
                      @NamedQuery(name = City.FIND_BY_NAME, query = "SELECT p FROM City p WHERE p.name = :name"),
                      @NamedQuery(name = City.FIND_ALL, query = "SELECT p FROM City p")
              })
public class City extends Place
{

    public static final String FIND_ALL = "City.findAll";
    public static final String FIND_BY_NAME = "City.findByName";
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name = "REGION_VIEW",
               joinColumns = @JoinColumn(name = "PLACE_ID"),
               inverseJoinColumns = @JoinColumn(name = "REGION_ID"))
    private Country country;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name = "REGION_VIEW",
               joinColumns = @JoinColumn(name = "PLACE_ID"),
               inverseJoinColumns = @JoinColumn(name = "REGION_ID"))
    private County county;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name = "REGION_VIEW",
               joinColumns = @JoinColumn(name = "PLACE_ID"),
               inverseJoinColumns = @JoinColumn(name = "REGION_ID"))
    private State state;

    public Country getCountry()
    {
        return country;
    }

    public void setCountry(Country country)
    {
        this.country = country;
    }

    public County getCounty()
    {
        return county;
    }

    public void setCounty(County county)
    {
        this.county = county;
    }

    public State getState()
    {
        return state;
    }

    public void setState(State state)
    {
        this.state = state;
    }

    @Override
    public String getLink()
    {
        Place[] places = {this, state};

        return toString(places);
    }

    @Override
    public String toString()
    {
        Place[] places = {this, county, state, country};

        return toString(places);
    }
}

@@ -1108,212 +1108,55 @@
 e%22;%0A
+%0A
     
-@ManyToOne(fetch = FetchType.LAZY, cascade = %7BCascadeType.MERGE, CascadeType.PERSIST%7D)%0A    @JoinTable(name = %22REGION_VIEW%22,%0A               joinColumns = @JoinColumn(name = %22PLACE_ID%22),
+public Country getCountry()
 %0A    
-   
+%7B%0A
         
 inve
@@ -1155,658 +1155,53 @@
     
-inverseJoinColumns = @JoinColumn(name = %22REGION_ID%22))%0A    private Country country;%0A    @ManyToOne(fetch = FetchType.LAZY, cascade = %7BCascadeType.MERGE, CascadeType.PERSIST%7D)%0A    @JoinTable(name = %22REGION_VIEW%22,%0A               joinColumns = @JoinColumn(name = %22PLACE_ID%22),%0A               inverseJoinColumns = @JoinColumn(name = %22REGION_ID%22))%0A    private County county;%0A    @ManyToOne(fetch = FetchType.LAZY, cascade = %7BCascadeType.MERGE, CascadeType.PERSIST%7D)%0A    @JoinTable(name = %22REGION_VIEW%22,%0A               joinColumns = @JoinColumn(name = %22PLACE_ID%22),%0A               inverseJoinColumns = @JoinColumn(name = %22REGION_ID%22))%0A    private State state;
+return getRegionByClass(Country.class);%0A    %7D
 %0A%0A  
@@ -1206,33 +1206,32 @@
     public Count
-r
 y getCountry()%0A 
@@ -1216,33 +1216,32 @@
  County getCount
-r
 y()%0A    %7B%0A      
@@ -1253,104 +1253,38 @@
 urn 
-country;%0A    %7D%0A%0A    public void setCountry(Country country)%0A    %7B%0A        this.country = country
+getRegionByClass(County.class)
 ;%0A  
@@ -1299,32 +1299,30 @@
  public 
-County getCounty
+State getState
 ()%0A    %7B
@@ -1341,106 +1341,60 @@
 urn 
-county;%0A    %7D%0A%0A    public void setCounty(County county)
+getRegionByClass(State.class);;
 %0A    
-%7B
+%7D%0A
 %0A    
-    this.county = county;%0A    %7D%0A
+@Override
 %0A   
@@ -1399,36 +1399,36 @@
    public St
-ate getState
+ring getLink
 ()%0A    %7B%0A   
@@ -1434,361 +1434,361 @@
     
-  return state;%0A
+StringBuilder sb = new StringBuilder(name);%0A  
     
-%7D%0A
 %0A    
-public void set
+  
 State
-(S
+ s
 tate 
-s
+= getS
 tate
-)
+();
 %0A    
-%7B
+  
 %0A      
-  this.
+if(
 state 
+!
 = 
-state;
+null)
 %0A    
-%7D%0A
+  %7B
 %0A    
-@Override%0A    public String getLink()
+    sb.append(%22, %22).append(state.getName());
 %0A    
-%7B%0A
   
+%7D%0A
       
-Place%5B%5D places = %7Bthis, state%7D;%0A%0A        return toString(places
+else%0A      %7B%0A        Country country = getCountry(
 );%0A    
-%7D%0A%0A
     
-@Override%0A    public String toString(
+%0A        if(country != null
 )%0A   
- %7B%0A
        
- Place%5B%5D places = %7Bthis, county, state, country%7D;%0A%0A   
+sb.append(%22, %22).append(country.getName());%0A      %7D%0A%0A
      
+ 
 return 
+sb.
 toSt
@@ -1796,14 +1796,8 @@
 ing(
-places
 );%0A 

Update City.java