blob: 5ad1cd9d6293f797b049203ffe940ce314055b1d [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.common.state.impl;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.lang.reflect.MethodUtils;
import org.eclipse.osbp.runtime.common.annotations.DtoUtils;
import org.eclipse.osbp.runtime.common.hash.HashUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* Responsible to cache changed objects.
*/
public class GlobalDataState extends DataState implements
PropertyChangeListener {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(GlobalDataState.class);
/**
* Instantiates a new global data state.
*
* @param sharedStateContext
* the shared state context
*/
public GlobalDataState(SharedStateContext sharedStateContext) {
super(sharedStateContext);
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.common.state.impl.DataState#register(java.lang.Object, java.lang.Object)
*/
@Override
public void register(Object key, Object object) {
checkDisposed();
if (contains(key)) {
// already contained
return;
}
super.register(key, object);
if (object != null) {
try {
MethodUtils.invokeMethod(object, "addPropertyChangeListener",
this);
} catch (SecurityException e) {
LOGGER.warn("Observer for dirtyState handling could not be added for "
+ object.getClass().getName());
} catch (IllegalAccessException e) {
LOGGER.warn("Observer for dirtyState handling could not be added for "
+ object.getClass().getName());
} catch (IllegalArgumentException e) {
LOGGER.warn("Observer for dirtyState handling could not be added for "
+ object.getClass().getName());
} catch (InvocationTargetException e) {
LOGGER.warn("Observer for dirtyState handling could not be added for "
+ object.getClass().getName());
} catch (NoSuchMethodException e) {
LOGGER.warn("Observer for dirtyState handling could not be added for "
+ object.getClass().getName());
}
}
}
/* (non-Javadoc)
* @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
@Override
public void propertyChange(PropertyChangeEvent event) {
Object object = event.getSource();
Class<?> objClass = object.getClass();
try {
if (DtoUtils.isDisposeField(objClass, event.getPropertyName())) {
handleDispose(event);
return;
}
// dirty field not forwarded to handle dirty
if (DtoUtils.isDirtyField(objClass, event.getPropertyName())) {
return;
}
} catch (SecurityException ignoreIt) {
LOGGER.error("{}", ignoreIt);
}
handleDirty(event);
}
/**
* Puts the object into the dirty state.
*
* @param event
* the event
*/
protected void handleDirty(PropertyChangeEvent event) {
Object object = event.getSource();
Class<?> objClass = object.getClass();
// set the dirty flag to true if available
DtoUtils.invokeDirtySetter(object, true);
sharedStateContext.handleDirty(
HashUtil.createObjectWithIdHash(objClass, object), object);
}
/**
* Invalidates the object in the cache.
*
* @param event
* the event
*/
protected void handleDispose(PropertyChangeEvent event) {
Object object = event.getSource();
Class<?> objClass = object.getClass();
sharedStateContext.invalidate(
HashUtil.createObjectWithIdHash(objClass, object), object);
}
}