DomainEntityDeleterBean.java from Texai at Krugle
Show DomainEntityDeleterBean.java syntax highlighted
/*
* DomainEntityDeleterBean.java
*
* Created on December 21, 2006, 10:55 AM
*
* Description: This stateless session bean deletes domain entities from the knowledge base,
* removing KB propositions which represent domain entity associations.
*
* Copyright (C) 2006 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.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.UUID;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;
import org.texai.kb.ejb.session.shared.AssociationFinderLocal;
import org.texai.kb.ejb.session.shared.TermDefinitionAccessorLocal;
import org.texai.kb.ejb.session.shared.TermDeleterFacadeLocal;
import org.texai.kb.ejb.session.shared.TermFinderFacadeLocal;
import org.texai.kb.entity.AtomicTerm;
import org.texai.util.ByteUtils;
import org.texai.util.TexaiException;
/**
*
* @author reed
*/
@Stateless
public class DomainEntityDeleterBean
extends AbstractDomainEntityAccessor
implements DomainEntityDeleterLocal {
/** 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 logger */
private transient Logger logger; // NOPMD
/** Creates a new instance of DomainEntityDeleterBean. */
public DomainEntityDeleterBean() {
super();
}
/** Deletes the given domain entity from the knowledge base.
*
* @param domainEntity the domain entity
*/
public void deleteDomainEntity(final Object domainEntity) {
//Preconditions
assert domainEntity != null : "domainEntity must not be null";
assert isDomainEntity(domainEntity) : domainEntity + " must be have a @DomainEntity class level annotation";
getLogger().debug("deleting " + domainEntity);
initializeAbstractSessionState();
setDomainEntity(domainEntity);
setDomainEntityClass(domainEntity.getClass());
gatherAnnotationsForDomainEntityClass();
configureDomainContextSettings();
configureDomainEntitySettings();
deleteDomainEntity(findDomainInstanceTerm());
}
/** Deletes the domain entity having the given term id from the knowledge base
*
* @param termId the id of the atomic term that represents the domain entity
*/
public void deleteDomainEntity(final UUID termId) {
//Preconditions
assert termId != null : "termId must not be null";
getLogger().debug("deleting domain entity having term id " + termId);
final AtomicTerm instanceTerm = termFinderFacade.findAtomicTermByTermId(ByteUtils.toBytes(termId));
if (instanceTerm == null) {
throw new TexaiException("domain entity not found for term id " + termId);
}
deleteDomainEntity(findDomainInstanceTerm());
}
/** Deletes the given domain entity represented by the given instance term from the knowledge base
*
* @param instanceTerm the atomic term that represents the domain entity
*/
public void deleteDomainEntity(final AtomicTerm instanceTerm) {
//Preconditions
assert instanceTerm != null : "instanceTerm must not be null";
getLogger().info("deleting: " + instanceTerm);
termDeleterFacade.deleteDomainInstanceTerm(instanceTerm);
}
/** Sets the term finder during out-of-the-container unit testing.
*
* @param termFinderFacade the term finder
*/
public void setTermFinderFacade(final TermFinderFacadeLocal termFinderFacade) {
//Preconditions
assert termFinderFacade != null : "termFinderFacade must not be null";
this.termFinderFacade = termFinderFacade;
}
/** Sets the term deleter during out-of-the-container unit testing.
*
* @param termDeleterFacade the term deleter
*/
public void setTermDeleterFacade(final TermDeleterFacadeLocal termDeleterFacade) {
//Preconditions
assert termDeleterFacade != null : "termDeleterFacade must not be null";
this.termDeleterFacade = termDeleterFacade;
}
/** Sets the association finder during out-of-the-container unit testing.
*
* @param associationFinder the association finder
*/
public void setAssociationFinder(final AssociationFinderLocal associationFinder) {
//Preconditions
assert associationFinder != null : "associationFinder must not be null";
this.associationFinder = associationFinder;
}
/** Sets the term definition accessor during out-of-the-container unit testing.
*
* @param termDefinitionAccessor the term definition accessor
*/
public void setTermDefinitionAccessor(final TermDefinitionAccessorLocal termDefinitionAccessor) {
//Preconditions
assert termDefinitionAccessor != null : "termDefinitionAccessor must not be null";
this.termDefinitionAccessor = termDefinitionAccessor;
}
/** Injects the shared session bean dependencies when executed out of the container. */
public void injectSharedBeanDependencies() {
if (termFinderFacade != null) {
termFinderFacade.setTermDefinitionAccessor(termDefinitionAccessor);
}
if (associationFinder != null) {
associationFinder.setTermFinderFacade(termFinderFacade);
}
if (termDefinitionAccessor != null) {
termDefinitionAccessor.setTermFinderFacade(termFinderFacade);
termDefinitionAccessor.setAssociationFinder(associationFinder);
}
if (termDeleterFacade != null) {
termDeleterFacade.setTermFinderFacade(termFinderFacade);
termDeleterFacade.setAssociationFinder(associationFinder);
}
}
/** Gets the logger.
*
* @return the logger
*/
protected Logger getLogger() {
if (logger == null) {
logger = Logger.getLogger(DomainEntityDeleterBean.class.getName());
}
return logger;
}
/** Finds the domain instance term.
*
* @return the domain instance term
*/
private AtomicTerm findDomainInstanceTerm() {
//Preconditions
assert getFieldAnnotationDictionary() != null : "fieldAnnotationDictionary must not be null";
assert !getFieldAnnotationDictionary().isEmpty() : "fieldAnnotationDictionary must not be empty";
assert getClassTermName() != null : "classTermName must not be null";
assert getClassTermName().length() > 0 : "classTermName must not be an empty string";
assert getDomainEntity() != null : "domainEntity() must not be null";
getLogger().debug(stackLevel() + " finding ID field for domainEntity " + getDomainEntity());
boolean isIdFound = false;
Object value = null;
for (final Field field : getFieldAnnotationDictionary().keySet()) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
try {
value = field.get(getDomainEntity());
} catch (final IllegalArgumentException ex) {
throw new TexaiException(ex);
} catch (final IllegalAccessException ex) {
throw new TexaiException(ex);
}
final Annotation annotation = getFieldAnnotationDictionary().get(field);
getLogger().debug(stackLevel() + " field: " + field + ", value: " + value + ", annotation: " + annotation);
if ("@javax.persistence.Id()".equals(annotation.toString())) {
getLogger().debug(stackLevel() + " found ID field");
isIdFound = true;
break;
}
}
if (!isIdFound) {
throw new TexaiException("Id field not found for domain entity " + getDomainEntity());
}
UUID termId;
AtomicTerm instanceTerm = null;
if (value == null) {
throw new TexaiException("Load domain entity before deleting it " + getDomainEntity().toString());
} else {
if (!(value instanceof UUID)) {
throw new TexaiException("Id field must be type UUID " + getDomainEntity() + ", is type " + value.getClass());
}
termId = (UUID) value;
instanceTerm = termFinderFacade.findAtomicTermByTermId(ByteUtils.toBytes(termId));
}
//Postconditions
assert instanceTerm != null : "instanceTerm must not be null";
return instanceTerm;
}
}
See more files for this project here