Package org.exolab.castor.persist.spi

Examples of org.exolab.castor.persist.spi.Identity


    throws PersistenceException {
        UpdateFlags flags = new UpdateFlags();
       
        ClassMolder fieldClassMolder = _fieldMolder.getFieldClassMolder();
        Object value = _fieldMolder.getValue(object, tx.getClassLoader());
        Identity curIdentity = (Identity) field;
        Identity newIdentity = null;
        if (value != null) {
            newIdentity = fieldClassMolder.getIdentity(tx, value);
            flags.setNewField(newIdentity);
        }

        // | yip: don't delete the following comment,
        //      until it proved working by time. :-P
        // if ids are the same or not canCreate
        //    if object is deleted
        //        warn
        //    if object are the same
        //        done
        //    not the same
        //        exception
        // ids not the same or canCreate
        //    if depend
        //       if old is not null
        //          delete old
        //          removeRelation
        //       if canCreate
        //          create new
        //    not depend and autoStore
        //       if old is not null
        //          removeRelation
        //       if canCreate
        //          createObject
        //    not depend nor autoStore
        //       if old is not null
        //          removeRelation
        //       if new is not null
        if (ClassMolderHelper.isEquals(curIdentity, newIdentity)) {
            /*
             * Let's deal with a situation where there's no dependent object (field == null),
             * a 'new' dependent object has been set (value != null), but as we are using a key
             * generator on this newly set object, calling fieldClassMolder.getIdentity() will
             * return null (and hence newField == null). In this case, we still have to mark this
             * new object for creation and instruct Castor to update the cache(s) as well.
             */
            if ((field == null) && (value != null)
                    &&  _fieldMolder.isDependent() && !tx.isRecorded(value)) {
                if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
                    flags.setUpdatePersist(true);
                }
                flags.setUpdateCache(true);
                tx.markCreate(fieldClassMolder, value, oid);
            }
           
            //TODO [WG]: can anybody please explain to me the meaning of the next two lines.
            if (!_debug) { return flags; }
            if (curIdentity == null) { return flags; } // do the next field if both are null

            if ((value != null) && tx.isDeleted(value)) {
                LOG.warn ("Deleted object found!");
                if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
                    flags.setUpdatePersist(true);
                }
                flags.setUpdateCache(true);
                _fieldMolder.setValue(object, null, tx.getClassLoader());
                return flags;
            }

            if (tx.isAutoStore() || _fieldMolder.isDependent()) {
                if (value != tx.fetch(fieldClassMolder, curIdentity, null)) {
                    throw new DuplicateIdentityException("");
                }
            }
        } else {
            if (_fieldMolder.isStored() /* && _fieldMolder.isCheckDirty() */) {
                flags.setUpdatePersist(true);
            }
            flags.setUpdateCache(true);

            if (_fieldMolder.isDependent()) {
                if (curIdentity != null) {
                    Object reldel = tx.fetch(fieldClassMolder, curIdentity, null);
                    if (reldel != null) {
                        tx.delete(reldel);
                    }
                }

                if ((value != null) && !tx.isRecorded(value)) {
                    tx.markCreate(fieldClassMolder, value, oid);
                }

            } else if (tx.isAutoStore()) {
                if (curIdentity != null) {
                    Object deref = tx.fetch(fieldClassMolder, curIdentity, null);
                    if (deref != null) {
                        fieldClassMolder.removeRelation(tx, deref, _classMolder, object);
                    }
                }

                if ((value != null) && !tx.isRecorded(value)) {
                    tx.markCreate(fieldClassMolder, value, null);
                }
            } else {
                if (curIdentity != null) {
                    Object deref = tx.fetch(fieldClassMolder, curIdentity, null);
                    if (deref != null) {
                        fieldClassMolder.removeRelation(tx, deref, _classMolder, object);
                    }
                }

                // yip: user're pretty easily to run into cache
                // integrity problem here, if user forgot to create
                // "value" explicitly. We autoload the value for him.
                if ((value != null) && !tx.isRecorded(value)) {
                    Identity fieldValue = fieldClassMolder.getIdentity(tx, value);
                    if (fieldValue != null) {
                        ProposedEntity temp = new ProposedEntity(fieldClassMolder);
                        tx.load(fieldValue, temp, null);
                        _fieldMolder.setValue(object, temp.getEntity(), tx.getClassLoader());
                    } else {
View Full Code Here


     *      org.castor.persist.TransactionContext, org.exolab.castor.persist.OID,
     *      java.lang.Object, org.exolab.castor.mapping.AccessMode, java.lang.Object)
     */
    public void update(final TransactionContext tx, final OID oid, final Object object,
            final AccessMode suggestedAccessMode, final Object field) throws PersistenceException {
        Identity nfield = (Identity) field;
       
        ClassMolder fieldClassMolder = _fieldMolder.getFieldClassMolder();
        Object o = _fieldMolder.getValue(object, tx.getClassLoader());
        if (_fieldMolder.isDependent()) {
            // depedent class won't have persistenceInfo in LockEngine
View Full Code Here

     */
    public void markDelete(final TransactionContext tx, final Object object, final Object field)
    throws PersistenceException {
        // persistanceCapable include many_to_one
        ClassMolder fieldClassMolder = _fieldMolder.getFieldClassMolder();
        Identity identity = (Identity) field;
        if (identity != null) {
            Object fetched = tx.fetch(fieldClassMolder, identity, null);
            if (fetched != null) {
                if (_fieldMolder.isDependent()) {
                    tx.delete(fetched);
View Full Code Here

        // field is not primitive type. Related object will be loaded
        // thru the transaction in action if needed.

        ClassMolder fieldClassMolder = _fieldMolder.getFieldClassMolder();

        Identity fieldValue = (Identity) proposedObject.getField(_fieldIndex);
        if (fieldValue != null) {
            // use the corresponding Persistent fields as the identity,
            // and we ask transactionContext in action to load it.
            Object temp;
            try {
View Full Code Here

        // not really. cus, the other created relation, may already
        // has reference to this object. so, how to deal with that?
        if (field != null) {
            ArrayList alist = (ArrayList) field;
            for (int j = 0; j < alist.size(); j++) {
                Identity fid = (Identity) alist.get(j);
                Object fetched = null;
                if (fid != null) {
                    fetched = tx.fetch(fieldClassMolder, fid, null);
                    if (fetched != null) {
                        fieldClassMolder.removeRelation(tx, fetched,
View Full Code Here

                flags.setUpdateCache(true);
            }
            while (removedItor.hasNext()) {
                // must be loaded thur transaction, so that the related object
                // is properly locked and updated before we delete it.
                Identity identity = (Identity) removedItor.next();
                if (!tx.isDeletedByOID(new OID(fieldClassMolder, identity))) {
                    ProposedEntity proposedValue = new ProposedEntity(fieldClassMolder);
                    Object reldel = tx.load(identity, proposedValue, null);
                    if (reldel != null && tx.isPersistent(reldel)) {
                        tx.writeLock(reldel, tx.getLockTimeout());

                        _fieldMolder.getRelationLoader().deleteRelation(
                                tx.getConnection(oid.getMolder().getLockEngine()),
                                oid.getIdentity(), identity);

                        fieldClassMolder.removeRelation(tx, reldel, _classMolder, object);
                    }
                }
            }

            Collection added = ClassMolderHelper.getAddedValuesList(tx,
                    orgFields, value, fieldClassMolder);
            Iterator addedItor = added.iterator();
            if (addedItor.hasNext()) {
                if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
                    flags.setUpdatePersist(true);
                }
                flags.setUpdateCache(true);
            }
            while (addedItor.hasNext()) {
                Object addedField = addedItor.next();
                tx.markModified(addedField, false/* updatePersist */,
                        true/* updateCache */);

                if (tx.isPersistent(addedField)) {
                    _fieldMolder.getRelationLoader().createRelation(
                            tx.getConnection(oid.getMolder().getLockEngine()),
                            oid.getIdentity(),
                            fieldClassMolder.getIdentity(tx, addedField));
                } else {
                    if (tx.isAutoStore()) {
                        if (!tx.isDeleted(addedField)) {
                            tx.markCreate(fieldClassMolder, addedField, null);
                        }
                    }
                }
            }

        } else {
            RelationCollection lazy = (RelationCollection) value;

            // this RelationCollection has to clean up its state at the end of
            // the
            // transaction
            tx.addTxSynchronizable(lazy);

            ArrayList deleted = lazy.getDeleted();
            if (!deleted.isEmpty()) {
                if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
                    flags.setUpdatePersist(true);
                }
                flags.setUpdateCache(true);

                Iterator itor = deleted.iterator();
                while (itor.hasNext()) {
                    flags.setUpdateCache(true);
                    Identity deletedId = (Identity) itor.next();
                    Object toBeDeleted = lazy.find(deletedId);
                    if (toBeDeleted != null) {
                        if (tx.isPersistent(toBeDeleted)) {
                            tx.writeLock(toBeDeleted, 0);

                            _fieldMolder.getRelationLoader().deleteRelation(
                                    tx.getConnection(oid.getMolder().getLockEngine()),
                                    oid.getIdentity(), deletedId);

                            fieldClassMolder.removeRelation(tx, toBeDeleted,
                                    this._classMolder, object);
                        }
//                    } else {
//                        // what to do if it happens?
                    }
                }
            }

            ArrayList added = lazy.getAdded();
            if (!added.isEmpty()) {
                if (_fieldMolder.isStored() && _fieldMolder.isCheckDirty()) {
                    flags.setUpdatePersist(true);
                }
                flags.setUpdateCache(true);

                Iterator itor = added.iterator();
                while (itor.hasNext()) {
                    Identity addedId = (Identity) itor.next();
                    Object toBeAdded = lazy.find(addedId);
                    if (toBeAdded != null) {
                        if (tx.isPersistent(toBeAdded)) {
                            _fieldMolder.getRelationLoader().createRelation(
                                    tx.getConnection(oid.getMolder().getLockEngine()),
View Full Code Here

     * all changes persisted properly.
     */
    public void runTest() throws PersistenceException {
        createPerson();

        Identity fullname = new Identity("First", "Person");
       
        // now test if the persisted objects model is created as expected
        loadAndModifyPerson1(fullname);
        loadAndModifyPerson2(fullname);
    }
View Full Code Here

                        id[j] = columns[j].toJava(SQLTypeInfos.getValue(
                                rs, columnIndex++, columns[j].getSqlType()));
                        fieldIndex++;
                        if (id[j] != null) { notNull = true; }
                    }
                    entity.setField(((notNull) ? new Identity(id) : null), i);
                } else {
                    ArrayList res = new ArrayList();
                    notNull = false;
                    Object[] id = new Object[columns.length];
                    for (int j = 0; j < columns.length; j++) {
                        id[j] = columns[j].toJava(SQLTypeInfos.getValue(
                                rs, columnIndex, columns[j].getSqlType()));
                        if (id[j] != null) { notNull = true; }
                        fieldIndex++;
                        columnIndex++;
                    }
                    if (notNull) { res.add(new Identity(id)); }
                    entity.setField(res, i);
                }
            }

            while (rs.next()) {
                fieldIndex = 1;
                columnIndex = ids.length + 1;
                processedTables.clear();
                if (fields[0].isJoined()) {
                    ClassDescriptor clsDesc = _engine.getDescriptor();
                    processedTables.add(new ClassDescriptorJDONature(clsDesc).getTableName());
                }

                for (int i = 0; i < fields.length; ++i) {
                    SQLFieldInfo field = fields[i];
                    SQLColumnInfo[] columns = field.getColumnInfo();
                    tableName = field.getTableName();
                    if (i > 0 && !field.isJoined() && !processedTables.contains(tableName)) {
                        columnIndex = columnIndex + ids.length;
                    }
                    processedTables.add(tableName);
                   
                    if (field.isMulti()) {
                        ArrayList res = (ArrayList) entity.getField(i);
                        notNull = false;
                        Object[] id = new Object[columns.length];
                        for (int j = 0; j < columns.length; j++) {
                            id[j] = columns[j].toJava(SQLTypeInfos.getValue(
                                    rs, columnIndex, columns[j].getSqlType()));
                            if (id[j] != null) { notNull = true; }
                            columnIndex++;
                        }
                        fieldIndex++;
                        if (notNull) {
                            Identity com = new Identity(id);
                            if (!res.contains(com)) { res.add(com); }
                        }
                    } else {
                        fieldIndex++;
                        columnIndex += columns.length;
View Full Code Here

            Object tmp = SQLTypeInfos.getValue(_rs, 1 + i, _identSqlType[i]);
            returnId[i] = _engine.getColumnInfoForIdentities()[i].toJava(tmp);
            if (tmp != null) { empty = false; }
        }
        if (empty) { return null; }
        return new Identity(returnId);
    }
View Full Code Here

        return new Identity(returnId);
    }

    // Get the next identity that is different from <identity>.
    public Identity nextIdentity(final Identity identity) throws PersistenceException {
        Identity nextIdentity = null;
        try {
            if (_lastIdentity == null) {
                if (_resultSetDone || !_rs.next()) {
                    _resultSetDone = true;
                    return null;
View Full Code Here

TOP

Related Classes of org.exolab.castor.persist.spi.Identity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.