DomainEntityManagerBean.java from Texai at Krugle
Show DomainEntityManagerBean.java syntax highlighted
/*
* DomainEntityManagerBean.java
*
* Created on March 6, 2007, 11:37 AM
*
* Description: Provides a facade for the commonly used domain entity persistence methods.
*
* Copyright (C) 2007 Stephen L. Reed.
*
* 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 2 of the License, or (at your option) any later version.
*
* 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 details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.kb.ejb.session;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.log4j.Logger;
import org.texai.kb.Constants;
import org.texai.kb.ejb.entity.AbstractReifiedTerm;
import org.texai.kb.ejb.entity.AbstractTerm;
import org.texai.kb.ejb.entity.AtomicTerm;
import org.texai.kb.ejb.entity.BinaryGAF;
import org.texai.kb.ejb.entity.Formula;
import org.texai.kb.ejb.entity.NonAtomicTerm;
import org.texai.kb.ejb.entity.Symbol;
import org.texai.kb.ejb.session.shared.AssociationFinderBean;
import org.texai.kb.ejb.session.shared.AssociationFinderLocal;
import org.texai.kb.ejb.session.shared.TermDefinitionAccessorBean;
import org.texai.kb.ejb.session.shared.TermDefinitionAccessorLocal;
import org.texai.kb.ejb.session.shared.TermDeleterFacadeBean;
import org.texai.kb.ejb.session.shared.TermDeleterFacadeLocal;
import org.texai.kb.ejb.session.shared.TermFinderFacadeBean;
import org.texai.kb.ejb.session.shared.TermFinderFacadeLocal;
import org.texai.util.TexaiException;
/**
*
* @author reed
*/
@Stateless
public class DomainEntityManagerBean implements DomainEntityManagerLocal {
/** the entity manager instance injected by the container */
@PersistenceContext(unitName=Constants.PERSISTENCE_UNIT_NAME)
private EntityManager entityManager; // NOPMD
/** the term finder, which is injected by the container */
@EJB
private TermFinderFacadeLocal termFinderFacade; // NOPMD
/** the term deleter, which is injected by the container */
@EJB
private TermDeleterFacadeLocal termDeleterFacade; // NOPMD
/** the association finder, which is injected by the container */
@EJB
private AssociationFinderLocal associationFinder; // NOPMD
/** the term definition accessor, which is injected by the container */
@EJB
private TermDefinitionAccessorLocal termDefinitionAccessor; // NOPMD
/** the domain entity loader, which is injected by the container */
@EJB
private DomainEntityLoaderLocal domainEntityLoader; // NOPMD
/** the domain entity persister, which is injected by the container */
@EJB
private DomainEntityPersisterLocal domainEntityPersister; // NOPMD
/** the domain entity deleter, which is injected by the container */
@EJB
private DomainEntityDeleterLocal domainEntityDeleter; // NOPMD
/** the creator */
private AbstractReifiedTerm creator;
/** the creation purpose */
private AbstractReifiedTerm creationPurpose;
/** the logger */
private transient Logger logger; // NOPMD
/** Creates a new instance of DomainEntityManagerBean. */
public DomainEntityManagerBean() {
super();
logger = Logger.getLogger(DomainEntityManagerBean.class);
}
/** Injects the shared session bean dependencies when executed out of the container. */
public void injectSharedBeanDependencies() {
//Preconditions
assert entityManager != null : "entityManager must not be null";
// construct the shared session beans
termFinderFacade = new TermFinderFacadeBean();
((TermFinderFacadeBean) termFinderFacade).setEntityManager(entityManager);
termDeleterFacade = new TermDeleterFacadeBean();
((TermDeleterFacadeBean) termDeleterFacade).setEntityManager(entityManager);
associationFinder = new AssociationFinderBean();
((AssociationFinderBean) associationFinder).setEntityManager(entityManager);
termDefinitionAccessor = new TermDefinitionAccessorBean();
// inject their shared dependencies
termFinderFacade.setTermDefinitionAccessor(termDefinitionAccessor);
associationFinder.setTermFinderFacade(termFinderFacade);
termDefinitionAccessor.setTermFinderFacade(termFinderFacade);
termDefinitionAccessor.setAssociationFinder(associationFinder);
termDeleterFacade.setAssociationFinder(associationFinder);
termDeleterFacade.setTermFinderFacade(termFinderFacade);
// construct the domain entity access session beans
domainEntityLoader = new DomainEntityLoaderBean();
((DomainEntityLoaderBean) domainEntityLoader).setEntityManager(entityManager);
((DomainEntityLoaderBean) domainEntityLoader).setAssociationFinder(associationFinder);
((DomainEntityLoaderBean) domainEntityLoader).setTermDefinitionAccessor(termDefinitionAccessor);
((DomainEntityLoaderBean) domainEntityLoader).setTermFinderFacade(termFinderFacade);
domainEntityPersister = new DomainEntityPersisterBean();
((DomainEntityPersisterBean) domainEntityPersister).setEntityManager(entityManager);
((DomainEntityPersisterBean) domainEntityPersister).setAssociationFinder(associationFinder);
((DomainEntityPersisterBean) domainEntityPersister).setTermDefinitionAccessor(termDefinitionAccessor);
((DomainEntityPersisterBean) domainEntityPersister).setTermDeleterFacade(termDeleterFacade);
((DomainEntityPersisterBean) domainEntityPersister).setTermFinderFacade(termFinderFacade);
domainEntityDeleter = new DomainEntityDeleterBean();
((DomainEntityDeleterBean) domainEntityDeleter).setEntityManager(entityManager);
((DomainEntityDeleterBean) domainEntityDeleter).setAssociationFinder(associationFinder);
((DomainEntityDeleterBean) domainEntityDeleter).setTermDefinitionAccessor(termDefinitionAccessor);
((DomainEntityDeleterBean) domainEntityDeleter).setTermDeleterFacade(termDeleterFacade);
((DomainEntityDeleterBean) domainEntityDeleter).setTermFinderFacade(termFinderFacade);
}
/** Sets the entity manager during out-of-the-container unit testing.
*
* @param entityManager the term entity manager
*/
public void setEntityManager(final EntityManager entityManager) {
//Preconditions
assert entityManager != null : "entityManager must not be null";
this.entityManager = entityManager;
}
/** Sets the creator.
*
* @param creator the creator
*/
public void setCreator(final AbstractReifiedTerm creator) {
//Preconditions
assert creator != null : "creator must not be null";
this.creator = creator;
}
/** Gets the creator.
*
* @return the creator
*/
public AbstractReifiedTerm getCreator() {
return creator;
}
/** Sets the creation purpose.
*
* @param creationPurpose the creation purpose
*/
public void setCreationPurpose(final AbstractReifiedTerm creationPurpose) {
//Preconditions
assert creationPurpose != null : "xxxxx must not be null";
this.creationPurpose = creationPurpose;
}
/** Gets the creation purpose.
*
* @return the creation purpose
*/
public AbstractReifiedTerm getCreationPurpose() {
return creationPurpose;
}
// facade methods from the term finder
/** Finds the AtomicTerm having the given termName or null if not found.
*
* @param termName the given term name
* @return the AtomicTerm having the given termName or null if not found
*/
public AtomicTerm findAtomicTermByTermName(final String termName) {
return termFinderFacade.findAtomicTermByTermName(termName);
}
/** Finds or creates the Symbol having the given nameValue.
*
* @param nameValue the given name value
* @return the Symbol having the given nameValue
*/
public Symbol findOrCreateSymbolByNameValue(final String nameValue) {
return termFinderFacade.findOrCreateSymbolByNameValue(nameValue);
}
/** Finds or creates the Formula having the given term list.
*
* @param termList the list of terms that constitute this formula
* @return the persisted Formula having the given term list
*/
public Formula findOrCreateFormulaByTermList(final List<AbstractTerm> termList) {
ensureManagedTerms();
return termFinderFacade.findOrCreateFormulaByTermList(termList, creator, creationPurpose);
}
/**
* Finds or creates the defined atomic term.
*
* @param termName the term name
* @param prettyName the pretty name
* @param commentString the comment string
* @param isaTerms the list of isa (type) terms
* @return the specified defined term
*/
public AtomicTerm findOrCreateDefinedTerm(
final String termName,
final String prettyName,
final String commentString,
final List<AbstractReifiedTerm> isaTerms) {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
ensureManagedTerms();
return termFinderFacade.findOrCreateDefinedTerm(
termName,
prettyName,
commentString,
isaTerms,
creator,
creationPurpose);
}
/**
* Finds or creates the specified binary predicate term with default list of isa (type) terms [BinaryPredicate, PredicateFocalInArg1],
* and list of genlPreds (super property) terms [conceptuallyRelated].
*
* @param termName the predicate name
* @param prettyName the pretty name
* @param commentString the comment string
* @param argConstraintTerms the list of argument constraint terms
* @return the specified binary predicate term
*/
public AtomicTerm findOrCreateBinaryPredicateTerm(
final String termName,
final String prettyName,
final String commentString,
final List<AbstractReifiedTerm> argConstraintTerms) {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
ensureManagedTerms();
return termFinderFacade.findOrCreateBinaryPredicateTerm(
termName,
prettyName,
commentString,
argConstraintTerms,
creator,
creationPurpose);
}
/**
* Finds or creates the specified binary predicate term.
*
*
* @param termName the predicate name
* @param prettyName the pretty name
* @param commentString the comment string
* @param isaTerms the list of isa (type) terms, defaults to [BinaryPredicate, PredicateFocalInArg1]
* @param genlPredTerms the list of genlPreds (super property) terms, defaults to [conceptuallyRelated]
* @param argConstraintTerms the list of argument constraint terms
* @return the specified binary predicate term
*/
public AtomicTerm findOrCreateBinaryPredicateTerm(
final String termName,
final String prettyName,
final String commentString,
final List<AbstractReifiedTerm> isaTerms,
final List<AbstractReifiedTerm> genlPredTerms,
final List<AbstractReifiedTerm> argConstraintTerms) {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
ensureManagedTerms();
return termFinderFacade.findOrCreateBinaryPredicateTerm(
termName,
prettyName,
commentString,
isaTerms,
genlPredTerms,
argConstraintTerms,
creator,
creationPurpose);
}
/**
* Finds or creates the specified collection term.
*
* @param termName the term name
* @param prettyName the pretty name
* @param commentString the comment string
* @param isaTerms the list of isa (type) terms
* @param genlsTerms the list of genls (superclass) terms
* @return the specified collection term
*/
public AtomicTerm findOrCreateCollectionTerm(
final String termName,
final String prettyName,
final String commentString,
final List<AbstractReifiedTerm> isaTerms,
final List<AbstractReifiedTerm> genlsTerms) {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
ensureManagedTerms();
return termFinderFacade.findOrCreateCollectionTerm(
termName,
prettyName,
commentString,
isaTerms,
genlsTerms,
creator,
creationPurpose);
}
/**
* Finds or creates the specified context term.
*
* @param termName the term name
* @param prettyName the pretty name
* @param commentString the comment string
* @param isaTerms the list of isa (type) terms which defaults to [Microtheory] when empty
* @param genlMtTerms the list of genlMt (super context) terms which defaults to [BaseKB] when empty
* @return the specified collection term
*/
public AtomicTerm findOrCreateContextTerm(
final String termName,
final String prettyName,
final String commentString,
final List<AbstractReifiedTerm> isaTerms,
final List<AbstractReifiedTerm> genlMtTerms) {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
ensureManagedTerms();
return termFinderFacade.findOrCreateContextTerm(
termName,
prettyName,
commentString,
isaTerms,
genlMtTerms,
creator,
creationPurpose);
}
// facade methods for the domain entity loader
/** Loads the domain entity from propositions in the knowledge base given its term id.
*
* @param termId the id of the atomic term that represents the domain entity
* @return the domain entity
*/
public Object loadDomainEntity(final Long termId) {
return domainEntityLoader.loadDomainEntity(termId);
}
/** Loads domain entities having the given property and value.
*
* @param property the given property
* @param value the value of the property
* @param domainEntityClass the class of the desired domain entity
* @return the domain entities having the given property and value
*/
public Set<Object> loadDomainEntitiesByPropertyValue(
final AtomicTerm property,
final Object value,
final Class domainEntityClass) {
return domainEntityLoader.loadDomainEntitiesByPropertyValue(property, value, domainEntityClass);
}
/** Loads the domain entity from propositions in the knowledge base given an identifying property and value.
*
* @param property the identifying property
* @param value the value of the property which identifies the desired domain entity
* @param domainEntityClass the class of the desired domain entity
* @return the domain entity having the given property and value, or null if not found
*/
public Object loadDomainEntityByIndentifyingPropertyValue(
final AtomicTerm property,
final Object value,
final Class domainEntityClass) {
return domainEntityLoader.loadDomainEntityByIndentifyingPropertyValue(property, value, domainEntityClass);
}
/** Returns an iterator over the set of domain entity terms that represent instances of the given domain entity class.
*
* @param domainEntityClass the given domain entity class
*/
public Iterator<Object> domainEntityIterator(final Class domainEntityClass) {
return domainEntityLoader.domainEntityIterator(domainEntityClass);
}
// facade methods for the domain entity persister
/** Persists the given domain entity as propositions in the knowledge base.
*
* @param domainEntity the domain entity
* @return the instance term that represents the domain entity
*/
public AtomicTerm persistDomainEntity(
final Object domainEntity) {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
ensureManagedTerms();
return domainEntityPersister.persistDomainEntity(domainEntity, creator, creationPurpose);
}
/** Sets the indicator to validate persisted gafs as a well-formed formulas.
*
* @param validateWellFormedFormula the indicator to validate persisted gafs as a well-formed formulas
*/
public void setValidateWellFormedFormula(boolean validateWellFormedFormula) {
domainEntityPersister.setValidateWellFormedFormula(validateWellFormedFormula);
}
// facade methods for the domain entity persister
/** Deletes the given domain entity from the knowledge base.
*
* @param domainEntity the domain entity
*/
public void deleteDomainEntity(final Object domainEntity) {
domainEntityDeleter.deleteDomainEntity(domainEntity);
}
// facade methods for the association finder
/** Returns an iterator over the set of binary gafs with the given predicate and given relevant context.
*
* @param predicate the predicate of matching binary gafs
* @param context the most specific context of those from which matching gafs are gathered
* @return an iterator over the binary ground atomic formulas having the given predicate and context
*/
public Iterator<BinaryGAF> binaryGAFByPredicateIterator(
final AtomicTerm predicate,
final AbstractReifiedTerm context) {
return associationFinder.binaryGAFByPredicateIterator(predicate, context);
}
// accessors to the wrapped session beans
/** Gets the term finder facade.
*
* @return the term finder facade
*/
public TermFinderFacadeLocal getTermFinderFacade() {
return termFinderFacade;
}
// Miscellaneous
/** Edits the specified binary GAF.
*
* @param predicate the predicate
* @param oldArg1 the old arg1 value
* @param newArg1 the new arg1 value
* @param oldArg2 the old arg2 value
* @param newArg2 the new arg2 value
* @param context the assertion context
* @return the edited binary GAF
*/
public BinaryGAF editBinaryGAF(
final AtomicTerm predicate,
final AbstractTerm oldArg1,
final AbstractTerm newArg1,
final AbstractTerm oldArg2,
final AbstractTerm newArg2,
final AbstractReifiedTerm context) {
final List<AbstractTerm> args = new ArrayList<AbstractTerm>();
args.add(oldArg1);
args.add(oldArg2);
BinaryGAF binaryGAF = termFinderFacade.findBinaryGAFByConstituents(
predicate,
args,
context);
if (binaryGAF == null) {
throw new TexaiException("binary gaf was not found for editing " + predicate + "(" + oldArg1 + ", " + oldArg2 + ") in " + context);
}
args.clear();
args.add(newArg1);
args.add(newArg2);
binaryGAF.setArgs(args);
entityManager.persist(binaryGAF);
return binaryGAF;
}
// private methods
/** Ensure that the creator and creation purpose atomic terms are managed by the entity manager. */
private void ensureManagedTerms() {
//Preconditions
assert creator != null : "creator must not be null";
assert creationPurpose != null : "creationPurpose must not be null";
if (!entityManager.contains(creator)) {
logger.info("reloading " + creator);
if (creator instanceof AtomicTerm) {
creator = termFinderFacade.findAtomicTermByTermId(creator.getTermId());
} else if (creator instanceof NonAtomicTerm) {
creator = termFinderFacade.findNonAtomicTermByTermId(creator.getTermId());
} else {
assert false : "invalid creator term type " + creator;
}
}
assert entityManager.contains(creator) : "creator must be managed";
if (!entityManager.contains(creationPurpose)) {
logger.info("reloading " + creationPurpose);
if (creationPurpose instanceof AtomicTerm) {
creationPurpose = termFinderFacade.findAtomicTermByTermId(creationPurpose.getTermId());
} else if (creationPurpose instanceof NonAtomicTerm) {
creationPurpose = termFinderFacade.findNonAtomicTermByTermId(creationPurpose.getTermId());
} else {
assert false : "invalid creationPurpose term type " + creationPurpose;
}
assert entityManager.contains(creationPurpose) : "creationPurpose must be managed";
}
}
}
See more files for this project here