
    
    
package performa.orm.types;

import java.util.*;
import oneit.utils.*;

    import oneit.appservices.dbenums.*;



/**
 * This class was generated using constGen.bat.
 * DO NOT MODIFY THIS CODE.
 * Edit the associated .xml file, and regenerate this file
 * constGen (directory) (file minus extension)
 * e.g. constGen C:\...\sql FieldType
 */

public class Industry extends oneit.appservices.dbenums.AbstractEnumeratedDBConstant

{
    
    public static final EnumeratedFactory FACTORY_Industry = new IndustryFactory();

    public static final oneit.logging.LoggingArea LOG_Industry = oneit.logging.LoggingArea.createLoggingArea(DB_ENUM, "Industry");


    private static Industry[] allIndustrys;

    private static boolean isStaticDBEnum = false;

    private static boolean reloadRegistered = false;
    private static boolean reloadRequired = false;


    public static boolean isMaintainable()
    {
        return !isStaticDBEnum;
    }

    public static boolean isReloadable()
    {
        return true;
    }

    public static void reloadOnNextGet()
    {
        reloadRequired = true;
    }

    public static String getReloadCheckTable()
    {
        return "tl_industry";
    }

    private static synchronized Industry[] getAllIndustrys ()
    {
        if (allIndustrys == null)
        {
            // look in general config for a static override
            oneit.appservices.dbenums.DBEnumStaticOptions staticOverride = (oneit.appservices.dbenums.DBEnumStaticOptions)oneit.appservices.config.ConfigMgr.getConfigObject("CONFIG.GLOBAL", "DBEnumStaticOverride:performa.orm.types.Industry", null);

            if(staticOverride != null)
            {
                // if there are items in the DB, email a SYSTEMERROR, since this is likely to cause major confusion/or is a sign of an error condition
                oneit.objstore.ObjectTransaction   transaction = getTransaction ();
                BO_Industry[]   boInstances;

                try
                {
                    boInstances = BO_Industry.searchALL (transaction);

                    if(boInstances.length > 0)
                    {
                        oneit.logging.LogMgr.log (LOG_Industry, oneit.logging.LogLevel.BUSINESS1, "DB Enumerated Type performa.orm.types.Industry conflict: Either delete entries in tl_industry database table or remove the entries for performa.orm.types.Industry from CONFIG.GLOBAL:DBEnumStaticOverrides");
                    }
                }
                // allow the situation when the table does not even exist
                catch(Exception e)
                {
                    oneit.logging.LogMgr.log (LOG_Industry, oneit.logging.LogLevel.TRACE, e, "DB Enumerated Type performa.orm.types.Industry, could not load values from DB table. This is not a problem, since the enum is loading from config.");
                }
                finally
                {
                    transaction.releaseResources();
                }

                // Load the static items.
                allIndustrys = new Industry[staticOverride.getLength()];
                oneit.objstore.BaseBusinessClass referenceObj = BO_Industry.REFERENCE_BO_Industry;

                for(int staticIndex=0; staticIndex < staticOverride.getLength(); staticIndex++)
                {
                    String    name        = staticOverride.getName(staticIndex);
                    String    value       = staticOverride.getValue(staticIndex);
                    String    description = staticOverride.getDescription(staticIndex);
                    boolean   disabled    = staticOverride.getDisabled(staticIndex);

                    allIndustrys[staticIndex] = new Industry(name, value, description, disabled);
                    
                    allIndustrys[staticIndex].SortOrder = staticOverride.getPrimitiveLong(referenceObj, "SortOrder", staticIndex, 0);
                }

                // set a flag so we can determine that this DBEnum has been loaded statically
                // this should be used to disable access to screens which manipulate the BO.
                isStaticDBEnum = true;
            }
            else
            {
                if(isReloadable() && !reloadRegistered)
                {
                    DBEnumReloader.getDBEnumReloader().registerDBEnum(Industry.class);
                    reloadRegistered = true;
                }

                allIndustrys = loadFromDatabase();
            }
        }
        else if(reloadRequired)
        {
            reloadRequired = false;

            reloadFromDatabase();
        }


        return allIndustrys;
    }

    private static Industry[] loadFromDatabase()
    {
        Industry[] newIndustrys = new Industry[0];

        oneit.objstore.ObjectTransaction   transaction = getTransaction ();
        BO_Industry[]   boInstances;

        try
        {
            oneit.logging.LogMgr.log (LOG_Industry, oneit.logging.LogLevel.DEBUG1, "Staring search to populate instances");
            boInstances = BO_Industry.searchALL (transaction);
            oneit.logging.LogMgr.log (LOG_Industry, oneit.logging.LogLevel.DEBUG1, "Instances returned:", new Integer (boInstances.length));
            newIndustrys = new Industry[boInstances.length];

            for (int boIndex = 0 ; boIndex < boInstances.length ; ++boIndex)
            {
                BO_Industry bo = boInstances[boIndex];
                boolean   disabled = bo.getDisabled ();

                newIndustrys[boIndex] = new Industry (bo.getName (), bo.getValue (), bo.getDescription (), disabled);
                newIndustrys[boIndex].SortOrder = bo.getSortOrder(); //
                
            }

            java.util.Arrays.sort (newIndustrys);

            transaction.commit();
            transaction.commitResources();

            return newIndustrys;
        }
        catch (Exception e)
        {
            throw new oneit.utils.NestedException (e);
        }
        finally
        {
            transaction.releaseResources();
        }
    }

