/*
FILE:     CifBuildReorganizer.h
*/
/*
VERSION:  4.101
*/
/*
DATE:     8/16/2004
*/
/*
  Comments and Questions to: sw-help@rcsb.rutgers.edu
*/
/*
COPYRIGHT 1999-2004 Rutgers - The State University of New Jersey

This software is provided WITHOUT WARRANTY OF MERCHANTABILITY OR
FITNESS FOR A PARTICULAR PURPOSE OR ANY OTHER WARRANTY, EXPRESS OR
IMPLIED.  RUTGERS MAKE NO REPRESENTATION OR WARRANTY THAT THE
SOFTWARE WILL NOT INFRINGE ANY PATENT, COPYRIGHT OR OTHER
PROPRIETARY RIGHT.

The user of this software shall indemnify, hold harmless and defend
Rutgers, its governors, trustees, officers, employees, students,
agents and the authors against any and all claims, suits,
losses, liabilities, damages, costs, fees, and expenses including
reasonable attorneys' fees resulting from or arising out of the
use of this software.  This indemnification shall include, but is
not limited to, any and all claims alleging products liability.
*/
/*
               PDB SOFTWARE LICENSE AGREEMENT

BY CLICKING THE ACCEPTANCE BUTTON OR INSTALLING OR USING 
THIS "SOFTWARE, THE INDIVIDUAL OR ENTITY LICENSING THE  
SOFTWARE ("LICENSEE") IS CONSENTING TO BE BOUND BY AND IS 
BECOMING A PARTY TO THIS AGREEMENT.  IF LICENSEE DOES NOT 
AGREE TO ALL OF THE TERMS OF THIS AGREEMENT
THE LICENSEE MUST NOT INSTALL OR USE THE SOFTWARE.

1. LICENSE AGREEMENT

This is a license between you ("Licensee") and the Protein Data Bank (PDB) 
at Rutgers, The State University of New Jersey (hereafter referred to 
as "RUTGERS").   The software is owned by RUTGERS and protected by 
copyright laws, and some elements are protected by laws governing 
trademarks, trade dress and trade secrets, and may be protected by 
patent laws. 

2. LICENSE GRANT

RUTGERS grants you, and you hereby accept, non-exclusive, royalty-free 
perpetual license to install, use, modify, prepare derivative works, 
incorporate into other computer software, and distribute in binary 
and source code format, or any derivative work thereof, together with 
any associated media, printed materials, and on-line or electronic 
documentation (if any) provided by RUTGERS (collectively, the "SOFTWARE"), 
subject to the following terms and conditions: (i) any distribution 
of the SOFTWARE shall bind the receiver to the terms and conditions 
of this Agreement; (ii) any distribution of the SOFTWARE in modified 
form shall clearly state that the SOFTWARE has been modified from 
the version originally obtained from RUTGERS.  

2. COPYRIGHT; RETENTION OF RIGHTS.  

The above license grant is conditioned on the following: (i) you must 
reproduce all copyright notices and other proprietary notices on any 
copies of the SOFTWARE and you must not remove such notices; (ii) in 
the event you compile the SOFTWARE, you will include the copyright 
notice with the binary in such a manner as to allow it to be easily 
viewable; (iii) if you incorporate the SOFTWARE into other code, you 
must provide notice that the code contains the SOFTWARE and include 
a copy of the copyright notices and other proprietary notices.  All 
copies of the SOFTWARE shall be subject to the terms of this Agreement.  

3. NO MAINTENANCE OR SUPPORT; TREATMENT OF ENHANCEMENTS 

RUTGERS is under no obligation whatsoever to: (i) provide maintenance 
or support for the SOFTWARE; or (ii) to notify you of bug fixes, patches, 
or upgrades to the features, functionality or performance of the 
SOFTWARE ("Enhancements") (if any), whether developed by RUTGERS 
or third parties.  If, in its sole discretion, RUTGERS makes an 
Enhancement available to you and RUTGERS does not separately enter 
into a written license agreement with you relating to such bug fix, 
patch or upgrade, then it shall be deemed incorporated into the SOFTWARE 
and subject to this Agreement. You are under no obligation whatsoever 
to provide any Enhancements to RUTGERS or the public that you may 
develop over time; however, if you choose to provide your Enhancements 
to RUTGERS, or if you choose to otherwise publish or distribute your 
Enhancements, in source code form without contemporaneously requiring 
end users or RUTGERS to enter into a separate written license agreement 
for such Enhancements, then you hereby grant RUTGERS a non-exclusive,
royalty-free perpetual license to install, use, modify, prepare
derivative works, incorporate into the SOFTWARE or other computer
software, distribute, and sublicense your Enhancements or derivative
works thereof, in binary and source code form.

4. FEES.  There is no license fee for the SOFTWARE.  If Licensee
wishes to receive the SOFTWARE on media, there may be a small charge
for the media and for shipping and handling.  Licensee is
responsible for any and all taxes.

5. TERMINATION.  Without prejudice to any other rights, Licensor
may terminate this Agreement if Licensee breaches any of its terms
and conditions.  Upon termination, Licensee shall destroy all
copies of the SOFTWARE.

6. PROPRIETARY RIGHTS.  Title, ownership rights, and intellectual
property rights in the Product shall remain with RUTGERS.  Licensee 
acknowledges such ownership and intellectual property rights and will 
not take any action to jeopardize, limit or interfere in any manner 
with RUTGERS' ownership of or rights with respect to the SOFTWARE.  
The SOFTWARE is protected by copyright and other intellectual 
property laws and by international treaties.  Title and related 
rights in the content accessed through the SOFTWARE is the property 
of the applicable content owner and is protected by applicable law.  
The license granted under this Agreement gives Licensee no rights to such
content.

7. DISCLAIMER OF WARRANTY.  THE SOFTWARE IS PROVIDED FREE OF 
CHARGE, AND, THEREFORE, ON AN "AS IS" BASIS, WITHOUT WARRANTY OF 
ANY KIND, INCLUDING WITHOUT LIMITATION THE WARRANTIES THAT IT 
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE 
OR NON-INFRINGING.  THE ENTIRE RISK AS TO THE QUALITY AND 
PERFORMANCE OF THE SOFTWARE IS BORNE BY LICENSEE.  SHOULD THE 
SOFTWARE PROVE DEFECTIVE IN ANY RESPECT, THE LICENSEE AND NOT 
LICENSOR ASSUMES THE ENTIRE COST OF ANY SERVICE AND REPAIR.  
THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF 
THIS AGREEMENT.  NO USE OF THE PRODUCT IS AUTHORIZED HEREUNDER 
EXCEPT UNDER THIS DISCLAIMER.

8. LIMITATION OF LIABILITY.  TO THE MAXIMUM EXTENT PERMITTED BY
APPLICABLE LAW,  IN NO EVENT WILL LICENSOR BE LIABLE FOR ANY 
INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 
OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, INCLUDING, 
WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK 
STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL 
OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF ADVISED OF THE
POSSIBILITY THEREOF. 
*/

