/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cli_fmw.delegate.lists;
import cli_fmw.delegate.cache.SecondaryCacheManager;
import framework.beans.EntityDetails;
import cli_fmw.main.ContentStateListener;
/**
*
* @param <TYPE>
* @author axe
*/
abstract public class DataChunk<TYPE extends EntityDetails> implements Cloneable {
private TYPE details;
private TYPE detailsOriginal;
private ContentStateListener csl;
private SecondaryCacheManager ecm;
@SuppressWarnings("unchecked")
public DataChunk(TYPE details) {
this.details = details;
this.detailsOriginal = (TYPE) details.clone();
ecm = new SecondaryCacheManager(details.getId() == 0);
}
final protected SecondaryCacheManager getSCM() {
return ecm;
}
public final void setId(int id) {
details.setId(id);
}
public final int getId() {
return details.getId();
}
public final TYPE getDetails() {
return details;
}
@SuppressWarnings("unchecked")
void commit() {
detailsOriginal = (TYPE) details.clone();
}
@SuppressWarnings("unchecked")
void restore() {
details = (TYPE) detailsOriginal.clone();
}
boolean isDirty() {
if(!details.equals(detailsOriginal)) {
return true;
}
return false;
}
@Override
public String toString() {
return "chunk[details = " + details.toString() + "]";
}
@Override
@SuppressWarnings("unchecked")
public DataChunk<TYPE> clone() {
try {
DataChunk<TYPE> one = (DataChunk<TYPE>) super.clone();
one.details = (TYPE) details.clone();
return one;
} catch(CloneNotSupportedException ex) {
ex.printStackTrace();
}
return null;
}
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
@SuppressWarnings("unchecked")
final DataChunk<TYPE> other = (DataChunk<TYPE>) obj;
if(this.details != other.details && (this.details == null || !this.details.equals(other.details))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + (this.details != null ? this.details.hashCode() : 0);
return hash;
}
public void setStateListener(ContentStateListener listener) {
if(listener == null) {
throw new IllegalArgumentException();
}
csl = listener;
}
/**
* Function notifyes all contentSateListeners about state changed event
*/
protected void fireContentStateEvent() {
if(csl == null) {
System.out.println("WARNING: no CSL in " + getClass().getSimpleName() + ". May be DETACHED?");
} else {
System.out.println("Content state change fired by " + this.getClass().getSimpleName()
+ " to " + csl.getClass().getSimpleName());
csl.contentStateChanged();
}
}
}