Package org.boco.seamwebappgen.utils

Source Code of org.boco.seamwebappgen.utils.EntityCheck

/***************************************************************************
*  Copyright (c) 2004 - 2008  Fabrizio Boco fabboco@users.sourceforge.net *
*                                                                         *
*                                                                         *
*   This is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Library General Public           *
*   License (version 2.1) as published by the Free Software Foundation    *
*                                                                         *
*   This library  is distributed in the hope that it will be useful,      *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this library; see the file COPYING.LIB. If not,    *
*   write to the Free Software Foundation, Inc., 59 Temple Place,         *
*   Suite 330, Boston, MA  02111-1307, USA                                *
*                                                                         *
***************************************************************************/
/**
- $Header: /usr/local/cvslocalrepository/SeamWebAppGenerator/src/org/boco/seamwebappgen/utils/Attic/EntityCheck.java,v 1.1.2.2 2008/04/22 05:37:55 fab Exp $
- $Author: fab $
- $Revision: 1.1.2.2 $
- $Date: 2008/04/22 05:37:55 $
- $Log: EntityCheck.java,v $
- Revision 1.1.2.2  2008/04/22 05:37:55  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.1.2.1  2008/04/19 13:12:38  fab
- Modifiche varie per rafactoring
-
- Revision 1.1.2.1  2008/04/19 11:18:33  fab
- Refactoring
-
- Revision 1.1.8.1  2008/04/19 10:07:02  fab
- Aggiornamento riferimenti licenza
-
- Revision 1.1  2007/09/29 13:17:22  fab
- Nuova versione iniziale del 29/09/2007
-
- Revision 1.1.2.4  2007/09/29 09:38:35  fab
- Fix per ridurre i messaggi a console
-
- Revision 1.1.2.3  2007/08/29 12:46:23  bob
- Tolta 'a' ed 'e' accentate
-
- Revision 1.1.2.2  2007/07/17 09:03:35  fab
- Rimosso check su MERGE perchè in qualche caso può servire
-
- Revision 1.1.2.1  2007/02/14 13:02:33  dev
- Modifiche per supportare chiave bean composta
-
- Revision 1.1  2006/10/23 08:25:32  dev
- Prima versione separata dal generatore struts
-
- Revision 1.3  2006/08/25 10:10:37  dev
- Modifiche per la gestione delle PopUp
-
- Revision 1.2  2006/04/20 10:15:11  dev
- Fix nella generazione delle pagine di ricerca sui listbox
- Intorudzione delle keywords CVS nei file generati
- Introduzione delle keywords per i file del generatore
-
**/
package org.boco.seamwebappgen.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Vector;

import javax.persistence.CascadeType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.boco.seamwebappgen.exception.EntityCheckMissingEqualsMethodException;
import org.boco.seamwebappgen.exception.EntityCheckMissingKeyException;
import org.boco.seamwebappgen.exception.EntityCheckMissingMappedByException;
import org.boco.seamwebappgen.exception.EntityCheckMissingSerializableInterfaceException;
import org.boco.seamwebappgen.exception.EntityCheckNoCollectionException;
import org.boco.seamwebappgen.exception.EntityCheckNoCollectionTypeException;
import org.boco.seamwebappgen.exception.EntityCheckWrongMappedByFieldException;
import org.boco.seamwebappgen.exception.EntityCheckWrongMappedByTypeException;
import org.boco.seamwebappgen.exception.EntityCheckWrongMergeException;
import org.boco.seamwebappgen.exception.EntityCheckWrongTableNameException;
import org.boco.seamwebappgen.exception.MissingTargetEntityException;
import org.boco.seamwebappgen.info.Relationship;


public class EntityCheck
{
  private String               beanName;
    private Class                c;
    private Vector<Relationship> relationships;
    private Vector<String>       messages = new Vector<String>();

    public Vector<String> getMessages()
    {
        return messages;
    }

    public boolean check(String beanName, Class c, Vector<Relationship> relationships) throws Exception
    {
      this.beanName = beanName;
        this.c = c;
        this.relationships = relationships;
        boolean ret = true;
        boolean ret1;

        Annotation annotations[] = c.getAnnotations();

        ret = ret && checkEntity(annotations);

        ret1 = checkTable(annotations);
        ret = ret && ret1;

        ret1 = checkSerializable();
        ret = ret && ret1;

        ret1 = checkId();
        ret = ret && ret1;

        ret1 = checkEqualsMethod();
        ret = ret && ret1;

        ret1 = checkRelationships();
        ret = ret && ret1;

        return ret;
    }

