Search in sources :

Example 1 with SelectedFieldsLockingPolicy

use of org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy in project eclipselink by eclipse-ee4j.

the class OptimisticLockingLinesSelectedFieldsLockingPolicyTest method setup.

@Override
protected void setup() {
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    descriptorToModify = project.getDescriptors().get(Employee.class);
    SelectedFieldsLockingPolicy lockingTestPolicy = new SelectedFieldsLockingPolicy();
    lockingTestPolicy.addLockFieldName("testField1");
    lockingTestPolicy.addLockFieldName("testField2");
    descriptorToModify.setOptimisticLockingPolicy(lockingTestPolicy);
    generator = new ProjectClassGenerator(project);
}
Also used : SelectedFieldsLockingPolicy(org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ProjectClassGenerator(org.eclipse.persistence.sessions.factories.ProjectClassGenerator)

Example 2 with SelectedFieldsLockingPolicy

use of org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy in project eclipselink by eclipse-ee4j.

the class OracleNoSQLPlatform method buildInteractionSpec.

/**
 * Allow the platform to build the interaction spec based on properties defined in the interaction.
 */
@Override
public InteractionSpec buildInteractionSpec(EISInteraction interaction) {
    InteractionSpec spec = interaction.getInteractionSpec();
    if (spec == null) {
        OracleNoSQLInteractionSpec noSqlSpec = new OracleNoSQLInteractionSpec();
        Object operation = interaction.getProperty(OPERATION);
        if (operation == null) {
            throw new EISException("'" + OPERATION + "' property must be set on the query's interation.");
        }
        if (operation instanceof String) {
            operation = OracleNoSQLOperation.valueOf((String) operation);
        }
        noSqlSpec.setOperation((OracleNoSQLOperation) operation);
        // Allows setting of consistency as a property.
        Object consistency = interaction.getProperty(CONSISTENCY);
        if (consistency == null) {
            // Default to session property.
            consistency = interaction.getQuery().getSession().getProperty(CONSISTENCY);
        }
        if (consistency instanceof Consistency) {
            noSqlSpec.setConsistency((Consistency) consistency);
        } else if (consistency instanceof String) {
            String constant = (String) consistency;
            if (constant.equals("ABSOLUTE")) {
                noSqlSpec.setConsistency(Consistency.ABSOLUTE);
            } else if (constant.equals("NONE_REQUIRED")) {
                noSqlSpec.setConsistency(Consistency.NONE_REQUIRED);
            } else {
                throw new EISException("Invalid consistency property value: " + constant);
            }
        }
        // Allows setting of durability as a property.
        Object durability = interaction.getProperty(DURABILITY);
        if (durability == null) {
            // Default to session property.
            durability = interaction.getQuery().getSession().getProperty(DURABILITY);
        }
        if (durability instanceof Durability) {
            noSqlSpec.setDurability((Durability) durability);
        } else if (durability instanceof String) {
            String constant = (String) durability;
            if (constant.equals("COMMIT_NO_SYNC")) {
                noSqlSpec.setDurability(Durability.COMMIT_NO_SYNC);
            } else if (constant.equals("COMMIT_SYNC")) {
                noSqlSpec.setDurability(Durability.COMMIT_SYNC);
            } else if (constant.equals("COMMIT_WRITE_NO_SYNC")) {
                noSqlSpec.setDurability(Durability.COMMIT_WRITE_NO_SYNC);
            } else {
                throw new EISException("Invalid durability property value: " + constant);
            }
        }
        // Allows setting of timeout as a property.
        Object timeout = interaction.getProperty(TIMEOUT);
        if (timeout == null) {
            // Default to session property.
            timeout = interaction.getQuery().getSession().getProperty(TIMEOUT);
        }
        if (timeout instanceof Number) {
            noSqlSpec.setTimeout(((Number) timeout).longValue());
        } else if (timeout instanceof String) {
            noSqlSpec.setTimeout(Long.parseLong(((String) timeout)));
        } else if (interaction.getQuery().getQueryTimeout() > 0) {
            noSqlSpec.setTimeout(interaction.getQuery().getQueryTimeout());
        }
        // Allows setting of version as a property.
        Object version = interaction.getProperty(VERSION);
        if (version == null) {
            // Default to session property.
            version = interaction.getQuery().getSession().getProperty(VERSION);
        }
        if (version == null) {
            if (interaction.getQuery().getDescriptor() != null) {
                ClassDescriptor descriptor = interaction.getQuery().getDescriptor();
                if (descriptor.usesOptimisticLocking() && descriptor.getOptimisticLockingPolicy() instanceof SelectedFieldsLockingPolicy) {
                    DatabaseField field = ((SelectedFieldsLockingPolicy) descriptor.getOptimisticLockingPolicy()).getLockFields().get(0);
                    if (interaction.getInputRow() != null) {
                        version = interaction.getInputRow().get(field);
                    }
                }
            }
        }
        if (version instanceof Version) {
            noSqlSpec.setVersion((Version) version);
        } else if (version instanceof byte[]) {
            noSqlSpec.setVersion(Version.fromByteArray((byte[]) version));
        }
        spec = noSqlSpec;
    }
    return spec;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) OracleNoSQLInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLInteractionSpec) InteractionSpec(jakarta.resource.cci.InteractionSpec) Consistency(oracle.kv.Consistency) Durability(oracle.kv.Durability) EISException(org.eclipse.persistence.eis.EISException) OracleNoSQLInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLInteractionSpec) SelectedFieldsLockingPolicy(org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy) Version(oracle.kv.Version) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 3 with SelectedFieldsLockingPolicy

