001: package hero.historic;
002:
003: /**
004: *
005: * Bonita
006: * Copyright (C) 1999 Bull S.A.
007: * Bull 68 route de versailles 78434 Louveciennes Cedex France
008: * Further information: bonita@objectweb.org
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023: * USA
024: *
025: *
026: --------------------------------------------------------------------------
027: * $Id: BonitaTransfer.java,v 1.9 2006/10/10 13:56:58 mvaldes Exp $
028: *
029: --------------------------------------------------------------------------
030: */
031:
032: import org.exolab.castor.mapping.Mapping;
033: import org.exolab.castor.xml.Marshaller;
034:
035: import hero.interfaces.BnProjectLocal;
036: import hero.interfaces.BnProjectLocalHome;
037: import hero.interfaces.BnProjectUtil;
038: import hero.interfaces.BnProjectValue;
039: import hero.interfaces.BnNodeValue;
040: import hero.interfaces.BnNodePropertyValue;
041: import hero.interfaces.BnProjectPropertyValue;
042: import hero.interfaces.EngineLocalHome;
043: import hero.interfaces.ProjectSessionLocal;
044: import hero.interfaces.ProjectSessionLocalHome;
045: import hero.interfaces.ProjectSessionUtil;
046: import hero.historic.ProjectHistoric;
047:
048: import hero.user.ReadEnv;
049: import hero.util.HeroException;
050:
051: import hero.interfaces.Constants;
052:
053: import java.io.FileWriter;
054: import java.io.File;
055: import javax.ejb.CreateException;
056: import javax.ejb.FinderException;
057: import javax.ejb.RemoveException;
058:
059: import java.util.Vector;
060:
061: public class BonitaTransfer {
062:
063: public static boolean TransferFile(String projectName)
064: throws HeroException {
065: FileWriter file = null;
066:
067: try {
068: ReadEnv renv = new ReadEnv();
069: String HISTORIC_DIR = renv.getVariable("BONITA_HOME")
070: + File.separator + "bonita-historic";
071:
072: if (!(new File(HISTORIC_DIR)).exists())
073: new File(HISTORIC_DIR).mkdir();
074:
075: ClassLoader cl = BonitaTransfer.class.getClassLoader();
076: Mapping mapping = new Mapping(cl);
077: ProjectHistoric ph = getDetails(projectName);
078: mapping.loadMapping(cl
079: .getResource("etc/xml/castor/mapping.xml"));
080:
081: if (isInstance(projectName)) {
082: if ((new File(HISTORIC_DIR + File.separator
083: + getModel(projectName))).exists())
084: file = new FileWriter(HISTORIC_DIR + File.separator
085: + getModel(projectName) + File.separator
086: + projectName + ".xml");
087: else {
088: new File(HISTORIC_DIR + File.separator
089: + getModel(projectName)).mkdir();
090: file = new FileWriter(HISTORIC_DIR + File.separator
091: + getModel(projectName) + File.separator
092: + projectName + ".xml");
093: }
094: } else
095: file = new FileWriter(HISTORIC_DIR + File.separator
096: + projectName + ".xml");
097:
098: Marshaller marshaller = new Marshaller(file);
099: marshaller.setMapping(mapping);
100: marshaller.setEncoding("UTF-8");
101: marshaller.marshal(ph);
102: return true;
103:
104: } catch (Exception e) {
105: e.printStackTrace();
106: System.out.println(e);
107: return false;
108: }
109: }
110:
111: private static ProjectHistoric getDetails(String projectName)
112: throws HeroException {
113: try {
114: BnProjectLocalHome pHome;
115: BnProjectLocal mProject;
116: try {
117: pHome = BnProjectUtil.getLocalHome();
118: } catch (javax.naming.NamingException ne) {
119: throw new HeroException(ne.getMessage());
120: }
121: try {
122: mProject = pHome.findByName(projectName);
123: } catch (FinderException fe) {
124: throw new HeroException(fe.getMessage());
125: }
126:
127: ProjectSessionLocalHome projectseshome = ProjectSessionUtil
128: .getLocalHome();
129: ProjectSessionLocal project = projectseshome.create();
130: ProjectHistoric ph = new ProjectHistoric();
131: if (isInstance(projectName))
132: project.initModelWithVersion(projectName, mProject
133: .getVersion());
134: else
135: project.initProject(projectName);
136: synchronized (project) {
137: BnProjectValue pv = project.getDetails();
138:
139: ph.setName(pv.getName());
140: ph.setCreationDate(pv.getCreationDate().toString());
141: if (pv.getEndDate() != null)
142: ph.setEndDate(pv.getEndDate().toString());
143: ph.setInitiator(pv.getCreator());
144: ph.setNodes(getNodes(pv));
145: ph.setProperties(getProjectProperties(pv));
146: ph.setVersion(pv.getVersion());
147: project.remove();
148: }
149: return (ph);
150: } catch (javax.naming.NamingException ne) {
151: throw new HeroException(ne.getMessage());
152: } catch (CreateException ce) {
153: throw new HeroException(ce.getMessage());
154: } catch (RemoveException re) {
155: throw new HeroException(re.getMessage());
156: }
157: }
158:
159: private static Vector getNodes(BnProjectValue pv)
160: throws HeroException {
161: Vector result = new Vector();
162: BnNodeValue[] nodes = pv.getBnNodes();
163: int i;
164: for (i = 0; i < nodes.length; i++) {
165: BnNodeValue node = nodes[i];
166: NodeHistoric nh = new NodeHistoric();
167: nh.setName(node.getName());
168: nh.setAnticipable(node.getAnticipable());
169: if (node.getDescription() != null)
170: nh.setDescription(node.getDescription());
171: nh.setExecutor(node.getExecutor());
172: nh.setRole(node.getBnRole().getName());
173: nh.setState(Constants.Nd.nodeStateName[node.getState()]);
174: nh.setType(Constants.Nd.nodeTypeName[node.getType()]);
175: if (node.getStartDate() != null)
176: nh.setStartDate(node.getStartDate().toString());
177: if (node.getEndDate() != null)
178: nh.setEndDate(node.getEndDate().toString());
179: nh.setProperties(getNodeProperties(node));
180: result.add(nh);
181: }
182: return result;
183: }
184:
185: private static Vector getNodeProperties(BnNodeValue nv)
186: throws HeroException {
187: Vector result = new Vector();
188: BnNodePropertyValue[] props = nv.getBnProperties();
189: int i;
190: for (i = 0; i < props.length; i++) {
191: BnNodePropertyValue prop = props[i];
192: PropertyHistoric ph = new PropertyHistoric();
193: ph.setKey(prop.getTheKey());
194: ph.setValue(prop.getTheValue());
195: result.add(ph);
196: }
197: return result;
198: }
199:
200: private static Vector getProjectProperties(BnProjectValue pv)
201: throws HeroException {
202: Vector result = new Vector();
203: BnProjectPropertyValue[] props = pv.getBnProperties();
204: int i;
205: for (i = 0; i < props.length; i++) {
206: BnProjectPropertyValue prop = props[i];
207: PropertyHistoric ph = new PropertyHistoric();
208: ph.setKey(prop.getTheKey());
209: ph.setValue(prop.getTheValue());
210: result.add(ph);
211: }
212: return result;
213: }
214:
215: private static boolean isInstance(String name) {
216: return (name.matches(".*_instance.*"));
217: }
218:
219: // Get the name of the project model of this instance
220: private static String getModel(String instanceName) {
221: int i = instanceName.indexOf("_instance");
222: return (instanceName.substring(0, i));
223: }
224:
225: }
|