Skip navigation links
uk.ac.ed.epcc.webapp.model.data

Class Repository

    • Field Detail

      • CONFIG_TAG_PREFIX

        protected static final java.lang.String CONFIG_TAG_PREFIX
        See Also:
        Constant Field Values
      • BACKUP_SUFFIX_ATTR

        public static final java.lang.String BACKUP_SUFFIX_ATTR
        See Also:
        Constant Field Values
      • REFERENCE_PREFIX

        public static final java.lang.String REFERENCE_PREFIX
        See Also:
        Constant Field Values
      • CACHE_FEATURE_PREFIX

        public static final java.lang.String CACHE_FEATURE_PREFIX
        See Also:
        Constant Field Values
      • REQUIRE_ID_KEY

        public static final Feature REQUIRE_ID_KEY
      • CHECK_INDEX

        public static final Feature CHECK_INDEX
      • READ_ONLY_FEATURE

        public static final Feature READ_ONLY_FEATURE
      • BACKUP_WITH_SELECT

        public static final Feature BACKUP_WITH_SELECT
      • use_id

        protected final boolean use_id
        can we use the unique id code for this table
    • Constructor Detail

      • Repository

        protected Repository(AppContext c,
                             java.lang.String tag)
                      throws java.sql.SQLException
        Create a Repository object private to force the use of the Factory method.
        Parameters:
        c - AppContext
        tag - String tag used to find repository.
        Throws:
        java.sql.SQLException
    • Method Detail

      • createBackupTable

        public void createBackupTable(java.lang.String name)
                               throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getBackup

        public Repository getBackup()
                             throws DataFault
        create a backup table structured like this one if backups are enabled.
        Returns:
        Repository or null
        Throws:
        DataFault
      • TableToTag

        public static java.lang.String TableToTag(AppContext c,
                                                  java.lang.String tag)
      • addUniqueName

        public java.lang.StringBuilder addUniqueName(java.lang.StringBuilder sb,
                                                     boolean qualify,
                                                     boolean quote)
        Add the primary key name of this table to a query
        Parameters:
        sb - StringBuilder
        qualify - boolean should we qualify with table name
        quote - boolean request quoting if supported
        Returns:
        modified StringBuilder
      • addTable

        public java.lang.StringBuilder addTable(java.lang.StringBuilder sb,
                                                boolean quote)
        Add the table name to a query. If this method is used to construct SQL statements then Repository sub-classes can implement table name mangling if required. Use this for schema updates but useaddSource(StringBuilder, boolean) in preference for select/update statements as qualified field names will use the alias
        Parameters:
        sb - StringBuilder
        quote - request quoting if supported
        Returns:
        modified StringBuilder
        See Also:
        addSource(StringBuilder, boolean)
      • addAlias

        public java.lang.StringBuilder addAlias(java.lang.StringBuilder sb,
                                                boolean quote)
        Add the table alias
        Parameters:
        sb -
        quote -
        Returns:
        modified StringBuilder
      • addSource

        public java.lang.StringBuilder addSource(java.lang.StringBuilder sb,
                                                 boolean quote)
        Add the table as a query source. Normally the same as addTable(StringBuilder, boolean) but is allowed to add an "AS alias" clause as well
        Parameters:
        sb -
        quote -
        Returns:
        modified StringBuilder
      • getTable

        public java.lang.String getTable()
      • convert

        public java.lang.Object convert(java.lang.String key,
                                        java.lang.Object value)
        Performs automatic type conversions to the canonical type as specified by the database table. This is to provide automatic casting based on the DB field type beyond that supported intrinsically in JDBC e.g. between Date and integer time fields. Note that Filters that generate their own SQL fragments will need to call this explicitly
        Parameters:
        key - Database field to base conversion on
        value - input object
        Returns:
        converted object
      • convert

        public <T> T convert(java.lang.Class<? extends T> target,
                             java.lang.Object value)
      • clearCache

        protected void clearCache()
        clear the Record cache.
      • clearFields

        protected void clearFields()
      • convertDate

        public final java.util.Date convertDate(java.lang.Object o)
        perform any conversions to Date supported by this Repository. This is public so external code that queries directly can use them.
        Parameters:
        o - Input Object
        Returns:
        Number
      • convertNumber

        public final java.lang.Number convertNumber(java.lang.Object o)
        Perform any object conversions to Number appropriate to thisRepository. This is public so external code that queries directly can also use them
        Parameters:
        o - input Object
        Returns:
        Number
      • convertBoolean

        public final java.lang.Boolean convertBoolean(java.lang.Object o,
                                                      java.lang.Boolean def)
      • addTypeProducer

        public void addTypeProducer(TypeProducer producer)
        Cache a TypeProducer in the repository. This allows the basic database meta-data to be augmented by run-time information.
        Parameters:
        producer -
      • hasTypeProducer

        public boolean hasTypeProducer(java.lang.String field)
      • getContext

        public AppContext getContext()
        get the AppContext associated with this Repository.
        Returns:
        the AppContext
      • flushCache

        public final void flushCache()
      • getFields

        public java.util.Set<java.lang.String> getFields()
        get the list of Record fields for this table in canonical order.
        Returns:
        Set of field names or null if Repository invalid
      • getIndexNames

        public java.util.Set<java.lang.String> getIndexNames()
      • getInfo

        public java.util.Collection<Repository.FieldInfo> getInfo()
        Get all the FieldInfo objects for this Repository
        Returns:
        Collection
      • getInfo

        public Repository.FieldInfo getInfo(java.lang.String key)
        get the FieldInfo object for a field null key always returns a null result
        Parameters:
        key -
        Returns:
        FieldInfo
      • getSQLContext

        public final SQLContext getSQLContext()
      • getResolution

        public long getResolution()
        Return the number of milliseconds per tick to use when using an integer type to specify a date or vice versa.
        Returns:
        the resolution
      • getTag

        public java.lang.String getTag()
        Get the Tag string used to reference this Repository
        Returns:
        String
      • getParamTag

        public java.lang.String getParamTag()
        get the tag used to qualify configuration parameters. This is normally the same as the reference tag but can be overridden to make it easy to support rolled-tables.
        Returns:
        String tag
      • getDBTag

        public final java.lang.String getDBTag()
        Get the tag used to retrieve the correct database connection for this table from a DatabaseService
        Returns:
        tag
      • getUniqueIdName

        protected java.lang.String getUniqueIdName()
        get the field name of the primary key
        Returns:
        String
      • isUniqueIdName

        public boolean isUniqueIdName(java.lang.String name)
        Is this the primary key field. Safer to provide a test than a query method as it can't be used to write a ad-hoc sql query.
        Parameters:
        name -
        Returns:
        boolean
      • getUniqueIdName

        protected java.lang.String getUniqueIdName(boolean qualify)
      • hasField

        public boolean hasField(java.lang.String key)
        Is the specified object a valid Field key for this table.
        Parameters:
        key -
        Returns:
        boolean true if key is valid
      • hasIndex

        public boolean hasIndex(java.lang.String index)
        Is there a named index of the specified name for this table.
        Parameters:
        index -
        Returns:
        boolean
      • getNumberExpression

        public <T extends java.lang.Number,X extends DataObjectNumberFieldExpression<T,X> getNumberExpression(java.lang.Class<X> filter_type,
                                                                                                                java.lang.Class<T> target,
                                                                                                                java.lang.String key)
        get a NumberFieldExpression for a field.
        Parameters:
        filter_type - type of the hosting object
        target - desired numeric type for the field.
        key - field name
        Returns:
        NumberFieldExpression
      • getDateExpression

        public <X extends DataObjectFieldValue<java.util.Date,X> getDateExpression(java.lang.Class<X> target,
                                                                                     java.lang.String key)
        get a Date valued FieldValue for a field. The underlying database field may be a time-stamp or a numeric field.
        Parameters:
        target - type of hosting object
        key - field name
        Returns:
        FieldValue
      • getReferenceExpression

        public <T extends DataObjectIndexedFieldValue getReferenceExpression(java.lang.Class<T> self,
                                                                               java.lang.String key)
        Get a IndexedFieldValue for a reference field
        Parameters:
        self - type of owning object
        key -
        Returns:
        IndexedFieldValue
      • getReferenceExpression

        public <T extends DataObject,I extends DataObjectIndexedFieldValue<T,I> getReferenceExpression(java.lang.Class<T> self,
                                                                                                         java.lang.String key,
                                                                                                         IndexedProducer<I> prod)
        Get a IndexedFieldValue for a field The field does not have to be tagged as a reference field
        Parameters:
        self -
        key -
        prod -
        Returns:
        IndexedFieldValue
      • insert

        protected int insert(Repository.Record r)
                      throws DataFault
        Default insert operation that uses Generated Keys to obtain the unique id. Not all DBs support this but it is a good default. If the id value is greater than zero this is taken as a required id to be inserted.
        Parameters:
        r -
        Returns:
        Throws:
        DataFault
      • setAllowBogusPut

        public boolean setAllowBogusPut(boolean f)
        control if it is an error to set a property with no corresponding database field.
        Parameters:
        f - boolean
        Returns:
        previous value
      • setAllowNull

        public boolean setAllowNull(boolean f)
        control if it is an error to set a property with a null value
        Parameters:
        f - boolean
        Returns:
        previous value
      • dbFieldtoTag

        public java.lang.String dbFieldtoTag(java.lang.String name)
        Map the actual name of the DB field to the tag used in the code. Normally this is the identity but this method allows field renaming.
      • tagToTable

        public static java.lang.String tagToTable(AppContext ctx,
                                                  java.lang.String tag)
        Map a tag name to the actual database table.
        Parameters:
        ctx -
        tag -
        Returns:
        table name
      • setResolution

        public void setResolution(long resolution)
        Parameters:
        resolution - the resolution to set
      • useID

        public boolean useID()
        Does this repository support id fields. Without this the table must be read only
        Returns:
        boolean
      • usesCache

        public boolean usesCache()
      • usesAlias

        public boolean usesAlias()
        Returns:
      • makeTargetObject

        public static <T> T makeTargetObject(java.lang.Class<T> target,
                                             java.sql.ResultSet rs,
                                             int pos)
                                      throws java.sql.SQLException
        Static method to result a field from a result set with a specified target class
        Type Parameters:
        T -
        Parameters:
        target -
        rs -
        pos -
        Returns:
        T
        Throws:
        java.sql.SQLException
      • getForeignKeyDescriptor

        public static java.lang.String getForeignKeyDescriptor(AppContext c,
                                                               java.lang.String tag,
                                                               boolean quote)
        Get a foreign key descriptor by tag.
        Parameters:
        c - AppContext
        tag - String
        quote -
        Returns:
        descriptor or null
      • flushCaches

        public static void flushCaches(AppContext c)
      • reset

        public static void reset(AppContext c,
                                 java.lang.String tag)
        Clear the cached repository
        Parameters:
        c -
        tag -
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • cleanup

        public void cleanup()
        Description copied from interface: AppContextCleanup
        AppContext is being closed. Only use this for cleanup that can't be handled by normal garbage collection or for state which is never returned by reference.
        Specified by:
        cleanup in interface AppContextCleanup
      • typeName

        public static java.lang.String typeName(int type)