1 23 24 package examples.invoice.persistclass; 25 26 import org.objectweb.jorm.api.*; 27 import org.objectweb.jorm.facility.naming.basidir.BasidBinder; 28 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgr; 29 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrRegistry; 30 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrImpl; 31 import org.objectweb.jorm.naming.api.PBinder; 32 import org.objectweb.jorm.naming.api.PName; 33 import org.objectweb.util.monolog.api.Logger; 34 import org.objectweb.util.monolog.api.LoggerFactory; 35 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 import examples.invoice.applications.JormInit; 40 41 44 public class ProdUnitsHelper implements MapHelper { 45 public final static String CN = "examples.invoice.persistclass.ProdUnits"; 46 private LoggerFactory loggerFactory; 47 private HashMap pobjMap = new HashMap (); 48 private ProductHelper productHelper; 49 50 public ProdUnitsHelper(ProductHelper ph) { 51 productHelper = ph; 52 } 53 54 58 public void map(PMapper m, LoggerFactory lf, byte clact) 59 throws PException { 60 productHelper.map(m, lf, clact); 61 loggerFactory = lf; 62 Logger logger = loggerFactory.getLogger(CN); 63 PClassMapping pcm = m.lookup(CN); 65 if (pcm == null) { 67 PBinder binder = new BasidBinder(m, CN, clact); 68 try { 69 pcm = (PClassMapping) Class.forName(m.cn2mn(CN) + PMapper.PCLASSMAPPINGAPPENDER).newInstance(); 70 } catch (Exception e) { 71 throw new PException("Cannot create PClassMapping (probably a ClassLoader problem)."); 72 } 73 pcm.setPBinder(binder); 74 binder.setPClassMapping(pcm); 75 binder.setCacheManager(JormInit.getInstance().getCacheManager()); 76 PolymorphIdMgr pim = PolymorphIdMgrRegistry.getPolymorphIdMgr(m); 77 if (pim == null) { 78 pim = new PolymorphIdMgrImpl(); 79 pim.init(m, PClassMapping.CREATE_STRUCTURE_IF_NEEDED); 80 PolymorphIdMgrRegistry.registerPolymorphIdMgr(pim); 81 } 82 ((PClassMappingCtrl) pcm).setPNameCoder("product", 83 pim.getRefNC(ProductHelper.CN)); 84 m.map(pcm); 85 } 86 PMapCluster cl = m.getPMapCluster(CN); 87 switch (clact) { 88 case PClassMapping.CREATE_STRUCTURE_IF_NEEDED: 89 cl.createMappingStructures(false); 90 break; 91 case PClassMapping.CLEANUP_REMOVEALL: 92 cl.deleteMappingStructures(); 93 cl.createMappingStructures(false); 94 break; 95 case PClassMapping.CLEANUP_REMOVEDATA: 96 cl.deleteData(); 97 break; 98 case PClassMapping.CLEANUP_DONOTHING: 99 break; 100 } 101 } 102 103 public ProductHelper getProductHelper() { 104 if (productHelper == null) 105 System.out.println("WARNING: productHelper is NULL in invoiceHelper!!"); 106 return productHelper; 107 } 108 109 112 public ProdUnits getProdUnits(PMapper m, PName pn) throws Exception { 113 ProdUnits res; 114 if (pn == null) { 115 System.out.println("WARNING: PName is null in getProdUnits within ProdUnitsHelper!!"); 116 return null; 117 } 118 PClassMapping pcm = m.lookup(CN); 119 PBinding pb = pcm.getPBinder().lookup(pn); 120 if (pb == null) { 121 Object conn = m.getConnection(); 122 try { 123 pb = pcm.createPBinding(); 124 pb.bind(pn); 125 res = new ProdUnits(this, conn, pb); 126 m.closeConnection(conn); 127 pobjMap.put(pb, res); 128 } catch (PException pe) { 129 m.closeConnection(conn); 130 throw pe; 131 } 132 } else { 133 res = (ProdUnits) pobjMap.get(pb); 134 } 135 return res; 136 } 137 138 141 public ProdUnits createProdUnits(PMapper m, String pr, int quant) throws PException { 142 ProdUnits res; 143 PBinding pb = m.lookup(CN).createPBinding(); 144 Object conn = m.getConnection(); 145 try { 146 PName pn = m.lookup(ProductHelper.CN).getPBinder().decodeString(pr); 147 res = new ProdUnits(this, conn, pb, pn, quant); 148 m.closeConnection(conn); 149 pobjMap.put(pb, res); 150 } catch (PException pe) { 151 m.closeConnection(conn); 152 throw pe; 153 } 154 return res; 155 } 156 157 160 public boolean deleteProdUnits(PMapper m, long id) throws Exception { 161 PName pn = m.lookup(CN).getPBinder().decodeLong(id); 162 ProdUnits ad = getProdUnits(m, pn); 163 if (ad == null) 164 return false; 165 Object conn = m.getConnection(); 166 try { 167 PBinding pb = ad.getPBinding(); 168 ad.delete(conn); 169 m.closeConnection(conn); 170 pobjMap.remove(pb); 171 } catch (PException pe) { 172 m.closeConnection(conn); 173 throw pe; 174 } 175 return true; 176 } 177 178 181 public Iterator listProdUnits(PMapper m) throws Exception { 182 Iterator res; 183 Object conn = m.getConnection(); 184 try { 185 res = m.lookup(CN).getPNameIterator(conn); 186 m.closeConnection(conn); 187 } catch (PException pe) { 188 m.closeConnection(conn); 189 throw pe; 190 } 191 return res; 192 } 193 } 194 | Popular Tags |