Package org.boco.seamwebappgen.seamGeneration

Source Code of org.boco.seamwebappgen.seamGeneration.PagesConfig

/***************************************************************************
*  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/seamGeneration/Attic/PagesConfig.java,v 1.1.2.4 2008/05/05 16:01:08 fab Exp $
- $Author: fab $
- $Revision: 1.1.2.4 $
- $Date: 2008/05/05 16:01:08 $
- $Log: PagesConfig.java,v $
- Revision 1.1.2.4  2008/05/05 16:01:08  fab
- Reintrodotta la onListLoad per mantenere la possibilità di utilizzare forceReload
- Introdotto codice custom nella afterTransactionSuccess
-
- Revision 1.1.2.3  2008/05/05 14:29:55  fab
- Modifiche alla procedura di installazione
- Modifiche per gestione entity home
-
- Revision 1.1.2.2.2.2  2008/05/04 14:35:49  fab
- *** empty log message ***
-
- Revision 1.1.2.2.2.1  2008/04/27 20:59:32  fab
- *** empty log message ***
-
- Revision 1.1.2.2  2008/04/22 05:37:57  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.1.2.1  2008/04/19 13:12:39  fab
- Modifiche varie per rafactoring
-
- Revision 1.1.2.1  2008/04/19 11:18:34  fab
- Refactoring
-
- Revision 1.3.4.2  2008/04/19 10:07:02  fab
- Aggiornamento riferimenti licenza
-
- Revision 1.3.4.1  2008/04/01 07:22:36  fab
- Implementazione 0688/C
-
- Revision 1.3  2008/01/26 10:39:02  fab
- Fix per generazione del file pages.xml indicata dalla dtd
-
- Revision 1.2  2007/12/21 11:07:04  fab
- Dopo merge per supporto jboss 4.2.2
-
- Revision 1.1.4.1  2007/12/18 14:22:41  fab
- Fix gestione errori mancanza conversazione
-
- Revision 1.1  2007/09/29 13:17:20  fab
- Nuova versione iniziale del 29/09/2007
-
- Revision 1.5.2.14  2007/08/30 13:26:47  fab
- Fix per la cancellazione dei file temporanei su windows
-
- Revision 1.5.2.13  2007/08/29 12:46:22  bob
- Tolta 'a' ed 'e' accentate
-
- Revision 1.5.2.12  2007/07/22 16:04:34  fab
- Modifica per inserire nella vista principale la creazione della conversazione
-
- Revision 1.5.2.11  2007/06/25 21:17:49  fab
- Fix generazione pages.xml che cancellava items
-
- Revision 1.5.2.10  2007/05/29 10:04:14  fab
- Modifica per inserire la descrizione di tutte le pagine (incluse quelle dei bean)
-
- Revision 1.5.2.9  2007/05/19 09:33:28  dev
- Moltissime modifiche
-
- Revision 1.5.2.8  2007/04/21 13:36:10  dev
- Modifiche varie alla generazione
- Modifiche per compatibilità con il nuovo jboss as 4.2.0.CR2
-
- Revision 1.5.2.7  2007/04/02 12:01:03  dev
- Inserita chiamata alla onListLoad anche per le pagine risultato della ricerca.
- In questo modo qaundo viene cancellato un bean dal risultato della ricerca, la lista viene correttamente aggiornata
-
- Revision 1.5.2.6  2007/03/25 10:04:42  dev
- Eliminate righe vuote dal file pages.xhtml
-
- Revision 1.5.2.5  2007/03/25 09:31:38  dev
- Modifiche per la gestione della sicurezza sulle azioni
-
- Revision 1.5.2.4  2007/03/24 17:26:00  dev
- Modifiche per generare più liste per il bean e gestione della sicurezza di ciascuna lista
-
- Revision 1.5.2.3  2007/03/22 18:15:07  dev
- *** empty log message ***
-
- Revision 1.5.2.2  2007/02/09 11:20:49  dev
- Modifiche per la gestione delle viste categorizzate
-
- Revision 1.5.2.1  2007/01/20 09:11:06  dev
- Tante modifiche che non sono descrivibili
-
- Revision 1.4  2007/01/07 14:35:08  dev
- Modifiche Dicembre 2006
-
- Revision 1.3  2006/12/10 16:40:05  dev
- Reingegnerizzazione del generatore per funzionare con le conversazioni anzichè la sessione
-
- Revision 1.2  2006/12/03 19:13:03  dev
- Fix per supportare bean ereditati
-
- Revision 1.1  2006/10/23 08:25:32  dev
- Prima versione separata dal generatore struts
-
- Revision 1.4  2006/08/30 13:33:06  dev
- Completata la generazione delle PopUp con l'aggiornamento della pages.xml
-
- Revision 1.3  2006/07/26 11:46:07  dev
- Gestione relazioni :1 con ListBox
-
- Revision 1.2  2006/07/16 17:41:44  dev
- *** empty log message ***
-
-
**/
package org.boco.seamwebappgen.seamGeneration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.IOUtils;
import org.boco.seamwebappgen.info.Bean;
import org.boco.seamwebappgen.info.InfoReader;
import org.boco.seamwebappgen.info.Relationship;
import org.boco.seamwebappgen.info.ShowList;
import org.boco.seamwebappgen.utils.PagesResolver;
import org.boco.seamwebappgen.utils.Utils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


