Package boco.ejb3PlugIn.utils

Source Code of boco.ejb3PlugIn.utils.Utils

/***************************************************************************
* 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/EJB3PlugIn/src/boco/ejb3PlugIn/utils/Utils.java,v 1.5.2.21 2008/06/09 13:55:29 fab Exp $
- $Author: fab $
- $Revision: 1.5.2.21 $
- $Date: 2008/06/09 13:55:29 $
- $Log: Utils.java,v $
- Revision 1.5.2.21  2008/06/09 13:55:29  fab
- Corretto bug che impediva il caricamento degli attributi di selezione nelle relazioni. Causato dalla modifica per supportare static final fields nei bean.
-
- Revision 1.5.2.20  2008/06/05 10:54:59  fab
- Fix gestione campi di tipo public static final
-
- Revision 1.5.2.19  2008/06/02 20:04:33  fab
- Eliminate System.out
-
- Revision 1.5.2.18  2008/05/21 20:46:35  fab
- Inserita cancellazione relazioni
-
- Revision 1.5.2.17  2008/05/05 12:40:11  fab
- Fix nella generazione dei packages
-
- Revision 1.5.2.16  2008/04/22 17:09:22  fab
- *** empty log message ***
-
- Revision 1.5.2.15  2008/04/22 06:17:38  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.5.2.14  2008/04/19 11:21:02  fab
- Modifiche a seguito del refactoring del generatore
-
- Revision 1.5.2.13  2008/04/19 10:26:49  fab
- Aggiornamento riferimenti licenza
-
- Revision 1.5.2.12  2008/04/02 07:31:36  fab
- Implementazione 0695/C
-
- Revision 1.5.2.11  2008/03/30 15:37:11  fab
-  Fix 0671/B
-
- Revision 1.5.2.10  2008/03/26 12:03:27  fab
- *** empty log message ***
-
- Revision 1.5.2.9  2008/02/22 13:32:42  fab
- Inserita la funzione che genera il metodo di copia del bean
-
- Revision 1.5.2.8  2007/10/01 15:16:05  fab
- Aggiornata dialog per la creazione del bean
- Riletta l'annotazione @ShowInForm
-
- Revision 1.5.2.7  2007/09/29 20:13:10  fab
- Fix del bug 0122/B
-
- Revision 1.5.2.6  2007/06/01 08:17:42  dev
- Fix nella addSeamGeneratorAnnotations
-
- Revision 1.5.2.5  2007/04/30 16:37:12  dev
- Modifiche per supportare l'opzione relatedAndNewItemsOnlyInPopUp nelle relazioni
-
- Revision 1.5.2.4  2007/04/13 19:33:43  dev
- Inserita la funzione addSeamGeneratorAnnotations
-
- Revision 1.5.2.3  2006/12/17 16:27:13  dev
- Fix nell'individuazione del beanTarget
-
- Revision 1.5.2.2  2006/12/17 16:15:35  dev
- Riorganizzata la classe Utils che era divisia in due !?
-
- Revision 1.6.2.2  2006/12/10 21:27:08  dev
- Inserita la funzione getCompilationUnit
-
- Revision 1.6.2.1  2006/08/28 08:48:49  dev
- Modifiche principli per il supporto di seam
-
- Revision 1.6  2006/06/01 16:08:41  dev
- Fix nella creazione dei nomi con lettere maiscole es. tipoIrregolarita
-
- Revision 1.5  2006/05/21 13:41:18  dev
- Corretta annotazione cvs
-

**/
package boco.ejb3PlugIn.utils;

import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Vector;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;

import boco.ejb3PlugIn.gui.BeanDialog;
import boco.ejb3PlugIn.gui.RelationshipAttributesSelectionDialog;
import boco.ejb3PlugIn.gui.RelationshipOption;

public class Utils
{

  public static String makeFirstLetterUpperCase(String in)
  {
    String tmp = in.toUpperCase();

    return tmp.substring(0, 1) + in.substring(1, in.length());
  }

