001: package simpleorm.data;
002:
003: import java.util.Comparator;
004:
005: /**
006: * Stores metadata for a field within an DRecordMeta.
007: */
008: public class DFieldMeta implements Comparator<DRecordInstance> {
009: DRecordMeta recordMeta;
010: final String name;
011: String prompt; // I18N etc.
012: DType type;
013: boolean primaryKey; // forms (part of) the primary key.
014:
015: int maxLength; // for Strings
016:
017: Object fieldImplementation; // eg. ddl utils Field object.
018:
019: public DFieldMeta(String name, DType type) { // SORM auto Adds.
020: this .name = name;
021: this .type = type;
022: }
023:
024: /**
025: * returns -1, 0, or 1 to indicate first <, =, > second sort order.
026: * Used by DQuery to order by selected fields.
027: * Here rather than DFieldInstance in case field has no value.
028: * nulls/no value sorts first.
029: */
030: public int compare(DRecordInstance first, DRecordInstance second) {
031: Object value1 = first.getObject(this );
032: Object value2 = second.getObject(this );
033: if (value1 == null && value2 != null)
034: return -1;
035: if (value1 != null && value2 == null)
036: return 1;
037: if (value1 instanceof Comparable
038: && value2 instanceof Comparable)
039: return ((Comparable) value1).compareTo(value2);
040: return 0;
041: }
042:
043: public String toString() {
044: return "{" + recordMeta + "." + name + "}";
045: }
046:
047: /////////////////////////////////////////
048:
049: public DRecordMeta getRecordMeta() { // SORM getSRecordMeta
050: return recordMeta;
051: }
052:
053: public DFieldMeta setRecord(DRecordMeta record) {
054: this .recordMeta = record;
055: return this ;
056: }
057:
058: public String getName() {
059: return name;
060: }
061:
062: public String getPrompt() {
063: return prompt;
064: }
065:
066: public DFieldMeta setPrompt(String prompt) {
067: this .prompt = prompt;
068: return this ;
069: }
070:
071: public DType getType() {
072: return type;
073: }
074:
075: public DFieldMeta setType(DType type) {
076: this .type = type;
077: return this ;
078: }
079:
080: public boolean isPrimaryKey() {
081: return primaryKey;
082: }
083:
084: public DFieldMeta setPrimaryKey(boolean primaryKey) {
085: this .primaryKey = primaryKey;
086: return this ;
087: }
088:
089: public int getMaxLength() {
090: return maxLength;
091: }
092:
093: public DFieldMeta setMaxLength(int maxLength) {
094: this .maxLength = maxLength;
095: return this ;
096: }
097:
098: public Object getFieldImplementation() {
099: return fieldImplementation;
100: }
101:
102: public void setFieldImplementation(Object fieldImplementation) {
103: this.fieldImplementation = fieldImplementation;
104: }
105: }
|