/**
*
* @author Fabrizio Boco
*
* @since 0.1
*/
public class PagesConfig
{

  public static void addListPages(String targetDir, Bean bean) throws FileNotFoundException, IOException
  {
    Vector<ShowList> showList = bean.getLists();

    for (ShowList list : showList)
    {
      String roles = "";

      if (list.getEnabledRoles().size() > 0)
      {
        roles = Utils.makeRolesCheckCondition(list.getEnabledRoles());
      }
     
      if (list.isMain())
        addListPage(targetDir, bean, list.getName(), roles, true);
      else
        //addListPage(targetDir, bean, list.getName(), roles, false);
        addListPage(targetDir, bean, list.getName(), roles, true);
    }
       
    // Inserisce anche una entry per le pagine del bean
    // per poter inserire la descrizione indispensabile per lo switcher
    addPage(targetDir, null, "/"+bean.getName()+".xhtml", null);
   
    if(bean.getSearchAttributes().size()>0)
      addSearchPage(targetDir, bean, "");   
  }

  public static void addSearchPage(String targetDir, Bean bean, String enablesRoles) throws FileNotFoundException, IOException
  {
    //addPage(targetDir, bean.getName() + "Manager.onSearchListLoad", "/search/" + bean.getName() + "SearchList.xhtml", enablesRoles);
    addPage(targetDir, null, "/search/" + bean.getName() + "SearchList.xhtml", enablesRoles);
  }
 
  public static void addListPage(String targetDir, Bean bean, String listName, String enablesRoles, boolean beginConversation) throws FileNotFoundException, IOException
  {
    addPage(targetDir, bean.getName() + "Manager.onListLoad", "/list/" + listName + "List.xhtml", enablesRoles,beginConversation);

    //addPage(targetDir, null, "/list/" + listName + "List.xhtml", enablesRoles,beginConversation);
  }

  public static void addPopUpPage(String sourceDir, String targetDir, Relationship rel) throws FileNotFoundException, IOException
  {

    addPage(targetDir, rel.getToBeanName() + "Manager.onPopUpLoad", "/popup/" + rel.getFromBeanName() + rel.getName() + "PopUp.xhtml", "");

    // Poiche' la relazione e' ereditata anche dai bean figli, deve essere generata
    // una entry in page anche per questi

    InfoReader infoReader = new InfoReader(sourceDir);
       
    Vector<String> derivedBeans = infoReader.getDerivedBeans(rel.getToBeanName());
    for (String derivedBean : derivedBeans)
    {
      addPage(targetDir, derivedBean + "Manager.onPopUpLoad", "/popup/" + rel.getFromBeanName() + rel.getName() + "PopUp.xhtml", "");
    }
   
    derivedBeans = infoReader.getDerivedBeans(rel.getFromBeanName());
    for (String derivedBean : derivedBeans)
    {
      addPage(targetDir, rel.getToBeanName() + "Manager.onPopUpLoad", "/popup/" + derivedBean + rel.getName() + "PopUp.xhtml", "");
    }
   
   
  }
 
  public static void addPage(String targetDir, String action, String viewId, String restrictions) throws FileNotFoundException, IOException
  {
    addPage(targetDir, action, viewId, restrictions, false);
  }
 
