1 16 package com.buchuki.ensmer; 17 18 import java.io.*; 19 import java.util.*; 20 import javax.swing.event.ChangeEvent ; 21 import javax.swing.event.ChangeListener ; 22 import com.buchuki.ensmer.object.*; 23 import com.buchuki.ensmer.prevayler.queries.*; 24 import groovy.lang.*; 25 import java.lang.reflect.Constructor ; 26 import org.prevayler.*; 27 28 38 public class Backhoe { 39 40 45 public Backhoe() { 46 File frontends = EnsmerManager.instance(). 47 getConfigManager().getSettingsDirectory("frontends"); 48 PackagePaths pathFinder = new PackagePaths(frontends, ".groovy"); 49 try { 50 classMap = pathFinder.getValidClassFileMap(Class.forName("com.buchuki.ensmer.object.Backend")); 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 } 55 56 63 public Frontend loadFrontend(Backend backend) throws Exception { 64 GroovyClassLoader gcl = new GroovyClassLoader(); 65 File file = classMap.get(backend.getClass()); 66 Class feClass = gcl.parseClass(file); 67 Frontend frontend = (Frontend) feClass.newInstance(); 68 frontend.setBackend(backend); 69 backend.addChangeListener(frontend); 70 backend.addChangeListener(new StoreChangesListener()); 71 return frontend; 72 } 73 74 86 public Backend getBackend(Long id) throws Exception { 87 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 88 Class backendClass = (Class ) prevayler.execute(new GetObjectBackendClassQuery(id)); 89 Constructor constructor = backendClass.getConstructor(Class.forName("java.io.Serializable")); 90 Serializable sel = (Serializable) prevayler.execute(new GetObjectDataQuery(id)); 91 Backend backend = (Backend) constructor.newInstance(sel); 92 backend.setId(id); 93 return backend; 94 } 95 96 108 public Backend findBackend(Long id) { 109 AreaManager areaMan = EnsmerManager.instance().getAreaManager(); 110 Long areaID = areaMan.getAreaIDForObject(id); 111 if (areaID == null) { 112 return null; 113 } 114 Area area = areaMan.getArea(areaID); 115 return area.getFrontend(id).getBackend(); 116 } 117 118 122 public Set<Class <? extends Backend>> getBackendClasses() { 123 return classMap.keySet(); 124 } 125 126 130 private Map<Class <? extends Backend>, File> classMap; 131 } 132 133 139 class StoreChangesListener implements ChangeListener { 140 146 public void stateChanged(ChangeEvent event) { 147 Backend backend = (Backend) event.getSource(); 148 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 149 try { 150 prevayler.execute(new SetObjectDataTransaction(backend.getId(), 151 backend.getSerializable())); 152 } catch (Exception e) { 153 e.printStackTrace(); 154 } 155 } 156 } 157 158 | Popular Tags |