001package dk.netarkivet.harvester.datamodel.eav;
002
003import java.sql.Connection;
004import java.sql.Timestamp;
005
006import com.antiaction.raptor.dao.AttributeBase;
007import com.antiaction.raptor.dao.AttributeTypeBase;
008import com.antiaction.raptor.sql.DBWrapper;
009
010/**
011 * Generic EAV attribute.
012 */
013public class ContentAttribute_Generic extends AttributeBase {
014
015        /**
016         * Construct attribute using the attribute type to initialise common fields.
017         * @param at attribute type
018         */
019        public ContentAttribute_Generic(AttributeTypeBase at) {
020                attributeType = at;
021                tree_id = at.tree_id;
022                type_id = at.id;
023        }
024
025        @Override
026        public void saveState(DBWrapper db, Connection conn) {
027                if ( id == 0 ) {
028                        db.attribute_insert( conn, this );
029                }
030                else {
031                        db.attribute_update( conn, this );
032                }
033        }
034
035        @Override
036        public Integer getInteger() {
037                if ( attributeType.datatype == 1 ) {
038                        return val_int;
039                }
040                else {
041                        throw new UnsupportedOperationException();
042                }
043        }
044
045        @Override
046        public void setInteger(Integer integer) {
047                if ( attributeType.datatype == 1 ) {
048                        val_int = integer;
049                }
050                else {
051                        throw new UnsupportedOperationException();
052                }
053        }
054
055        @Override
056        public Timestamp getTimestamp() {
057                if ( attributeType.datatype == 2 ) {
058                        return val_datetime;
059                }
060                else {
061                        throw new UnsupportedOperationException();
062                }
063        }
064
065        @Override
066        public void setTimestamp(Timestamp timestamp) {
067                if ( attributeType.datatype == 2 ) {
068                        val_datetime = timestamp;
069                }
070                else {
071                        throw new UnsupportedOperationException();
072                }
073        }
074
075        @Override
076        public String getVarchar() {
077                if ( attributeType.datatype == 3 ) {
078                        return val_varchar;
079                }
080                else {
081                        throw new UnsupportedOperationException();
082                }
083        }
084
085        @Override
086        public void setVarchar(String varchar) {
087                if ( attributeType.datatype == 3 ) {
088                        val_varchar = varchar;
089                }
090                else {
091                        throw new UnsupportedOperationException();
092                }
093        }
094
095        @Override
096        public String getText() {
097                if ( attributeType.datatype == 4 ) {
098                        return val_text;
099                }
100                else {
101                        throw new UnsupportedOperationException();
102                }
103        }
104
105        @Override
106        public void setText(String text) {
107                if ( attributeType.datatype == 4 ) {
108                        val_text = text;
109                }
110                else {
111                        throw new UnsupportedOperationException();
112                }
113        }
114
115}