1 23 24 package examples.invoice.persistclass; 25 26 import org.objectweb.jorm.facility.naming.basidir.BasidBinder; 27 import org.objectweb.jorm.naming.api.PBinder; 28 import org.objectweb.jorm.naming.api.PName; 29 import org.objectweb.jorm.naming.api.PNamingContext; 30 import org.objectweb.jorm.util.api.Loggable; 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.PClassMappingCtrl; 35 import org.objectweb.jorm.api.PMapCluster; 36 import org.objectweb.jorm.api.PBinding; 37 import org.objectweb.util.monolog.api.Logger; 38 import org.objectweb.util.monolog.api.LoggerFactory; 39 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 43 import examples.invoice.applications.JormInit; 44 45 48 public class InvoiceHelper implements MapHelper { 49 public final static String CN = "examples.invoice.persistclass.Invoice"; 50 private LoggerFactory loggerFactory; 51 private HashMap pobjMap = new HashMap (); 52 private AddressHelper addressHelper; 53 private ProductHelper productHelper; 54 private ProdUnitsHelper prodUnitsHelper; 55 56 public InvoiceHelper(AddressHelper ah, ProductHelper ph, ProdUnitsHelper puh) { 57 addressHelper = ah; 58 productHelper = ph; 59 prodUnitsHelper = puh; 60 } 61 62 66 public void map(PMapper m, LoggerFactory lf, byte clact) 67 throws PException { 68 addressHelper.map(m, lf, clact); 69 productHelper.map(m, lf, clact); 70 prodUnitsHelper.map(m, lf, clact); 71 loggerFactory = lf; 72 Logger logger = loggerFactory.getLogger(CN); 73 PClassMapping pcm = m.lookup(CN); 74 if (pcm == null) { 76 PBinder sbinder = new BasidBinder(PNamingContext.CTINT); 78 PClassMapping sm; 79 sm = m.createGenClassMapping(); 80 ((Loggable) sm).setLoggerFactory(loggerFactory); 81 sm.setPBinder(sbinder); 82 sbinder.setPClassMapping(sm); 83 sbinder.setCacheManager(JormInit.getInstance().getCacheManager()); 84 ((PClassMappingCtrl) sm).setPNameCoder(m.lookup(prodUnitsHelper.CN).getPBinder()); 85 PBinder binder = new BasidBinder(PNamingContext.CTINT); 87 try { 88 pcm = (PClassMapping) Class.forName(m.cn2mn(CN) + 89 PMapper.PCLASSMAPPINGAPPENDER) 90 .newInstance(); 91 } catch (Exception e) { 92 throw new PException("Cannot create PClassMapping (probably a ClassLoader problem)."); 93 } 94 pcm.setPBinder(binder); 95 binder.setPClassMapping(pcm); 96 binder.setCacheManager(JormInit.getInstance().getCacheManager()); 97 ((PClassMappingCtrl) pcm).setPNameCoder("address", m.lookup(addressHelper.CN).getPBinder()); 98 ((PClassMappingCtrl) pcm).setGenClassMapping("puset", sm); 99 ((PClassMappingCtrl) pcm).setPNameCoder("puset", sm.getPBinder()); 100 m.map(pcm); 101 PMapCluster cl = m.getPMapCluster(CN); 102 switch (clact) { 103 case PClassMapping.CREATE_STRUCTURE_IF_NEEDED: 104 cl.createMappingStructures(false); 105 break; 106 case PClassMapping.CLEANUP_REMOVEALL: 107 cl.deleteMappingStructures(); 108 cl.createMappingStructures(false); 109 break; 110 case PClassMapping.CLEANUP_REMOVEDATA: 111 cl.deleteData(); 112 break; 113 case PClassMapping.CLEANUP_DONOTHING: 114 break; 115 } 116 } 117 } 118 119 public AddressHelper getAddressHelper() { 120 if (addressHelper == null) 121 System.out.println("WARNING: addressHelper is NULL in invoiceHelper!!"); 122 return addressHelper; 123 } 124 125 public ProdUnitsHelper getProdUnitsHelper() { 126 if (prodUnitsHelper == null) 127 System.out.println("WARNING: prodUnitsHelper is NULL in invoiceHelper!!"); 128 return prodUnitsHelper; 129 } 130 131 134 public Invoice getInvoice(PMapper m, PName pn) throws PException { 135 Invoice res; 136 if (pn == null) 137 return null; 138 PClassMapping pcm = m.lookup(CN); 139 PBinding pb = pcm.getPBinder().lookup(pn); 140 if (pb == null) { 141 Object conn = m.getConnection(); 142 try { 143 pb = pcm.createPBinding(); 144 pb.bind(pn); 145 res = new Invoice(this, conn, pb); 146 pobjMap.put(pb, res); 147 m.closeConnection(conn); 148 } catch (PException pe) { 149 m.closeConnection(conn); 150 throw pe; 151 } 152 } else { 153 res = (Invoice) pobjMap.get(pb); 154 } 155 return res; 156 } 157 158 161 public Invoice createInvoice(PMapper m, int n, String ar, String [] purs) 162 throws PException { 163 Invoice res; 164 PBinding pb = m.lookup(CN).createPBinding(); 165 Object conn = m.getConnection(); 166 try { 167 PName pn = m.lookup(CN).getPNameCoder("address").decodeString(ar); 168 PName[] pus = new PName[purs.length]; 169 for (int i = 0; i < purs.length; i++) 170 pus[i] = m.lookup(CN).getGenClassMapping("puset").getPNameCoder().decodeString(purs[i]); 171 res = new Invoice(this, conn, pb, n, pn, pus); 172 m.closeConnection(conn); 173 pobjMap.put(pb, res); 174 } catch (PException pe) { 175 m.closeConnection(conn); 176 throw pe; 177 } 178 return res; 179 } 180 181 184 public boolean deleteInvoice(PMapper m, String id) throws Exception { 185 PName pn = m.lookup(CN).getPBinder().decodeString(id); 186 Invoice p = getInvoice(m, pn); 187 if (p == null) 188 return false; 189 Object conn = m.getConnection(); 190 try { 191 PBinding pb = p.getPBinding(); 192 p.delete(conn); 193 m.closeConnection(conn); 194 pobjMap.remove(pb); 195 } catch (PException pe) { 196 m.closeConnection(conn); 197 throw pe; 198 } 199 return true; 200 } 201 202 205 public Iterator listInvoices(PMapper m) throws Exception { 206 Iterator res; 207 Object conn = m.getConnection(); 208 try { 209 res = m.lookup(CN).getPNameIterator(conn); 210 m.closeConnection(conn); 211 } catch (PException pe) { 212 m.closeConnection(conn); 213 throw pe; 214 } 215 return res; 216 } 217 218 226 private String upperFL(String word) { 227 if (word != null && word.length() > 0) 228 return word.substring(0, 1).toUpperCase() + word.substring(1); 229 else 230 return word; 231 } 232 } 233 | Popular Tags |