/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.delegate.client;
import cli_fmw.delegate.DelegateStateful;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.DirectoryItemNotFoundException;
import framework.utils.Comparator;
import cli_fmw.utils.MessageBox;
import clips.delegate.directory.simple.clientDocumentType.DirectoryDocumentType;
import clips.delegate.directory.simple.clientDocumentType.DirectoryDocumentTypeItem;
import clips.delegate.directory.simple.documentGivenPlace.DirectoryDocumentGivenPlace;
import clips.delegate.directory.simple.documentGivenPlace.DirectoryDocumentGivenPlaceItem;
import framework.beans.client.clientDocument.ClientDocumentDetails;
import framework.utils.Converter;
import java.util.Date;
/**
* Интерфейс DelegateStateful пустой, прицеплен сюды, что б класс адекватно
* юзался из лехиных комбобоксовых моделек
* @author lacoste
*/
public class ClientDocumentData extends DelegateStateful{
private ClientDocumentDetails details;
private ClientDocumentDetails detailsOriginal;
public ClientDocumentData(ContentStateListener csl) {
this.detailsOriginal = new ClientDocumentDetails();
this.details = detailsOriginal.clone();
addContentStateListener(csl);
}
public ClientDocumentData(ContentStateListener csl, ClientDocumentDetails details){
this.detailsOriginal = details;
this.details = detailsOriginal.clone();
addContentStateListener(csl);
}
public ClientDocumentDetails getDetails() {
return details;
}
public int getId() {
return details.id;
}
public DirectoryDocumentTypeItem getType() throws DirectoryItemNotFoundException {
try {
DirectoryDocumentType dir = DirectoryLocator.getDirectory(DirectoryDocumentType.class);
return dir.getItemFromID(details.typeID);
} catch (ClipsException ex) {
return null;
}
}
public void setType(DirectoryDocumentTypeItem item) {
if (item != null && details.typeID == item.getID()) {
return;
}
if (item == null || item.getID() == 0) {
details.typeID = 0;
setDate(null);
setSeries("");
setNumber("");
setGivenPlace(null);
return;
}
details.typeID = item.getID();
fireContentStateEvent();
}
public String getSeries() {
return details.series;
}
public void setSeries(String series) {
details.series = series;
fireContentStateEvent();
}
public String getNumber() {
return details.number;
}
public void setNumber(String number) {
details.number = number;
fireContentStateEvent();
}
public DirectoryDocumentGivenPlaceItem getGivenPlace() throws DirectoryItemNotFoundException {
try {
DirectoryDocumentGivenPlace dir = DirectoryLocator.getDirectory(DirectoryDocumentGivenPlace.class);
return dir.getItemFromID(details.enterpriseID);
} catch (ClipsException ex) {
return null;
}
}
public void setGivenPlace(DirectoryDocumentGivenPlaceItem item) {
if (item == null) {
details.enterpriseID = 0;
}
else if (details.enterpriseID != item.getID()) {
details.enterpriseID = item.getID();
}
fireContentStateEvent();
}
public Date getDate() {
return details.date;
}
public void setDate(Date date) {
if (!Comparator.dataEqual(details.date, date)) {
details.date = date;
fireContentStateEvent();
}
}
@Override
public String toString() {
String toStr = "";
try {
if (getType() != null && getType().getID() != 0) {
toStr += getType().getTitle();
}
toStr += getSeries() != null && !getSeries().isEmpty() ? " " + getSeries() : "";
toStr += getNumber() != null && !getNumber().isEmpty() ? " " + getNumber() : "";
if (getGivenPlace() != null && getGivenPlace().getID() != 0) {
toStr += ", выдан: " + getGivenPlace().getTitle();
}
if (getDate() != null) {
toStr += ", " + Converter.dateToString(getDate());
}
} catch (DirectoryItemNotFoundException ex) {
MessageBox.showException(ex);
}
return toStr;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ClientDocumentData other = (ClientDocumentData) obj;
if (this.details != other.details && (this.details == null || !this.details.equals(other.details))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 29 * hash + (this.details != null ? this.details.hashCode() : 0);
return hash;
}
@Override
public void save1() throws ClipsException {
}
void commit(){
detailsOriginal = details.clone();
}
@Override
public void restore() {
details = detailsOriginal.clone();
}
@Override
public boolean isDirty() {
if (detailsOriginal != null){
return !detailsOriginal.equals(details);
}else{
return false;
}
// throw new UnsupportedOperationException("Not supported yet.");
}
}