#ifndef _CIF_REORG_BUILD_H_
#define _CIF_REORG_BUILD_H_

#include "CifReorganizerBase.h"
#include "FileNavigator.h"
#include "FileNavigatorError.h"

// a structure to manage persistent vs. in-memory objects
typedef struct _item_index_table {
  uWord numKeys; // the number of object names

  char ** keys; // the list of object names

  // An zero indicates that the object is not on disk or not in memory
  uWord * objectIds; // the object numbers (used to fetch from disk)
  uWord * memIds; // the object numbers (used to fetch from memory)
} itemList;

class SubcategoryBuildObj : public SubcategoryObjBase {
 private:
  int GetDescription();
  int GetExamplesCase();
  int GetExamplesDetail();
  int GetMethods();

 public:
  SubcategoryBuildObj();
  SubcategoryBuildObj(const char * dataBlockName, const char * subcategoryId);
  ~SubcategoryBuildObj() { Reset(); };
  void SetId(const char * dataBlockName, const char * subcategoryId);

  // Get the whole object from disk, but specify the object by its object#
  int GetObject(uWord which);

  // The Fetch methods return a reference to the private data members
  inline char  * FetchDescription() { return _description; };
  inline char ** FetchExamplesCase(uWord & n)
                                   { n = _examplesNum; return _examplesCase; };
  inline char ** FetchExamplesDetail(uWord & n)
                                   { n = _examplesNum; return _examplesDetail; };
  inline char ** FetchMethods(uWord & n) { n = _methodsNum; return _methods; };
};

