1 package hero.historic; 2 3 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 ; 50 import java.io.File ; 51 import javax.ejb.CreateException ; 52 import java.util.Vector ; 53 54 public class BonitaTransfer { 55 56 57 public static boolean TransferFile(String projectName) throws HeroException{ 58 FileWriter file=null; 59 60 try { 61 ReadEnv renv = new ReadEnv(); 62 String HISTORIC_DIR = renv.getVariable("BONITA_HOME")+File.separator+ "bonita-historic"; 63 64 if (!(new File (HISTORIC_DIR)).exists()) 65 new File (HISTORIC_DIR).mkdir(); 66 67 ClassLoader 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 (HISTORIC_DIR+File.separator+getModel(projectName))).exists()) 75 file = new FileWriter (HISTORIC_DIR+File.separator+getModel(projectName)+File.separator+projectName+".xml"); 76 else 77 { 78 new File (HISTORIC_DIR+File.separator+getModel(projectName)).mkdir(); 79 file = new FileWriter (HISTORIC_DIR+File.separator+getModel(projectName)+File.separator+projectName+".xml"); 80 } 81 } 82 else 83 file = new FileWriter (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 e) {e.printStackTrace(); 91 System.out.println(e); 92 return false; 93 } 94 95 } 96 97 private static ProjectHistoric getDetails(String 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 ne) { 117 throw new HeroException(ne.getMessage()); 118 } catch (CreateException ce) { 119 throw new HeroException(ce.getMessage()); 120 } 121 } 122 123 private static Vector getNodes(BnProjectValue pv) throws HeroException{ 124 Vector result = new Vector (); 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 getNodeProperties(BnNodeValue nv) throws HeroException{ 148 Vector result = new Vector (); 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 getProjectProperties(BnProjectValue pv) throws HeroException{ 163 Vector result = new Vector (); 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 name){ 178 return (name.matches(".*_instance.*")); 179 } 180 181 private static String getModel(String instanceName) { 183 int i = instanceName.indexOf("_instance"); 184 return (instanceName.substring(0, i)); 185 } 186 187 } | Popular Tags |