  public static String makeFirstLetterLowerCase(String in)
  {
    String tmp = in.toLowerCase();

    return tmp.substring(0, 1) + in.substring(1, in.length());
  }

  /**
   * Dato un nome di un campo genera il metodo getter
   *
   * @param field
   * Nome del campo
   *
   * @return Nome del metodo getter
   */
  public static String makeGet(String field)
  {
    String tmp = field.toUpperCase();
    String tmp1 = tmp.substring(0, 1);

    return "get" + tmp1 + field.substring(1);
  }

  /**
   * Dato un campo <Field> restituisce il metodo
   *
   * set<Field>
   *
   */
  public static String makeSet(String field)
  {
    String tmp = field.toUpperCase();
    String tmp1 = tmp.substring(0, 1);

    return "set" + tmp1 + field.substring(1);
  }

  /**
   * Dato un metodo
   *
   * get<Field>
   *
   * restituisce il metodo
   *
   * set<Field>
   *
   */
  public static String switchGet2Set(String method)
  {
    String field = method.substring(3, method.length());

    return makeSet(field);
  }

  /**
   * Dato un campo <Field> restituisce il metodo
   *
   * add<Field>
   *
   */
  public static String makeAdd(String field)
  {
    String tmp = field.toUpperCase();
    String tmp1 = tmp.substring(0, 1);

    return "add" + tmp1 + field.substring(1);
  }

  /**
   * Dato un campo <Field> restituisce il metodo
   *
   * remove<Field>
   *
   */
  public static String makeRemove(String field)
  {
    String tmp = field.toUpperCase();
    String tmp1 = tmp.substring(0, 1);

    return "remove" + tmp1 + field.substring(1);
  }

  /**
   * Dato un campo <Field> restituisce il metodo
   *
   * removeAll<Field>
   *
   */
  public static String makeRemoveAll(String field)
  {
    String tmp = field.toUpperCase();
    String tmp1 = tmp.substring(0, 1);

    return "removeAll" + tmp1 + field.substring(1);
  }

  /**
   * Restituisce la lista ordinata dei nomi dei bean presenti
   */
  public static String[] getBeanNames(IPackageFragment entityPackage) throws JavaModelException
  {
    IJavaElement beans[] = entityPackage.getChildren();
    String[] beanNames = new String[beans.length];

    for (int i = 0; i < beanNames.length; i++)
    {
      String beanName = beans[i].getElementName();

      beanNames[i] = beanName.substring(0, beanName.length() - 5);
    }

    Arrays.sort(beanNames);

    return beanNames;
  }

  /**
   * Restituisce il compilationUnit di uno specifico bean
   *
   */
  public static ICompilationUnit getCompilationUnit(IPackageFragment entityPackage, String beanName) throws JavaModelException
  {
    IJavaElement beans[] = entityPackage.getChildren();

    for (int i = 0; i < beans.length; i++)
    {
      //System.out.println(beans[i].getElementName());

      if (beans[i].getElementName().equals(beanName + ".java"))
        return (ICompilationUnit) beans[i];
    }

    return null;
  }

  /**
   * Converte un metodo
   * <br>
   * getXxxxx
   * <br>
   * in
   * <br>
   * Xxxxx
   */
  public static String get2Bean(String method)
  {
    return method.substring(3, method.length());
  }

  /**
   * Restituisce il target bean di una relazione
   * 
   */
  public static String getTargetBean(ICompilationUnit compilationUnit, String relationship) throws JavaModelException
  {
    IType thisType = compilationUnit.getAllTypes()[0];

    IMethod method = thisType.getMethod(relationship, null);

    String returnType = method.getReturnType();

    if (!returnType.contains("List"))
    {
      // return get2Bean(relationship);

      return returnType.substring(1, returnType.length() - 1);
    }

    // Collection<Bean>
    int start = returnType.indexOf("<");
    int end = returnType.indexOf(">");

    return returnType.substring(start + 2, end - 1);
  }

  /**
   * Dal compilation unit di un bean, individua il compilation unit di un altro bean con nome beanName
   */
  public static ICompilationUnit getCompilationUnit(ICompilationUnit compilationUnit, String beanName)
  {
    IPackageFragment thisPackage = (IPackageFragment) compilationUnit.getParent();

    return thisPackage.getCompilationUnit(beanName + ".java");
  }