    private boolean checkEntity(Annotation annotations[])
    {
        for (Annotation annotation : annotations)
        {
            if (annotation.annotationType().equals(javax.persistence.Entity.class))
            {
                messages.add("check @Entity: OK");
                return true;
            }
        }

        messages.add("check @Entity: Missing @Entity annotation");
        return false;
    }

    private boolean checkTable(Annotation annotations[]) throws EntityCheckWrongTableNameException
    {
        String className = c.getSimpleName().toUpperCase();

        for (Annotation annotation : annotations)
        {
            if (annotation.annotationType().equals(javax.persistence.Table.class))
            {
                Table table = (Table) annotation;

                if (table.name().toUpperCase().compareTo(className) != 0)
                  throw new EntityCheckWrongTableNameException(beanName,table.name());

                return true;
            }
        }

        messages.add("check @Table: Missing @Table annotation");
        return false;
    }

    private boolean checkSerializable() throws Exception
    {
        Class interfaces[] = c.getInterfaces();
       
        for (Class interf : interfaces)
        {

            if (interf.getName().equals("java.io.Serializable"))         
            {
                messages.add("check Serializable interface: OK");
                return true;
            }
        }

        //System.out.println("--------------------------->Estende "+c.getGenericSuperclass());
        //System.out.println("--------------------------->Estende Altro "+(!c.getGenericSuperclass().equals(Object.class)));

        if (!c.getGenericSuperclass().equals(Object.class))
        {
            messages.add("check Serializable interface: OK");
            return true;
        }

       
        throw new EntityCheckMissingSerializableInterfaceException(beanName);
    }

    private boolean checkId() throws Exception
    {
        Vector<Annotation> annotations = getAllAnnotations();

        for (Annotation annotation : annotations)
        {
            if (annotation.annotationType().equals(javax.persistence.Id.class) ||
               annotation.annotationType().equals(javax.persistence.EmbeddedId.class)
              )
            {
                messages.add("check @Id: OK");
                return true;
            }
        }

        if (!c.getGenericSuperclass().equals(Object.class))
        {
            messages.add("check @Id: OK");
            return true;
        }
       
        throw new EntityCheckMissingKeyException(beanName);
    }

    private boolean checkEqualsMethod() throws Exception
    {
        // Method methods[] = c.getMethods();
        Method methods[] = c.getDeclaredMethods();

        for (Method method : methods)
        {
            if (method.getName().equals("equals"))
            {
                messages.add("check equals method: OK");
                return true;
            }
        }

        throw new EntityCheckMissingEqualsMethodException(beanName);
    }

    private boolean checkRelationships() throws Exception
    {
        boolean ret = true;
        boolean ret1;

        for (Relationship relationship : relationships)
        {
            if (!relationship.isFromMany() && relationship.isToMany())
            {
                ret1 = checkOneToMany(relationship);
                ret = ret && ret1;
            }
           
            if (relationship.isFromMany() && !relationship.isToMany())
            {
                ret1 = checkManyToOne(relationship);
                ret = ret && ret1;
            }
         
        }
       
        return ret;
    }