use of org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy in project eclipselink by eclipse-ee4j.

the class EntityMappingsAdvancedJUnitTestCase method testProjectOptimisticLockingSettings.

/**
 * Verifies that the optimistic-locking settings were read correctly from XML.
 */
public void testProjectOptimisticLockingSettings() {
    ServerSession session = JUnitTestCase.getServerSession(m_persistenceUnit);
    ClassDescriptor descriptor = session.getDescriptor(Project.class);
    if (descriptor == null) {
        fail("Project descriptor was not found in the PU [" + m_persistenceUnit + "]");
    } else {
        OptimisticLockingPolicy policy = descriptor.getOptimisticLockingPolicy();
        if (policy instanceof SelectedFieldsLockingPolicy) {
            List<DatabaseField> lockFields = ((SelectedFieldsLockingPolicy) policy).getLockFields();
            if (lockFields.isEmpty() || lockFields.size() > 1) {
                fail("Invalid amount of lock fields were set on Project's selected fields locking policy.");
            } else {
                DatabaseField lockField = lockFields.get(0);
                assertTrue("Incorrect lock field was set on Project's selected fields locking policy.", lockField.getName().equals("VERSION"));
            }
        } else {
            fail("A SelectedFieldsLockingPolicy was not set on the Project descriptor.");
        }
    }
}
Also used : SelectedFieldsLockingPolicy(org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) OptimisticLockingPolicy(org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy)

Example 4 with SelectedFieldsLockingPolicy

use of org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy in project eclipselink by eclipse-ee4j.

the class EntityMappingsAdvancedJUnitTestCase method testProjectOptimisticLockingSettings.

/**
 * Verifies that the optimistic-locking settings were read correctly from XML.
 */
public void testProjectOptimisticLockingSettings() {
    DatabaseSessionImpl session = getDatabaseSession();
    ClassDescriptor descriptor = session.getDescriptor(Project.class);
    if (descriptor == null) {
        fail("Project descriptor was not found in the PU [" + m_persistenceUnit + "]");
    } else {
        OptimisticLockingPolicy policy = descriptor.getOptimisticLockingPolicy();
        if (policy instanceof SelectedFieldsLockingPolicy) {
            List<DatabaseField> lockFields = ((SelectedFieldsLockingPolicy) policy).getLockFields();
            if (lockFields.isEmpty() || lockFields.size() > 1) {
                fail("Invalid amount of lock fields were set on Project's selected fields locking policy.");
            } else {
                DatabaseField lockField = lockFields.get(0);
                assertTrue("Incorrect lock field was set on Project's selected fields locking policy.", lockField.getName().equals("VERSION"));
            }
        } else {
            fail("A SelectedFieldsLockingPolicy was not set on the Project descriptor.");
        }
    }
}
Also used : SelectedFieldsLockingPolicy(org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) OptimisticLockingPolicy(org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy)

Example 5 with SelectedFieldsLockingPolicy

use of org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy in project eclipselink by eclipse-ee4j.

the class ProjectClassGenerator method addOptimisticLockingLines.

protected void addOptimisticLockingLines(NonreflectiveMethodDefinition method, OptimisticLockingPolicy policy) {
    String policyClassName = policy.getClass().getName();
    String packageName = policyClassName.substring(0, policyClassName.lastIndexOf('.'));
    if (packageName.equals("org.eclipse.persistence.descriptors")) {
        policyClassName = Helper.getShortClassName(policy);
    }
    method.addLine(policyClassName + " lockingPolicy = new " + policyClassName + "();");
    if (policy instanceof SelectedFieldsLockingPolicy) {
        SelectedFieldsLockingPolicy fieldPolicy = (SelectedFieldsLockingPolicy) policy;
        for (DatabaseField field : fieldPolicy.getLockFields()) {
            method.addLine("lockingPolicy.addLockFieldName(\"" + field.getQualifiedName() + "\");");
        }
    } else if (policy instanceof VersionLockingPolicy) {
        VersionLockingPolicy versionPolicy = (VersionLockingPolicy) policy;
        method.addLine("lockingPolicy.setWriteLockFieldName(\"" + versionPolicy.getWriteLockField().getQualifiedName() + "\");");
        if (versionPolicy.isStoredInObject()) {
            method.addLine("lockingPolicy.storeInObject();");
        }
        if (policy instanceof TimestampLockingPolicy) {
            TimestampLockingPolicy timestampPolicy = (TimestampLockingPolicy) policy;
            if (timestampPolicy.usesLocalTime()) {
                method.addLine("lockingPolicy.useLocalTime();");
            }
        }
    }
    method.addLine("descriptor.setOptimisticLockingPolicy(lockingPolicy);");
}
Also used : SelectedFieldsLockingPolicy(org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy) TimestampLockingPolicy(org.eclipse.persistence.descriptors.TimestampLockingPolicy) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy)

Aggregations

SelectedFieldsLockingPolicy (org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy)5 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 OptimisticLockingPolicy (org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy)2 InteractionSpec (jakarta.resource.cci.InteractionSpec)1 Consistency (oracle.kv.Consistency)1 Durability (oracle.kv.Durability)1 Version (oracle.kv.Version)1 TimestampLockingPolicy (org.eclipse.persistence.descriptors.TimestampLockingPolicy)1 VersionLockingPolicy (org.eclipse.persistence.descriptors.VersionLockingPolicy)1 EISException (org.eclipse.persistence.eis.EISException)1 OracleNoSQLInteractionSpec (org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLInteractionSpec)1 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)1 ProjectClassGenerator (org.eclipse.persistence.sessions.factories.ProjectClassGenerator)1 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)1 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)1