  /**
   * Restituisce le sole relazioni Master_Detail di cui il bean è Detail
   *
   */
  public static Vector<String> getMasterDetailRelations(ICompilationUnit compilationUnit) throws JavaModelException
  {
    Vector<String> ret = new Vector<String>();

    IType thisType = compilationUnit.getAllTypes()[0];

    IMethod methods[] = thisType.getMethods();

    for (int i = 0; i < methods.length; i++)
    {
      if (methods[i].getElementName().startsWith("get") && !methods[i].getElementName().startsWith("getId") && !methods[i].getElementName().contains("UID") && isMasterDetail(methods[i]))
      {
        ret.add(methods[i].getElementName());
      }
    }

    return ret;
  }

  /**
   * Indica se una metodo è una relazione MASTER_DETAIL di cui il bean è DETAIL, cioe' se contiene una annotazione delle seguenti:
   * <br>
   * <br>
   * @ManyToOne
   * <br>
   * e contiene
   * <br>
   * MASTER_DETAIL
   */
  public static boolean isMasterDetail(IMethod method) throws JavaModelException
  {
    String source = method.getSource();

    if (source.contains("@ManyToOne"))
    {
      if (source.contains("@Relationship"))
      {
        if (source.contains("MASTER_DETAIL"))
          return true;
        else
          return false;
      }
      return true;
    }
    return false;
  }

  /**
   * Restituisce tutte le relazioni di un bean
   *
   */
  public static Vector<String> getRelations(ICompilationUnit compilationUnit) throws JavaModelException
  {
    Vector<String> ret = new Vector<String>();

    IType thisType = compilationUnit.getAllTypes()[0];

    IMethod methods[] = thisType.getMethods();

    for (int i = 0; i < methods.length; i++)
    {
      if (methods[i].getElementName().startsWith("get") && !methods[i].getElementName().startsWith("getId") && !methods[i].getElementName().contains("UID") && isRelationship(methods[i]))
      {
        ret.add(methods[i].getElementName());
      }
    }

    return ret;
  }

  /**
   * Restituisce tutti gli attributi di un bean
   *
   */
  public static Vector<String> getAttributes(ICompilationUnit compilationUnit) throws JavaModelException
  {
    Vector<String> ret = new Vector<String>();

    IType thisType = compilationUnit.getAllTypes()[0];

    IField fields[] = thisType.getFields();

    for (int i = 0; i < fields.length; i++)
    {
      IMethod method = thisType.getMethod(makeGet(fields[i].getElementName()), null);

      if (!fields[i].getElementName().contains("UID") && !isRelationship(method))
        ret.add(fields[i].getElementName());
    }

    return ret;
  }

  /**
   * Indica se una metodo è una relazione, cioe' se contiene una annotazione delle seguenti:
   * <br>
   * <br>
   * @OneToMany
   * <br>
   * @OneToOne
   * <br>
   * @ManyToOne
   * <br>
   * @ManyToMany
   * 
   */
  public static boolean isRelationship(IMethod method)
  {
    String source = null;
    try
    {
      source = method.getSource();
    }
    catch (JavaModelException e)
    {
      ;
    }

    if (source == null)
      return false;

    if (source.contains("@OneToMany"))
      return true;

    if (source.contains("@OneToOne"))
      return true;

    if (source.contains("@ManyToOne"))
      return true;

    if (source.contains("@ManyToMany"))
      return true;

    return false;
  }

