1 23 24 package org.objectweb.jorm.mapper.fos.genclass; 25 26 import org.objectweb.jorm.api.PAccessor; 27 import org.objectweb.jorm.api.PBinding; 28 import org.objectweb.jorm.api.PBindingCtrl; 29 import org.objectweb.jorm.api.PClassMapping; 30 import org.objectweb.jorm.api.PException; 31 import org.objectweb.jorm.api.PExceptionIO; 32 import org.objectweb.jorm.api.PExceptionProtocol; 33 import org.objectweb.jorm.api.PGenClassAccessor; 34 import org.objectweb.jorm.api.PIndexedElem; 35 import org.objectweb.jorm.api.PMappingCallback; 36 import org.objectweb.jorm.api.PMappingStructuresManager; 37 import org.objectweb.jorm.api.PNameIterator; 38 import org.objectweb.jorm.api.PStateGraph; 39 import org.objectweb.jorm.genclass.api.FieldDesc; 40 import org.objectweb.jorm.genclass.lib.GenClassMapping; 41 import org.objectweb.jorm.lib.PBindingImpl; 42 import org.objectweb.jorm.metainfo.api.MetaObject; 43 import org.objectweb.jorm.mapper.fos.lib.FosPMappingStructuresManager; 44 import org.objectweb.jorm.naming.api.PName; 45 import org.objectweb.jorm.type.api.PType; 46 import org.objectweb.medor.tuple.api.TupleCollection; 47 import org.objectweb.perseus.connector.ra.fos.FosConnection; 48 import org.objectweb.perseus.fos.api.FosAccess; 49 import org.objectweb.perseus.fos.api.FosException; 50 import org.objectweb.perseus.fos.api.FosStructure; 51 52 import java.io.File ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutputStream ; 55 import java.io.Serializable ; 56 import java.math.BigDecimal ; 57 import java.util.Date ; 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 61 public class FosGenClassMapping 62 extends GenClassMapping 63 implements FosGenClassProp, FosStructure { 64 protected String className = null; 65 protected String fieldName = null; 66 protected String genClassLevel = null; 67 protected String dirName = null; 68 69 private void initDirName() { 70 if (dirName == null) { 71 dirName = className + File.separator + fieldName + File.separator 72 + genClassLevel; 73 } 74 } 75 76 78 85 public void init(PMappingCallback mapper, MetaObject metaclass) throws PException { 86 super.init(mapper, metaclass); 87 initDirName(); 88 ((FosPMappingStructuresManager) mapper.getPMappingStructuresManager()) 89 .addDirName(genClassPType.getJormName(), dirName); 90 } 91 92 97 public PBinding createPBinding() throws PException { 98 return new PBindingImpl(this); 99 } 100 101 102 111 public PNameIterator getPNameIterator(Object conn, 112 boolean withSubType, 113 boolean prefetching, 114 Object txctx) throws PException { 115 throw new PException("Not supported yet"); 116 } 117 118 124 public boolean isConform(String mappername) { 125 return mappername.equals("fos"); 126 } 127 128 public boolean exist(PBinding pb, Object conn) throws PException { 129 byte status = pb.getStatus(); 130 byte nextState = 131 PStateGraph.nextStatePBinding(status, PBinding.ACTION_EXIST); 132 if (nextState == PBinding.LIFECYCLE_ERROR) 133 throw new PExceptionProtocol("Unauthorized operation"); 134 status = nextState; 135 FosConnection connection; 136 if (conn == null) 137 throw new PExceptionProtocol("Cannot check the existence without a connection"); 138 else if (conn instanceof FosConnection) 139 connection = (FosConnection) conn; 140 else 141 throw new PExceptionProtocol("Cannot check the existence with this kind of connection"); 142 try { 143 return connection.exist(dirName, pb.getPName().encodeString()); 144 } catch (FosException fe) { 145 throw new PExceptionIO(fe, "Problem while testing the existence of the DSI."); 146 } 147 } 148 149 public void read(PBinding pb, Object conn, PAccessor pa) throws PException { 150 byte status = pb.getStatus(); 151 byte nextState = 152 PStateGraph.nextStatePBinding(status, PBinding.ACTION_READ); 153 if (nextState == PBinding.LIFECYCLE_ERROR) 154 throw new PExceptionProtocol("Unauthorized operation"); 155 FosConnection connection; 156 if (conn == null) 157 throw new PExceptionProtocol("Cannot read without a connection"); 158 else if (conn instanceof FosConnection) 159 connection = (FosConnection) conn; 160 else 161 throw new PExceptionProtocol("Cannot read with this kind of connection"); 162 try { 163 connection.read(dirName, pb.getPName().encodeString(), this, pa); 164 } catch (FosException fe) { 165 throw new PExceptionIO(fe, "Problem while reading the DSI."); 166 } 167 ((PBindingCtrl) pb).setStatus(nextState); 168 } 169 170 public void write(PBinding pb, Object conn, PAccessor pa) throws PException { 171 byte status = pb.getStatus(); 172 byte nextState = 173 PStateGraph.nextStatePBinding(status, PBinding.ACTION_WRITE); 174 if (nextState == PBinding.LIFECYCLE_ERROR) 175 throw new PExceptionProtocol("Unauthorized operation"); 176 FosConnection connection; 177 if (conn == null) 178 throw new PExceptionProtocol("Cannot write without a connection"); 179 else if (conn instanceof FosConnection) 180 connection = (FosConnection) conn; 181 else 182 throw new PExceptionProtocol("Cannot write with this kind of connection"); 183 try { 184 if (status == PBinding.LIFECYCLE_DELTOWRITE) { 185 connection.delete(dirName, pb.getPName().encodeString()); 186 } else if (status == PBinding.LIFECYCLE_NEWTOWRITE) { 187 connection.write(dirName, pb.getPName().encodeString(), this, pa); 188 } else if (status == PBinding.LIFECYCLE_ACTIVEFORIO) { 189 connection.write(dirName, pb.getPName().encodeString(), this, pa); 190 } 191 } catch (FosException fe) { 192 throw new PExceptionIO(fe, "Problem while writing the DSI."); 193 } 194 ((PBindingCtrl) pb).setStatus(nextState); 195 } 196 197 200 public PClassMapping[] getSubPCMs() throws PException { 201 return null; 202 } 203 204 206 public void setClassName(String cn) { 207 className = cn; 208 } 209 210 public void setFieldName(String fn) { 211 fieldName = fn; 212 } 213 214 public void setGenClassLevel(String level) { 215 genClassLevel = level; 216 } 217 218 220 227 public void readFile(ObjectInputStream ois, FosAccess conn, Object ctxt) 228 throws Exception { 229 PGenClassAccessor paccessorGenClass = null; 230 if (ctxt == null) 231 throw new PExceptionProtocol("Accessor must not be null"); 232 else if (ctxt instanceof PGenClassAccessor) 233 paccessorGenClass = (PGenClassAccessor) ctxt; 234 else 235 throw new PExceptionProtocol("Wrong type of accessor"); 236 int nbelement = ois.readInt(); 238 paccessorGenClass.paSetNbElem(nbelement); 239 for (int j = 0; j < nbelement; j++) { 240 PIndexedElem pie = paccessorGenClass.createPIndexedElem(); 241 FieldDesc[] indexes = getIndexFields(); 243 for (int i = 0; i < indexes.length; i++) { 244 switch (indexes[i].type.getTypeCode()) { 245 case PType.TYPECODE_BYTE: 246 pie.pieSetByteIndexField(indexes[i].name, ois.readByte()); 247 break; 248 case PType.TYPECODE_CHAR: 249 pie.pieSetCharIndexField(indexes[i].name, ois.readChar()); 250 break; 251 case PType.TYPECODE_INT: 252 pie.pieSetIntIndexField(indexes[i].name, ois.readInt()); 253 break; 254 case PType.TYPECODE_LONG: 255 pie.pieSetLongIndexField(indexes[i].name, ois.readLong()); 256 break; 257 case PType.TYPECODE_SHORT: 258 pie.pieSetShortIndexField(indexes[i].name, ois.readShort()); 259 break; 260 case PType.TYPECODE_DATE: 261 case PType.TYPECODE_OBJBYTE: 262 case PType.TYPECODE_OBJCHAR: 263 case PType.TYPECODE_OBJINT: 264 case PType.TYPECODE_OBJLONG: 265 case PType.TYPECODE_OBJSHORT: 266 case PType.TYPECODE_STRING: 267 pie.pieSetStringIndexField(indexes[i].name, 268 (String ) ois.readObject()); 269 break; 270 default: 271 throw new PExceptionProtocol("Unauthorized index type: (" 272 + indexes[i].name + ", " 273 + indexes[i].type.getJormName()); 274 } 275 } 276 FieldDesc[] elements = getElemFields(); 278 switch (elements[0].type.getTypeCode()) { 279 case PType.TYPECODE_BOOLEAN: 280 pie.pieSetBooleanElem(ois.readBoolean()); 281 break; 282 case PType.TYPECODE_OBJBOOLEAN: 283 pie.pieSetObooleanElem((Boolean ) ois.readObject()); 284 break; 285 case PType.TYPECODE_BYTE: 286 pie.pieSetByteElem(ois.readByte()); 287 break; 288 case PType.TYPECODE_OBJBYTE: 289 pie.pieSetObyteElem((Byte ) ois.readObject()); 290 break; 291 case PType.TYPECODE_CHAR: 292 pie.pieSetCharElem(ois.readChar()); 293 break; 294 case PType.TYPECODE_OBJCHAR: 295 pie.pieSetOcharElem((Character ) ois.readObject()); 296 break; 297 case PType.TYPECODE_SHORT: 298 pie.pieSetBooleanElem(ois.readBoolean()); 299 break; 300 case PType.TYPECODE_OBJSHORT: 301 pie.pieSetOshortElem((Short ) ois.readObject()); 302 break; 303 case PType.TYPECODE_INT: 304 pie.pieSetIntElem(ois.readInt()); 305 break; 306 case PType.TYPECODE_OBJINT: 307 pie.pieSetOintElem((Integer ) ois.readObject()); 308 break; 309 case PType.TYPECODE_LONG: 310 pie.pieSetLongElem(ois.readLong()); 311 break; 312 case PType.TYPECODE_OBJLONG: 313 pie.pieSetOlongElem((Long ) ois.readObject()); 314 break; 315 case PType.TYPECODE_FLOAT: 316 pie.pieSetFloatElem(ois.readFloat()); 317 break; 318 case PType.TYPECODE_OBJFLOAT: 319 pie.pieSetOfloatElem((Float ) ois.readObject()); 320 break; 321 case PType.TYPECODE_DOUBLE: 322 pie.pieSetDoubleElem(ois.readDouble()); 323 break; 324 case PType.TYPECODE_OBJDOUBLE: 325 pie.pieSetOdoubleElem((Double ) ois.readObject()); 326 break; 327 case PType.TYPECODE_DATE: 328 pie.pieSetDateElem((Date ) ois.readObject()); 329 break; 330 case PType.TYPECODE_CHARARRAY: 331 pie.pieSetCharArrayElem((char[]) ois.readObject()); 332 break; 333 case PType.TYPECODE_BYTEARRAY: 334 pie.pieSetByteArrayElem((byte[]) ois.readObject()); 335 break; 336 case PType.TYPECODE_SERIALIZED: 337 pie.pieSetSerializedElem((Serializable ) ois.readObject()); 338 break; 339 case PType.TYPECODE_BIGDECIMAL: 340 pie.pieSetBigDecimalElem((BigDecimal ) ois.readObject()); 341 break; 342 case PType.TYPECODE_STRING: 343 pie.pieSetStringElem((String ) ois.readObject()); 344 break; 345 case PType.TYPECODE_REFERENCE: 346 pie.pieSetRefElem(getPNameCoder() 347 .decodeString((String ) ois.readObject())); 348 break; 349 default: 350 throw new PExceptionProtocol("Unauthorized element type: (" 351 + elements[0].name + ", " 352 + elements[0].type.getJormName()); 353 } 354 paccessorGenClass.paAdd(pie, conn); 355 } 356 } 357 358 365 public void writeFile(ObjectOutputStream oos, FosAccess conn, Object ctxt) 366 throws Exception { 367 PGenClassAccessor paccessorGenClass = null; 368 if (ctxt == null) 369 throw new PExceptionProtocol("Accessor must not be null"); 370 else if (ctxt instanceof PGenClassAccessor) 371 paccessorGenClass = (PGenClassAccessor) ctxt; 372 else 373 throw new PExceptionProtocol("Wrong type of accessor"); 374 oos.writeInt(paccessorGenClass.paGetNbElem()); 376 Iterator pies = paccessorGenClass.paIterator(); 377 while (pies.hasNext()) { 379 PIndexedElem pie = (PIndexedElem) pies.next(); 380 if (pie.getElemStatus() == PIndexedElem.ELEM_DELETED) 381 continue; 382 FieldDesc[] indexes = getIndexFields(); 384 for (int i = 0; i < indexes.length; i++) { 385 switch (indexes[i].type.getTypeCode()) { 386 case PType.TYPECODE_BYTE: 387 oos.writeByte(pie.pieGetByteIndexField(indexes[i].name)); 388 break; 389 case PType.TYPECODE_CHAR: 390 oos.writeChar(pie.pieGetCharIndexField(indexes[i].name)); 391 break; 392 case PType.TYPECODE_INT: 393 oos.writeInt(pie.pieGetIntIndexField(indexes[i].name)); 394 break; 395 case PType.TYPECODE_LONG: 396 oos.writeLong(pie.pieGetLongIndexField(indexes[i].name)); 397 break; 398 case PType.TYPECODE_SHORT: 399 oos.writeShort(pie.pieGetShortIndexField(indexes[i].name)); 400 break; 401 case PType.TYPECODE_DATE: 402 case PType.TYPECODE_OBJBYTE: 403 case PType.TYPECODE_OBJCHAR: 404 case PType.TYPECODE_OBJINT: 405 case PType.TYPECODE_OBJLONG: 406 case PType.TYPECODE_OBJSHORT: 407 case PType.TYPECODE_STRING: 408 oos.writeObject(pie.pieGetStringIndexField(indexes[i].name)); 409 break; 410 default: 411 throw new PExceptionProtocol("Unauthorized index type: (" 412 + indexes[i].name + ", " 413 + indexes[i].type.getJormName() + ")"); 414 } 415 } 416 FieldDesc[] elements = getElemFields(); 418 switch (elements[0].type.getTypeCode()) { 419 case PType.TYPECODE_BOOLEAN: 420 oos.writeBoolean(pie.pieGetBooleanElem()); 421 break; 422 case PType.TYPECODE_OBJBOOLEAN: 423 oos.writeObject(pie.pieGetObooleanElem()); 424 break; 425 case PType.TYPECODE_BYTE: 426 oos.writeByte(pie.pieGetByteElem()); 427 break; 428 case PType.TYPECODE_OBJBYTE: 429 oos.writeObject(pie.pieGetObyteElem()); 430 break; 431 case PType.TYPECODE_CHAR: 432 oos.writeChar(pie.pieGetCharElem()); 433 break; 434 case PType.TYPECODE_OBJCHAR: 435 oos.writeObject(pie.pieGetOcharElem()); 436 break; 437 case PType.TYPECODE_SHORT: 438 oos.writeShort(pie.pieGetShortElem()); 439 break; 440 case PType.TYPECODE_OBJSHORT: 441 oos.writeObject(pie.pieGetOshortElem()); 442 break; 443 case PType.TYPECODE_INT: 444 oos.writeInt(pie.pieGetIntElem()); 445 break; 446 case PType.TYPECODE_OBJINT: 447 oos.writeObject(pie.pieGetOintElem()); 448 break; 449 case PType.TYPECODE_LONG: 450 oos.writeLong(pie.pieGetLongElem()); 451 break; 452 case PType.TYPECODE_OBJLONG: 453 oos.writeObject(pie.pieGetOlongElem()); 454 break; 455 case PType.TYPECODE_FLOAT: 456 oos.writeFloat(pie.pieGetFloatElem()); 457 break; 458 case PType.TYPECODE_OBJFLOAT: 459 oos.writeObject(pie.pieGetOfloatElem()); 460 break; 461 case PType.TYPECODE_DOUBLE: 462 oos.writeDouble(pie.pieGetDoubleElem()); 463 break; 464 case PType.TYPECODE_OBJDOUBLE: 465 oos.writeObject(pie.pieGetOdoubleElem()); 466 break; 467 case PType.TYPECODE_DATE: 468 oos.writeObject(pie.pieGetDateElem()); 469 break; 470 case PType.TYPECODE_CHARARRAY: 471 oos.writeObject(pie.pieGetCharArrayElem()); 472 break; 473 case PType.TYPECODE_BYTEARRAY: 474 oos.writeObject(pie.pieGetByteArrayElem()); 475 break; 476 case PType.TYPECODE_SERIALIZED: 477 oos.writeObject(pie.pieGetSerializedElem()); 478 break; 479 case PType.TYPECODE_BIGDECIMAL: 480 oos.writeObject(pie.pieGetBigDecimalElem()); 481 break; 482 case PType.TYPECODE_STRING: 483 oos.writeObject(pie.pieGetStringElem()); 484 break; 485 case PType.TYPECODE_REFERENCE: 486 PName pn = pie.pieGetRefElem(); 487 oos.writeObject(pn.encodeString()); 488 break; 489 default: 490 throw new PExceptionProtocol("Unauthorized element type: (" 491 + elements[0].name + ", " 492 + elements[0].type.getJormName() + ")"); 493 } 494 } 495 } 496 497 500 public HashMap getAssociationTable() { 501 return null; 503 } 504 505 508 public void addAssociation(PClassMapping targetClass, int[] indexes) { 509 511 } 512 513 516 public int[] getIndexesTable(PClassMapping targetClass) { 517 return null; 519 } 520 521 524 public PName resolve(Object conn, PName pname) throws PException { 525 return null; 527 } 528 529 532 public boolean match(Object obj, boolean b) throws PException{ 533 return false; 535 } 536 537 540 public PName getDecodedPName(TupleCollection tc, PName pname, boolean b) throws PException { 541 return null; 543 } 544 545 548 public void init(PMappingStructuresManager pmsm) throws PException { 549 551 } 552 553 556 public void classDefined(PMappingStructuresManager pmsm) throws PException { 557 559 } 560 } 561 | Popular Tags |