    private boolean checkOneToMany(Relationship relationship) throws Exception
    {
        Method method = c.getMethod(relationship.getName(), (Class[])null);

       
        ParameterizedType returnType;
        try
        {
            returnType = (ParameterizedType)method.getGenericReturnType();
        }
        catch (ClassCastException e)
        {
            messages.add("check 1-M "+relationship.getName()+": field is not a Collection");
            return false;
        }
       
//        if (!returnType.getRawType().toString().equals("interface java.util.Collection"))
//            throw new EntityCheckNoCollectionException(beanName, relationship.getName());
               
        //System.out.println(returnType.getActualTypeArguments()[0].toString());
        //System.out.println("class "+relationship.getFromBeanName());
       
        if (!returnType.getActualTypeArguments()[0].toString().equals("class "+relationship.getToBeanClassName()))
          throw new EntityCheckNoCollectionTypeException(beanName, relationship.getName());
       
        OneToMany annotation = (OneToMany)method.getAnnotation(OneToMany.class);
        Class targetEntity = annotation.targetEntity();

        if (targetEntity.getName().equals("void"))
          throw new MissingTargetEntityException(beanName, relationship.getName());
           

        CascadeType cascades[] = annotation.cascade();
       
        for(CascadeType cascade:cascades)
        {
          // Rimossa perche' ci sono casi in cui e' necessario avere il MERGE !!!
//            if (cascade.equals(CascadeType.MERGE) || cascade.equals(CascadeType.ALL))
//              throw new EntityCheckWrongMergeException(beanName, relationship.getName());
        }
       
        if (annotation.mappedBy().equals(""))
          throw new EntityCheckMissingMappedByException(beanName, relationship.getName());
       
       
        Field targetFields[] = targetEntity.getDeclaredFields();
       
        if(!contains(targetFields,annotation.mappedBy()))
          throw new EntityCheckWrongMappedByTypeException(beanName, relationship.getName());
       
        if(!type(targetFields,annotation.mappedBy()))         
          throw new EntityCheckWrongMappedByFieldException(beanName, relationship.getName(), annotation.mappedBy(), targetEntity.getSimpleName());

        return true;
    }

    private boolean checkManyToOne(Relationship relationship) throws NoSuchMethodException
    {
        Method method = c.getMethod(relationship.getName(), (Class[])null);

        /*
        ParameterizedType returnType;
        try
        {
            returnType = (ParameterizedType)method.getGenericReturnType();
        }
        catch (ClassCastException e)
        {
            messages.add("check M-1 "+relationship.getName()+": field is not a Collection");
            return false;
        }
        //System.out.println(returnType.getRawType().toString());
        //System.out.println(returnType.getActualTypeArguments()[0].toString());
        //System.out.println(relationship.getToBeanClassName());
       
        if (!returnType.getRawType().toString().equals("interface java.util.Collection"))
        {
           
            messages.add("check M-1 "+relationship.getName()+": field is not a Collection");
            return false;
        }
       
        if (!returnType.getActualTypeArguments()[0].toString().equals("class "+relationship.getToBeanClassName()))
        {
           
            messages.add("check M-1 "+relationship.getName()+": Wrong type of Collection");
            return false;
        }
        */
       
        ManyToOne annotation = (ManyToOne)method.getAnnotation(ManyToOne.class);
        Class targetEntity = annotation.targetEntity();

        if (targetEntity.getName().equals("void"))
        {
           
            messages.add("check M-1 "+relationship.getName()+": Missing Target Entity");
            return false;
        }

        /*
         * CascadeType cascades[] = annotation.cascade();
        if (annotation.mappedBy().equals(""))
        {
            messages.add("check 1-M "+relationship.getName()+": Missing mappedBy");
            return false;               
        }           
       
       
        Field targetFields[] = targetEntity.getDeclaredFields();
       
        if(!contains(targetFields,annotation.mappedBy()))
        {
            messages.add("check 1-M "+relationship.getName()+": Wrong mappedBy field");
            return false;               
        }           
       
        if(!type(targetFields,annotation.mappedBy()))
        {
            messages.add("check 1-M "+relationship.getName()+": Wrong type of "+annotation.mappedBy()+" in "+targetEntity.getSimpleName());
            return false;               
        }           
        */
       
        messages.add("check M-1 "+relationship.getName()+": OK");
        return true;
    }

    private Vector<Annotation> getAllAnnotations()
    {
        Vector<Annotation> ret = new Vector<Annotation>();

        // Method methods[] = c.getMethods();
        Method methods[] = c.getDeclaredMethods();

        for (Method method : methods)
        {
            Annotation annotations[] = method.getAnnotations();

            for (Annotation annotation : annotations)
            {
                ret.add(annotation);
            }
        }

        return ret;
    }
   
   
    private boolean contains(Field fields[], String field)
    {
        for(Field f:fields)
        {
            if (f.getName().equals(field))
                return true;
        }
        return false;
    }

    private boolean type(Field fields[], String field)
    {
        for(Field f:fields)
        {
            if (f.getName().equals(field) && f.getType().equals(c))
                return true;
        }
        return false;
    }

}
TOP

Related Classes of org.boco.seamwebappgen.utils.EntityCheck

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.