/*
 *  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.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;

import static javax.persistence.GenerationType.SEQUENCE;

/**
 * @author Joe Hendrix
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.INTEGER)
@NamedQueries({
                      @NamedQuery(name = Place.FIND_BY_NAME, query = "SELECT p FROM Place p WHERE p.name = :name"),
                      @NamedQuery(name = Place.FIND_ALL, query = "SELECT p FROM Place p")
              })
public abstract class Place
{

    public static final String FIND_ALL = "Place.findAll";
    public static final String FIND_BY_NAME = "Place.findByName";

    @Id
    @SequenceGenerator(name = "PLACE_SEQUENCE", sequenceName = "PLACE_SEQUENCE", allocationSize = 1)
    @GeneratedValue(strategy = SEQUENCE, generator = "PLACE_SEQUENCE")
    private int id;
    @Column(unique = true, nullable = false)
    private String name;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public abstract String getLink();

    protected String toString(Place[] places)
    {
        boolean first = true;
        StringBuilder sb = new StringBuilder();

        for ( Place place : places )
        {
            if ( place != null )
            {
                if ( !first )
                    sb.append(", ");

                sb.append(place.getName());
                first = false;
            }
        }

        return sb.toString();
    }
}

@@ -1627,16 +1627,300 @@
 ng name;
+%0A      @ManyToOne(fetch = FetchType.LAZY, cascade = %7BCascadeType.MERGE, CascadeType.PERSIST%7D)%0A    @JoinTable(name = %22REGION_OF%22,%0A               joinColumns = @JoinColumn(name = %22PLACE_ID%22),%0A               inverseJoinColumns = @JoinColumn(name = %22REGION_ID%22))%0A    private Place region;
 %0A%0A    pu
@@ -2161,32 +2161,184 @@
 me = name;%0A    %7D
+%0A    %0A    public Place getRegion()%0A    %7B%0A      return region;%0A    %7D%0A    %0A    public void setRegion(Place region)%0A    %7B%0A      this.region = region;%0A    %7D
 %0A%0A    public abs
@@ -2357,24 +2357,28 @@
  getLink();%0A
+    
 %0A    protect
@@ -2384,38 +2384,59 @@
 ted 
-String toString(Place%5B%5D places
+Place getRegionByClass(Class%3C? extends Place%3E clazz
 )%0A  
@@ -2449,38 +2449,42 @@
     
- 
- boolean first = true
+Place region = this.region
 ;%0A      
   St
@@ -2483,229 +2483,416 @@
     
+%0A
   
-StringBuilder sb = new StringBuilder();%0A%0A        for ( Place place : places )%0A        %7B%0A            if ( place != null )
+    while(region != null)%0A      %7B%0A        if(clazz.equals(region.getClass()))%0A        %7B%0A          return region;%0A        %7D%0A        %0A        region = region.getRegion();
 %0A      
+%7D%0A
       
-%7B
 %0A      
-          if ( !first 
+return null;%0A    %7D%0A    %0A    @Override%0A    public String toString(
 )%0A    
+%7B%0A
       
-          sb.append(%22, %22);%0A%0A         
+StringBuilder sb = new StringBuilder(name);%0A      %0A      Place region = this.region;%0A      %0A      while(region != null)%0A      %7B%0A
        
+ 
 sb.a
@@ -2897,21 +2897,35 @@
 .append(
-place
+%22, %22).append(region
 .getName
@@ -2926,17 +2926,16 @@
 etName()
-)
 ;%0A      
@@ -2940,51 +2940,51 @@
     
-        first = false;%0A     
+region = region.getRegion();%0A
       
- 
 %7D%0A      
   %7D%0A
@@ -2979,23 +2979,17 @@
 %7D%0A      
-  %7D%0A%0A  
+%0A
       re

Update Place.java