class CategoryBuildObj : public CategoryObjBase {
 private:
  int GetDescription();
  int GetDescriptionNDB();
  int GetMandatoryCode();
  int GetKeyItems();
  int GetExamplesCase();
  int GetExamplesDetail();
  int GetExamplesCaseNDB();
  int GetExamplesDetailNDB();
  int GetCategoryGroups();
  int GetCategoryMethods();

 public:
  CategoryBuildObj();
  CategoryBuildObj(const char * dataBlockName, const char * _categoryId);
  ~CategoryBuildObj() { Reset();};
  void SetId(const char * dataBlockName, const char * _categoryId);

  // Get the whole object from disk, but specify the object by its object#
  int GetObject(uWord which);

  // The Fetch methods return a reference to the private data members
  inline char * FetchDescription() { return _description; };
  inline char * FetchDescriptionNDB() { return _descriptionNDB; };
  inline char * FetchMandatoryCode() { return _mandatoryCode; };
  inline char ** FetchKeyItems(uWord & n)
                                     { n = _keyItemsNum; return _keyItems; };
  inline char ** FetchExamplesCase(uWord & n)
                                   { n = _examplesNum; return _examplesCase; };
  inline char ** FetchExamplesDetail(uWord & n)
                                   { n = _examplesNum; return _examplesDetail; };
  inline char ** FetchExamplesCaseNDB(uWord & n)
                                   { n = _examplesNumNDB; return _examplesCaseNDB; };
  inline char ** FetchExamplesDetailNDB(uWord & n)
                                   { n = _examplesNumNDB; return _examplesDetailNDB; };
  inline char ** FetchCategoryGroups(uWord & n) 
                         { n = _categoryGroupsNum; return _categoryGroups; };
  inline char ** FetchCategoryMethods(uWord & n) 
                         { n = _categoryMethodsNum; return _categoryMethods; };

};

class ItemBuildObj : public ItemObjBase {
 private:
  int GetDescription();
  int GetDescriptionNDB();
  int GetType();
  int GetPrimitiveCode();
  int GetRegex();
  int GetExamplesCase();
  int GetExamplesDetail();
  int GetEnumeration();
  int GetEnumerationDetail();
  int GetExamplesCaseNDB();
  int GetExamplesDetailNDB();
  int GetEnumerationNDB();
  int GetEnumerationDetailNDB();
  int GetMandatoryCode();
  int GetDefaultValue();
  int GetUnits();
  int GetRangeMin();
  int GetRangeMax();
  int GetAliases();
  int GetAliasesDictionary();
  int GetAliasesDictionaryVersion();
  int GetItemStructure();
  int GetItemStructureOrganization();
  int GetDependents();
  int GetRelated();
  int GetRelatedFunctionCode();
  int GetSubcategories();
  int GetLinkedChildren();
  int GetLinkedParents();
  int GetMethods();
  int GetTypeConditions();

 public:
  ItemBuildObj();
  ItemBuildObj(const char * dataBlockName, const char * itemName);
  ~ItemBuildObj() { Reset(); };
  void SetName(const char * dataBlockName, const char * itemName);

  // Get the whole object from disk, but specify the object by its object#
  int GetObject(uWord which);

  // The Fetch methods return a reference to the private data members
  inline char * FetchDescription() { return _description; };
  inline char * FetchDescriptionNDB() { return _descriptionNDB; };
  inline char * FetchType() { return _type; };
  inline char * FetchPrimitiveCode() { return _primitiveCode; };
  inline char * FetchRegex() { return _regex; };
  inline char ** FetchExamplesCase(uWord & n)
                        { n = _examplesNum; return _examplesCase; };
  inline char ** FetchExamplesDetail(uWord & n)
                        { n = _examplesNum; return _examplesDetail; };
  inline char ** FetchEnumeration(uWord & n)
                        { n = _enumerationNum; return _enumeration; }; 
  inline char ** FetchEnumerationDetail(uWord & n)
                        { n = _enumerationNum; return _enumerationDetail; }; 

  inline char ** FetchExamplesCaseNDB(uWord & n)
                        { n = _examplesNumNDB; return _examplesCaseNDB; };
  inline char ** FetchExamplesDetailNDB(uWord & n)
                        { n = _examplesNumNDB; return _examplesDetailNDB; };
  inline char ** FetchEnumerationNDB(uWord & n)
                        { n = _enumerationNumNDB; return _enumerationNDB; }; 
  inline char ** FetchEnumerationDetailNDB(uWord & n)
                        { n = _enumerationNumNDB; return _enumerationDetailNDB; }; 