    private static void reloadFromDatabase()
    {
        if(!isReloadable())
        {
            return;
        }

        // get the existing ones in a map, by value
        Industry[] reloadedItems = loadFromDatabase();
        Map  existingItemsByValue = CollectionUtils.group(allIndustrys,
                                                          new ObjectTransform[] {AbstractEnumerated.TRANSFORM_TO_VALUE},
                                                          new Comparator[] { null},
                                                          CollectionUtils.GROUP_DONT_COMPARE_CHILDREN);

        List newItemsList = new ArrayList();

        for(int i=0; i < reloadedItems.length; i++)
        {
            List              oldVerionList = (List)existingItemsByValue.remove(reloadedItems[i].getValue());
            Industry oldVersion    = (oldVerionList == null || oldVerionList.isEmpty()) ? null : (Industry)oldVerionList.iterator().next();

            if(oldVersion != null)
            {
                oldVersion.disabled = reloadedItems[i].disabled;
                oldVersion.description = reloadedItems[i].description;

                oldVersion.SortOrder = reloadedItems[i].SortOrder;
                

                newItemsList.add(oldVersion);
            }
            else
            {
                newItemsList.add(reloadedItems[i]);
            }
        }

        // add any old ones which have not been covered
        // There should not be any, since deletion is not allowed
        // but we need to make sure...
        for(Iterator it=existingItemsByValue.entrySet().iterator(); it.hasNext(); )
        {
            newItemsList.add(it.next());
        }

        allIndustrys = (Industry[])newItemsList.toArray(new Industry[0]);
        oneit.logging.LogMgr.log(LOG_Industry, oneit.logging.LogLevel.PROCESSING1, "Completed Reload for Industry");
    }


    public BO_Industry getIndustryBO (oneit.objstore.ObjectTransaction objTran)
    {
        BO_Industry[]    theBOs = BO_Industry.searchBY_VALUE (objTran, getValue());

        if (theBOs.length >= 1)
        {
            return theBOs[0];
        }
        else
        {
            throw new IllegalArgumentException ("No such Industry value:" + getValue ());
        }
    }

    private transient long SortOrder;

    private Industry (String name, String value, String description, boolean disabled)
    {
        super (name, value, description, disabled);
    }

    public long getSortOrder()
    {
    return SortOrder;
    }
    
    public static final Comparator COMPARE_BY_POSITION = new CompareEnumByPosition (allIndustrys);



    static
    {
        defineAdditionalData ();
    }

    public boolean isEqual (Industry other)
    {
    return this.name.equals (other.name);
    }

    public Enumeration getAllInstances ()
    {
        return Industry.getAll ();
    }


    private Object readResolve() throws java.io.ObjectStreamException
    {
        return Industry.forName (this.name);
    }


    public EnumeratedFactory getFactory ()
    {
        return FACTORY_Industry;
    }

    public static Industry forName (String name)
    {
        if (name == null) { return null; }

        Industry[] all = getAllIndustrys();
        int enumIndex = AbstractEnumerated.getIndexForName (all, name);

        return all[enumIndex];
    }


    public static Industry forValue (String value)
    {
        if (value == null) { return null; }

        Industry[] all = getAllIndustrys();
        int enumIndex = AbstractEnumerated.getIndexForValue (getAllIndustrys (), value);

        return all[enumIndex];
    }


    public static java.util.Enumeration getAll ()
    {
        return AbstractEnumerated.getAll (getAllIndustrys());
    }
    
    
    public static Industry[] getIndustryArray ()
    {
        return (Industry[])getAllIndustrys().clone ();
    }


    public static void defineAdditionalData ()
    {
        
    }


    static class IndustryFactory implements EnumeratedFactory
    {
        public AbstractEnumerated getForName (String name)
        {
            return Industry.forName (name);
        }

        public AbstractEnumerated getForValue (String name)
        {
            return Industry.forValue (name);
        }

        public Enumeration getAll ()
        {
            return Industry.getAll ();
        }
    }


    public Map getAdditionalAttributes ()
    {
        Map attribs = new HashMap ();

        attribs.put ("SortOrder", ArrayFormatter.toObject(getSortOrder()));
        
        return attribs;
    }
}

    
