All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.nakedobjects.nof.reflect.java.JavaDomainObjectContainer Maven / Gradle / Ivy
package org.nakedobjects.nof.reflect.java;
import java.lang.reflect.Array;
import java.util.Enumeration;
import org.nakedobjects.applib.ApplicationException;
import org.nakedobjects.applib.DomainObjectContainer;
import org.nakedobjects.applib.PersistFailedException;
import org.nakedobjects.applib.RepositoryException;
import org.nakedobjects.applib.security.RoleMemento;
import org.nakedobjects.applib.security.UserMemento;
import org.nakedobjects.noa.adapter.NakedCollection;
import org.nakedobjects.noa.adapter.NakedObject;
import org.nakedobjects.noa.adapter.ResolveState;
import org.nakedobjects.noa.persist.InstancesCriteria;
import org.nakedobjects.noa.persist.NakedObjectPersistor;
import org.nakedobjects.noa.security.Session;
import org.nakedobjects.noa.spec.NakedObjectSpecification;
import org.nakedobjects.nof.core.context.NakedObjectsContext;
import org.nakedobjects.nof.core.persist.AllInstances;
import org.nakedobjects.nof.core.persist.PatternCriteria;
import org.nakedobjects.nof.core.persist.TitleCriteria;
import org.nakedobjects.nof.core.util.Assert;
public class JavaDomainObjectContainer implements DomainObjectContainer {
private static Object[] convertToArray(final NakedCollection instances, final Class cls) {
Object[] array = (Object[]) Array.newInstance(cls, instances.size());
Enumeration e = instances.elements();
int i = 0;
while (e.hasMoreElements()) {
array[i++] = ((NakedObject) e.nextElement()).getObject();
}
return array;
}
private NakedObject adapterFor(final Object object) {
NakedObject adapter = NakedObjectsContext.getObjectLoader().getAdapterForElseCreateAdapterForTransient(object);
return adapter;
}
public Object[] allInstances(final Class cls, final boolean includeSubclasses) {
NakedObjectSpecification spec = NakedObjectsContext.getReflector().loadSpecification(cls);
return findInstances(cls, new AllInstances(spec, includeSubclasses));
}
public void disposeInstance(final Object persistentObject) {
if (persistentObject == null) {
throw new IllegalArgumentException("Must specify a reference for disposing an object");
}
NakedObject adapter = NakedObjectsContext.getObjectLoader().getAdapterFor(persistentObject);
if (!isPersistent(persistentObject)) {
throw new RepositoryException("Object not persistent: " + adapter);
}
NakedObjectsContext.getUpdateNotifer().addDisposedObject(adapter);
NakedObjectsContext.getObjectPersistor().destroyObject(adapter);
}
public Object findInstance(final Class cls, final Object pattern, final boolean includeSubclasses) {
Object[] instances = findInstances(cls, pattern, includeSubclasses);
if (instances.length > 1) {
throw new RepositoryException("Found more that one instance matching pattern " + pattern);
}
return instances.length == 0 ? null : instances[0];
}
public Object findInstance(final Class cls, final String title, final boolean includeSubclasses) {
Object[] instances = findInstances(cls, title, includeSubclasses);
if (instances.length > 1) {
throw new RepositoryException("Found more that one instance of " + cls + " with title " + title);
}
return instances.length == 0 ? null : instances[0];
}
protected Object[] findInstances(final Class cls, final InstancesCriteria criteria) {
NakedCollection instances = NakedObjectsContext.getObjectPersistor().findInstances(criteria);
return convertToArray(instances, cls);
}
public Object[] findInstances(Class cls, Object pattern, boolean includeSubclasses) {
Assert.assertEquals(cls, pattern.getClass());
return findInstances(cls, new PatternCriteria(includeSubclasses, pattern));
}
public Object[] findInstances(Class cls, String title, boolean includeSubclasses) {
NakedObjectSpecification spec = NakedObjectsContext.getReflector().loadSpecification(cls);
return findInstances(cls, new TitleCriteria(spec, title, includeSubclasses));
}
public Object firstInstance(final Class cls, final Object pattern, final boolean includeSubclasses) {
Object[] instances = findInstances(cls, pattern, includeSubclasses);
return instances.length == 0 ? null : instances[0];
}
public Object firstInstance(final Class cls, final String title, final boolean includeSubclasses) {
Object[] instances = findInstances(cls, title, includeSubclasses);
return instances.length == 0 ? null : instances[0];
}
public UserMemento getUser() {
Session session = NakedObjectsContext.getSession();
String name = session.getUserName();
RoleMemento[] roles = new RoleMemento[0];
UserMemento user = new UserMemento(name, roles);
return user;
}
public void informUser(final String message) {
NakedObjectsContext.getMessageBroker().addMessage(message);
}
public boolean isPersistent(final Object object) {
NakedObject adapter = adapterFor(object);
return !adapter.getOid().isTransient();
}
public void makePersistent(final Object transientObject) {
NakedObject adapter = NakedObjectsContext.getObjectLoader().getAdapterFor(transientObject);
if (isPersistent(transientObject)) {
throw new PersistFailedException("Object already persistent: " + adapter);
}
NakedObjectsContext.getObjectPersistor().makePersistent(adapter);
}
public Object newInstance(final Class ofClass, final Object sameStateAs) {
if (isPersistent(sameStateAs)) {
return newPersistentInstance(ofClass);
} else {
return newTransientInstance(ofClass);
}
}
public Object newPersistentInstance(final Class ofClass) {
NakedObjectSpecification spec = NakedObjectsContext.getReflector().loadSpecification(ofClass);
NakedObject adapter = NakedObjectsContext.getObjectLoader().createTransientInstance(spec);
NakedObjectsContext.getObjectPersistor().makePersistent(adapter);
return adapter.getObject();
}
public Object newTransientInstance(final Class ofClass) {
NakedObjectSpecification spec = NakedObjectsContext.getReflector().loadSpecification(ofClass);
return NakedObjectsContext.getObjectLoader().createTransientInstance(spec).getObject();
}
public void objectChanged(final Object object) {
if (object != null) {
NakedObject adapter = adapterFor(object);
objectPersistor().objectChanged(adapter);
}
}
protected NakedObjectPersistor objectPersistor() {
return NakedObjectsContext.getObjectPersistor();
}
public void raiseError(final String message) {
throw new ApplicationException(message);
}
public void resolve(final Object parent) {
NakedObject adapter = adapterFor(parent);
ResolveState resolveState = adapter.getResolveState();
if (resolveState.isResolvable(ResolveState.RESOLVING)) {
objectPersistor().resolveImmediately(adapter);
}
}
public void resolve(final Object parent, final Object field) {
if (field == null) {
resolve(parent);
}
}
public void warnUser(final String message) {
NakedObjectsContext.getMessageBroker().addWarning(message);
}
}
// Copyright (c) Naked Objects Group Ltd.