at.spardat.xma.datasource.DomainFmt Maven / Gradle / Ivy
The newest version!
package at.spardat.xma.datasource;
/*
* @(#) $Id: $
*
* Copyright 2009/2010 by sIT Solutions, A-1110 Wien, Geiselbergstr.21-25. All rights reserved.
*/
import at.spardat.enterprise.fmt.AParseException;
import at.spardat.enterprise.fmt.AStringFmt;
import at.spardat.enterprise.fmt.IFmt;
import at.spardat.enterprise.util.Types;
import at.spardat.xma.session.XMASession;
public class DomainFmt extends IFmt {
/**
* Display just the short value of each entry.
*/
public static final int SHORT = IFmt.LAST_STYLE * 2;
/**
* Display the long value of each domain entry.
*/
public static final int LONG = SHORT * 2;
/**
* Display short and long value of each entry, separated by dash.
*/
public static final int SHORT_LONG = LONG * 2;
/**
* Display the key value of each domain entry
*/
public static final int KEY = SHORT_LONG * 2;
private Domain domain;
public DomainFmt(String dataSource, XMASession session, int style) {
domain = Domain.getInstance(dataSource, session);
this.style_ = style;
}
public String format(String internal) {
if (internal == null || internal.length()==0) {
return "";
}
String shortValue = domain.getShortValue(internal);
if (shortValue.length() == 0){
shortValue = internal;
}
String longValue = domain.getLongValue(internal);
if (longValue.length() == 0) {
longValue = shortValue;
}
if ((style_ & LONG) != 0) {
return longValue;
} else if ((style_ & SHORT_LONG) != 0) {
return shortValue + " - " + longValue;
} else if ((style_ & SHORT) != 0) {
return shortValue;
} else {
return internal;
}
}
public boolean isLegalExternalChar(char aChar) {
return true;
}
public boolean isLegalInternal(String internal) {
return domain.isInTable(internal);
}
public boolean isOneWay() {
return true;
}
public int maxLenOfExternal() {
return AStringFmt.MAX_MAX_LEN;
}
public boolean mayBeAppliedTo(byte type) {
return type == Types.T_DOM || type == Types.T_STRING;
}
public String parse(String external) throws AParseException {
IDomRow[] domRows = getDomain().getDomRows();
for (int i = 0; i < domRows.length; i++) {
IDomRow domRow = domRows[i];
if ((style_ & LONG) != 0 && domRow.getLongValue().equals(external) || (style_ & SHORT) != 0 && domRow.getShortValue().equals(external)) {
return domRow.getKey();
}
}
throw new RuntimeException("Could not find internal value for '"+external+"'");
}
public Domain getDomain() {
return domain;
}
}