Skip to content

Commit

Permalink
Language tag specification support on JOPA level - for loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jun 12, 2017
1 parent eef32fb commit d0b8db4
Show file tree
Hide file tree
Showing 27 changed files with 213 additions and 100 deletions.
2 changes: 1 addition & 1 deletion jopa-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cz.cvut.kbss.jopa</groupId>
<artifactId>jopa-all</artifactId>
<version>0.9.6</version>
<version>0.9.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public interface Session {
*
* @return UnitOfWork
*/
public UnitOfWork acquireUnitOfWork();
UnitOfWork acquireUnitOfWork();

/**
* Release this session and all its children.
*/
public void release();
void release();

/**
* Remove the given object from the session's live object cache. This is
Expand All @@ -39,5 +39,5 @@ public interface Session {
* @param context
* Entity context URI
*/
public void removeObjectFromCache(Object object, URI context);
void removeObjectFromCache(Object object, URI context);
}
2 changes: 1 addition & 1 deletion jopa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cz.cvut.kbss.jopa</groupId>
<artifactId>jopa-all</artifactId>
<version>0.9.6</version>
<version>0.9.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion jopa-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cz.cvut.kbss.jopa</groupId>
<artifactId>jopa-all</artifactId>
<version>0.9.6</version>
<version>0.9.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand Down Expand Up @@ -52,8 +52,8 @@ public EntityManagerFactoryImpl(final Map<String, String> properties) {
private OntologyStorageProperties initStorageProperties() {
return OntologyStorageProperties.driver(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_CLASS))
.ontologyUri(configuration.get(JOPAPersistenceProperties.ONTOLOGY_URI_KEY))
.physicalUri(configuration
.get(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY))
.physicalUri(
configuration.get(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY))
.username(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_USERNAME))
.password(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_PASSWORD))
.build();
Expand Down Expand Up @@ -114,7 +114,7 @@ private void verifyOpen() {
*/
private synchronized void initServerSession() {
if (serverSession == null) {
this.serverSession = new ServerSession(storageProperties, configuration.getProperties(), metamodel);
this.serverSession = new ServerSession(storageProperties, configuration, metamodel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ protected void finalize() throws Throwable {
@Override
public UnitOfWorkImpl getCurrentPersistenceContext() {
if (this.persistenceContext == null) {
this.persistenceContext = (UnitOfWorkImpl) this.serverSession.acquireUnitOfWork();
this.persistenceContext = (UnitOfWorkImpl) serverSession.acquireUnitOfWork();
persistenceContext.setEntityManager(this);
}
return this.persistenceContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand All @@ -14,10 +14,12 @@
*/
package cz.cvut.kbss.jopa.oom;

import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.annotations.FetchType;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.metamodel.*;
import cz.cvut.kbss.jopa.sessions.LoadingParameters;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
Expand All @@ -27,17 +29,26 @@

class AxiomDescriptorFactory {

private final Configuration configuration;

private String puLanguage;

AxiomDescriptorFactory(Configuration configuration) {
this.configuration = configuration;
}

AxiomDescriptor createForEntityLoading(LoadingParameters<?> loadingParams, EntityType<?> et) {
final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(loadingParams.getIdentifier()));
descriptor.setSubjectContext(loadingParams.getDescriptor().getContext());
descriptor.addAssertion(Assertion.createClassAssertion(false));
addForTypes(loadingParams, et, descriptor);
addForProperties(loadingParams, et, descriptor);
this.puLanguage = configuration.get(JOPAPersistenceProperties.LANG);
for (Attribute<?, ?> att : et.getAttributes()) {
if (!shouldLoad(att.getFetchType(), loadingParams.isForceLoad())) {
continue;
}
final Assertion a = createAssertion(att);
final Assertion a = createAssertion(att, loadingParams.getDescriptor().getAttributeDescriptor(att));
addAssertionToDescriptor(loadingParams.getDescriptor(), att, descriptor, a);
}
return descriptor;
Expand All @@ -55,9 +66,8 @@ private boolean shouldLoad(FetchType fetchType, boolean forceLoad) {
return fetchType != FetchType.LAZY || forceLoad;
}

private void addAssertionToDescriptor(Descriptor entityDescriptor,
FieldSpecification<?, ?> att, final AxiomDescriptor descriptor,
final Assertion assertion) {
private void addAssertionToDescriptor(Descriptor entityDescriptor, FieldSpecification<?, ?> att,
final AxiomDescriptor descriptor, final Assertion assertion) {
descriptor.addAssertion(assertion);
final URI attContext = entityDescriptor.getAttributeDescriptor(att).getContext();
if (attContext != null) {
Expand All @@ -69,39 +79,41 @@ private void addForProperties(LoadingParameters<?> loadingParams, EntityType<?>
final PropertiesSpecification<?, ?, ?, ?> props = et.getProperties();
if (props != null && shouldLoad(props.getFetchType(), loadingParams.isForceLoad())) {
final Assertion propsAssertion = Assertion.createUnspecifiedPropertyAssertion(props.isInferred());
addAssertionToDescriptor(loadingParams.getDescriptor(), props, descriptor,
propsAssertion);
addAssertionToDescriptor(loadingParams.getDescriptor(), props, descriptor, propsAssertion);
}
}

private Assertion createAssertion(Attribute<?, ?> att) {
private Assertion createAssertion(Attribute<?, ?> att, Descriptor descriptor) {
assert att != null;
switch (att.getPersistentAttributeType()) {
case OBJECT:
return Assertion.createObjectPropertyAssertion(att.getIRI().toURI(), att.isInferred());
case DATA:
return Assertion.createDataPropertyAssertion(att.getIRI().toURI(), att.isInferred());
return Assertion
.createDataPropertyAssertion(att.getIRI().toURI(), descriptor.getLanguage().orElse(puLanguage),
att.isInferred());
case ANNOTATION:
return Assertion.createAnnotationPropertyAssertion(att.getIRI().toURI(),
att.isInferred());
return Assertion
.createAnnotationPropertyAssertion(att.getIRI().toURI(),
descriptor.getLanguage().orElse(puLanguage), att.isInferred());
default:
throw new IllegalArgumentException("Illegal persistent attribute type "
+ att.getPersistentAttributeType());
throw new IllegalArgumentException(
"Illegal persistent attribute type " + att.getPersistentAttributeType());
}
}

AxiomDescriptor createForFieldLoading(URI primaryKey, Field field, Descriptor entityDescriptor,
EntityType<?> et) {
AxiomDescriptor createForFieldLoading(URI primaryKey, Field field, Descriptor entityDescriptor, EntityType<?> et) {
this.puLanguage = configuration.get(JOPAPersistenceProperties.LANG);
final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(primaryKey));
FieldSpecification<?, ?> fieldSpec = MappingUtils.getFieldSpecification(field, et);
Assertion assertion;
final Assertion assertion;
if (et.getTypes() != null && fieldSpec.equals(et.getTypes())) {
assertion = Assertion.createClassAssertion(et.getTypes().isInferred());
} else if (et.getProperties() != null && fieldSpec.equals(et.getProperties())) {
assertion = Assertion.createUnspecifiedPropertyAssertion(et.getProperties()
.isInferred());
assertion = Assertion.createUnspecifiedPropertyAssertion(et.getProperties().isInferred());
} else {
assertion = createAssertion((Attribute<?, ?>) fieldSpec);
assertion = createAssertion((Attribute<?, ?>) fieldSpec,
entityDescriptor.getAttributeDescriptor(fieldSpec));
}
addAssertionToDescriptor(entityDescriptor, fieldSpec, descriptor, assertion);
return descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ObjectOntologyMapperImpl(UnitOfWorkImpl uow, Connection connection) {
this.cache = uow.getLiveObjectCache();
this.storageConnection = Objects.requireNonNull(connection);
this.metamodel = uow.getMetamodel();
this.descriptorFactory = new AxiomDescriptorFactory();
this.descriptorFactory = new AxiomDescriptorFactory(uow.getConfiguration());
this.instanceRegistry = new InstanceRegistry();
this.pendingPersists = new PendingChangeRegistry();
this.entityBuilder = new EntityConstructor(this);
Expand Down Expand Up @@ -156,8 +156,7 @@ public <T> void persistEntity(URI primaryKey, T entity, Descriptor descriptor) {
assert entity != null;
assert descriptor != null;

@SuppressWarnings("unchecked")
final EntityType<T> et = (EntityType<T>) getEntityType(entity.getClass());
@SuppressWarnings("unchecked") final EntityType<T> et = (EntityType<T>) getEntityType(entity.getClass());
try {
if (primaryKey == null) {
primaryKey = generateIdentifier(et);
Expand Down Expand Up @@ -253,8 +252,7 @@ public <T> void removeEntity(URI primaryKey, Class<T> cls, Descriptor descriptor

@Override
public <T> void updateFieldValue(T entity, Field field, Descriptor descriptor) {
@SuppressWarnings("unchecked")
final EntityType<T> et = (EntityType<T>) getEntityType(entity.getClass());
@SuppressWarnings("unchecked") final EntityType<T> et = (EntityType<T>) getEntityType(entity.getClass());
final URI pkUri = EntityPropertiesUtils.getPrimaryKey(entity, et);

entityBreaker.setCascadeResolver(new PersistCascadeResolver(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@

import cz.cvut.kbss.jopa.model.MetamodelImpl;
import cz.cvut.kbss.jopa.query.NamedQueryManager;
import cz.cvut.kbss.jopa.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This is the implementation of the basic Session operations. Other more
* specific methods are to be implemented in descendants.
*/
public abstract class AbstractSession implements Session, MetamodelProvider {
public abstract class AbstractSession implements Session, MetamodelProvider, ConfigurationHolder {

protected static final Logger LOG = LoggerFactory.getLogger(AbstractSession.class);

private final Configuration configuration;

protected AbstractSession(Configuration configuration) {
this.configuration = configuration;
}

public UnitOfWork acquireUnitOfWork() {
UnitOfWork uow = new UnitOfWorkImpl(this);
LOG.trace("UnitOfWork acquired.");
Expand All @@ -52,6 +60,11 @@ public void releaseObjectCache() {
getLiveObjectCache().evictAll();
}

@Override
public Configuration getConfiguration() {
return configuration;
}

/**
* Get the current live object cache.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import cz.cvut.kbss.jopa.query.NamedQueryManager;
import cz.cvut.kbss.jopa.sessions.cache.CacheFactory;
import cz.cvut.kbss.jopa.transactions.EntityTransaction;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.jopa.utils.Wrapper;
import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
Expand All @@ -50,15 +51,17 @@ public class ServerSession extends AbstractSession implements Wrapper {
private Map<UnitOfWork, Set<Object>> uowsToEntities;

ServerSession() {
super(new Configuration(Collections.emptyMap()));
this.metamodel = null;
this.managedClasses = null;
}

public ServerSession(OntologyStorageProperties storageProperties, Map<String, String> properties,
public ServerSession(OntologyStorageProperties storageProperties, Configuration configuration,
MetamodelImpl metamodel) {
super(configuration);
this.metamodel = metamodel;
this.managedClasses = processTypes(metamodel.getEntities());
initialize(storageProperties, properties, metamodel);
initialize(storageProperties, configuration, metamodel);
}

/**
Expand All @@ -78,19 +81,19 @@ private Set<Class<?>> processTypes(Set<EntityType<?>> entities) {
* cache.
*
* @param storageProperties Storage properties
* @param properties Map of setup properties
* @param configuration Session configuration
* @param metamodel Metamodel of the managed classes and their attributes.
*/
private void initialize(OntologyStorageProperties storageProperties,
Map<String, String> properties, Metamodel metamodel) {
assert properties != null;
private void initialize(OntologyStorageProperties storageProperties, Configuration configuration,
Metamodel metamodel) {
assert configuration != null;
assert metamodel != null;
this.runningTransactions = new HashMap<>();
this.activePersistenceContexts = new IdentityHashMap<>();
this.uowsToEntities = new HashMap<>();
this.liveObjectCache = CacheFactory.createCache(properties);
this.liveObjectCache = CacheFactory.createCache(configuration.getProperties());
liveObjectCache.setInferredClasses(metamodel.getInferredClasses());
this.storageAccessor = new DefaultStorageAccessor(storageProperties, properties);
this.storageAccessor = new DefaultStorageAccessor(storageProperties, configuration.getProperties());
}

protected ConnectionWrapper acquireConnection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class UnitOfWorkImpl extends AbstractSession implements UnitOfWork, Query
private final CacheManager cacheManager;

public UnitOfWorkImpl(AbstractSession parent) {
super(parent.getConfiguration());
this.parent = Objects.requireNonNull(parent);
this.cloneMapping = createMap();
this.cloneToOriginals = createMap();
Expand Down Expand Up @@ -1110,11 +1111,6 @@ private void storageCommit() {
}
}

@Override
public Configuration getConfiguration() {
return entityManager.getConfiguration();
}

@Override
public <T> T unwrap(Class<T> cls) {
if (cls.isAssignableFrom(getClass())) {
Expand Down
Loading

0 comments on commit d0b8db4

Please sign in to comment.