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.api.PMapper; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.api.PClassMapping; 31 import org.objectweb.jorm.api.PMapCluster; 32 import org.objectweb.jorm.api.PBinding; 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 AddressHelper implements MapHelper { 45 public final static String CN = "examples.invoice.persistclass.Address"; 46 private LoggerFactory loggerFactory; 47 private HashMap pobjMap = new HashMap (); 48 49 53 public void map(PMapper m, LoggerFactory lf, byte clact) 54 throws PException { 55 loggerFactory = lf; 56 Logger logger = loggerFactory.getLogger(CN); 57 PClassMapping pcm = m.lookup(CN); 59 if (pcm == null) { 61 PBinder binder = new AddressIdBinder(); 62 String cn = m.cn2mn(CN) + PMapper.PCLASSMAPPINGAPPENDER; 63 try { 64 pcm = (PClassMapping) Class.forName(cn).newInstance(); 65 } catch (Exception e) { 66 throw new PException("Cannot create PClassMapping (probably a ClassLoader problem): " + cn); 67 } 68 pcm.setPBinder(binder); 69 binder.setPClassMapping(pcm); 70 binder.setCacheManager(JormInit.getInstance().getCacheManager()); 71 m.map(pcm); 72 } 73 PMapCluster cl = m.getPMapCluster(CN); 74 switch (clact) { 75 case PClassMapping.CREATE_STRUCTURE_IF_NEEDED: 76 cl.createMappingStructures(false); 77 break; 78 case PClassMapping.CLEANUP_REMOVEALL: 79 cl.deleteMappingStructures(); 80 cl.createMappingStructures(false); 81 break; 82 case PClassMapping.CLEANUP_REMOVEDATA: 83 cl.deleteData(); 84 break; 85 case PClassMapping.CLEANUP_DONOTHING: 86 break; 87 } 88 } 89 90 93 public Address getAddress(PMapper m, PName pn) throws PException { 94 Address res; 95 if (pn == null) { 96 System.out.println("WARNING: PName is null in getAddress within AddressHelper!!"); 97 return null; 98 } 99 PClassMapping pcm = m.lookup(CN); 100 PBinding pb = pcm.getPBinder().lookup(pn); 101 if (pb == null) { 102 Object conn = m.getConnection(); 103 try { 104 pb = pcm.createPBinding(); 105 pb.bind(pn); 106 res = new Address(conn, pb); 107 pobjMap.put(pb, res); 108 m.closeConnection(conn); 109 } catch (PException pe) { 110 m.closeConnection(conn); 111 throw pe; 112 } 113 } else { 114 res = (Address) pobjMap.get(pb); 115 } 116 return res; 117 } 118 119 122 public Address createAddress(PMapper m, String n, String s, String z, 123 String t, String c) throws PException { 124 Address res; 125 PBinding pb = m.lookup(CN).createPBinding(); 126 Object conn = m.getConnection(); 127 try { 128 res = new Address(conn, pb, n, s, z, t, c); 129 m.closeConnection(conn); 130 pobjMap.put(pb, res); 131 } catch (PException pe) { 132 m.closeConnection(conn); 133 throw pe; 134 } 135 return res; 136 } 137 138 141 public boolean deleteAddress(PMapper m, String id) throws PException { 142 PName pn = m.lookup(CN).getPBinder().decodeString(id); 143 Address ad = getAddress(m, pn); 144 if (ad == null) 145 return false; 146 Object conn = m.getConnection(); 147 try { 148 PBinding pb = ad.getPBinding(); 149 ad.delete(conn); 150 m.closeConnection(conn); 151 pobjMap.remove(pb); 152 } catch (PException pe) { 153 m.closeConnection(conn); 154 throw pe; 155 } 156 return true; 157 } 158 159 162 public Iterator listAddress(PMapper m) throws Exception { 163 Iterator res; 164 Object conn = m.getConnection(); 165 try { 166 res = m.lookup(CN).getPNameIterator(conn); 167 m.closeConnection(conn); 168 } catch (PException pe) { 169 m.closeConnection(conn); 170 throw pe; 171 } 172 return res; 173 } 174 } 175 | Popular Tags |