  inline char *  FetchMandatoryCode() { return _mandatoryCode; };
  inline char *  FetchDefaultValue() { return _defaultValue; };
  inline char *  FetchUnits() { return _units; };
  inline char ** FetchRangeMin(uWord & n)
                        { n = _rangeNum; return _rangeMin; }; 
  inline char ** FetchRangeMax(uWord & n)
                        { n = _rangeNum; return _rangeMax; }; 
  inline char ** FetchAliases(uWord & n)
                        { n = _aliasesNum; return _aliases; }; 
  inline char ** FetchAliasesDictionary(uWord & n)
                        { n = _aliasesNum; return _aliasesDictionary; }; 
  inline char ** FetchAliasesDictionaryVersion(uWord & n)
                        { n = _aliasesNum; return _aliasesDictionaryVersion; }; 
  inline char *  FetchItemStructure() { return _itemStructure; };
  inline char *  FetchItemStructureOrganization()
                        { return _itemStructureOrganization; };
  inline char ** FetchDependents(uWord & n)
                        { n = _dependentsNum; return _dependents; }; 
  inline char ** FetchRelated(uWord & n)
                        { n = _relatedNum; return _related; }; 
  inline char ** FetchRelatedFunctionCode(uWord & n)
                        { n = _relatedNum; return _relatedFunctionCode; }; 
  inline char ** FetchSubcategories(uWord & n)
                        { n = _subcategoriesNum; return _subcategories; }; 
  inline char ** FetchLinkedChildren(uWord & n)
                        { n = _linkedChildrenNum; return _linkedChildren; }; 
  inline char ** FetchLinkedParents(uWord & n)
                        { n = _linkedParentsNum; return _linkedParents; }; 
  inline char ** FetchMethods(uWord & n)
                        { n = _methodsNum; return _methods; }; 
  inline char ** FetchTypeConditions(uWord & n)
                        { n = _typeConditionsNum; return _typeConditions; }; 

};

class DictionaryBuildObj : public DictionaryObjBase {
 private:
  itemList _itemsList; // a list of items
  itemList _subcategoriesList; // a list of subcategories
  itemList _categoriesList; // a list of categories

  uWord _lastItemInMem; // index of the last item object in memory
  uWord _lastSubcategoryInMem;// index of the last subcategory object in memory
  uWord _lastCategoryInMem; // index of the last category object in memory

  // arrays of pointers to the in-memory objects. Use memIds to access these
  ItemBuildObj ** _itemObjs;
  SubcategoryBuildObj ** _subcategoryObjs;
  CategoryBuildObj ** _categoryObjs;  

  int GetTitle();
  int GetVersion();
  int GetDescription();
  int GetHistoryVersion();
  int GetHistoryUpdate();
  int GetHistoryRevision();
  int GetCategories();
  int GetSubcategories();
  int GetItems();
  int GetMethods();
  int GetMethodsId();
  int GetMethodsList();
  int GetMethodsListDetail();
  int GetMethodsListInline();
  int GetMethodsListCode();
  int GetMethodsListLanguage();
  int GetCategoryGroupList();
  int GetCategoryGroupListParents();
  int GetCategoryGroupListDescription();
  int GetItemStructureListCode();
  int GetItemStructureListIndex();
  int GetItemStructureListDimension();
  int GetItemTypeListCode();
  int GetItemTypeListPrimitiveCode();
  int GetItemTypeListConstruct();
  int GetItemTypeListDetail();
  int GetItemUnitsListCode();
  int GetItemUnitsListDetail();
  int GetItemUnitsConversionOperator();
  int GetItemUnitsConversionFactor();
  int GetItemUnitsConversionFromCode();
  int GetItemUnitsConversionToCode();

 public:
  //  static const int DICTIONARY_FIELDS = 32; // the number of stored fields
  DictionaryBuildObj();
  DictionaryBuildObj(const char * dname);
  ~DictionaryBuildObj() { Reset(); };
  void SetName(const char * dname);

  void Reset(); // frees owned memory and calls Clear()
  void Clear(); // initializes the object

  // Get the whole object from disk, but specify the object by its object#
  int GetObject(uWord which);

  // These methods return a reference to the entire object asked for
  CategoryBuildObj * GetCategory(const char * categoryId);
  SubcategoryBuildObj * GetSubcategory(const char * subcategoryId);
  ItemBuildObj * GetItem(const char * itemName);