  /**
   * Genera le annotazioni per una relazione sulla base delle informazioni fornite
   */
  public static void addSeamGeneratorAnnotations(IMethod method, ICompilationUnit compilationUnit, RelationshipOption relationshipOption, RelationshipAttributesSelectionDialog sourceAttributeSelectionOption, RelationshipAttributesSelectionDialog targetAttributeSelectionOption) throws JavaModelException
  {
    String newMethodSourceCode = "";
    String oldMethodAnnotations = "";
    String oldMethodSourceCode = "";

    boolean master;

    if (method.getSource().contains("@OneToMany"))
      master = true;
    else
      master = false;

    // String targetBean = Utils.getTargetBean(compilationUnit, method.getElementName());

    String selectedAttributes = "";
    String masterAttributes = "";

    //    System.out.println(relationshipOption.getImplementationType());
    //    System.out.println(RelationshipOption.MASTER_DETAIL);

    if (relationshipOption.isUsed() && (relationshipOption.getImplementationType() != RelationshipOption.MASTER_DETAIL))
    {
      // Acquisizione degli attributi di selezione per LISTBOX e POPUP

      selectedAttributes = sourceAttributeSelectionOption.getSelectedAttributes();
    }

    if (relationshipOption.isUsed() && (relationshipOption.getImplementationType() == RelationshipOption.MASTER_DETAIL) && !master)
    {
      masterAttributes = sourceAttributeSelectionOption.getSelectedAttributes();
    }

    // Dal codice esistente rimuove l'annotazione se presente
    StringTokenizer st = new StringTokenizer(method.getSource(), "\t\n\r\f");
    while (st.hasMoreTokens())
    {
      String token = st.nextToken();
      // System.out.println(".." + token);

      if (!token.contains("@Relationship"))
      {
        if (token.startsWith("@"))
          oldMethodAnnotations += token + "\r\n";
        else
          oldMethodSourceCode += token + "\r\n";
      }
    }

    IType type = compilationUnit.getTypes()[0];

    //Rimuove il metodo esistente
    type.getMethod(method.getElementName(), null).delete(true, null);

    if (!relationshipOption.isUsed())
      newMethodSourceCode += "@Relationship(used=false, orderInForm=0";
    else
    {
      if (relationshipOption.getImplementationType() == RelationshipOption.POPUP)
        newMethodSourceCode += "@Relationship(used=true, orderInForm=" + relationshipOption.getOrder() + ", type=" + relationshipOption.getImplementationType2String() + ", selectionAttributes=" + selectedAttributes + " ";

      if (relationshipOption.getImplementationType() == RelationshipOption.LISTBOX || relationshipOption.getImplementationType() == RelationshipOption.OPTIONS)
        newMethodSourceCode += "@Relationship(used=true, orderInForm=" + relationshipOption.getOrder() + ", type=" + relationshipOption.getImplementationType2String() + ", selectionAttributes=" + selectedAttributes;

      if (relationshipOption.getImplementationType() == RelationshipOption.MASTER_DETAIL && master)
        newMethodSourceCode += "@Relationship(used=true, orderInForm=" + relationshipOption.getOrder() + ", type=" + relationshipOption.getImplementationType2String();

      if (relationshipOption.getImplementationType() == RelationshipOption.MASTER_DETAIL && !master)
        newMethodSourceCode += "@Relationship(used=true, orderInForm=" + relationshipOption.getOrder() + ", type=" + relationshipOption.getImplementationType2String() + ", masterAttributes=" + masterAttributes;
    }

    if (relationshipOption.isRequired())
      newMethodSourceCode += ", required=true";

    if (relationshipOption.isHelp())
      newMethodSourceCode += ", help=true";

    if (relationshipOption.isRelatedAndNewItemsOnly())
      newMethodSourceCode += ", relatedAndNewItemsOnlyInPopUp=true";

    newMethodSourceCode += " )\r\n";

    newMethodSourceCode = oldMethodAnnotations + newMethodSourceCode + oldMethodSourceCode + "\r\n";

    type.createMethod(newMethodSourceCode, null, true, null);

    if (!compilationUnit.getSource().contains("import org.boco.seamwebappgen.annotations.Relationship"))
      compilationUnit.createImport("org.boco.seamwebappgen.annotations.Relationship", null, null);

    if (!compilationUnit.getSource().contains("import org.boco.seamwebappgen.annotations.RelationshipImplementationType"))
      compilationUnit.createImport("org.boco.seamwebappgen.annotations.RelationshipImplementationType", null, null);

    if (relationshipOption.getImplementationType() == RelationshipOption.POPUP && !compilationUnit.getSource().contains("import org.boco.seamwebappgen.annotations.PopUpEnabledActionType"))
      compilationUnit.createImport("org.boco.seamwebappgen.annotations.PopUpEnabledActionType", null, null);

    compilationUnit.save(null, true);
  }