  public static void addPage(String targetDir, String action, String viewId, String restrictions, boolean beginConversation) throws FileNotFoundException, IOException
  {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    Document document;

    String pagesFile = targetDir + File.separator + "html" + File.separator + "WEB-INF" + File.separator + "pages.xml";

    try
    {
      builder = factory.newDocumentBuilder();
      builder.setEntityResolver(new PagesResolver());

      document = builder.parse(new File(pagesFile));

      NodeList pageList = document.getElementsByTagName("pages");
      Node pagesNode = pageList.item(0);

      NodeList existingPages = ((Element) pagesNode).getElementsByTagName("page");
      boolean found = false;

      for (int i = 0; i < existingPages.getLength(); i++)
      {
        Element item = (Element) existingPages.item(i);

        if (item.getAttribute("view-id").compareTo(viewId) == 0)
        {
          pagesNode.removeChild(item);
        }
      }

      // Elimina le righe vuote inutili
      NodeList textNodes = pagesNode.getChildNodes();

      for(int s=0; s<textNodes.getLength(); s++)
      {
        Node text = textNodes.item(s);       
       
        if (text != null && text.getNodeType() != Node.COMMENT_NODE && text.getNodeType() == Node.TEXT_NODE)
        {
          String content = text.getTextContent();
         
          if (content.contains("\n"))
          {
            pagesNode.removeChild(text);
          }
        }
      }           
           
      if (!found)
      {
        Element newPage = document.createElement("page");
       
        // Inserisce la pagina
        newPage.setAttribute("view-id", viewId);
       
        if (action != null)
          newPage.setAttribute("action", "#{" + action + "}");

        // Aggiunge la description - Indispensabile per il conversation switcher
        Element description = document.createElement("description");
               
        // Aggiunge la begin conversation se necessario
        if (beginConversation)
        {
          Element conversation = document.createElement("begin-conversation");
          conversation.setAttribute("join", "true");
          newPage.appendChild(conversation);
        }
       
        Text td = document.createTextNode(viewId.substring(viewId.lastIndexOf("/")+1, viewId.lastIndexOf(".")));
        description.appendChild(td);
        newPage.appendChild(description);
       
        if (restrictions!=null && !restrictions.equals(""))
        {
          // Aggiunge le condizioni di sicurezza <restrict>

          Element restrict = document.createElement("restrict");

          Text t = document.createTextNode(restrictions);
          restrict.appendChild(t);

          newPage.appendChild(restrict);
        }

        /** Individua il primo nodo di tipo exception **/
       
        NodeList exceptions = ((Element) pagesNode).getElementsByTagName("exception");
        if(exceptions!=null)
        {
          pagesNode.insertBefore(newPage, exceptions.item(0));
        }
        else
          pagesNode.appendChild(newPage);
      }     

      File f = new File(pagesFile + "NEW");

      TransformerFactory xformFactory = TransformerFactory.newInstance();
      Transformer idTransform = xformFactory.newTransformer();

      //idTransform.setOutputProperty("encoding", "UTF-8");
      //idTransform.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd");
      //idTransform.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN");

      idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
      idTransform.setOutputProperty("indent", "yes");
      idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
      idTransform.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "3");

      Source input = new DOMSource(document);

      Result output = new StreamResult(f);

      idTransform.transform(input, output);

      File oldTargetFile = new File(pagesFile);

      if (oldTargetFile.exists() && IOUtils.contentEquals(new FileInputStream(new File(pagesFile + "NEW")), new FileInputStream(oldTargetFile)))
      {
        // Il nuovo file generato e' uguale al precedente
        // Il nuovo viene cancellato

        f.deleteOnExit();
      }
      else
      {
        // Il nuovo file generato e' diverso dal precedente
        // Il nuovo file viene copiato su quello esistente e poi cancellato
        IOUtils.copy(new FileInputStream(new File(pagesFile + "NEW")), new FileOutputStream(new File(pagesFile)));

        f.deleteOnExit();
      }
    }
    catch (ParserConfigurationException ce)
    {
      System.out.println("Errore " + ce.getMessage());
    }
    catch (SAXParseException spe)
    {
      String message;

      message = "Errore Parser: " + spe.getMessage();
      message += "<br>";
      message += "Linea " + spe.getLineNumber();
      message += "<br>";
      message += "Colonna " + spe.getColumnNumber();
    }
    catch (IOException io)
    {
      io.printStackTrace();
      System.out.println("Errore " + io.getMessage());
    }
    catch (SAXException pe)
    {
      System.out.println("Errore " + pe.getMessage());
    }
    catch (TransformerConfigurationException io)
    {
      io.printStackTrace();
    }
    catch (TransformerException io)
    {
      io.printStackTrace();
    }
  }

  private static String getPageName(String viewId)
  {
    int start;
    int end;

    start = viewId.lastIndexOf("/") + 1;
    end = viewId.indexOf(".");

    return viewId.substring(start, end);
  }

}
TOP

Related Classes of org.boco.seamwebappgen.seamGeneration.PagesConfig

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.