BonitaTransfer.java :  » Workflow-Engines » bonita-v3.1 » hero » historic » Java Open Source

Java Open Source » Workflow Engines » bonita v3.1 
bonita v3.1 » hero » historic » BonitaTransfer.java
package hero.historic;

/**
*
* Bonita
* Copyright (C) 1999 Bull S.A.
* Bull 68 route de versailles  78434 Louveciennes Cedex France
* Further information: bonita@objectweb.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
* USA
*
*
--------------------------------------------------------------------------
* $Id: BonitaTransfer.java,v 1.9 2006/10/10 13:56:58 mvaldes Exp $
*
--------------------------------------------------------------------------
*/

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Marshaller;

import hero.interfaces.BnProjectLocal;
import hero.interfaces.BnProjectLocalHome;
import hero.interfaces.BnProjectUtil;
import hero.interfaces.BnProjectValue;
import hero.interfaces.BnNodeValue;
import hero.interfaces.BnNodePropertyValue;
import hero.interfaces.BnProjectPropertyValue;
import hero.interfaces.EngineLocalHome;
import hero.interfaces.ProjectSessionLocal;
import hero.interfaces.ProjectSessionLocalHome;
import hero.interfaces.ProjectSessionUtil;
import hero.historic.ProjectHistoric;

import hero.user.ReadEnv;
import hero.util.HeroException;

import hero.interfaces.Constants;

import java.io.FileWriter;
import java.io.File;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

import java.util.Vector;

public class BonitaTransfer {


  public static boolean TransferFile(String projectName) throws HeroException{
      FileWriter file=null;

        try {
      ReadEnv renv = new ReadEnv();
      String HISTORIC_DIR = renv.getVariable("BONITA_HOME")+File.separator+ "bonita-historic";

    if (!(new File(HISTORIC_DIR)).exists())
      new File(HISTORIC_DIR).mkdir();

    ClassLoader cl = BonitaTransfer.class.getClassLoader();
        Mapping mapping = new Mapping(cl);
          ProjectHistoric ph = getDetails(projectName);
          mapping.loadMapping(cl.getResource("etc/xml/castor/mapping.xml"));

            if (isInstance(projectName))
            {
               if ((new File(HISTORIC_DIR+File.separator+getModel(projectName))).exists())
                   file = new FileWriter(HISTORIC_DIR+File.separator+getModel(projectName)+File.separator+projectName+".xml");
               else
               {
                     new File(HISTORIC_DIR+File.separator+getModel(projectName)).mkdir();
                     file = new FileWriter(HISTORIC_DIR+File.separator+getModel(projectName)+File.separator+projectName+".xml");
               }
            }
            else
              file = new FileWriter(HISTORIC_DIR+File.separator+projectName+".xml");

            Marshaller marshaller = new Marshaller(file);
            marshaller.setMapping(mapping);
            marshaller.setEncoding("UTF-8");
            marshaller.marshal(ph);
            return true;

        } catch (Exception e) {e.printStackTrace();
            System.out.println(e);
            return false;
        }
  }

  private static ProjectHistoric getDetails(String projectName) throws HeroException{
  try {
    BnProjectLocalHome pHome;
    BnProjectLocal mProject;
    try {
      pHome = BnProjectUtil.getLocalHome();
    } catch (javax.naming.NamingException ne) {
      throw new HeroException(ne.getMessage());
    }
    try {
      mProject = pHome.findByName(projectName);
    } catch (FinderException fe) {
      throw new HeroException(fe.getMessage());
    }
    
    ProjectSessionLocalHome projectseshome = ProjectSessionUtil.getLocalHome();
    ProjectSessionLocal project = projectseshome.create();
    ProjectHistoric ph = new ProjectHistoric();
    if (isInstance(projectName))
      project.initModelWithVersion(projectName,mProject.getVersion());
    else
      project.initProject(projectName);
    synchronized(project){
      BnProjectValue pv = project.getDetails();

      ph.setName(pv.getName());
      ph.setCreationDate(pv.getCreationDate().toString());
      if (pv.getEndDate()!=null)
        ph.setEndDate(pv.getEndDate().toString());
      ph.setInitiator(pv.getCreator());
      ph.setNodes(getNodes(pv));
      ph.setProperties(getProjectProperties(pv));
      ph.setVersion(pv.getVersion());
      project.remove();
    }
    return(ph);
   } catch (javax.naming.NamingException ne) {
     throw new HeroException(ne.getMessage());
   } catch (CreateException ce) {
     throw new HeroException(ce.getMessage());
   } catch (RemoveException re) {
     throw new HeroException(re.getMessage());
   }
  }

  private static Vector getNodes(BnProjectValue pv) throws HeroException{
    Vector result = new Vector();
    BnNodeValue[] nodes=pv.getBnNodes();
    int i;
    for (i=0;i<nodes.length;i++)
    {
      BnNodeValue node = nodes[i];
      NodeHistoric nh = new NodeHistoric();
      nh.setName(node.getName());
      nh.setAnticipable(node.getAnticipable());
      if (node.getDescription() != null)
        nh.setDescription(node.getDescription());
      nh.setExecutor(node.getExecutor());
      nh.setRole(node.getBnRole().getName());
      nh.setState(Constants.Nd.nodeStateName[node.getState()]);
      nh.setType(Constants.Nd.nodeTypeName[node.getType()]);
      if (node.getStartDate()!=null)
        nh.setStartDate(node.getStartDate().toString());
      if (node.getEndDate()!=null)
        nh.setEndDate(node.getEndDate().toString());
      nh.setProperties(getNodeProperties(node));
      result.add(nh);
    }
    return result;
  }

  private static Vector getNodeProperties(BnNodeValue nv) throws HeroException{
    Vector result = new Vector();
    BnNodePropertyValue[] props=nv.getBnProperties();
    int i;
    for (i=0;i<props.length;i++)
    {
      BnNodePropertyValue prop = props[i];
      PropertyHistoric ph = new PropertyHistoric();
      ph.setKey(prop.getTheKey());
      ph.setValue(prop.getTheValue());
      result.add(ph);
    }
    return result;
  }

  private static Vector getProjectProperties(BnProjectValue pv) throws HeroException{
    Vector result = new Vector();
    BnProjectPropertyValue[] props=pv.getBnProperties();
    int i;
    for (i=0;i<props.length;i++)
    {
      BnProjectPropertyValue prop = props[i];
      PropertyHistoric ph = new PropertyHistoric();
      ph.setKey(prop.getTheKey());
      ph.setValue(prop.getTheValue());
      result.add(ph);
    }
    return result;
  }

  private static boolean isInstance(String name){
    return (name.matches(".*_instance.*"));
  }

  // Get the name of the project model of this instance
  private static String getModel(String instanceName) {
    int i = instanceName.indexOf("_instance");
    return (instanceName.substring(0, i));
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.