1 23 24 package examples.invoice.persistclass; 25 26 import org.objectweb.jorm.naming.api.PBinder; 27 import org.objectweb.jorm.naming.api.PName; 28 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrRegistry; 29 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgr; 30 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrImpl; 31 import org.objectweb.jorm.api.PMapper; 32 import org.objectweb.jorm.api.PException; 33 import org.objectweb.jorm.api.PClassMapping; 34 import org.objectweb.jorm.api.PMapCluster; 35 import org.objectweb.jorm.api.PBinding; 36 import org.objectweb.util.monolog.api.Logger; 37 import org.objectweb.util.monolog.api.LoggerFactory; 38 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 42 import examples.invoice.applications.JormInit; 43 44 47 public class ProductHelper implements MapHelper { 48 public final static String CN = "examples.invoice.persistclass.Product"; 49 private LoggerFactory loggerFactory; 50 private HashMap pobjMap = new HashMap (); 51 52 56 public void map(PMapper m, LoggerFactory lf, byte clact) 57 throws PException { 58 loggerFactory = lf; 59 Logger logger = loggerFactory.getLogger(CN); 60 PClassMapping pcm = m.lookup(CN); 62 if (pcm == null) { 64 PolymorphIdMgr pim = PolymorphIdMgrRegistry.getPolymorphIdMgr(m); 65 if (pim == null) { 66 pim = new PolymorphIdMgrImpl(); 67 pim.init(m, clact); 68 PolymorphIdMgrRegistry.registerPolymorphIdMgr(pim); 69 } 70 PBinder binder = pim.getPBinder(CN); 71 try { 72 pcm = (PClassMapping) Class.forName(m.cn2mn(CN) + PMapper.PCLASSMAPPINGAPPENDER).newInstance(); 73 } catch (Exception e) { 74 throw new PException("Cannot create PClassMapping (probably a ClassLoader problem)."); 75 } 76 pcm.setPBinder(binder); 77 binder.setPClassMapping(pcm); 78 binder.setCacheManager(JormInit.getInstance().getCacheManager()); 79 m.map(pcm); 80 } 81 PMapCluster cl = m.getPMapCluster(CN); 82 switch (clact) { 83 case PClassMapping.CREATE_STRUCTURE_IF_NEEDED: 84 cl.createMappingStructures(false); 85 break; 86 case PClassMapping.CLEANUP_REMOVEALL: 87 cl.deleteMappingStructures(); 88 cl.createMappingStructures(false); 89 break; 90 case PClassMapping.CLEANUP_REMOVEDATA: 91 cl.deleteData(); 92 break; 93 case PClassMapping.CLEANUP_DONOTHING: 94 break; 95 } 96 } 97 98 101 public Product getProduct(PMapper m, PName pn) throws PException { 102 Product res; 103 if (pn == null) 104 return null; 105 PClassMapping pcm = m.lookup(CN); 106 PBinding pb = pcm.getPBinder().lookup(pn); 107 if (pb == null) { 108 Object conn = m.getConnection(); 109 try { 110 pb = pcm.createPBinding(); 111 pb.bind(pn); 112 res = new Product(conn, pb); 113 pobjMap.put(pb, res); 114 m.closeConnection(conn); 115 } catch (PException pe) { 116 m.closeConnection(conn); 117 throw pe; 118 } 119 } else { 120 res = (Product) pobjMap.get(pb); 121 } 122 return res; 123 } 124 125 128 public Product createProduct(PMapper m, String n, char s, String c, float w, float p) throws PException { 129 Product res; 130 PBinding pb = m.lookup(CN).createPBinding(); 131 Object conn = m.getConnection(); 132 try { 133 res = new Product(conn, pb, n, s, c, w, p); 134 m.closeConnection(conn); 135 pobjMap.put(pb, res); 136 } catch (PException pe) { 137 pe.getNestedException().printStackTrace(); 138 m.closeConnection(conn); 139 throw pe; 140 } 141 return res; 142 } 143 144 147 public boolean deleteProduct(PMapper m, String id) throws PException { 148 PName pn = m.lookup(CN).getPBinder().decodeString(id); 149 Product p = getProduct(m, pn); 150 if (p == null) 151 return false; 152 Object conn = m.getConnection(); 153 try { 154 PBinding pb = p.getPBinding(); 155 p.delete(conn); 156 m.closeConnection(conn); 157 pobjMap.remove(pb); 158 } catch (PException pe) { 159 m.closeConnection(conn); 160 throw pe; 161 } 162 return true; 163 } 164 165 168 public Iterator listProducts(PMapper m) throws PException { 169 Iterator res; 170 Object conn = m.getConnection(); 171 try { 172 res = m.lookup(CN).getPNameIterator(conn); 173 m.closeConnection(conn); 174 } catch (PException pe) { 175 m.closeConnection(conn); 176 throw pe; 177 } 178 return res; 179 } 180 } 181 | Popular Tags |