  public static String getTypeValue(String code, String key)
  {
    StringTokenizer st = new StringTokenizer(code, "( = \",)");
    while (st.hasMoreTokens())
    {
      String token = st.nextToken();
      if (token.equals(key))
      {
        token = st.nextToken();
        return token;
      }
    }

    return null;
  }

  public static Boolean getBooleanValue(String code, String key)
  {
    StringTokenizer st = new StringTokenizer(code, "( = \",)");

    while (st.hasMoreTokens())
    {
      String token = st.nextToken();
      if (token.equals(key))
      {
        token = st.nextToken();
        return new Boolean(token);
      }
    }

    return new Boolean(false);
  }

  public static Integer getIntegerValue(String code, String key)
  {
    StringTokenizer st = new StringTokenizer(code, "( = \",)");

    while (st.hasMoreTokens())
    {
      String token = st.nextToken();
      if (token.equals(key))
      {
        token = st.nextToken();
        return new Integer(token);
      }
    }

    return new Integer(0);
  }

  /**
   * Dato un metodo get<Field> o set<Field> restituisce <Field>
   *
   *
   */
  public static String makeMethod2Field(String getMethod)
  {
    return makeFirstLetterLowerCase(getMethod.substring(3, getMethod.length()));
  }

  /**
   * Dato un metodo di una relazione :N
   * <br>
   * get<Field>s o set<Field>s
   *<br>
   * restituisce <Field>
   *
   */
  public static String makeMethod2SingularField(String getMethod)
  {
    return makeFirstLetterLowerCase(getMethod.substring(3, getMethod.length() - 1));
  }

  public static String makeSecurityAnnotation(BeanDialog beanDialog)
  {
    String ret = "@Security(";

    if (!beanDialog.getCreateEnabledRoles().equals(""))
    {
      ret += "rolesEnabledToCreate={" + beanDialog.getCreateEnabledRoles() + "}, ";
    }
    else
    {
      if (beanDialog.getDeleteCondition().equals(""))
        ret += "createCondition=\"\", ";
      else
        ret += "createCondition=\"#{" + beanDialog.getCreateCondition() + "}\", ";
    }

    if (!beanDialog.getEditEnabledRoles().equals(""))
    {
      ret += "rolesEnabledToEdit={" + beanDialog.getEditEnabledRoles() + "}, ";
    }
    else
    {
      if (beanDialog.getDeleteCondition().equals(""))
        ret += "editCondition=\"\", ";
      else
        ret += "editCondition=\"#{" + beanDialog.getEditCondition() + "}\", ";
    }

    if (!beanDialog.getDeleteEnabledRoles().equals(""))
    {
      ret += "rolesEnabledToDelete={" + beanDialog.getDeleteEnabledRoles() + "} ";
    }
    else
    {
      if (beanDialog.getDeleteCondition().equals(""))
        ret += "deleteCondition=\"\" ";
      else
        ret += "deleteCondition=\"#{" + beanDialog.getDeleteCondition() + "}\" ";
    }

    ret += ")\r\n";

    return ret;
  }

  public static String makeShowListAnnotation(BeanDialog beanDialog)
  {
    String ret = "";

    ret += "@ShowList(names = {" + beanDialog.getViewNames() + "}, filters={" + beanDialog.getFilters() + "}, enabledRoles={" + beanDialog.getEnableRoles() + "}, mains={" + beanDialog.getMain() + "}, newMenuOptionConditions={" + beanDialog.getNewMenuOptionConditions() + "}, printMenuOptionConditionsInList={" + beanDialog.getprintMenuOptionConditionsInList() + "})\r\n";

    return ret;
  }

}
TOP

Related Classes of boco.ejb3PlugIn.utils.Utils

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.