KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > historic > BonitaTransfer


1 package hero.historic;
2
3 /**
4 *
5 * Bonita
6 * Copyright (C) 1999 Bull S.A.
7 * Bull 68 route de versailles 78434 Louveciennes Cedex France
8 * Further information: bonita@objectweb.org
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 *
26 --------------------------------------------------------------------------
27 * $Id: BonitaTransfer.java,v 1.3 2005/03/18 14:53:18 mvaldes Exp $
28 *
29 --------------------------------------------------------------------------
30 */

31
32 import org.exolab.castor.mapping.Mapping;
33 import org.exolab.castor.xml.Marshaller;
34
35 import hero.interfaces.BnProjectValue;
36 import hero.interfaces.BnNodeValue;
37 import hero.interfaces.BnNodePropertyValue;
38 import hero.interfaces.BnProjectPropertyValue;
39 import hero.interfaces.ProjectSessionLocal;
40 import hero.interfaces.ProjectSessionLocalHome;
41 import hero.interfaces.ProjectSessionUtil;
42 import hero.historic.ProjectHistoric;
43
44 import hero.user.ReadEnv;
45 import hero.util.HeroException;
46
47 import hero.interfaces.Constants;
48
49 import java.io.FileWriter JavaDoc;
50 import java.io.File JavaDoc;
51 import javax.ejb.CreateException JavaDoc;
52 import java.util.Vector JavaDoc;
53
54 public class BonitaTransfer {
55             
56     
57     public static boolean TransferFile(String JavaDoc projectName) throws HeroException{
58         FileWriter JavaDoc file=null;
59         
60         try {
61         ReadEnv renv = new ReadEnv();
62         String JavaDoc HISTORIC_DIR = renv.getVariable("BONITA_HOME")+File.separator+ "bonita-historic";
63         
64         if (!(new File JavaDoc(HISTORIC_DIR)).exists())
65             new File JavaDoc(HISTORIC_DIR).mkdir();
66         
67         ClassLoader JavaDoc cl = BonitaTransfer.class.getClassLoader();
68         Mapping mapping = new Mapping(cl);
69             ProjectHistoric ph = getDetails(projectName);
70             mapping.loadMapping(cl.getResource("etc/xml/castor/mapping.xml"));
71
72             if (isInstance(projectName))
73             {
74                if ((new File JavaDoc(HISTORIC_DIR+File.separator+getModel(projectName))).exists())
75                     file = new FileWriter JavaDoc(HISTORIC_DIR+File.separator+getModel(projectName)+File.separator+projectName+".xml");
76                else
77                {
78                     new File JavaDoc(HISTORIC_DIR+File.separator+getModel(projectName)).mkdir();
79                     file = new FileWriter JavaDoc(HISTORIC_DIR+File.separator+getModel(projectName)+File.separator+projectName+".xml");
80                }
81             }
82             else
83                 file = new FileWriter JavaDoc(HISTORIC_DIR+File.separator+projectName+".xml");
84             
85             Marshaller marshaller = new Marshaller(file);
86             marshaller.setMapping(mapping);
87             marshaller.marshal(ph);
88             return true;
89
90         } catch (Exception JavaDoc e) {e.printStackTrace();
91             System.out.println(e);
92             return false;
93         }
94
95     }
96     
97     private static ProjectHistoric getDetails(String JavaDoc projectName) throws HeroException{
98     try {
99         ProjectSessionLocalHome projectseshome = ProjectSessionUtil.getLocalHome();
100         ProjectSessionLocal project = projectseshome.create();
101         if (isInstance(projectName))
102             project.initModel(projectName);
103         else
104             project.initProject(projectName);
105         
106         BnProjectValue pv = project.getDetails();
107          
108         ProjectHistoric ph = new ProjectHistoric();
109         ph.setName(pv.getName());
110         ph.setCreationDate(pv.getCreationDate().toString());
111         ph.setEndDate(pv.getEndDate().toString());
112         ph.setInitiator(pv.getCreator());
113         ph.setNodes(getNodes(pv));
114         ph.setProperties(getProjectProperties(pv));
115         return(ph);
116      } catch (javax.naming.NamingException JavaDoc ne) {
117          throw new HeroException(ne.getMessage());
118      } catch (CreateException JavaDoc ce) {
119          throw new HeroException(ce.getMessage());
120      }
121     }
122     
123     private static Vector JavaDoc getNodes(BnProjectValue pv) throws HeroException{
124         Vector JavaDoc result = new Vector JavaDoc();
125         BnNodeValue[] nodes=pv.getBnNodes();
126         int i;
127         for (i=0;i<nodes.length;i++)
128         {
129             BnNodeValue node = nodes[i];
130             NodeHistoric nh = new NodeHistoric();
131             nh.setName(node.getName());
132             nh.setAnticipable(node.getAnticipable());
133             if (node.getDescription() != null)
134                 nh.setDescription(node.getDescription());
135             nh.setExecutor(node.getExecutor());
136             nh.setRole(node.getBnRole().getName());
137             nh.setState(Constants.Nd.nodeStateName[node.getState()]);
138             nh.setType(Constants.Nd.nodeTypeName[node.getType()]);
139             nh.setStartDate(node.getStartDate().toString());
140             nh.setEndDate(node.getEndDate().toString());
141             nh.setProperties(getNodeProperties(node));
142             result.add(nh);
143         }
144         return result;
145     }
146     
147     private static Vector JavaDoc getNodeProperties(BnNodeValue nv) throws HeroException{
148         Vector JavaDoc result = new Vector JavaDoc();
149         BnNodePropertyValue[] props=nv.getBnProperties();
150         int i;
151         for (i=0;i<props.length;i++)
152         {
153             BnNodePropertyValue prop = props[i];
154             PropertyHistoric ph = new PropertyHistoric();
155             ph.setKey(prop.getTheKey());
156             ph.setValue(prop.getTheValue());
157             result.add(ph);
158         }
159         return result;
160     }
161     
162     private static Vector JavaDoc getProjectProperties(BnProjectValue pv) throws HeroException{
163         Vector JavaDoc result = new Vector JavaDoc();
164         BnProjectPropertyValue[] props=pv.getBnProperties();
165         int i;
166         for (i=0;i<props.length;i++)
167         {
168             BnProjectPropertyValue prop = props[i];
169             PropertyHistoric ph = new PropertyHistoric();
170             ph.setKey(prop.getTheKey());
171             ph.setValue(prop.getTheValue());
172             result.add(ph);
173         }
174         return result;
175     }
176     
177     private static boolean isInstance(String JavaDoc name){
178         return (name.matches(".*_instance.*"));
179     }
180     
181     // Get the name of the project model of this instance
182
private static String JavaDoc getModel(String JavaDoc instanceName) {
183         int i = instanceName.indexOf("_instance");
184         return (instanceName.substring(0, i));
185     }
186     
187 }
Popular Tags