  // Must be called before any objects are asked for. These let the object
  // know where to locate all of the objects in the dictionary datablock
  int BuildItemList();
  int BuildSubcategoryList();
  int BuildCategoryList();

  // The Fetch methods return a reference to the private data members
  inline char * FetchTitle() { return _title; };
  inline char * FetchVersion() { return _version; }; 
  inline char * FetchDescription() { return _description; };
  inline char ** FetchHistoryVersion(uWord & n)
          { n = _historyNum; return _historyVersion; };
  inline char ** FetchHistoryUpdate(uWord & n)
          { n = _historyNum; return _historyUpdate; };
  inline char ** FetchHistoryRevision(uWord & n)
          { n = _historyNum; return _historyRevision; };
  inline char ** FetchSubcategories(uWord & n)
          { n = _subcategoriesNum; return _subcategories; };
  inline char ** FetchCategories(uWord & n)
          { n = _categoriesNum; return _categories; };
  inline char ** FetchItems(uWord & n)
          { n = _itemsNum; return _items; };
  inline char ** FetchMethods(uWord & n) { n = _methodsNum; return _methods; };
  inline char ** FetchMethodsId(uWord & n)
          { n = _methodsNum; return _methodsId; };
  inline char ** FetchMethodsList(uWord & n)
          { n = _methodsListNum; return _methodsList; };
  inline char ** FetchMethodsListDetail(uWord & n)
          { n = _methodsListNum; return _methodsListDetail; };
  inline char ** FetchMethodsListInline(uWord & n)
          { n = _methodsListNum; return _methodsListInline; };
  inline char ** FetchMethodsListCode(uWord & n)
          { n = _methodsListNum; return _methodsListCode; };
  inline char ** FetchMethodsListLanguage(uWord & n)
          { n = _methodsListNum; return _methodsListLanguage; };
  inline char ** FetchCategoryGroupList(uWord & n)
          { n = _categoryGroupListNum; return _categoryGroupList; };
  inline char ** FetchCategoryGroupListParents(uWord & n)
          { n = _categoryGroupListNum; return _categoryGroupListParents; };
  inline char ** FetchCategoryGroupListDescription(uWord & n)
          { n = _categoryGroupListNum; return _categoryGroupListDescription; };
  inline char ** FetchItemStructureListCode(uWord & n)
          { n = _itemStructureListNum; return _itemStructureListCode; };
  inline char ** FetchItemStructureListIndex(uWord & n)
          { n = _itemStructureListNum; return _itemStructureListIndex; };
  inline char ** FetchItemStructureListDimension(uWord & n)
          { n = _itemStructureListNum; return _itemStructureListDimension; };
  inline char ** FetchItemTypeListCode(uWord & n)
          { n = _itemTypeListNum; return _itemTypeListCode; };
  inline char ** FetchItemTypeListPrimitiveCode(uWord & n)
          { n = _itemTypeListNum; return _itemTypeListPrimitiveCode; };
  inline char ** FetchItemTypeListConstruct(uWord & n)
          { n = _itemTypeListNum; return _itemTypeListConstruct; };
  inline char ** FetchItemTypeListDetail(uWord & n)
          { n = _itemTypeListNum; return _itemTypeListDetail; };
  inline char ** FetchItemUnitsListCode(uWord & n) 
          { n = _itemUnitsListNum; return _itemUnitsListCode; };
  inline char ** FetchItemUnitsListDetail(uWord & n) 
          { n = _itemUnitsListNum; return _itemUnitsListDetail; };
  inline char ** FetchItemUnitsConversionOperator(uWord & n)
          { n = _itemUnitsConversionNum;return _itemUnitsConversionOperator; };
  inline char ** FetchItemUnitsConversionFactor(uWord & n)
          { n = _itemUnitsConversionNum; return _itemUnitsConversionFactor; };
  inline char ** FetchItemUnitsConversionFromCode(uWord & n)
          { n = _itemUnitsConversionNum; return _itemUnitsConversionFromCode; };
  inline char ** FetchItemUnitsConversionToCode(uWord & n)
          { n = _itemUnitsConversionNum; return _itemUnitsConversionToCode; };

  // Debugging tools that get all objects and print them out
  int GetAllItems();
  int GetAllSubcategories();
  int GetAllCategories();
};

#endif

